我正在尝试收集在不同进程上计算的 2 个矩阵,我正在使用 mpi allgatherv 函数来这样做,但我对结果感到惊讶。
在我正在处理的示例中,矩阵的形状是(20,80),我想将它们收集到一个具有(40,80)形状的矩阵中;由进程 0 计算的矩阵存储在前 20 行中。
这是我正在使用的代码示例(示例中的 nbRegimes 为 0):
boost::mpi::communicator world;
int rank = world.rank();
std::vector< ArrayXXd> phiOutLoc(nbRegimes);
for (int iReg = 0; iReg < nbRegimes; ++iReg)
phiOutLoc[iReg].resize(nScenarioCur * nDimension, nbPoints);
... computation and storage into phiOutLoc[iReg] ...
//gathering results
ArrayXXd storeGlob(nScenarios * nDimension, nbPoints);
for (int iReg = 0; iReg < nbRegimes; ++iReg)
{
boost::mpi::all_gatherv<double>(world, phiOutLoc[iReg].data(), phiOutLoc[iReg].size(), storeGlob.data());
}
例如,这是在进程 0 和进程 1 上计算的 phiOutLoc[iReg] 第一行的开头:
rank 0
0 -353509 -366699 -379888 -393077 -406267 -419456 -432646 ...
rank 1
0 -399021 -413908 -428795 -443683 -458570 -473457 -488345 ...
这些行应分别存储在 storeGlob 的索引 0 行和索引 20 行中;如果我正确理解了 all_gatherv 函数的行为。
但是 storeGlob 的索引 0 行看起来像这样:
storeGlob row 0:
0 -366699 -393077 -419456 ... 0 -413908 -443683 -473457
和 storeGlob 的索引 20 行:
storeGlob row 20:
-353509 -379888 -406267 -432646 ... -399021 -428795 -458570 -488345
storeGlob 的索引 0 行由 phiOutLoc[iReg] 矩阵的第一行的偶数索引填充。奇数索引存储在索引 20 行中。
我真的不明白为什么会以这种方式进行。
这种行为是否正常,有没有办法按照我想要的方式收集两个矩阵?