<p>This exact problem is discussed on Markus Wittmann's Blog, <a href="https://blogs.fau.de/wittmann/2013/02/mpi-node-local-rank-determination/" rel="nofollow">MPI Node-Local Rank determination</a>.</p>

<p>There, three strategies are suggested:</p>

<ol>
<li><em>A naive, portable solution employs MPI_Get_processor_name or gethostname to create an unique identifier for the node and performs an MPI_Alltoall on it.</em> [...]</li>
<li>[Method 2] <em>relies on MPI_Comm_split, which provides an easy way to split a communicator into subgroups (sub-communicators).</em> [...]</li>
<li><em>Shared memory can be utilized, if available.</em> [...]</li>
</ol>

<p>For some working code (presumably LGPL licensed?), Wittmann links to <a href="http://git.rrze.uni-erlangen.de/gitweb/?p=apsm.git;a=blob;f=MpiNodeRank.cpp;hb=HEAD" rel="nofollow">MpiNodeRank.cpp</a> from the <a href="http://arxiv.org/abs/1302.4280" rel="nofollow">APSM library</a>.</p>
How to determine MPI rank/process number local to a socket/node
<p>Say, I run a parallel program using MPI. Execution command</p>

<pre><code>mpirun -n 8 -npernode 2 &lt;prg&gt;
</code></pre>

<p>launches 8 processes in total. That is 2 processes per node and 4 nodes in total. (OpenMPI 1.5). Where a node comprises 1 CPU (dual core) and network interconnect between nodes is InfiniBand.</p>

<p>Now, the rank number (or process number) can be determined with </p>

<pre><code>int myrank;
MPI_Comm_rank(MPI_COMM_WORLD, &amp;myrank);
</code></pre>

<p>This returns a number between 0 and 7. </p>

<p>But, How can I determine the node number (in this case a number between 0 and 3) and the process number within a node (number between 0 and 1)?</p>