joaorodr84
joaorodr84

Reputation: 1312

Why do I get a segmentation fault when I allocate memory for a matrix?

I am working on an MPI program with matrixes. I need 5 matrixes in each process. When I create the 5th matrix, I get a segmentation fault.

Here are some screenshots:

Here it work, when the sPrevParts matrix is commented out enter image description here

Here it does a segmentation fault! :s enter image description here Here again the segmentation fault... enter image description here

Here is this part of the code (if you need the whole code, tell me, please).

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");
        }
    }

NOTE: This is being run by ALL the processes.

Could it be that I have used up all the memory? How can I fix this? Thanks

Upvotes: 0

Views: 209

Answers (1)

Domi
Domi

Reputation: 24528

The void * gives it away:

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

You never actually allocate your matrix, but instead only a 2D array of pointers which you then treat as an array of double.

  1. Don't ever do that.
  2. You are probably developing with a 32-bit run-time, where your pointers are only half the size of double (or some other kind of system where pointers are simply smaller than double).

Consider this example for a basic reference on how to work with matrices when using MPI.

Upvotes: 1

Related Questions