MPI分布阵列

MPI distributing array

本文关键字:阵列 分布 MPI      更新时间:2023-10-16

我正试图用MPI结构写一个c++程序。我想从一个巨大的文件中读取,并将数字存储到一个数组中。我希望数组是本地的,也就是说,我不希望所有线程都拥有整个数组,因为数组非常庞大。每个线程进行本地计算,并为进一步的计算"发送"answers"接收"。最好的方法是什么?我在网上看到的所有代码都使用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:我的数据结构比数组更复杂。I存储了一个巨大的图。它更像是一个链表数组或者向量数组。

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;
}

但是如果你有数组,如果你有一个图,你应该看看图分区器来分割你的图,并将碎片发送到不同的进程。我觉得崔莉诺斯为你做了一切。否则,你可以使用Scotch或Metis为你的图形上色,然后使用MPI将每种颜色发送给处理器。