MPI_Send双指针

时间:2016-01-20 20:49:07

标签: c++ segmentation-fault mpi

我有以下代码:

double * myX;
double * myY;
double * myZ;
int amount;
int count;      // number of process

void SomeClass::someMethod(double *x, double *y, double *z, int amount) {

    if (myId == 0) {
        myX = x;
        myY = y;
        myZ = z;
        amount = amount;
        for(int i = 1; i < count; ++i) {
            MPI_Send(&amount, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
            MPI_Send(myX, amount, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
            MPI_Send(myY, amount, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
            MPI_Send(myX, amount, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
        }
    }                                         
}

void SomeClass::anotherMethod(void) {

    if(myId != 0) {
        MPI_Recv(&amount, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Recv(myX, amount, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Recv(myY, amount, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Recv(myZ, amount, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);         
    }

    // rest of code
    MPI_Reduce(args);
}

但我遇到问题,因为我在运行此代码时收到Null buffer pointer或在更改内容时收到Segmentation fault,例如在var name之前设置&然后运行。

MPI_init和其他必需的函数在其他类中调用,我也创建了这个类对象。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

MPI_Recv会将收到的数据复制到第一个参数指定的缓冲区(以下情况下为myX):

MPI_Recv(myX, amount, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

问题在于你还没有创建缓冲区来存储它。

你可以这样做:

myX = new double[amount];

例如,要创建缓冲区,请不要忘记以后再次释放内存:

delete[] myX;