MPI Matrix Multiplication with Dynamic Allocation: Seg. Fault

前端 未结 1 1966
长情又很酷
长情又很酷 2021-01-07 03:11

I\'m making a matriz multiplication program in OpenMPI, and I got this error message:

[Mecha Liberta:12337] *** Process received signal ***
[Mecha Liberta:12         


        
相关标签:
1条回答
  • 2021-01-07 04:01

    This is a common problem with C and multidimensional arrays and MPI.

    In this line, say:

    MPI_Send(&b, NCA*NCB, MPI_INT, dest, tag, MPI_COMM_WORLD);
    

    you're telling MPI to send NCAxNCB integers starting at b to dest,MPI_COMM_WORLD with tag tag. But, b isn't a pointer to NCAxNCB integers; it's a pointer to NCA pointers to NCB integers.

    So what you want to do is to ensure your arrays are contiguous (probably better for performance anyway), using something like this:

    int **alloc_2d_int(int rows, int cols) {
        int *data = (int *)malloc(rows*cols*sizeof(int));
        int **array= (int **)malloc(rows*sizeof(int*));
        for (int i=0; i<rows; i++)
            array[i] = &(data[cols*i]);
    
        return array;
    }
    
      /* .... */
    
      int **a, **b, **r;
    
      a = alloc_2d_int(l, c);
      b = alloc_2d_int(l, c);
      r = alloc_2d_int(l, c);
    

    and then

      MPI_Send(&sent, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); 
      MPI_Send(&lines, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);  
      MPI_Send(&(a[sent][0]), lines*NCA, MPI_INT, dest, tag, MPI_COMM_WORLD); 
      MPI_Send(&(b[0][0]), NCA*NCB, MPI_INT, dest, tag, MPI_COMM_WORLD);
    
    MPI_Recv(&sent, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);  
    MPI_Recv(&lines, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
    MPI_Recv(&(a[0][0]), lines*NCA, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
    MPI_Recv(&(b[0][0]), NCA*NCB, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); 
    

    should work more as expected.

    0 讨论(0)
提交回复
热议问题