Multi-threading with Intel MPI

Multi-threading with Intel MPI

I am trying to establish communication between threads on different processes using Intel MPI.

However, it does not seem to work as I would expect it to.

Here's the code snippet:

             MPI_Comm_dup(MPI_COMM_WORLD, &dup_world_comm0);

             MPI_Comm_group(dup_world_comm0, &world_group0);

             MPI_Comm_create(dup_world_comm0, world_group0, &world_comm0); 

              /*inside a thread*/

               if (myrank == 0) {//process 0
                        i = find_sum();
                        if (index == 0) {//thread id 0
                                MPI_Send(&i, 1, MPI_INT, 1, 1, world_comm0);
                                MPI_Recv(&y, 1, MPI_INT, 1, 1, world_comm0, &status);

                if (myrank == 1) {//process 1
                        if (index == 0) {//thread id 0
                                MPI_Recv(&i, 1, MPI_INT, 0, 1, world_comm0, &status);
                                MPI_Send(&i, 1, MPI_INT, 0, 1, world_comm0);


I am spawning a single thread on each process and am trying to establish some communication between these threads using MPI_Send/Recv.

But, many a times, the execution hangs on MPI_Recv or MPI_Send in process 1.

Is their something that I am missing here or doing wrong  or is the library not supposed to work in this way with threads?

I have attached the code file for reference.


Download test_MPI_thread.cpp6.65 KB
2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Kunal,

I am able to run your code with only one modification (not related to this, see below).  How are you compiling?  Did you remember to link to the multithreaded MPI library?  I compiled with:

mpiicpc test_MPI_thread.cpp -mt_mpi

Using the Intel® C++ Compiler 2013 Update 5.

Your code appears fine, but I do have a few recommendations.  When you are creating the communicators (lines 208 through 213), the world_comm0 and world_comm1 communicators you are creating are identical to the dup_world_comm0 and dup_world_comm1 communicators you previously created.  These are at all points after creation identical to MPI_COMM_WORLD.  Unless you have future plans for them, you could simplify to just using MPI_COMM_WORLD.

The modification I had to make was to remove the MPI_Recv on line 221.  If you are launching this spawned from a different MPI program, this is fine.  But as a standalone code, this doesn't work, as parentcomm is null.  I usually put an if (parentcomm!=MPI_COMM_NULL) block to protect calls like this.

James Tullos
Technical Consulting Engineer
Intel® Cluster Tools

Leave a Comment

Please sign in to add a comment. Not a member? Join today