diff --git a/hpc_mpi/src/bin_reduce.c b/hpc_mpi/src/bin_reduce.c new file mode 100644 index 0000000..fce1f0f --- /dev/null +++ b/hpc_mpi/src/bin_reduce.c @@ -0,0 +1,64 @@ +/* + * bin_reduce.c + * + * Created on: 16 Jun 2016 + * Author: johannes + */ + +#include +#include "mpi.h" +#include "bin_reduce.h" + +int int_log2(int x) { + int r; + while (x >>= 1) { + r++; + } + return r; +} + +int Bin_Reduce(const void *sendbuf, void *recvbuf, int count, + MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) { + if (root != 0) { + fprintf(stderr, "Sorry, root!=0 not allowed"); + return -1; + } + + int r, p; + + MPI_Comm_rank(comm, &r); + MPI_Comm_size(comm, &p); + + int tree_depth = int_log2(p); + int i = 0; + int depth; + int parent = 0; + + // maximum possible number of nodes with current depth + int max_nodes = (1 << (tree_depth + 1)) - 1; + + for (depth = 1; i != r; depth++) { + parent = i; + max_nodes /= 2; + if (r > i + max_nodes) { + i += max_nodes + 1; + } else { + i++; + } + } + + printf("%i: %i\n", r, parent); + + /*if (depth != tree_depth) { + MPI_Recv(); + MPI_Reduce_local(); + MPI_Recv(); + MPI_Reduce_local(); + } + + if (r != root) { + MPI_Send(); + }*/ + + return 0; +} diff --git a/hpc_mpi/src/bin_reduce.h b/hpc_mpi/src/bin_reduce.h new file mode 100644 index 0000000..aa421b0 --- /dev/null +++ b/hpc_mpi/src/bin_reduce.h @@ -0,0 +1,14 @@ +/* + * bin_reduce.h + * + * Created on: 16 Jun 2016 + * Author: johannes + */ + +#ifndef BIN_REDUCE_H_ +#define BIN_REDUCE_H_ + +int Bin_Reduce(const void *sendbuf, void *recvbuf, int count, + MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm); + +#endif /* BIN_REDUCE_H_ */ diff --git a/hpc_mpi/src/hpc_mpi.c b/hpc_mpi/src/hpc_mpi.c index 7776d2e..58a1710 100644 --- a/hpc_mpi/src/hpc_mpi.c +++ b/hpc_mpi/src/hpc_mpi.c @@ -10,6 +10,7 @@ #include #include #include "mpi.h" +#include "bin_reduce.h" int Binom_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm); @@ -31,10 +32,12 @@ int main(int argc, char* argv[]) { int a = my_rank; int recv; - Binom_Reduce(&a, &recv, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); + Bin_Reduce(&a, &recv, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); + + //Binom_Reduce(&a, &recv, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (my_rank == 0) { - printf("%i\n", recv); + //printf("%i\n", recv); } /* shut down MPI */ MPI_Finalize(); @@ -79,3 +82,9 @@ int Binom_Reduce(const void *sendbuf, void *recvbuf, int count, return 0; } + +int Fib_Reduce(const void *sendbuf, void *recvbuf, int count, + MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) { + return 0; +} +