如何知道MPI在哪个进程上运行? [英] How to know the which core a process is running on in MPI?

查看:64
本文介绍了如何知道MPI在哪个进程上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在一个项目中,我需要知道MPI当前在其上运行该进程的处理器的coreid? MPI中有一个名为 MPI_Get_processor_name(char * name,int * resultlen)的函数.这仅给出运行该进程的节点的名称.我想知道运行它的核心的ID?是否有可能?如果是这样,谁能给我这样做的代码片段?

I am currently working on a project where I need to know the coreid of the processor on which the the process currently runs on in MPI? There is a function in MPI called MPI_Get_processor_name( char *name, int *resultlen ). This only gives the name of the node on which the process is running. I want to know the id of the core on which it is running? Is it possible? If it is so can anyone give me the code snippet for doing it?

谢谢

推荐答案

以下代码为绑定它们的每个进程提供了coreid.这需要Hristo Iliev在先前答案的注释中建议的hwloc库.

Here is the code which gives the coreids for each process on which they are bound. This needs the hwloc library as suggested by Hristo Iliev in the previous answer's comments.

    #include <stdio.h>
    #include "mpi.h"
    #include <hwloc.h>

    int main(int argc, char* argv[])
    {
        int rank, size;
        cpu_set_t mask;
        long num;
        int proc_num(long num);

        hwloc_topology_t topology;
        hwloc_cpuset_t cpuset;
        hwloc_obj_t obj;


        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);

        hwloc_topology_init ( &topology);
        hwloc_topology_load ( topology);

        hwloc_bitmap_t set = hwloc_bitmap_alloc();
        hwloc_obj_t pu;
        int err;

        err = hwloc_get_proc_cpubind(topology, getpid(), set, HWLOC_CPUBIND_PROCESS);
        if (err) {
        printf ("Error Cannot find\n"), exit(1);
        }

        pu = hwloc_get_pu_obj_by_os_index(topology, hwloc_bitmap_first(set));
        printf ("Hello World, I am %d and pid: %d coreid:%d\n",rank,getpid(),hwloc_bitmap_first(set));

        int my_coreid = hwloc_bitmap_first(set);
        int all_coreid[size];
        hwloc_bitmap_free(set);
        hwloc_topology_destroy(topology);
        MPI_Finalize();
        return 0;

}

这篇关于如何知道MPI在哪个进程上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆