gpt4 book ai didi

c - 使用 MPI_Allgather 分发结构

转载 作者:行者123 更新时间:2023-12-04 10:40:11 24 4
gpt4 key购买 nike

我必须使用 MPI_Allgather() 向所有进程发送一个结构。我似乎没有收到任何明显的错误,但是代码不起作用。当我检查我是否在 recv[] 中收到任何值时,它显示没有。如果我只发送一个变量而不是使用类似代码的结构,那是可行的,所以我不确定发生了什么。该结构具有静态数组,因此内存应该是连续的,或者我应该使用 MPI_Pack 还是什么?这是代码:

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

#define NUMEL 21
struct mystruct{
int sendarray[10];
int a;
char array2[10];
};

typedef struct mystruct struct_t;

int main (int argc, char ** argv)
{
MPI_Status status;
int rank, size;
char *recv;
int i, j;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// init
struct_t * fd = (struct_t*)malloc(sizeof(*fd));;
for (i=0;i<10;i++){
fd->sendarray[i] = 0;
fd->array2[i] = 0;
}
recv = (char *) malloc ( size*NUMEL);

// put some stuff in your array
for (i=0;i<size;i++){
if(rank == i){
fd->sendarray[i] = i *10;
fd->array2[i] = i *20;
fd->a = rank;
}
if(fd->sendarray[i] != 0)
printf("My rank is %d, fd->sendarray[%d] is %d\n", rank, i, fd->sendarray[i]);
}

// gather data from all now..
MPI_Allgather (fd, NUMEL, MPI_BYTE, recv, NUMEL * size, MPI_INT, MPI_COMM_WORLD);

// check if correct data has been received
for (i=0;i<size*NUMEL;i++){
if(recv[i] != 0)
printf("My rank is %d and recv[i]=%d and i is %d\n", rank, recv[i],i);
}
MPI_Finalize();
}

最佳答案

第一次看到 Allgather 时可能会有些困惑。这里发生了一些事情。

首先,要收集的计数——发送计数和接收计数——是每个进程发送的数据量,以及从每个进程接收的数据量.

其次,allgather 的工作方式是连接发送的数据。所以如果你有

int send[3];
int recv[9];

每个进程的发送数组如下所示:

send:
+---+---+---+
| 0 | 0 | 0 | rank 0
+---+---+---+

+---+---+---+
| 1 | 1 | 1 | rank 1
+---+---+---+

+---+---+---+
| 2 | 2 | 2 | rank 2
+---+---+---+

然后调用

MPI_Allgather(send, 3, MPI_INT,  recv, 3, MPI_INT,  MPI_COMM_WORLD);

会导致:

recv:
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 1 | 1 | 1 | 2 | 2 | 2 |
+---+---+---+---+---+---+---+---+---+

因此,提取正确数据的代码版本是:

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

struct mystruct{
int sendarray[10];
int a;
char array2[10];
};

typedef struct mystruct struct_t;

int main (int argc, char ** argv)
{
int rank, size;
struct_t *recv;
int i, j;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// init
struct_t * fd = (struct_t*)malloc(sizeof(*fd));
for (i=0;i<10;i++){
fd->sendarray[i] = 0;
fd->array2[i] = 0;
}
recv = malloc ( size * sizeof(*fd) );

// put some stuff in your array
fd->sendarray[rank] = rank*10;
fd->array2[rank] = rank*20;
fd->a = rank;
printf("My rank is %d, fd->sendarray[%d] is %d\n", rank, i, fd->sendarray[i]);

// gather data from all now..
MPI_Allgather (fd, sizeof(*fd), MPI_BYTE, recv, sizeof(*fd), MPI_BYTE, MPI_COMM_WORLD);


// check if correct data has been received
if (rank == 0) {
printf("Received:\n");
for (i=0;i<size;i++){
printf("---\n");
printf("int array: ");
for (j=0; j<10; j++) printf("%3d ", recv[i].sendarray[j]);
printf("\nint: "); printf("%3d\n", recv[i].a);
printf("char array: ");
for (j=0; j<10; j++) printf("%3d ", (int)(recv[i].array2[j]));
printf("\n");
}
}
MPI_Finalize();
return 0;
}

请注意,它将这些结构收集到相当于这些结构的数组中。使用 4 个处理器运行可以得到:

My rank is 0, fd->sendarray[10]  is 0
My rank is 1, fd->sendarray[10] is 1
My rank is 2, fd->sendarray[10] is 2
My rank is 3, fd->sendarray[10] is 3

Received:
---
int array: 0 0 0 0 0 0 0 0 0 0
int: 0
char array: 0 0 0 0 0 0 0 0 0 0
---
int array: 0 10 0 0 0 0 0 0 0 0
int: 1
char array: 0 20 0 0 0 0 0 0 0 0
---
int array: 0 0 20 0 0 0 0 0 0 0
int: 2
char array: 0 0 40 0 0 0 0 0 0 0
---
int array: 0 0 0 30 0 0 0 0 0 0
int: 3
char array: 0 0 0 60 0 0 0 0 0 0

如果您真的只想收集相应的元素,那么您只需从结构中的特定位置发送一个 int/char 即可:

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

struct mystruct{
int sendarray[10];
int a;
char array2[10];
};

typedef struct mystruct struct_t;

int main (int argc, char ** argv)
{
int rank, size;
struct_t fd;
struct_t recv;
int i, j;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// init
for (i=0;i<10;i++){
fd.sendarray[i] = 0;
fd.array2[i] = 0;

recv.sendarray[i] =999;
recv.array2[i] = 99;
}
recv.a =999;

// put some stuff in your array
fd.sendarray[rank] = rank*10;
fd.array2[rank] = (char)(rank*20);
fd.a = rank;
printf("My rank is %d, fd.sendarray[%d] is %d\n", rank, rank, fd.sendarray[rank]);

// gather data from all now.. send the int:
MPI_Allgather (&(fd.sendarray[rank]), 1, MPI_INT, recv.sendarray, 1, MPI_INT, MPI_COMM_WORLD);
// then the char
MPI_Allgather (&(fd.array2[rank]), 1, MPI_CHAR, recv.array2, 1, MPI_CHAR, MPI_COMM_WORLD);

// check if correct data has been received
if (rank == 0) {
printf("Received:\n");
printf("---\n");
printf("int array: ");
for (j=0; j<10; j++) printf("%3d ", recv.sendarray[j]);
printf("\nint: "); printf("%3d\n", recv.a);
printf("char array: ");
for (j=0; j<10; j++) printf("%3d ", (int)(recv.array2[j]));
printf("\n");
}
MPI_Finalize();

return 0;
}

如果我们用 4 个进程运行它,我们会得到:

My rank is 0, fd.sendarray[0]  is 0
My rank is 1, fd.sendarray[1] is 10
My rank is 2, fd.sendarray[2] is 20
My rank is 3, fd.sendarray[3] is 30
Received:
---
int array: 0 10 20 30 999 999 999 999 999 999
int: 999
char array: 0 20 40 60 99 99 99 99 99 99

关于c - 使用 MPI_Allgather 分发结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6603750/

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