MPI_gather中的致命错误(使用MPI进行矩阵乘法) [英] Fatal error in MPI_gather (matrix multiplication using MPI)
问题描述
您好,我最近刚刚在MPI(OpenMP)之后乱搞我的并行编程!我运行了一个代码,用于将虚拟机(Ubuntu)上的矩阵与4个内核相乘。不幸的是,我收到了一个错误:
Hello, I've just recently been messing around with my parallel programming in MPI (OpenMP after this!) and I ran a code to multiply matrices on a virtual machine (Ubuntu) with 4 cores. Unfortunately, I got an error:
PMPI_Gather中的致命错误:无效的缓冲区指针,错误堆栈:
PMPI_Gather(856):MPI_Gather(sbuf = 0x601d20,scount = 100,MPI_FLOAT,rbuf = 0x601d20,rcount = 100,MPI_FLOAT,root = 0,MPI_COMM_WORLD)失败>
PMPI_Gather(797):缓冲区不能别名
Fatal error in PMPI_Gather: Invalid buffer pointer, error stack:
PMPI_Gather(856): MPI_Gather(sbuf=0x601d20, scount=100, MPI_FLOAT, rbuf=0x601d20, rcount=100, MPI_FLOAT, root=0, MPI_COMM_WORLD) failed
PMPI_Gather(797): Buffers must not be aliased
这是从SpeedGoComputing获得的代码:
This is the code obtained from SpeedGoComputing:
#include <mpi.h>
const int size = 20;
float a[20][20];
float b[20][20];
float c[20][20];
void multiply(int istart, int iend)
{
int i,j,k;
for (i = istart; i <= iend; ++i) {
for (j = 0; j < size; ++j) {
for (k = 0; k < size; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
int main(int argc, char* argv[])
{
int rank, nproc;
int istart, iend;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
// Initialize buffers.
int i;
int j;
for (i = 0; i < size; ++i) {
for (j = 0; j < size; ++j) {
a[i][j] = (float)i + j;
b[i][j] = (float)i - j;
c[i][j] = 0.0f;
}
}
}
// Broadcast matrices to all workers.
MPI_Bcast(a, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
MPI_Bcast(b, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
MPI_Bcast(c, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
// Partition work by i-for-loop.
istart = (size / nproc) * rank;
iend = (size / nproc) * (rank + 1) - 1;
// Compute matrix multiplication in [istart,iend]
// of i-for-loop.
// C <- C + A x B
multiply(istart, iend);
// Gather computed results.
MPI_Gather(c + (size/nproc*rank),
size*size/nproc,
MPI_FLOAT,
c + (size/nproc*rank),
size*size/nproc,
MPI_FLOAT,
0,
MPI_COMM_WORLD);
if (rank == 0) {
// Compute remaining multiplications
// when size % nproc > 0.
if (size % nproc > 0) {
multiply((size/nproc)*nproc, size-1);
}
}
MPI_Finalize();
return 0;
}
我的尝试:
我怀疑MPI_Gather的sendbuf参数有问题,所以我将其更改为MPI_IN_PLACE,但即使这样也行不通。我也尝试了MPI_gatherV,但是没有用。
我试过在Visual Studio中调试它,它说有一个断点。在if(rank == 0):行。也许它与指针和地址有关,我不太确定。我会读它。
如果有人可以提供帮助,请谢谢!我不是很擅长C.
What I have tried:
I suspect there's something wrong with the sendbuf argument of MPI_Gather, so I changed it to MPI_IN_PLACE but even that didn't work. I tried MPI_gatherV too and that didn't work.
I've tried debugging this in Visual Studio and it says there was a breakpoint reached. At the "if (rank == 0)": line. Perhaps it has something to do with pointers and addresses, I'm not very sure. I will be reading up on it.
If anyone could help, please and thank you! I'm not very adept at C.
推荐答案
在你的电话中打电话给
In your call to
MPI_Gather(c + (size/nproc*rank),
size*size/nproc,
MPI_FLOAT,
c + (size/nproc*rank),
size*size/nproc,
MPI_FLOAT,
0,
MPI_COMM_WORLD);
发送和接收缓冲区相同,不使用 MPI_IN_PLACE [ ^ ]选项。
这篇关于MPI_gather中的致命错误(使用MPI进行矩阵乘法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!