Critical section in MPI?
Asked Answered
P

2

12

I have some code to print a 2D array to the standard output. The problem is that when I run it, every process writes to the output and the data overlaps, rendering it unusable.

How can i build a critical section in MPI so that only one process at a time enters the section where I display the output?

I'm using OpenMPI.

Preoccupied answered 12/1, 2012 at 19:26 Comment(0)
R
20

Separate it out by using MPI_Barriers.

rank = 0;
while (rank < total_processes) {
   if (myrank == rank) {
       printf ("Array printed by rank: %d\n", myrank);
       print_array();
       fflush (stdout);
   }
   rank ++;
   MPI_Barrier ();
}
Rheumatoid answered 12/1, 2012 at 19:29 Comment(5)
You might want to increment rank in there somewhere. :)Wirer
Thanks for the reply! It's a little bit better but still overlaps :(Preoccupied
Well, it doesn't seem to be flushing properly then. Maybe you should try writing to different files. Each rank can write to to output_$rank.txt or something like that.Rheumatoid
Yes, that was my solution in the end. I outputted to several files. Thanks a lot!Preoccupied
I have a similar issue except I'm trying to write to a sqlite database. Is there a more efficient way to do this then the above answer? I would like to let each process execute in whatever order they happen to be in.Speedwriting
F
1
#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.

Ferrotype answered 8/5, 2020 at 15:47 Comment(1)
MPI_send() and MPI_recv() are blocking point to point communication,so when a process calls MPI_recv() it enters a busy waiting state till it receives the message,and the same happens with MPI_send it waits the msg. is delivered to the source.Ferrotype

© 2022 - 2024 — McMap. All rights reserved.