2

I am trying to run below program

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

main(int argc, char **argv){
      int size, myrank;
      MPI_init(NULL, NULL);
      MPI_Comm_size(MPI_COMM_WORLD,&size);
      MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
      printf("My rank is\n",myrank);
}

command to compile and run:

mpicc hello.c
mpirun -np 4 a.out

Expected Output:

My rank is 0
My rank is 1
My rank is 2
My rank is 3

Actual Output:

My rank is 0
My rank is 0
My rank is 0
My rank is 0

Why did I get the output with all ranks equal to zero, and what should I do to get the expected output?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sawan
  • 236
  • 3
  • 12

1 Answers1

3

Your line

printf("My rank is\n",myrank);

is missing %d:

printf("My rank is %d\n",myrank);

Furthermore, MPI_init should be MPI_Init, and you are missing a MPI_Finalize statement.

Apart from that, make sure that a.out that you run is really the one you think it is, for instance using

mpirun -np 4 ./a.out

rather than

mpirun -np 4 a.out

and make sure that both mpicc and mpirun come from the same implementation : OpenMPI, IntelMPI, MPich, etc, do not like to mix commands from one another.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • I don't think that what the OP shows here is the actual program code. `printf()` won't output any number given the current format string and also a call to `MPI_Finalize()` is missing, therefore the MPI job will terminate abnormally the very moment one rank returns from `main`. – Hristo Iliev Aug 29 '14 at 13:27