MPI printf和发送/接收字符串

时间:2018-06-07 20:09:07

标签: c mpi send

我是MPI的新人。我试图通过Send和Recv发送字符串(稍后将是文件路径)。但是,输出很奇怪。我究竟做错了什么?

我有两台名为buckeye和paso的机器(以七叶树为主)。我的主机文件是:

paso slots=4
buckeye slots=4

我使用以下内容编译(下面的源文件helloworld.c):

mpicc helloworld.c -o hello

然后我用:

运行它
mpirun -np 8 --hostfile hosts ./hello "Test argument"

我有等级4 - 主 - 将消息“你好”发送到等级5(同一台机器),然后打印出来。我还打印出处理器的等级和名称。 这是源代码

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

int main(int argc, char** argv){
    MPI_Init(&argc, &argv);
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    printf("Hello world from processor %s, rank %d, out of %d processors...\n",
        processor_name, rank, world_size);
    //printf("Input argument is %s\n", argv[1]);

    /* communication */
    if (rank == 4){
        char * test  = "hello";
        printf("I am rank 4\n");
        MPI_Send(&test, 7, MPI_CHAR, 5, 0, MPI_COMM_WORLD);
    }
    else if (rank == 5){
        char * buf[10];
        MPI_Status status;
        int ierr;
        ierr = MPI_Recv(&buf, 7, MPI_CHAR, 4, 0, MPI_COMM_WORLD, &status);
        if (ierr == MPI_SUCCESS)
            printf("Rank %d (processor %s) received value %s.\n", rank, processor_name, buf);
        else   
            printf("Rank %d (processor %s) did not sucessfully receive a value!\n", rank, processor_name);
    }
    else
        printf("I'm rank %d\n", rank);
    MPI_Finalize();
}

我有两个主要问题:

1)为什么第5级收到的价值不好?

2)为什么不排名0-3打印出第二个printf(即“我是排名x”)? 他们打印出第一行。

这是输出

Hello world from processor buckeye, rank 4, out of 8 processors...
I am rank 4
Hello world from processor buckeye, rank 5, out of 8 processors...
Rank 5 (processor buckeye) received value a
                                           @.
Hello world from processor buckeye, rank 6, out of 8 processors...
I'm rank 6
Hello world from processor buckeye, rank 7, out of 8 processors...
I'm rank 7
Hello world from processor paso, rank 0, out of 8 processors
Hello world from processor paso, rank 1, out of 8 processors
Hello world from processor paso, rank 2, out of 8 processors
Hello world from processor paso, rank 3, out of 8 processors

提前非常感谢!

0 个答案:

没有答案