スライド 1 - Home Page of Koji OKAMURA

Sample answer of the last week's report.
int my_reduce(int *a, int *b, int c)
{
int i, p, myid, procs;
int *t;
MPI_Status st;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
if (myid == 0){
t = (int *)malloc(c*sizeof(int));
MPI_Recv(t, c, MPI_INT, 1, 0, MPI_COMM_WORLD, &st);
for (i = 0; i < N; i++)
b[i] = a[i] + t[i];
for (p = 2; p < procs; p++){
MPI_Recv(t, c, MPI_INT, p, 0, MPI_COMM_WORLD, &st);
for (i = 0; i < N; i++)
b[i] += t[i];
}
} else{
MPI_Send(a, c, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
return 0;
}
1