Reputation: 31
I am a MPI beginner, If i write a code like this in c++, how would the system dicide how many ranks the program will have? This is not like the logic we follow in object-oriented language, when you define an array, you know precisely the size. What mechanism they use in MPI to let the system decide how many rank and the entire size? Is it flexible? Decide by mechine power? Or just automatically generating when calling?
int main(int argc, char *argv[]){
MPI_Init(NULL, NULL);
int size, rank;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 4)
master();
else
slave(rank);
MPI_Finalize();
return 0;}
Upvotes: 3
Views: 4456
Reputation: 4444
Here is a good reference:
And here is a tutorial:
And here is a related question
To answer your question:
Rank is the (logical) process number (like a thread id)
Size is the total number of processes (allocated, parallelism)
When you use MPI_Init, see this: https://www.sharcnet.ca/help/index.php/Getting_Started_with_MPI to observe how to set number of processes.
mpirun -n 4 ./parhello
You can construct your argv[] array with "-n N" and specify world size to MPI_init call.
You can also set the world size using environment variables,
Borrowing from another question/answer:
MPI_Comm_size returns the size of a communicator. In our example, MPI_COMM_WORLD (which is constructed for us by MPI) encloses all of the processes in the job, so this call should return the amount of processes that were requested for the job.
MPI_Comm_rank returns the rank of a process in a communicator. Each process inside of a communicator is assigned an incremental rank starting from zero. The ranks of the processes are primarily used for identification purposes when sending and receiving messages.
Rank is used to distinguish processes from one another. You may have a "master" process (rank = 0) that sends out messages to "slave" applications on rank 1-15. Or you may use other topologies to divide workloads.
Upvotes: 5