首页 > 解决方案 > 如何让 MPI 在多核上运行?

问题描述

我是 MPI 和一般并行编程的新手,所以这对你们中的一些人来说可能太明显了。

我想使用我所有的内核运行一个简单的 hello world 程序。

我目前安装并配置了 MPICH3,如下所示我知道本教程是关于 MPICH2 的,但这是我找到的最接近的)。

我复制了这段代码:

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

int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    // Print off a hello world message
    printf("Hello world from processor %s, rank %d out of %d processors\n",
           processor_name, world_rank, world_size);

    // Finalize the MPI environment.
    MPI_Finalize();
}

我试着像这样编译它mpicc mpi_implementation.c。到目前为止,一切都很好。问题是,当我使用它运行它时,mpiexec -np 4 ./a.out我得到以下输出:

Hello world from processor grodeBase, rank 0 out of 1 processors
Hello world from processor grodeBase, rank 0 out of 1 processors
Hello world from processor grodeBase, rank 0 out of 1 processors
Hello world from processor grodeBase, rank 0 out of 1 processors

实际上这是对的,因为我只有一个处理器,但我的意思是在许多内核上运行程序,我不知道该怎么做。它会自动发生而我只是没有看到吗?有什么办法可以确保我所有的核心都被使用了吗?而且,我怎样才能具体提到其中之一?

PS如果您需要任何进一步的信息,请告诉我。

标签: cparallel-processingmpimpich

解决方案


推荐阅读