Fib_Reduce implementiert

This commit is contained in:
Johannes Winklehner 2016-06-18 11:55:26 +02:00
parent 5a844a56bd
commit 327ca4d5cb
6 changed files with 168 additions and 49 deletions

View file

@ -7,7 +7,7 @@
#include <stdio.h>
#include <string.h>
#include "mpi.h"
#include <mpi.h>
#include "bin_reduce.h"
int int_log2(int x) {

View file

@ -0,0 +1,48 @@
/*
* binom_reduce.c
*
* Created on: 18 Jun 2016
* Author: johannes
*/
#include <mpi.h>
#include <string.h>
#include "binom_reduce.h"
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);
MPI_Free_mem(reduced);
}
MPI_Free_mem(recv);
return 0;
}

View file

@ -0,0 +1,14 @@
/*
* binom_reduce.h
*
* Created on: 18 Jun 2016
* Author: johannes
*/
#ifndef BINOM_REDUCE_H_
#define BINOM_REDUCE_H_
int Binom_Reduce(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
#endif /* BINOM_REDUCE_H_ */

87
hpc_mpi/src/fib_reduce.c Normal file
View file

@ -0,0 +1,87 @@
/*
* fib_reduce.c
*
* Created on: 18 Jun 2016
* Author: johannes
*/
#include <stdio.h>
#include <string.h>
#include <mpi.h>
#include "fib_reduce.h"
int Fib_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, size;
MPI_Status status;
void *recv_right;
MPI_Comm_rank(comm, &r);
MPI_Comm_size(comm, &p);
MPI_Type_size(datatype, &size);
int temp;
int fibm1 = 1;
int fib = 1;
while (fib - 1 < p) {
temp = fib;
fib += fibm1;
fibm1 = temp;
}
int right = fibm1;
int left = fib - fibm1;
int i = 0;
int parent = 0;
while (i != r) {
parent = i;
if (r >= i + left) {
i += left;
temp = left;
left = right - left;
right = temp;
} else {
i++;
right -= left;
left -= right;
}
}
if (r == root) {
recv_right = recvbuf;
} else {
MPI_Alloc_mem(size * count, MPI_INFO_NULL, &recv_right);
}
if (right - 1 > 0 && r + 1 < p) {
void *recv_left;
MPI_Alloc_mem(count * size, MPI_INFO_NULL, &recv_left);
MPI_Recv(recv_left, count, datatype, r + 1, 0, comm, &status);
MPI_Reduce_local(sendbuf, recv_left, count, datatype, op);
if (left - 1 > 0 && r + left < p) {
MPI_Recv(recv_right, count, datatype, r + left, 0, comm, &status);
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) {
MPI_Send(recv_right, count, datatype, parent, 0, comm);
MPI_Free_mem(recv_right);
}
return 0;
}

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

@ -0,0 +1,14 @@
/*
* fib_reduce.h
*
* Created on: 18 Jun 2016
* Author: johannes
*/
#ifndef FIB_REDUCE_H_
#define FIB_REDUCE_H_
int Fib_Reduce(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
#endif /* FIB_REDUCE_H_ */

View file

@ -9,11 +9,10 @@
*/
#include <stdio.h>
#include <string.h>
#include "mpi.h"
#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);
#include "binom_reduce.h"
#include "fib_reduce.h"
int main(int argc, char* argv[]) {
int my_rank; /* rank of process */
@ -32,7 +31,7 @@ int main(int argc, char* argv[]) {
int a = my_rank;
int recv;
Bin_Reduce(&a, &recv, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
Fib_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);
@ -45,46 +44,3 @@ int main(int argc, char* argv[]) {
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);
MPI_Free_mem(reduced);
}
MPI_Free_mem(recv);
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;
}