为矩阵分配内存时,为什么会出现分段错误?

时间:2014-09-06 02:39:31

标签: c memory matrix segmentation-fault mpi

我正在研究一个带矩阵的MPI程序。我在每个过程中需要5个矩阵。当我创建第5个矩阵时,我得到了分段错误。

以下是一些截图:

当sPrevParts矩阵被注释掉时,它可以工作 enter image description here

这里有分段错误! :■ enter image description here 这里又是分段错误...... enter image description here

以下是代码的这一部分(如果您需要整个代码,请告诉我)。

MATRIX_CREATE FUNCTION

    /* M(m*n) as array of rows, call free(p) */
    void **matrix_create(size_t m, size_t n, size_t size) {
       size_t i; 
       void **p= (void **) malloc(m*n*size+ m*sizeof(void *));
       char *c=  (char*) (p+m);
       for(i=0; i<m; ++i)
          p[i]= (void *) c+i*n*size;
       return p;
    }

MAIN

    /* Variables for the partial matrixes */
    double **aParts, **mParts, **mPrevParts, **sParts, **sPrevParts;
    /* Gets the rows of the partial matrixes of each process */
    rows = sendcounts[myrank] / n;
    /* Allocates memory for the partial A matrix of each process */
    aParts = (double**)matrix_create(rows, n, sizeof(double));
    /* Allocates memory for the partial M matrix of each process */
    mParts = (double**)matrix_create(rows, n, sizeof(double));
    /* Allocates memory for the partial S matrix of each process */
    sParts = (double**)matrix_create(rows, n, sizeof(double));
    /* Allocates memory for the previous partial M matrix of each process */
    mPrevParts = (double**)matrix_create(rows, n, sizeof(double));
    /* Allocates memory for the previous partial S matrix of each process */
    //PrevParts = (double**)matrix_create(rows, n, sizeof(double));

    MPI_Barrier(MPI_COMM_WORLD);

    /* Scatters the A matrix through all the processes */
    MPI_Scatterv(&a[0][0], sendcounts, displs, MPI_DOUBLE, &aParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);        
    /* Scatters the M matrix through all the processes */
    MPI_Scatterv(&m[0][0], sendcounts, displs, MPI_DOUBLE, &mParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
    MPI_Scatterv(&m[0][0], sendcounts, displs, MPI_DOUBLE, &mPrevParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
    /* Scatters the S matrix through all the processes */
    MPI_Scatterv(&s[0][0], sendcounts, displs, MPI_DOUBLE, &sParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
    //MPI_Scatterv(&s[0][0], sendcounts, displs, MPI_DOUBLE, &sPrevParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);

    int i;
    for (i = 0; i < npes; ++i) {
        MPI_Barrier(MPI_COMM_WORLD);
        if (myrank == i) {
            printf("%d\n", i);
            matrix_print(aParts, rows, n, "aParts");
            matrix_print(mParts, rows, n, "mParts");
            matrix_print(sParts, rows, n, "sParts");
            matrix_print(mPrevParts, rows, n, "mPrevParts");
        }
    }

注意:所有流程都在运行。

难道我用尽了所有的记忆?我怎样才能解决这个问题?感谢

1 个答案:

答案 0 :(得分:1)

void *放弃了它:

malloc(m*n*size+ m*sizeof(void *));

你永远不会实际分配你的矩阵,而是只有一个二维指针数组,你可以将它们视为一个double的数组。

  1. 不要那样做。
  2. 您可能正在使用32位运行时开发,其中指针的大小只有double的一半(或指针只是小于double的其他类型的系统)。
  3. 在使用this example时,请考虑MPI有关如何使用矩阵的基本参考。

相关问题