我想在 MPI 中将两个矩阵相乘,但我的两个矩阵都是 vector 形式。
vector <vector <int> > Mat1;
MxN 顺序
vector <vector <int> > Mat2;
哪个是NxL阶
结果也是 vector 形式
vector <vector <int> > Mat3;
这是 MxL 订单。
我正在使用 MPI_BCAST 将值广播为
MPI_Bcast(Mat2.data(), Mat2.size(), MPI_INT, 0, MPI_COMM_WORLD);
并使用分散和收集函数接收数据作为
MPI_Scatterv(Mat1.data(), CNTS, SNTS, MPI_INT, MatPartS.data(), MatPartS.size(), MPI_INT, 0, MPI_COMM_WORLD);
for (int i = 0; i < myRowsSize; ++i)
{
for (int j = 0; j < L; ++j)
{
ResultMatPart[i][j]=0;
for (int k = 0; k < N; ++k)
{
ResultMatPart[i][j] += MatPartS[i][k] * Mat2[k][j];
}
}
}
MPI_Gatherv(ResultMatPart.data(), ResultMatPart.size(), MPI_INT, ResultMat.data(), RCNTS, SCNTS, MPI_INT, 0, MPI_COMM_WORLD);
Blockquote
我可以编译该程序,但它崩溃并且不产生任何输出。
它打印的消息是:
Fatal error in PMPI_Scatterv: Message truncated, error stack:
PMPI_Scatterv(376).....: MPI_Scatterv(sbuf=0x108fe00, scnts=0x108fef0, displs=0x108ffc0, MPI_INT, rbuf=0x1090270, rcount=4, MPI_INT, root=0, MPI_COMM_WORLD) failed
MPIR_Scatterv_impl(187):
MPIR_Scatterv(106).....:
MPIR_Localcopy(340)....: Message truncated; 64 bytes received but buffer size is 16
对如何在 MPI 中传递多维 STL vector 有帮助吗?
非常感谢
小号
最佳答案
那些 MPI 函数需要一个一维数组。您需要将 2D vector 收集到一个 vector 中。这只是为什么使用 vector 的 vector 来表示矩阵很糟糕的原因之一。
看看我针对其他人的 vector 对 vector 困境给出的这个答案。它提供了一个使用单个 vector 的简单矩阵类:
Get the first column of a matrix represented by a vector of vectors
关于c++ - 如何在 MPI 中使用多维 STL vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702418/