Een*_*oku 4 c parallel-processing mpi mpich
我从 MPI 开始。我想尝试一个经典的“Hello,world”程序,它也会打印每个进程的编号以及其他一些信息。我的程序可以工作,但我对mpiexec 的实际工作原理有点困惑。问题是,当我指定进程数时,有时它们不会启动。例如,我使用这个命令:
mpiexec -np 4 ./hello
Run Code Online (Sandbox Code Playgroud)
但只有 2 或 3 个进程被启动,而不是 4 个。启动的进程数量发生变化,所以我真的很困惑。问题出在我的电脑上(我只有双核)还是正常现象?
你好ç:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我的错误非常愚蠢。我只是在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;
}
Run Code Online (Sandbox Code Playgroud)