I'm having trouble getting the correct number of processes to output on a hello world program. I'm fairly certain this program should work since it was provided by the textbook that we are using. The code is as follows:
#include <stdio.h>
#include <string.h>
#include <mpi.h>
const int MAX_STRING = 100;
int main(int argc, char * argv[])
{
char greeting[MAX_STRING];
int comm_sz;
int my_rank;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank != 0)
{
sprintf(greeting, "Greetings from process %d of %d!\n", my_rank, comm_sz);
MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}
else
{
printf("Greetings from process %d of %d!\n", my_rank, comm_sz);
for (int q = 1; q < comm_sz; q++)
{
MPI_Recv(greeting, MAX_STRING, MPI_CHAR, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%s\n", greeting);
}
}
MPI_Finalize();
return 0;
}
I am compiling it by running
mpicxx hello_mpi.cpp -O3
Which compiles fine, then I execute it by
mpiexec -n 4 ./a.out
Using 4 as an example number of threads. I should be receiving an output as
Greetings from process 0 of 4!
Greetings from process 1 of 4!
Greetings from process 2 of 4!
Greetings from process 3 of 4!
But instead I am getting
Greetings from process 0 of 1!
Greetings from process 0 of 1!
Greetings from process 0 of 1!
Greetings from process 0 of 1!
I'm not sure why I'm getting this result instead of a rank from each thread. Again, this was the code provided by the textbook we are using (although originally written in C, I tried compiling an executing in C but ran into the same error).
Any ideas?