简体   繁体   中英

Sending a column of a dynamically allocated 2d array in MPI

I'm working on a project where i would use MPI for parallel programming, i will be using point to point (send/recv) communications and collective communications (MPI_Gatherv ,...), I'm allocating the local arrays as contiguous 2d arrays, and I need to send the edge column of an array to another, which i tried as shown in the code below. Now, The below code produces almost the correct results, except for a strange change in an element in the received array as follows

2 3 3 3 3
2 3 3 3 3
2 3 3 3 3
2 3 3 3 3
2 0 3 3 3

The element b[4][1] = 0 !! is the problem, i can't see why this element is modified although it's not located in the range of the datatype g_col , i checked the receive message using MPI_Get_count and it shows that 5 elements are received (which is correct), so how does this element change ? I'm allocating the array using the method shown below for use in MPI_Gatherv , as i create a subarray and use a resized data type for collecting the local arrays in a global array

I'm running the code using 4 cores.

Thanks in advance.

    #include <iostream>
    #include <mpi.h>
    #include <cmath>
    #include <cstdlib>

    using namespace std;

    // Allocate a contiguous  2d array 
    int** crt_arr_2d(int im, int jm){
    int** arr;
    arr = (int**)calloc(im, sizeof(int*));
    arr[0] = (int*)calloc(im*jm, sizeof(int));
    for (int i = 0; i < im; i++){
        arr[i] = arr[0] + i*jm;
    }
    return arr;
}

    // De-allocate 2d array
    void dstr_arr_2d(int** ar, int im){
    free(ar[0]);
    free(ar);
}

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

    MPI_Init(&argc, &argv);
    MPI_Comm World;
    World = MPI_COMM_WORLD;
    int proc, nprocs;
    MPI_Comm_rank(World, &proc);
    MPI_Comm_size(World, &nprocs);

    int im_local = 5; // Number of rows in local array
    int jm_local = 5; // Number of columns in local array

    int** a = crt_arr_2d(im_local, jm_local);
    int** b = crt_arr_2d(im_local, jm_local);

    // Initialize data
    if (proc == 0){
        for (int i = 0; i < im_local; i++){
            for (int j = 0; j < jm_local; j++){
                a[i][j] = 2;
            }
        }
    }
    if (proc == 1){
        for (int i = 0; i < im_local; i++){
            for (int j = 0; j < jm_local; j++){
                b[i][j] = 3;
            }
        }
    }

    MPI_Datatype g_col;
    MPI_Type_vector(im_local, 1, jm_local, MPI_INT, &g_col);
    MPI_Type_commit(&g_col);

    if (proc == 0){
        MPI_Send(&a[0][4], im_local, g_col, 1, 1, World);
    }

    if (proc == 1){
        MPI_Recv(&b[0][0], im_local, g_col, 0, 1, World, MPI_STATUSES_IGNORE);
        for (int i = 0; i < im_local; i++){
            for (int j = 0; j < jm_local; j++){
                cout << b[i][j] << " ";
            }
            cout << endl;
        }
    }

    MPI_Barrier(World);

    //MPI_Type_free(&g_col);
    //dstr_arr_2d(a,im_local);
    //dstr_arr_2d(b,im_local);

    MPI_Finalize();
    return 0;
}

如果我正确理解您要实现的目标,则应使用count=1而不是count=im_local调用MPI_Send()MPI_Recv()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM