Skip to content

Instantly share code, notes, and snippets.

@Ari-Roda
Last active January 24, 2018 10:37
Show Gist options
  • Save Ari-Roda/39f1377139fa927d0d4af3cab9fbb7fc to your computer and use it in GitHub Desktop.
Save Ari-Roda/39f1377139fa927d0d4af3cab9fbb7fc to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#define N 20
int my_reduce(int *a, int *b, int c)
{
int i;
for (i = 0; i < c; i++){
b[i] = a[i] + b[i];
}
return 0;
}
int main(int argc, char *argv[])
{
int i, myid, procs, val;
MPI_Status status;
int a[N], b[N];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
for (i = 0; i < N; i++){
a[i] = i;
b[i] = 0;
}
if (myid == 0){
printf("b[%d] = %d , correct answer = %d\n", i, b[i], i*procs);
MPI_Recv(b, N, MPI_INT, i, 0, MPI_COMM_WORLD,&status);
}
if (myid != 0){
printf("b[%d] = %d , correct answer = %d\n", i, b[i], i*procs);
MPI_Recv(b, N, MPI_INT, i, 0, MPI_COMM_WORLD,&status);
}
my_reduce(a, b, N);
MPI_Send(b, N, MPI_INT, myid, 0, MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment