I am new to MPI and am trying to figure out the subtleties in it, and for that reason I am just playing around with an easy example. The question I have is that when I run the code below with 2 processors, even though the printf statement is BEFORE the MPI_Init both processors print it. Is there anyway to not have this happen, and only use the multiple processors when the environment is initialized? Below is my minimum working example.
#include <mpi.h>
#include<stdio.h>
#define send_data_tag 2001
#define return_data_tag 2002
double add(int a);
int main(int argc, char ** argv)
{
int rank, size, len;
char host[MPI_MAX_PROCESSOR_NAME];
printf("Will all processors print this?");
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Get_processor_name(host, &len);
if(rank == 0)
{
int test = 1;
int final_test;
MPI_Send(&test, 1 , MPI_INT, 1, send_data_tag, MPI_COMM_WORLD);
MPI_Recv(&final_test, 1, MPI_INT, 1, return_data_tag, MPI_COMM_WORLD, &status);
printf("%d\n", final_test);
}
else
{
int test_recv , final_test;
MPI_Recv( &test_recv, 1, MPI_INT, 0, send_data_tag, MPI_COMM_WORLD, &status);
final_test = test_recv + 1;
MPI_Send(&final_test, 1, MPI_INT, 0, return_data_tag, MPI_COMM_WORLD);
}
MPI_Finalize();
}