I have 3 function and 4 cores. I want execute each function in new thread using MPI and C++
I write this
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
size--;
if (rank == 0)
{
Thread1();
}
else
{
if(rank == 1)
{
Thread2();
}
else
{
Thread3();
}
}
MPI_Finalize();
But it execute just Thread1(). How i must change code?
Thanks!
Print to screen the current value of variable
size(possibly without decrementing it) and you will find1. That is: “there is1process running”.You are likely running your compiled code the wrong way. Consider to use
mpirun(ormpiexec, depending on your MPI implementation) to execute it, i.e.the
-npparameter specifies the number of processes you will start (doing so, yourMPI_Comm_sizewill be 4 as you expect).Currently, though, you are not using anything explicitly owing to C++. You can consider some C++ binding of MPI such as
Boost.MPI.I worked a little bit on the code you provided. I changed it a little bit producing this working mpi code (I provided some needed correction in capital letters).
FYI:
compilation (under gcc, mpich):
execution
output
be aware that
stdoutis likely messed out.Are you sure you are compiling your code the right way?