0

I have two threads (rank 0 and 1), each containing a 3D matrix (x0,y,z) and (x1,y,z) with a different size along the x dimension. I would like to send a specific plane (x0 constant,y,z) from the first thread to the second and replace one of its face (x1 constant, y, z). The following code I made seems to work well when the two matrices have identical dimensions (even in x), but does not send the right face when x0 != x1 :

double ***alloc2(int x, int y,int z){

    int i, j;

    double ***array = (double ***) malloc(sizeof(double ***)*x);
    for (i = 0; i<x; i++){
        array[i] = (double **) malloc(sizeof(double*)*y);
        for (j=0; j<y; j++){
            array[i][j] = (double *) malloc(sizeof(double)*z);}}

    return array;
}


int main(int argc, char *argv[]){

MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD, &nbr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

/* Some long code I skiped */
/* ... */

MPI_Datatype sub;
MPI_Type_vector(nL+1, nL+1, nL_thread, MPI_DOUBLE, &sub);
MPI_Type_commit(&sub);

if(rank == 0){
    MPI_Send(&c_new[3][0][0], 1, sub, rank+1,01, MPI_COMM_WORLD);
    MPI_Recv(&c_new[4][0][0], 1, sub, rank+1,10, MPI_COMM_WORLD, &status);}
if(rank == 1){
    MPI_Recv(&c_new[0][0][0], 1, sub, rank-1,01, MPI_COMM_WORLD, &status);
    MPI_Send(&c_new[1][0][0], 1, sub, rank-1,10, MPI_COMM_WORLD);}

}

nL is the length in the y and z dimensions, same for all threads, nL_thread is the x dimension (in this particular case, nL_thread = 3 for rank 1 and 4 for rank 0). here I am trying to replace the faces (0,y,z) of rank 1 by (3,y,z) of rank 0, and (4,y,z) of rank 0 by (1,y,z) of rank 1.

Kyraz
  • 11
  • 4
  • `sizeof(double)` in all allocations does not seem right. In fact the whole allocation does not make much sense. – Osiris Nov 16 '18 at 06:59
  • If you are using thread parallelisation you should use a proper 3D array, not some segmented nonsense. Different threads accessing different regions of the heap will be inefficient. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) for ways to fix the program. – Lundin Nov 16 '18 at 08:52
  • Indeed Osiris, I edited my malloc function : – Kyraz Nov 16 '18 at 09:03
  • MPI thread is not the appropriate terminology here. You can use MPI task or MPI process in order to avoid any confusion. Did you allocate your 3D matrix in contiguous memory ? This is a requirement for what you are trying to achieve. Please edit your question with a [MCVE] in order to clarify what you are doing. – Gilles Gouaillardet Nov 16 '18 at 14:27

0 Answers0