简体   繁体   English

MPI向自己发送和接收大数据

[英]MPI send and receive large data to self

I try to send and receive a large array to self-rank using MPI.我尝试使用 MPI 发送和接收一个大型数组以进行自我排序。 The code works well for small arrays (array size <10000) when I further increase the array size to 100000, it will deadlock.当我将数组大小进一步增加到 100000 时,该代码适用于小型 arrays(数组大小<10000),它会死锁。 Here is my code:这是我的代码:

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

int main(int argc, char *argv[])
{

MPI_Init(&argc, &argv);

int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);

int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Status stat;


    int N = 100000;

    // Allocate memory for A on CPU
    double *A = (double*)malloc(N*sizeof(double));
    double *B = (double*)malloc(N*sizeof(double));

    // Initialize all elements of A to 0.0
    for(int i=0; i<N; i++){
        A[i] = 0.0;
    }

    int tag1 = 10;
    int tag2 = 20;

//printf("rank=%d",rank);


    MPI_Send(A, N, MPI_DOUBLE, 0, tag1, MPI_COMM_WORLD);
    
    MPI_Recv(B, N, MPI_DOUBLE, 0, tag1, MPI_COMM_WORLD, &stat);

MPI_Finalize();

return 0;
}

I compile the code by: mpicc我通过以下方式编译代码:mpicc
command for the run: mpirun -np 1./运行命令:mpirun -np 1./

That's the definition of send and receive: they are blocking.这就是发送和接收的定义:它们是阻塞的。 The statement after the send will not execute until the send has been successfully completed.在发送成功完成之前,发送之后的语句不会执行。

  1. The safe way to solve this is using MPI_Isend and MPI_Irecv .解决此问题的安全方法是使用MPI_IsendMPI_Irecv
  2. The case for a small messages which don't block is an optimization that is system dependent and you can not count on it.不阻塞的小消息的情况是依赖于系统的优化,您不能指望它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM