MPI - 更改启动的进程数量

4

我开始学习MPI。我想试试一个经典的“Hello, world”程序,它将打印每个进程的编号和一些其他信息。我的程序可以运行,但我有些困惑于 mpiexec 的工作方式。问题是,当我指定进程数时,有时它们就无法启动。例如,我使用以下命令:

mpiexec -np 4 ./hello

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


hello.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;
}
1个回答

5

我的错误非常愚蠢。我只是在 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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接