1

I am kinda new to MPI, so forgive me if it is a trivial question. I have a quad core CPU. And I want to run an OpenMPI C++ program that uses two processes on a single core. Is there any way to do so? If so, then how? I referred to this link on stack overflow which, most probably, says that there might be a way... If so, then how can I do it?

Community
  • 1
  • 1
Python_user
  • 1,378
  • 3
  • 12
  • 25

2 Answers2

2

Since MPI spawns separate processes, the scheduling of processes onto cores is generally/usually performed by your operating system. You can still achieve what you want by manually setting the affinity of the processes to a specific core.

You can do this in Linux using the sched_setaffinity function.

To pin a process to a specific core, you can use the following approach:

#include <mpi.h>
#include <sched.h>

// ...

void pin_to_core(int cpuid) {                                            
    cpu_set_t set;                                                       
    CPU_ZERO(&set);                                                      
    CPU_SET(cpuid,&set);                                                                                                                                                                                                         
    sched_setaffinity(0,sizeof(cpu_set_t),&set);
}

// ....

int main(int argc, char* argv[]) {
    MPI_Init(&argc, &argv);
    pin_to_core(0); // pin process to core 0
    // ...
    MPI_Finalize();
}

Make sure to call this function after you call MPI_Init in your main function and specify the core you want to pin the process to.

Patrick
  • 900
  • 7
  • 14
  • Open MPI (and in fact every major MPI implementation out there) has a built-in **portable** mechanism to specify process binding/pinning. – Hristo Iliev Apr 02 '15 at 08:40
1

The proper way to implement this with Open MPI is to provide a rankfile which tells the library where to place each process. The following rankfile should suffice in your case:

rank 0=localhost slot=0
rank 1=localhost slot=0

It tells Open MPI that both ranks should run on the local host and be bound to OS CPU 0, which is usually the first core. Provide the rankfile in addition to the hostfile or the host list:

$ mpiexec --host localhost --report-bindings --rankfile ./rankfile -np 2 hostname
[cluster:03247] MCW rank 0 bound to socket 0[core 0]: [B . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .] (slot list 0)
[cluster:03247] MCW rank 1 bound to socket 0[core 0]: [B . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .] (slot list 0)
cluster
cluster
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186