0

I have an algorithm parallelized in main() using C with openMPI; it works perfect but now I want to move the code over to an external function.

void my_parallel_function(int v[], int size, int rank)
{
   if(rank==0)
   {
     MPI_Send(&v[0], 5, MPI_INT, 1, 1, MPI_COMM_WORLD);
   }
   else
   {
     MPI_Recv(&v[0], 5, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD,&status);
     printf("value of item 1: %d", v[0]);
   }
}

int main(int argc, char *argv[])
{        
    int rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Status status;

    int vect[100];

    if (rank==0)
    {
      my_parallel_function(vect, size, rank);
    }

    MPI_Finalize();
    return 0;
}

The above code is only intended to illustrate what I would like to do. I can run my code from the console but then there is no response, it just hangs.

Any ideas? Thanks a lot!

Bruce Dean
  • 2,798
  • 2
  • 18
  • 30
John Smith
  • 97
  • 2
  • 10

2 Answers2

2

There's a problem with calling your function. You should let the function be called by all MPI processes. And you handled Send and Receive in your function correctly. (pointed out by @xeroqu)

In your current code, only the master process calls the function and sends data, and no other processes reach MPI_Recv, so it should hang!

Just remove if (rank==0) in your main.

Update

Take a look at the following code. I made two changes. First, I call my_parallel_function for all ranks. Second, I send data from master node to all other nodes (not just one). So all other nodes receive some data and won't block.

#include <mpi.h>
#include <stdio.h>

void my_parallel_function(int v[], int size, int rank)
{
    if(rank==0)
    {
        int i;
        for( i=1; i<size; i++ )
            MPI_Send(&v[0], 5, MPI_INT, /*Destination:*/i, 1, MPI_COMM_WORLD);
    }
    else
    {
        MPI_Status status;
        MPI_Recv(&v[0], 5, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status);
        printf("value of item 1: %d\n", v[0]);
    }
}

int main(int argc, char *argv[])
{
    int rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Status status;

    int vect[100];

    my_parallel_function(vect, size, rank);

    MPI_Finalize();
    return 0;
}
saeedn
  • 3,288
  • 1
  • 21
  • 20
  • if i remove the if (rank==0) the other processes will output some info to the i/o and will get in while and a switch loop menu. – John Smith Nov 12 '13 at 13:16
  • while and switch loop menu? I added a code to my answer. See if it helps. – saeedn Nov 12 '13 at 17:05
  • Thank you very much, now it works fine, but now i have another problem cause of removing if(rank==0), please check it here: http://stackoverflow.com/questions/19932496/openmpi-gets-waiting-for-ever – John Smith Nov 12 '13 at 17:14
0

In addition to saeedn's answer, your MPI_Send() seems to send data from rank0 to rank1 only. All other processes (except rank0) execute MPI_Recv and wait. Since there is no process sending data to these processes, this might also cause your program hang.

xeroqu
  • 425
  • 5
  • 14