-1

i am trying these logic code, and i do not know what is different between them. I am trying to use MPI_Send() and MPI_Recv() in my program. As i understand, in MPI, processess communicate via their ranks of each processor, and tags of each message. So , what is different if i try

Logic 1:

int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int number;
if (world_rank == 0) {
    number = -1;
    MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (world_rank == 1) {
    MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,
             MPI_STATUS_IGNORE);
    printf("Process 1 received number %d from process 0\n",
           number);
}

Logic 2:

int world_rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  int world_size;
  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  int number;
      if (world_rank == 0) {
        number = -1;
        int i =0;
        for(i = 1 ; i< world_size;i++){
           MPI_Send(&number, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
        }
      }else{
        MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
         printf("Process 1 received number %d from process 0\n",
           number);
      }

I try: mpirun -np 100 ./test <arguments>

  • Logic 1: after few minutes, my computer hangs on.
  • Logic 2: it works, and print 100 lines: Process kali received number -1 from process 0

I think both logic will get rank of process, and parse it to parameter on MPI_Send. What is different???

I am working on Debian Kali Linux, with OpenMPI 1.8. I am new to MPI. Thanks for help.

November Rain
  • 206
  • 3
  • 13

2 Answers2

1

It is strongly different.

On the one hand, in Logic 1, a single message is sent from process 0 to process 1. On the other hand, in Logic 2, world_size-1 messages are sent by process 0 and each remaining process receives one message from 0. The second case could be replaced by a call to MPI_Bcast().

Had you tried mpirun -np 2 ./test <arguments> these code would have done the same thing...but it is the only case !

Both the code abstracts seem correct. The failure in the first case may be due to the fact that the integer number is not initialized on processes 2 to world_size. For instance, if number is the length of an array, it can trigger a segmentation fault. If number is part of a stopping condition in a for loop, it can trigger an infine loop (or a very long one).

francis
  • 9,525
  • 2
  • 25
  • 41
  • Hi francis, thanks for helping. As i understand, in MPI, we have 2 way to communicate ( send-recv) message: 1. Point-to-point communication: In this case, it is logic 1. 2. Broadcast communication: In this case, it is logic 2. Thanks again. – November Rain Sep 24 '15 at 14:30
  • @NovemberRain Close. You do have two ways to communicate (well actually more with newer versions of MPI, but originally there were only 2): 1. Point-to-point communication (this means any time you call a send, or isend, or sendrecv on one node and a matching recv, or irecv, or sendrecv on the other node). 2. Broadcast communication (this means any time you call a collective operator, such as broadcast, reduce, etc on EVERY node involved in the communication). So while your logic 2 is emulating a broadcast, it is not actually a broadcast communication. Still just case 1. – NoseKnowsAll Sep 24 '15 at 17:36
  • @NoseKnowsAll : your answer is so awesome. I can understand clearly about MPI communication. Thanks in advanced. – November Rain Sep 25 '15 at 17:01
0

Edited

As we are missing the full source code, it is not possible to determine whether the initialization/finalization functions were properly used.

Here we have the same source code form the initial answer + what's required to properly run an mpi app:

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

int
main (int argc, char *argv[])
{
    MPI_Init(&argc, &argv);

    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    int number;
    if (world_rank == 0) {
        number = -1;
        MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
    } else if (world_rank == 1) {
        MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,
                 MPI_STATUS_IGNORE);
        printf("Process 1 received number %d from process 0\n",
               number);
    }

    MPI_Finalize();
}

Compile:

> mpicc -std=c99 -O2 -g -Wall -I. -o app app.c -lm

Run:

> mpirun -n 10 app
Process 1 received number -1 from process 0
>

Basically everything is working fine, so I guess the problem could be related to initialization/finalization.

Initial Response

Your application is hanging-up with logic 1 because there are 99 processes waiting for a message, but the master process is only sending the message to process identified by rank 1.

As you're using a blocking function (i.e. MPI_Send vs. MPI_Isend), there are 98 processes waiting forever until a message arrives from process ranked with 0, tag=0, and communicator MPI_COMM_WORLD.

  • This is wrong, in the first logic, there are just 2 involved processes. Note the test for equality. So it should just be rank 0 sending and rank 1 receiving. – haraldkl Sep 25 '15 at 01:06
  • @haraldkl you're right...i just overlooked it. The other potential flaw there is the missing MPI_Finalize() statement, but without the full source code is hard to say. – Carlos Montemuino Sep 25 '15 at 11:11
  • HI, of course i have to call `MPI_Init()` and `MPI_Finalize()`, but i do not want to write because it is not necessary ( i think so). Thanks for reply. – November Rain Sep 25 '15 at 16:59