gpt4 book ai didi

c - 由于某种原因,我的其他进程没有从我的第一个进程(C 中的 MPI)接收数据

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:46 27 4
gpt4 key购买 nike

我想做的是制作一个由 1 和 0 组成的数组,并将整个数组分成 4 个进程(不管我的测试用例有多少个 4)并让每个进程打印其等级、数组长度、和数组本身。但是当我运行代码时,我得到了这样的结果:

Enter length and sequence length
100 4
1101000010110110101110101
1110101001111100011100100
1011100110011100001011010
1000110101001000001000111

Rank: 0 Length: 25

我不确定为什么会这样。由于某种原因,我无法将数据正确传递给其他进程(我对 MPI 很陌生)

这是我的代码:

#include "ore_header.h"

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

srand(time(NULL));

int my_rank, p;
int total;
int length;
int seq_length;
void generate_sequence(int *arr, int n);
int subsequence_check(int *arr,int n, int m);


MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

MPI_Request reqs[p];
MPI_Status stats[p];
int p_length;
int p_total[p];
int *buf[p];
if (my_rank == 0) {
printf("Enter length and sequence length\n");
scanf("%d %d",&length, &seq_length);
p_length = length / p;
for (int i = 0; i < p; i++) {
buf[i] = (int*)malloc(p_length*sizeof(int));
generate_sequence(buf[i], p_length); //Generates a sequence
MPI_Isend(&buf[i], p_length, MPI_INT, i, 0, MPI_COMM_WORLD, &reqs[i]); //Send each buffer to each sequence
MPI_Wait(&reqs[my_rank], &stats[my_rank]); //Wait for data to be sent
}
}
MPI_Irecv(&buf[my_rank], p_length, MPI_INT, 0, 0, MPI_COMM_WORLD, &reqs[my_rank]); //Receive Data from process 0
MPI_Wait(&reqs[my_rank], &stats[my_rank]); //Wait for communication to end
printf("\nRank: %d Length: %d\n",my_rank,p_length);
for (int i = 0; i < p_length; i++) {
printf("%d",buf[i]);
}

//tot = subsequence_check(buf,length,seq_length);
//printf("\n\nTotal: %d\n",tot);
MPI_Finalize();
return (0);
}

更新

感谢您的反馈,我已经解决了问题,现在它可以按预期工作:

#include "ore_header.h"

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

srand(time(NULL));

int my_rank, p;
int total;
int length;
int flag;
int seq_length;
void generate_sequence(int *arr, int n);
int subsequence_check(int *arr,int n, int m);


MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);


int p_length;
int *buf[p];
if (my_rank == 0) {
printf("Enter length and sequence length\n");
scanf("%d %d",&length, &seq_length);
p_length = length / p;
for (int i = 0; i < p; i++) {
buf[i] = (int*)malloc(p_length*sizeof(int));
generate_sequence(buf[i], p_length);
MPI_Send(buf[i], p_length, MPI_INT, i, 0, MPI_COMM_WORLD);
printf("Data sent to process %d\n", i);
}
}
MPI_Bcast(&p_length, 1, MPI_INT, 0, MPI_COMM_WORLD);
buf[my_rank] = (int*)malloc(p_length*sizeof(int));
MPI_Status stats[p];
MPI_Request reqs[p];
MPI_Irecv(buf[my_rank], p_length, MPI_INT, 0, 0, MPI_COMM_WORLD, &reqs[my_rank]);
while(1) {
MPI_Test(&reqs[my_rank], &flag, &stats[my_rank]);
if (flag) break;
}
printf("\nData received on process: %d Length: %d\n",my_rank,p_length);
for (int i = 0; i < p_length; i++) {
printf("%d",buf[my_rank][i]);
}
printf("\n");

//tot = subsequence_check(buf,length,seq_length);
//printf("\n\nTotal: %d\n",tot);
MPI_Finalize();
return (0);
}

这是我的输出:

Enter length and sequence length
100 4
0101100011001110110010011
Data sent to process 0
0010011010000001001010111
Data sent to process 1
0011111111101101100000111
Data sent to process 2
0000000100010011100001111
Data sent to process 3

Data received on process: 0 Length: 25
0101100011001110110010011

Data received on process: 1 Length: 25
0010011010000001001010111

Data received on process: 2 Length: 25
0011111111101101100000111

Data received on process: 3 Length: 25
0000000100010011100001111

最佳答案

MPI_Isend() 需要一个指向缓冲区的指针,而 &buf[i] 是一个指向缓冲区指针的指针。因此,您可以尝试 MPI_Isend(buf[i],... 吗?

此外,请求reqs[i]是在MPI_Isend()中设置的,但是MPI_Wait()只检查的完成>要求[我的等级]。因此,可以建议另一种修改:

MPI_Isend(buf[i], p_length, MPI_INT, i, 0, MPI_COMM_WORLD, &reqs[i]); //Send each buffer to each sequence
MPI_Wait(&reqs[i], &stats[i]); //Wait for data to be sent

MPI_Isend() 之后立即调用 MPI_Wait() 似乎等同于调用 MPI_Send(),特别是如果未测试返回的 MPI_Status通过类似 MPI_Test_cancelled() 的方式.在本例中,对 MPI_Wait() 的连续调用可以很容易地替换为对 MPI_Waitall() 的单个调用。在 for 循环的末尾。

编辑:@a3f 关于 MPI_Irecv()p_length 的值不是 0 的问题仍然有效...

关于c - 由于某种原因,我的其他进程没有从我的第一个进程(C 中的 MPI)接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817339/

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