Skip to content

Instantly share code, notes, and snippets.

@mcopik
Created January 7, 2019 13:40
Show Gist options
  • Select an option

  • Save mcopik/207f28583e875a3b97eca53a8a846893 to your computer and use it in GitHub Desktop.

Select an option

Save mcopik/207f28583e875a3b97eca53a8a846893 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;
}
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 = 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