English 中文(简体)
MPI Barrier C++
原标题:
  • 时间:2010-01-13 19:11:04
  •  标签:
  • mpi
  • barrier

I want to use MPI (MPICH2) on windows. I write this command:

MPI_Barrier(MPI_COMM_WORLD);  

And I expect it blocks all Processors until all group members have called it. But it is not happen. I add a schematic of my code:

int a;  
if(myrank == RootProc)  
   a = 4;  
MPI_Barrier(MPI_COMM_WORLD);  
cout << "My Rank = " << myrank << "	a = " << a << endl;  

(With 2 processor:) Root processor (0) acts correctly, but processor with rank 1 doesn t know the a variable, so it display -858993460 instead of 4.

Can any one help me?
Regards

最佳回答

You re only assigning a in process 0. MPI doesn t share memory, so if you want the a in process 1 to get the value of 4, you need to call MPI_Send from process 0 and MPI_Recv from process 1.

问题回答

Variable a is not initialized - it is possible that is why it displays that number. In MPI, variable a is duplicated between the processes - so there are two values for a, one of which is uninitialized. You want to write:

int a = 4;
if (myrank == RootProc)
...

Or, alternatively, do an MPI_send in the Root (id 0), and an MPI_recv in the slave (id 1) so the value in the root is also set in the slave.

Note: that code triggers a small alarm in my head, so I need to check something and I ll edit this with more info. Until then though, the uninitialized value is most certainly a problem for you. Ok I ve checked the facts - your code was not properly indented and I missed the missing {}. The barrier looks fine now, although the snippet you posted does not do too much, and is not a very good example of a barrier because the slave enters it directly, whereas the root will set the value of the variable to 4 and then enter it. To test that it actually works, you probably want some sort of a sleep mechanism in one of the processes - that will yield (hope it s the correct term) the other process as well, preventing it from printing the cout until the sleep is over.

Blocking is not enough, you have to send data to other processes (memory in not shared between processes).

To share data across ALL processes use:

int MPI_Bcast(void* buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm )

so in your case:

MPI_Bcast(&a, 1, MPI_INT, 0, MPI_COMM_WORLD);

here you send one integer pointed by &a form process 0 to all other. //MPI_Bcast is sender for root process and receiver for non-root processes

You can also send some data to specyfic process by:

int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, 
              int tag, MPI_Comm comm )

and then receive by:

int MPI_Recv(void* buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status)




相关问题
From OpenMP to MPI

I just wonder how to convert the following openMP program to a MPI program #include <omp.h> #define CHUNKSIZE 100 #define N 1000 int main (int argc, char *argv[]) { int i, ...

Test MPI_Barrier C++

How can I be sure that MPI_Barrier act correctly? What s the method of test for that? Thank you

MPI Barrier C++

I want to use MPI (MPICH2) on windows. I write this command: MPI_Barrier(MPI_COMM_WORLD); And I expect it blocks all Processors until all group members have called it. But it is not happen. I add ...

MPI buffered send/receive order

I m using MPI (with fortran but the question is more specific to the MPI standard than any given language), and specifically using the buffered send/receive functions isend and irecv. Now if we ...

Mpi usage in Windows

I installed mpi into windows and I can use its libraries. The problem is that in windows when I write mpiexec -n 4 proj.exe into command prompt it does not make the proper operations. 4 different ...

shared memory, MPI and queuing systems

My unix/windows C++ app is already parallelized using MPI: the job is splitted in N cpus and each chunk is executed in parallel, quite efficient, very good speed scaling, the job is done right. But ...

GCC performance

I am doing parallel programming with MPI on Beowulf cluster. We wrote parallel algorithm for simulated annealing. It works fine. We expect 15 time faster execution than with serial code. But we did ...

热门标签