Binary Reduce funktioniert

This commit is contained in:
Johannes Winklehner 2016-06-16 19:04:45 +02:00
parent ce7e11c41b
commit 5a844a56bd
2 changed files with 44 additions and 15 deletions

View file

@ -6,6 +6,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "mpi.h" #include "mpi.h"
#include "bin_reduce.h" #include "bin_reduce.h"
@ -24,41 +25,69 @@ int Bin_Reduce(const void *sendbuf, void *recvbuf, int count,
return -1; return -1;
} }
int r, p; int r, p, size;
MPI_Status status;
MPI_Comm_rank(comm, &r); MPI_Comm_rank(comm, &r);
MPI_Comm_size(comm, &p); 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 i = 0;
int depth; int depth;
int parent = 0; int parent = 0;
// maximum possible number of nodes with current depth // maximum possible number of nodes in a subtree with current depth
int max_nodes = (1 << (tree_depth + 1)) - 1; 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++) { for (depth = 1; i != r; depth++) {
parent = i; parent = i;
max_nodes /= 2;
if (r > i + max_nodes) { if (r > i + max_nodes) {
i += max_nodes + 1; i += max_nodes + 1;
} else { } else {
i++; i++;
} }
max_nodes /= 2;
} }
printf("%i: %i\n", r, parent); printf("%i: %i\n", r, parent);
/*if (depth != tree_depth) { if (depth != tree_depth && r + 1 < p) {
MPI_Recv(); MPI_Alloc_mem(count * size, MPI_INFO_NULL, &recv_left);
MPI_Reduce_local(); MPI_Recv(recv_left, count, datatype, r + 1, 0, comm, &status);
MPI_Recv(); printf("%i received %i from %i\n", r, *((int*) recv_left), r + 1);
MPI_Reduce_local(); 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);
}
MPI_Free_mem(recv_left);
} else {
memcpy(recv_right, sendbuf, count * size);
} }
if (r != root) { if (r != root) {
MPI_Send(); 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; return 0;
} }

View file

@ -37,7 +37,7 @@ int main(int argc, char* argv[]) {
//Binom_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) { if (my_rank == 0) {
//printf("%i\n", recv); printf("%i\n", recv);
} }
/* shut down MPI */ /* shut down MPI */
MPI_Finalize(); MPI_Finalize();