Search code examples
parallel-processingmpiopenmpimessage-passing

Why can't I open get 8 processes at a time?


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!!


Solution

  • 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