使用MPI_Alltoallv时,内存范围重叠

时间:2014-03-23 17:39:53

标签: c mpi

我们尝试执行的任务是使用MPI创建一个解决p进程泊松问题的程序。当试图找到矩阵的转置时出现问题,并且我们必须使用MPI_Alltoallv,因为对于每个过程,完整矩阵被分成一个较小的矩阵。我们的代码不完整。我们到达转置部分时停止了,因为MPI_Alltoallv给了我们错误:

在第77行的文件src / mpid / ch3 / src / ch3u_buffer.c中断言失败:FALSE memcpy参数内存范围重叠,dst_ = 0x7fff5d5efa80 src_ = 0x7fff5d5efa88 len_ = 256,

在3个进程上运行时,n = 8。(mpirun -np 3 ./program 8)。

链接到失败的文件来源:ch3u_buffer.c

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <mpi.h>

typedef double Real;

/* function prototypes */
Real *createRealArray (int n);
Real **createReal2DArray (int m, int n);
void transpose (Real **bt, Real **b, int m);


int main(int argc, char **argv ) {
Real *diag, **b, **bt, *z; 
double *sndbuf, *rcvbuf;
Real pi, h, umax;
int i, j, n, m, k, nn, size, rank, err, rest, c;

/* the total number of grid points in each spatial direction is (n+1) */
/* the total number of degrees-of-freedom in each spatial direction is (n-1) */
/* this version requires n to be a power of 2 */

if( argc < 2 ) {
    printf("need a problem size\n");
    return 1;
}

//Problem size
n  = atoi(argv[1]);
m  = n-1;
nn = 4*n;

err = MPI_Init(&argc, &argv); /* Initialize MPI */
err = MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get nr of tasks */
err = MPI_Comm_rank(MPI_COMM_WORLD, &rank);    /* Get id of this process */
if (err != MPI_SUCCESS) {
    printf("MPI failed!\n");
    exit(1);
}

int* colArray;
int* sCount;
int* sDispl;
rest = m % size;

colArray = (int*) calloc(size, sizeof(int));
sCount = (int*) calloc(size, sizeof(int));
sDispl = (int*) calloc(size, sizeof(int));

for (i=0; i<size; i++){
    if (rest < size-i) {
        colArray[i] = floor(m/size);
    }
    else {
        colArray[i] = floor(m/size) + 1;
    }  
}

sDispl[0] = 0;
for (i=0; i<size; i++) {
    sCount[i] = colArray[rank]*colArray[i]*sizeof(double);
    if (i>0) sDispl[i] = sDispl[i-1] + sCount[i-1];
    // printf("Rank: %d, sCount[%d]: %d \n", rank, i, sCount[i]);
    // printf("Rank: %d, sDispl[%d]: %d \n", rank, i, sDispl[i]);
}

k = colArray[rank];

diag    = createRealArray (m);
b       = createReal2DArray (m,k);
//sndbuf  = createRealArray(m*k);
//rcvbuf  = createRealArray(m*k);
bt      = createReal2DArray (m,k);
z       = createRealArray (nn);

sndbuf   = (double  *)malloc(m*k*sizeof(double));
rcvbuf   = (double  *)malloc(m*k*sizeof(double));

h    = 1./(Real)n;
pi   = 4.*atan(1.);

for (i=0; i < m; i++) {
    diag[i] = 2.*(1.-cos((i+1)*pi/(Real)n));
}

for (j=0; j < m; j++) {
    for (i=0; i < k; i++) {
        b[j][i] = h*h;
    }
}

if (rank == 2) {
    printf("Matrise for rank: %d \n", rank);
    for (j=0; j < m; j++) {
        for (i=0; i < k; i++) {
            printf("%f \t", b[j][i]);
        }
        printf("\n");
    }
}

c = 0;
for (i=0; i<m; i++) {
    for (j=0; j<m; j++) {
        sndbuf[c] = (double)b[i][j];
        c++;
    }
}

if (rank == 2) {
    printf("sndbuf fra rank: %d \n", rank);
    for (i=0;i<m*k;i++) {
        printf("%f\n", sndbuf[i]);
    }
}   

if (rank == 2) {
    printf("sCount fra rank: %d \n", rank);
    for (i=0; i<size; i++) {
        printf("%d \t", sCount[i]);
    }
    printf("\n");
    printf("sDispl fra rank: %d \n", rank);
    for (i=0; i<size; i++) {
        printf("%d \t", sDispl[i]);
    }
}


err = MPI_Alltoallv(&sndbuf, sCount, sDispl, MPI_DOUBLE, 
                    &rcvbuf, sCount, sDispl, MPI_DOUBLE, 
                    MPI_COMM_WORLD);

if (err != MPI_SUCCESS) {
    printf("Error in MPI_Alltoallv!\n");
    exit(1);
}


// transpose (bt,b,m);


err = MPI_Finalize();          /* Terminate MPI */
if (err != MPI_SUCCESS) {
    printf("Error in MPI_Finalize!\n");
    exit(1);
}

return 0;
}

void transpose (Real **bt, Real **b, int m)
{
int i, j;
for (j=0; j < m; j++) {
for (i=0; i < m; i++) {
bt[j][i] = b[i][j];
}
}
}

Real *createRealArray (int n)
{
Real *a;
int i;
a = (Real *)malloc(n*sizeof(Real));
for (i=0; i < n; i++) {
a[i] = 0.0;
}
return (a);
}

Real **createReal2DArray (int n1, int n2)
{
int i, n;
Real **a;
a    = (Real **)malloc(n1   *sizeof(Real *));
a[0] = (Real  *)malloc(n1*n2*sizeof(Real));
for (i=1; i < n1; i++) {
a[i] = a[i-1] + n2;
}
n = n1*n2;
memset(a[0],0,n*sizeof(Real));
return (a);
}

我想MPI_Alltoallv的输入肯定有问题。有人可以帮我找出它是什么吗?对不起,如果代码有点乱。

编辑:我尝试从MPI_Alltoallv更改(&amp; sndbuf ..&amp; rcvbuf,...);到MPI_Alltoallv(sndbuf .. rcvbuf,...);有人说,但它仍然给出了同样的错误。

1 个答案:

答案 0 :(得分:1)

您错误地计算了MPI_Alltoallv的计数和位移。当您以字节为单位计算它们时,两者都应该以相应MPI数据类型的数据元素为单位:

sDispl[0] = 0;
for (i=0; i<size; i++) {
    sCount[i] = colArray[rank]*colArray[i]*sizeof(double);
                                          ^^^^^^^^^^^^^^^---- problem
    if (i>0) sDispl[i] = sDispl[i-1] + sCount[i-1];
    // printf("Rank: %d, sCount[%d]: %d \n", rank, i, sCount[i]);
    // printf("Rank: %d, sDispl[%d]: %d \n", rank, i, sDispl[i]);
}

加下划线的乘法很可能是罪魁祸首。 MPI尝试访问超过其长度的7倍的已分配内存。在同一线程内对malloc()的连续调用可能会返回位置紧密的内存块,因此会出现重叠错误。不要乘以sizeof(double)