我是一名MPI新手,正在编写一个简单的MPI程序,用于计算矩阵和向量的点积,即A*b=c。然而,我的代码不起作用。以下是源代码。
如果我将A、b、c和buffer的声明替换为
double A[16], b[4], c[4], buffer[8];
请在代码中注释与分配和释放操作相关的行,我的代码可以正常工作并且结果是正确的。在这种情况下,我想问题应该与指针相关,但我不知道如何解决问题。
还有一件事,在我的代码中,缓冲区只有4个元素,但缓冲区大小必须大于8,否则它不起作用。
#include<mpi.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int nx = 4, ny = 4, nxny;
int ix, iy;
double *A = nullptr, *b = nullptr, *c = nullptr, *buffer = nullptr;
double ans;
// info MPI
int myGlobalID, root = 0, numProc;
int numSent;
MPI_Status status;
// functions
void get_ixiy(int);
int main(){
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &numProc);
MPI_Comm_rank(MPI_COMM_WORLD, &myGlobalID);
nxny = nx * ny;
A = new double(nxny);
b = new double(ny);
c = new double(nx);
buffer = new double(ny);
if(myGlobalID == root){
// init A, b
for(int k = 0; k < nxny; ++k){
get_ixiy(k);
b[iy] = 1;
A[k] = k;
}
numSent = 0;
// send b to each worker processor
MPI_Bcast(&b, ny, MPI_DOUBLE, root, MPI_COMM_WORLD);
// send a row of A to each worker processor, tag with row number
for(ix = 0; ix < min(numProc - 1, nx); ++ix){
for(iy = 0; iy < ny; ++iy){
buffer[iy] = A[iy + ix * ny];
}
MPI_Send(&buffer, ny, MPI_DOUBLE, ix+1, ix+1, MPI_COMM_WORLD);
numSent += 1;
}
for(ix = 0; ix < nx; ++ix){
MPI_Recv(&ans, 1, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int sender = status.MPI_SOURCE;
int ansType = status.MPI_TAG;
c[ansType] = ans;
// send another row to worker process
if(numSent < nx){
for(iy = 0; iy < ny; ++iy){
buffer[iy] = A[iy + numSent * ny];
}
MPI_Send(&buffer, ny, MPI_DOUBLE, sender, numSent+1,
MPI_COMM_WORLD);
numSent += 1;
}
else
MPI_Send(MPI_BOTTOM, 0, MPI_DOUBLE, sender, 0, MPI_COMM_WORLD);
}
for(ix = 0; ix < nx; ++ix){
std::cout << c[ix] << " ";
}
std::cout << std::endl;
delete [] A;
delete [] b;
delete [] c;
delete [] buffer;
}
else{
MPI_Bcast(&b, ny, MPI_DOUBLE, root, MPI_COMM_WORLD);
if(myGlobalID <= nx){
while(1){
MPI_Recv(&buffer, ny, MPI_DOUBLE, root, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if(status.MPI_TAG == 0) break;
int row = status.MPI_TAG - 1;
ans = 0.0;
for(iy = 0; iy < ny; ++iy) ans += buffer[iy] * b[iy];
MPI_Send(&ans, 1, MPI_DOUBLE, root, row, MPI_COMM_WORLD);
}
}
}
MPI_Finalize();
return 0;
} // main
void get_ixiy(int k){
ix = k / ny;
iy = k % ny;
}
下面列出了错误信息。
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 7455 RUNNING AT ***
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault:
11 (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
b[k] = 1
时,会发生数组溢出。我建议您保持代码简单,并声明double A[4][4]
。 - Gilles GouaillardetMPI_Bcast(b, ...)
而不是...(&b)
。 - Gilles Gouaillardet