强制MPI使用指定的否。核心

时间:2015-04-01 16:31:27

标签: multithreading parallel-processing multiprocessing openmpi

我是MPI的新手,所以如果这是一个微不足道的问题,请原谅我。我有一个四核CPU。我想运行一个在单个核心上使用两个进程的OpenMPI C ++程序。有没有办法这样做?如果是这样,那怎么样?我提到了this link on stack overflow很可能,说可能是一种方式......如果是这样,那我该怎么做呢?

2 个答案:

答案 0 :(得分:2)

由于MPI会生成单独的进程,因此通常/通常由您的操作系统执行将进程调度到核心上。您仍然可以通过手动设置进程与特定核心的亲和力来实现您的目标。

您可以使用sched_setaffinity函数在Linux中执行此操作。

固定进程到特定核心,您可以使用以下方法:

#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();
}

确保在之后调用此函数,在MPI_Init函数中调用main并指定要 pin 进程的核心

答案 1 :(得分:1)

使用Open MPI实现此功能的正确方法是提供 rankfile ,它告诉库放置每个进程的位置。在您的情况下,以下rankfile就足够了:

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

它告诉Open MPI两个等级应该在本地主机上运行并绑定到OS CPU 0,这通常是第一个核心。将rankfile 另外提供给主机文件或主机列表:

$ 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