0

I'm a beginner to MPI. When I coded my first program, I came into a tough problem to me.

MPI_Init(&argc, &argv) ; 
MPI_Comm_rank( MPI_COMM_WORLD, &rank) ; 
MPI_Comm_size( MPI_COMM_WORLD, &size)  ;
printf("Process: %d\n", rank);
printf("Procs_num: %d\n", size);

and:

mpicc main.c -o main 
mpirun -np 8 main

But all I get is 8 duplicates of:

Process: 0
Procs_num: 1

What I expect to get is:

Process: 0~7 
Procs_num: 8

I guess it is because there is no 8 processes in MPI_COMM_WORLD, but I cannot figure out the reason and have no idea about how to solve it.

And I hope you will catch what I want to express. Many thx!!

Peiyun
  • 149
  • 1
  • 7
  • 4
    Check the answer to this question: http://stackoverflow.com/questions/4039608/why-do-all-my-open-mpi-processes-have-rank-0. You probably have more than one MPI library installed, and the `mpicc` command is coming from one, and `mpirun` from the other. – Greg Inozemtsev Aug 11 '12 at 05:46
  • @GregInozemtsev may be. I will try it. – Peiyun Aug 11 '12 at 06:19

1 Answers1

0

Apparently you forgot to call MPI_Finalize

Here is the correct code:

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

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

    MPI_Init(&argc, &argv) ; 
    MPI_Comm_rank( MPI_COMM_WORLD, &rank) ; 
    MPI_Comm_size( MPI_COMM_WORLD, &size)  ;
    printf("Process: %d\n", rank);
    printf("Procs_num: %d\n", size);

    MPI_Finalize();                         

   return(0);
}

Then you get your 8 fifferents ranks:

~/mpi_tests/examples > mpirun -np 8 ~/mpi_tests/examples/t
Process: 4
Procs_num: 8
Process: 5
Procs_num: 8
Process: 0
Procs_num: 8
Process: 1
Procs_num: 8
Process: 7
Procs_num: 8
Process: 2
Procs_num: 8
Process: 3
Procs_num: 8
Process: 6
Procs_num: 8
Stephane Rouberol
  • 4,286
  • 19
  • 18
  • 1
    Omitting `MPI_FINALIZE` might make an MPI program enter an undefined state (and often crash) when one of the processes exits without calling `MPI_FINALIZE` but it would not lead to the behaviour, described by the OP. – Hristo Iliev Aug 11 '12 at 19:51