sending blocks of 2D array in C using MPI

前端 未结 2 2102
走了就别回头了
走了就别回头了 2020-11-22 08:09

How do you send blocks of 2-D array to different processors? Suppose the 2D array size is 400x400 an I want to send blocks of sizes 100X100 to different processors. The idea

2条回答
  •  情深已故
    2020-11-22 08:32

    I just found it easier to check it that way.

    #include 
    #include 
    #include 
    #include "mpi.h"
    
    /*
     This is a version with integers, rather than char arrays, presented in this
     very good answer: http://stackoverflow.com/a/9271753/2411320
     It will initialize the 2D array, scatter it, increase every value by 1 and then gather it back.
    */
    
    int malloc2D(int ***array, int n, int m) {
        int i;
        /* allocate the n*m contiguous items */
        int *p = malloc(n*m*sizeof(int));
        if (!p) return -1;
    
        /* allocate the row pointers into the memory */
        (*array) = malloc(n*sizeof(int*));
        if (!(*array)) {
           free(p);
           return -1;
        }
    
        /* set up the pointers into the contiguous memory */
        for (i=0; i

    Output:

    linux16:>mpicc -o main main.c
    linux16:>mpiexec -n 4 main Global array is:
     1  2  3  4
     5  6  7  8
     9 10 11 12
    13 14 15 16
    Local process on rank 0 is:
    | 1  2 |
    | 5  6 |
    Local process on rank 1 is:
    | 3  4 |
    | 7  8 |
    Local process on rank 2 is:
    | 9 10 |
    |13 14 |
    Local process on rank 3 is:
    |11 12 |
    |15 16 |
    Processed grid:
     2  3  4  5
     6  7  8  9
    10 11 12 13
    14 15 16 17
    

提交回复
热议问题