gpt4 book ai didi

c - Ubuntu 上的 MPI 代码只使用了一个处理器

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:45 24 4
gpt4 key购买 nike

我正在尝试使用 Open MPI 学习并行计算。我在 MacBook Pro 上使用 Ubuntu 16 启动。

我已经安装了 OpenMP 并尝试运行 hello_world 来测试它。

#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 编译它没有问题,但是当我尝试启动它时,我得到了与 ./hello_world -n 4 相同的结果,./hello_world -n 2, ./hello_world -np 4 等等

它总是这样写:

Hello world from processor ubuntu-mac , rank 0 out of 1 processor

我不明白为什么它不能在多个处理器上运行...我启动它不正确还是我的配置或其他什么?

最佳答案

您没有正确运行它,应该使用 mpirunmpiexec 启动程序,以便 MPI 可以产生所需数量的进程。假设你的程序在文件 hello.c 中,你可以按如下方式编译和运行它:

mpicc -o hello hello.c
mpirun -np 4 ./hello

应该显示以下示例输出:

Hello world from processor sagan, rank 1 out of 4 processors
Hello world from processor sagan, rank 2 out of 4 processors
Hello world from processor sagan, rank 3 out of 4 processors
Hello world from processor sagan, rank 0 out of 4 processors

独立运行该程序,正如您似乎正在做的那样,只会产生一个进程,因为 hello 程序不会解析您提供给它的标志 -n。另一方面,mpirun 使用 -np 标志来生成所需数量的进程。

关于c - Ubuntu 上的 MPI 代码只使用了一个处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com