Search code examples
cmpiopenmpiprocessor

MPI code on Ubuntu have only one processor used


I'm trying to learn parallel computing with Open MPI. I use a Ubuntu 16 boot on a MacBook Pro.

I've installed OpenMP and try to run an hello_world to test it.

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

int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);

// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);

// Print off a hello world message
printf("Hello world from processor %s, rank %d"
       " out of %d processors\n",
       processor_name, world_rank, world_size);

// Finalize the MPI environment.
MPI_Finalize();
}

I had no problem to compile it with mpicc but when I try to launch it, I've got the same result for ./hello_world -n 4, ./hello_world -n 2, ./hello_world -np 4 and so on.

It always writes:

Hello world from processor ubuntu-mac , rank 0 out of 1 processor

I don't understand why can't it run on several processor... Do I launch it incorrectly or is it my config or anything else?


Solution

  • You are running it incorrectly, the program should be launched with mpirun or mpiexec, such that MPI can spawn the desired number of processes. Let's say you have your program in file hello.c, you can compile and run it as follows:

    mpicc -o hello hello.c
    mpirun -np 4 ./hello
    

    Which should show the following example output:

    Hello world from processor sagan, rank 1 out of 4 processors
    Hello world from processor sagan, rank 2 out of 4 processors
    Hello world from processor sagan, rank 3 out of 4 processors
    Hello world from processor sagan, rank 0 out of 4 processors
    

    Running the program standalone, as you seem to be doing, will only spawn one process, since the hello program does not parse the flag -n you are giving to it. mpirun, on the other hand, uses the -np flag to spawn the desired number of processes.