gpt4 book ai didi

使用 MPI 连接文本文件

转载 作者:行者123 更新时间:2023-11-30 19:37:10 24 4
gpt4 key购买 nike

有没有一种简单的方法可以将来自多个内核的多个文本文件连接到一个内核上。

核心0

1 4 6 4 2 6
4 5 4 2 4 7
3 5 6 7 8 5

核心1

5 6 7 5 3 6
5 6 7 8 5 4
6 4 3 5 6 7

核心2

6 7 8 5 3 6
4 5 7 3 4 5
8 7 6 5 2 3
6 7 8 6 5 4
8 9 0 3 2 1

我想将核心 0 上的文本文件与核心 1 和核心 2 中的文本文件一起添加。我知道这类似于...

int textSize = ...; // size of text file on each core 
if (rank == 0) {
int size = malloc(sizeof(float) * textSize);
}
MPI_Gather(&name_of_text_file, textSize, MPI_FLOAT, *size, textSize, MPI_FLOAT, 0, MPI_COMM_WORLD);

如有任何帮助,我们将不胜感激。

最佳答案

我知道您指的不是文本文件,而是通过指针访问的算术数据。我也理解您的问题是并非所有进程都具有相同大小的数据。

在这种情况下,您可能正在寻找 MPI_Gatherv功能。它的工作原理与 MPI_Gather 函数非常相似,但您可以为每个进程定义将发送多少数据(recvcounts 数组,在接收级别指定)以及将它们放入目标位置(displs 数组)。

以下是 MPI Forum 文档中的两个示例:

简单 MPI_Gather,其中只有主级别为完整的数据数组分配内存:

MPI_Comm comm; 
int gsize,sendarray[100];
int root, myrank, *rbuf;
...
MPI_Comm_rank( comm, myrank);
if ( myrank == root) {
MPI_Comm_size( comm, &gsize);
rbuf = (int *)malloc(gsize*100*sizeof(int));
}
MPI_Gather( sendarray, 100, MPI_INT, rbuf, 100, MPI_INT, root, comm);

Example of MPI_Gather (from MPI Forum)

在此示例中,所有进程都有 100 个整数 block 。根据您的情况,您可能需要将 int 更改为 float

MPI_Gatherv:

MPI_Comm comm; 
int gsize,sendarray[100];
int root, *rbuf, stride;
int *displs,i,*rcounts;
...
MPI_Comm_size( comm, &gsize);
rbuf = (int *)malloc(gsize*stride*sizeof(int));
displs = (int *)malloc(gsize*sizeof(int));
rcounts = (int *)malloc(gsize*sizeof(int));
for (i=0; i<gsize; ++i) {
displs[i] = i*stride;
rcounts[i] = 100;
}
MPI_Gatherv( sendarray, 100, MPI_INT, rbuf, rcounts, displs, MPI_INT,
root, comm);

Example of MPI_Gatherv (from MPI Forum)

在此示例中,接收相同数量的元素(100 个整数)并定期放入由 stride 整数分隔的内存区域中。目标数组 (rbuf) 可以存储 (number_of_ranks * stride) 个整数。

您完全可以忽略固定大小的步幅,并为每个进程单独设置displsrcounts(如果您知道的话)。如果您不知道它们,请查看 MPI 论坛页面中的最后一个示例。

无论如何,我假设您对数据使用单指针(例如 array[i])而不是双指针(例如 array[i][j])。这需要一些额外的努力来设置正确的索引,但在 MPI 中使用更简单,并且在某些情况下也更快。

关于使用 MPI 连接文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40228635/

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