Binary Tree Reduce wird bald funktionieren

This commit is contained in:
Johannes Winklehner 2016-06-16 17:45:55 +02:00
parent 7be8b179ba
commit ce7e11c41b
3 changed files with 89 additions and 2 deletions

64
hpc_mpi/src/bin_reduce.c Normal file
View file

@ -0,0 +1,64 @@
/*
* bin_reduce.c
*
* Created on: 16 Jun 2016
* Author: johannes
*/
#include <stdio.h>
#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;
}

14
hpc_mpi/src/bin_reduce.h Normal file
View file

@ -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_ */

View file

@ -10,6 +10,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "mpi.h" #include "mpi.h"
#include "bin_reduce.h"
int Binom_Reduce(const void *sendbuf, void *recvbuf, int count, int Binom_Reduce(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm); 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 a = my_rank;
int recv; 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) { if (my_rank == 0) {
printf("%i\n", recv); //printf("%i\n", recv);
} }
/* shut down MPI */ /* shut down MPI */
MPI_Finalize(); MPI_Finalize();
@ -79,3 +82,9 @@ int Binom_Reduce(const void *sendbuf, void *recvbuf, int count,
return 0; 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;
}