MPI将大量数据发送给自我

发布于 2025-02-12 02:58:33 字数 942 浏览 0 评论 0原文

我尝试使用MPI发送并接收一个大型阵列。该代码适用于小阵列(阵列尺寸< 10000)时,当我将阵列大小进一步增加到100000时,它将死去。 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
运行的命令:Mpirun -NP 1 ./

I try to send and receive a large array to self-rank using MPI. The code works well for small arrays (array size <10000) when I further increase the array size to 100000, it will deadlock. 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
command for the run: mpirun -np 1 ./

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2025-02-19 02:58:33

这就是发送和接收的定义:它们正在阻止。在发送发送后,发送后的声明将不会执行。

  1. 解决此问题的安全方法是使用mpi_isendmpi_irecv
  2. 一个不阻止的小消息的情况是系统依赖的优化,您无法指望它。

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.
  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文