gpt4 book ai didi

c - MPI - 更改启动的进程数

转载 作者:行者123 更新时间:2023-12-05 03:13:58 27 4
gpt4 key购买 nike

我从 MPI 开始。我想尝试一个经典的“Hello, world”程序,它也会打印每个进程的编号和一些其他信息。我的程序可以运行,但我对 mpiexec 的实际运行方式不太了解。问题是,当我指定进程数时,有时它们不会启动。例如,我使用这个命令:

mpiexec -np 4 ./hello

但只有 2 或 3 个进程启动,而不是 4 个。启动进程的数量发生变化,所以我真的很困惑。问题出在我的电脑上(我只有双核)还是这是正常现象?


你好.c:

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

int main(){
MPI_Init(NULL, NULL);

// Number of processes
int world_size;
MPI_Comm_size( MPI_COMM_WORLD, &world_size );

// Number of current process
int process_id;
MPI_Comm_rank( MPI_COMM_WORLD, &process_id );

// Processor name
char processor_name[ MPI_MAX_PROCESSOR_NAME ];
int name_len;
MPI_Get_processor_name( processor_name, &name_len );

printf("Hello! - sent from process %d running on processor %s.\n\
Number of processes is %d.\n\
Length of proc name is %d.\n\
***********************\n",
process_id, processor_name, world_size, name_len);

return 0;
}

最佳答案

我的错误很愚蠢。我只是在return 之前缺少MPI_Finalize() 函数。


正确代码:

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

int main(){
MPI_Init(NULL, NULL);

// Number of processes
int world_size;
MPI_Comm_size( MPI_COMM_WORLD, &world_size );

// Number of current process
int process_id;
MPI_Comm_rank( MPI_COMM_WORLD, &process_id );

// Processor name
char processor_name[ MPI_MAX_PROCESSOR_NAME ];
int name_len;
MPI_Get_processor_name( processor_name, &name_len );

printf("Hello! - sent from process %d running on processor %s.\n\
Number of processes is %d.\n\
Length of proc name is %d.\n\
***********************\n",
process_id, processor_name, world_size, name_len);

MPI_Finalize();
return 0;
}

关于c - MPI - 更改启动的进程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594626/

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