Val*_*ier 1 c processor mpi openmpi
我正在尝试使用 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();
}
Run Code Online (Sandbox Code Playgroud)
我没有问题,编译它mpicc,但是当我尝试启动它,我已经得到了同样的结果./hello_world -n 4,./hello_world -n 2,./hello_world -np 4等等。
它总是写道:
来自处理器 ubuntu-mac 的 Hello world,在 1 个处理器中排名 0
我不明白为什么它不能在多个处理器上运行......我是否错误地启动了它还是我的配置或其他任何东西?
小智 7
您运行不正确,应使用mpirun或启动程序mpiexec,以便 MPI 可以生成所需数量的进程。假设您的程序在文件 hello.c 中,您可以按如下方式编译和运行它:
mpicc -o hello hello.c
mpirun -np 4 ./hello
Run Code Online (Sandbox Code Playgroud)
这应该显示以下示例输出:
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
Run Code Online (Sandbox Code Playgroud)
像您似乎正在做的那样,独立运行程序只会产生一个进程,因为 hello 程序不会解析-n您提供给它的标志。mpirun,另一方面,使用该-np标志产生所需数量的进程。