#include<iostream>
#include"mpi.h"
void main(int args, char **argv) {
int i;
int nitems = 5 ;
int totalnodes, mynode;
double *array;
int trap_key = 0;
MPI_Status status;
MPI_Init(&args, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
array = new double[nitems];
for (i = 0; i < nitems; i++)
array[i] = double(i);
if (mynode != trap_key) //this will allow only process 0 to skip this stmt
MPI_Recv(&trap_key, 1, MPI_INT, mynode - 1, 0, MPI_COMM_WORLD, &status);
//here is the critical section
cout << "I am Process: " << mynode << endl;
for (i = 0; i < nitems; i++)
cout << "array[" << i << "] =" << array[i] << endl;
if(mynode != totalnodes - 1) // this will allow only the last process to
//skip this
MPI_Send(&trap_key, 1, MPI_INT, mynode + 1, 0, MPI_COMM_WORLD);
MPI_Finalize();
}
build this code then open cmd in the debug file directory and
type in : mpiexec yourprojectname.exe
so simply what i did is surrounding the critical section with receive and send operations so each process waits for the previous one to finish.
rank
in there somewhere. :) – Wirer