Binom_Reduce funktioniert soweit denk ich, ob kommutativität passt bin

ich nicht sicher
This commit is contained in:
Johannes Winklehner 2016-05-11 22:00:19 +02:00
commit 265bb2ac36

78
hpc_mpi/src/hpc_mpi.c Normal file
View file

@ -0,0 +1,78 @@
/*
============================================================================
Name : hpc_mpi.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello MPI World in C
============================================================================
*/
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int Binom_Reduce(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
int main(int argc, char* argv[]) {
int my_rank; /* rank of process */
int p; /* number of processes */
/* start up MPI */
MPI_Init(&argc, &argv);
/* find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);
int a = my_rank;
int recv;
Binom_Reduce(&a, &recv, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (my_rank == 0) {
printf("%i\n", recv);
}
/* shut down MPI */
MPI_Finalize();
return 0;
}
int Binom_Reduce(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
int r, p, size;
MPI_Status status;
void *recv;
void *reduced;
MPI_Comm_rank(comm, &r);
MPI_Comm_size(comm, &p);
MPI_Type_size(datatype, &size);
MPI_Alloc_mem(count * size, MPI_INFO_NULL, &recv);
if (r != root) {
MPI_Alloc_mem(count * size, MPI_INFO_NULL, &reduced);
} else {
reduced = recvbuf;
}
memcpy(reduced, sendbuf, count * size);
int i = 1;
while ((r + i) % (2 * i) != 0 && i < p) {
if (r + i < p) {
MPI_Recv(recv, count, datatype, r + i, i, comm, &status);
MPI_Reduce_local(recv, reduced, count, datatype, op);
}
i <<= 1;
}
if (r != root) {
MPI_Send(reduced, count, datatype, r - i, i, comm);
}
return 0;
}