gpt4 book ai didi

c++ - MPI分布阵列

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:30 26 4
gpt4 key购买 nike

我正在尝试使用 MPI 结构编写 C++ 程序。我想从一个大文件中读取并将数字存储到一个数组中。我希望数组是本地的,即我不希望所有线程都拥有整个数组,因为数组非常庞大。每个线程都进行本地计算,并“发送”和“接收”以进行进一步计算。做这个的最好方式是什么?我在网上看到的所有代码都使用 rand() 函数生成本地数组,但我想从文件中读取值。

这可能是我想要的:

int main() 
{
// Read from a file
// store in array a[] temporarily
//MPI_init();
//My thread should have an array b[] that is a subset of a[]
//MY code to do a numerical simulation
//MPI_finalise();
return 0;
}

PS:我的数据结构比数组复杂。我存储了一个巨大的图表。它更像是一个链表数组或 vector 数组。

最佳答案

MPI 是进程而非线程之间的消息传递系统。当进程在不同的机器上运行时,这是一个真正的区别。

如果您想执行的操作完全独立于图表的每个部分,我会进行并行读取。其他原因如果你想要一个读取并传播一个数组你会得到类似的东西:

int main(int argc, char * argv[]) 
{
MPI_init(&argc, &argv);
int prank; MPI_Comm_rank(MPI_COMM_WORLD, &prank);
int psize; MPI_Comm_size(MPI_COMM_WORLD, &psize);
if(prank == 0) {
// Read from a file
// store in array a[] temporarily

MPI_Scatter(a, length(a)/psize, MPI_DATATYPE_OF_A, b, leanght(a)/psize, MPI_DATATYPE_OF_A, 0, MPI_COMM_WORLD);
// this works only if length(a) is a multiple of psize, otherwhy you should go for MPI_Scatterv
} else {
MPI_Scatter(NULL, 0, MPI_DATATYPE_OF_A, b, leanght(a)/psize, MPI_DATATYPE_OF_A, 0, MPI_COMM_WORLD);
}
//My thread should have an array b[] that is a subset of a[]
//MY code to do a numerical simulation

MPI_finalise();
return 0;
}

但是如果你有数组,这就是想法,如果你有一个图,你应该看一下图分区器来分割你的图并将碎片发送到不同的过程。我想Trilinos为你做一切。否则为什么你可以使用 Scotch或 Metis 为您的图形着色,然后使用 MPI 将每种颜色发送到处理器。

关于c++ - MPI分布阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082537/

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