Reputation: 2629
I want to get some nodes with MPI program and do something between each 2 nodes. I'm wondering how it could be possible to get nodes(compute machine) with MPI? Any help would be appreciate.
Upvotes: 1
Views: 2944
Reputation: 9489
What you can do is create a per-node communicator using MPI_Comm_split_type()
using the hint MPI_COMM_TYPE_SHARED
. And then, getting the ranks of the processes on each node, and using them to create communicators of processes of the same local ranks.
From there, the processes inside the communicator corresponding of processes of local ranks 0 will all be on different nodes. You can therefore use it for any subsequent tests.
The code would look something like this:
int globalRank, localRank;
MPI_Comm nodeComm, masterComm;
MPI_Comm_rank( MPI_COMM_WORLD, &globalRank);
MPI_Comm_split_type( MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, globalRank,
MPI_INFO_NULL, &nodeComm );
MPI_Comm_rank( nodeComm, &localRank);
MPI_Comm_split( MPI_COMM_WORLD, localRank, globalRank, &masterComm );
MPI_Comm_free( &nodeComm );
if ( localRank == 0 ) {
// Now, each process of masterComm is on a different node
// so you can play with them to do what you want
int mRank, mSize;
MPI_Comm_rank( masterComm, &mRank );
MPI_Comm_size( masterComm, &mSize );
// do something here
}
MPI_Comm_free( &masterComm );
EDIT:
I'm still very unsure what you're after, but if what you want is the list of the nodes you're in, a possibility is to use MPI_Get_processor_name()
to retrieve the name of the current process' node and print it like this (as follow up to the previous part to replace the "do something here" comment):
char name[MPI_MAX_PROCESSOR_NAME];
int len;
MPI_Get_processor_name( name, &len );
printf( "Node number %d/%d is %s\n", mRank, mSize, name );
Now if this is the only thing you want to do, you'd probably much better of investigating around your MPI job submission tool, which can be, depending on you MPI flavour, mpirun
, mpiexec
, prun
, srun
, orterun
, etc. These usually come with command line arguments permitting to select the number of processes to launch, the number of nodes to use, the number of processes per node and even some ways of precisely placing each process on the various selected nodes. You can for example look for the (potential) -npernode
that both OpenMPI- and MPICH-based libraries do offer for their mpirun
utilities.
You could for example simply use:
mpirun -npernode 1 hostname
Upvotes: 6