MPI - 改变了启动的流程数量

时间:2014-12-21 22:53:47

标签: c parallel-processing mpi mpich

我是从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 个答案:

答案 0 :(得分:3)

我的错误非常愚蠢。在返回之前,我只是缺少 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;
}