C

时间:2018-07-26 13:17:28

标签: c parallel-processing mpi

我正在尝试使用MPI库在C中编写程序,该库使用0 to 9010增量发送task 0数字float,并以顺序方式接收相同的数字,但转换为{ {1}}。

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

int main (int argc, char *argv[])
{
    int numranks, rank, tag=1, alpha, i;
    float beta;
    MPI_Status stats[10];

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numranks);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (rank == 0) {
        if (numranks > 2) 
            printf("Numranks=%d. Only 2 needed. Ignoring extra...\n",numranks);
        for (i=0; i<10; i++) {
            alpha = i*10;
            MPI_Send(&alpha, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
            printf("Task %d sent = %d\n",rank,alpha);
        }
    }

    if (rank == 1) {
        for (i=0; i<10; i++) {
            MPI_Recv(&beta, 1, MPI_FLOAT, 0, tag, MPI_COMM_WORLD, &stats[i]);
            printf("Task %d received = %f\n",rank,beta);
        }
    }

    MPI_Finalize();
}

我通过2个进程运行代码

mpirun -n 2 ./a.out

运行时结果如下:

Task 0 sent = 0
Task 0 sent = 10
Task 0 sent = 20
Task 0 sent = 30
Task 0 sent = 40
Task 0 sent = 50
Task 0 sent = 60
Task 0 sent = 70
Task 1 received = 0.000000
Task 0 sent = 80
Task 0 sent = 90
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000
Task 1 received = 0.000000

我打算得到的东西

Task 0 sent = 0
Task 0 sent = 10
Task 0 sent = 20
Task 0 sent = 30
Task 0 sent = 40
Task 0 sent = 50
Task 0 sent = 60
Task 0 sent = 70
Task 0 sent = 80
Task 0 sent = 90
Task 1 received = 0.000000
Task 1 received = 10.000000
Task 1 received = 20.000000
Task 1 received = 30.000000
Task 1 received = 40.000000
Task 1 received = 50.000000
Task 1 received = 60.000000
Task 1 received = 70.000000
Task 1 received = 80.000000
Task 1 received = 90.000000

如您所见,它没有正确接收到正确的消息,也没有按我希望的顺序运行。

0 个答案:

没有答案