Skip to content

Instantly share code, notes, and snippets.

@mcopik
Last active January 7, 2019 13:41
Show Gist options
  • Save mcopik/00a3cfa6a23792d6b7331eca87c21527 to your computer and use it in GitHub Desktop.
Save mcopik/00a3cfa6a23792d6b7331eca87c21527 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 = 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