gpt4 book ai didi

c - MPI 将非阻塞发送和接收与 C 中的 waitall 结合起来

转载 作者:行者123 更新时间:2023-11-30 14:49:15 26 4
gpt4 key购买 nike

我需要编写一个程序,使用 MPI 非阻塞发送和接收调用来与合作伙伴任务进行通信。伙伴任务是任务等级 + numberOfTask/2,反之亦然。发送给合作伙伴的消息是发送者排名。

我尝试过这样做:

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define MASTER 0

int main (int argc, char *argv[]) {
int i, numtasks, taskid, len, partner, message;
char hostname[MPI_MAX_PROCESSOR_NAME];

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

MPI_Request reqsTx[numtasks], reqsRx[numtasks];
MPI_Status stats[numtasks];

/* need an even number of tasks */
if (numtasks % 2 != 0) {
if (taskid == MASTER)
printf("Quitting. Need an even number of tasks: numtasks=%d\n", numtasks);
} else {
if (taskid == MASTER)
printf("MASTER: Number of MPI tasks is: %d\n", numtasks);

MPI_Get_processor_name(hostname, &len);
printf ("Hello from task %d\n", taskid);

/* determine partner and then send to partner */
if (taskid < numtasks/2) {
partner = numtasks/2 + taskid;
MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsTx[taskid]);
} else if (taskid >= numtasks/2) {
partner = taskid - numtasks/2;
MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsTx[taskid]);
}

printf ("Send %d completed\n", taskid);
MPI_Waitall(numtasks, reqsTx, stats);
printf("Done waiting for transmit!");

/* determine partner and then receive from partner */
if (taskid < numtasks/2) {
MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsRx[taskid]);
} else if (taskid >= numtasks/2) {
MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsRx[taskid]);
}

printf ("Receive %d completed\n", taskid);
MPI_Waitall(numtasks, reqsRx, stats);
printf("Done waiting for receive!");

/* print partner info and exit*/
printf("Task %d is partner with %d\n", taskid, message);
}
MPI_Finalize();
}

但是第一个 MPI_Waitall(numtasks, reqsTx, stats);失败并且程序终止。有人知道我在这里做错了什么吗?

致以诚挚的问候

最佳答案

我想我发现了这个问题。问题似乎是我试图创建超出需要的请求。

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define MASTER 0

int main (int argc, char *argv[]) {
int i, numtasks, taskid, len, partner, message;
char hostname[MPI_MAX_PROCESSOR_NAME];

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

MPI_Status stats[2];
MPI_Request reqs[2];

/* need an even number of tasks */
if (numtasks % 2 != 0) {
if (taskid == MASTER)
printf("Quitting. Need an even number of tasks: numtasks=%d\n", numtasks);
} else {
if (taskid == MASTER)
printf("MASTER: Number of MPI tasks is: %d\n", numtasks);

MPI_Get_processor_name(hostname, &len);
printf ("Hello from task %d\n", taskid);

if (taskid < numtasks/2)
partner = numtasks/2 + taskid;
else if (taskid >= numtasks/2)
partner = taskid - numtasks/2;

MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqs[0]);
MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqs[1]);

MPI_Waitall(2, reqs, stats);
if (taskid == MASTER)
printf("Done waiting!\n");

/* print partner info and exit*/
printf("Task %d is partner with %d\n", taskid, message);
}
MPI_Finalize();
}

关于c - MPI 将非阻塞发送和接收与 C 中的 waitall 结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com