diff --git a/hpc_mpi/src/bin_reduce.c b/hpc_mpi/src/bin_reduce.c index fce1f0f..a88cf05 100644 --- a/hpc_mpi/src/bin_reduce.c +++ b/hpc_mpi/src/bin_reduce.c @@ -6,6 +6,7 @@ */ #include +#include #include "mpi.h" #include "bin_reduce.h" @@ -24,41 +25,69 @@ int Bin_Reduce(const void *sendbuf, void *recvbuf, int count, return -1; } - int r, p; + int r, p, size; + MPI_Status status; MPI_Comm_rank(comm, &r); MPI_Comm_size(comm, &p); + MPI_Type_size(datatype, &size); - int tree_depth = int_log2(p); + int tree_depth = int_log2(p) + 1; int i = 0; int depth; int parent = 0; - // maximum possible number of nodes with current depth - int max_nodes = (1 << (tree_depth + 1)) - 1; + // maximum possible number of nodes in a subtree with current depth + int max_nodes = ((1 << tree_depth) - 1) / 2; + + void *recv_left; + void *recv_right; + + if (r == root) { + recv_right = recvbuf; + } else { + printf("%i gets some memory\n", r); + MPI_Alloc_mem(count * size, MPI_INFO_NULL, &recv_right); + } for (depth = 1; i != r; depth++) { parent = i; - max_nodes /= 2; if (r > i + max_nodes) { i += max_nodes + 1; } else { i++; } + max_nodes /= 2; } printf("%i: %i\n", r, parent); - /*if (depth != tree_depth) { - MPI_Recv(); - MPI_Reduce_local(); - MPI_Recv(); - MPI_Reduce_local(); - } + if (depth != tree_depth && r + 1 < p) { + MPI_Alloc_mem(count * size, MPI_INFO_NULL, &recv_left); + MPI_Recv(recv_left, count, datatype, r + 1, 0, comm, &status); + printf("%i received %i from %i\n", r, *((int*) recv_left), r + 1); + MPI_Reduce_local(sendbuf, recv_left, count, datatype, op); + printf("right child of %i: %i\n", r, r + max_nodes + 1); + if (r + max_nodes + 1 < p) { + MPI_Recv(recv_right, count, datatype, r + max_nodes + 1, 0, comm, + &status); + printf("%i received %i from %i\n", r, *((int*) recv_right), + r + max_nodes + 1); + MPI_Reduce_local(recv_left, recv_right, count, datatype, op); + } else { + memcpy(recv_right, recv_left, count * size); + } - if (r != root) { - MPI_Send(); - }*/ + MPI_Free_mem(recv_left); + } else { + memcpy(recv_right, sendbuf, count * size); + } + + if (r != root) { + printf("%i sends %i to %i\n", r, *((int*) recv_right), parent); + MPI_Send(recv_right, count, datatype, parent, 0, comm); + MPI_Free_mem(recv_right); + } return 0; } diff --git a/hpc_mpi/src/hpc_mpi.c b/hpc_mpi/src/hpc_mpi.c index 58a1710..9e83b84 100644 --- a/hpc_mpi/src/hpc_mpi.c +++ b/hpc_mpi/src/hpc_mpi.c @@ -37,7 +37,7 @@ int main(int argc, char* argv[]) { //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();