Skip to content

Instantly share code, notes, and snippets.

@mcopik
Created January 9, 2019 16:34
Show Gist options
  • Select an option

  • Save mcopik/7d61e7318492ebfc4501cbaf843f5d7a to your computer and use it in GitHub Desktop.

Select an option

Save mcopik/7d61e7318492ebfc4501cbaf843f5d7a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
void f(double * b, double * e)
{
while(b != e) {
*b += 2;
++b;
}
}
double h(double * data)
{
double acc_rcv;
MPI_Reduce(data, &acc_rcv, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
return acc_rcv;
}
struct X
{
static double h(double * data)
{
double acc_rcv;
MPI_Scan(data, &acc_rcv, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
return acc_rcv;
}
};
int main(int argc, char ** argv)
{
MPI_Init(&argc, &argv);
int size = atoi(argv[1]);
int ranks, rank_id;
MPI_Comm_size(MPI_COMM_WORLD, &ranks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank_id);
size /= ranks;
int start = size * rank_id;
double * data = (double*) malloc(sizeof(double)*size);
f(data, data + size);
double acc_rcv = X::h(data);
acc_rcv += h(data);
if(rank_id == 0)
printf("%f\n", acc_rcv);
free(data);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment