c - MPI communicate large two dimensional arrays -


related previous question concerning copying of 2- 1-dimensional arrays, wondered if there better way whole business, try achieve. so, want mpi communicated couple of large 2 dimensional arrays (which allocated dynamically arrays of arrays, known size @ compile time) master clients , vice versa. data supposed scattered to/gathered clients column-wise.

what doing @ moment map 2-dimensional arrays (which in fact sub-arrays of 3-dimensional ones) statically allocated 1-dimensional arrays, send these 1-dim. arrays via mpi , rebuild 2-dimensional arrays again @ receiver. however, have feeling there should (much) more efficient that...

thanks lot!

c99 has support dynamic multidimensional arrays: using them can avoid copy data @ all. example following code compile gcc -std=c99:

#include <stdio.h> #include <stdlib.h>  int main(int argc, char **argv) {   int m = atoi(argv[1]);   int n = atoi(argv[2]);   int p = atoi(argv[3]);    // if defined in stack... i'm not sure it's defined in stack   // in way - shouldn't size known @ compile time?   // float a[m][n][p];   // in heap:   float (*a)[n][p] = malloc(m*n*p*sizeof(float));   (int i=0; i<m; ++i) {     (int j=0; j<n; ++j) {       (int k=0; k<p; ++k) {         a[i][j][k] = 100.*i + 10.*j + k;       }     }   }   (int i=0; i<m; ++i) {     (int j=0; j<n; ++j) {       (int k=0; k<p; ++k) {         if (k>0) printf(",");         printf("%7.2f", a[i][j][k]);       }       printf("\n");     }     printf("\n");   }   free(a); } 

(execute ./a.out 2 3 4 example - no error checking... patient please).

if using c89, when looking optimized code think should sacrifice syntax best memory layout, , write same code as:

#include <stdio.h> #include <stdlib.h>  #define arr(a,i,j,k) ((a).a[(i)*a.p*a.n + (j)*a.p + (k)])   struct arr3d {   float *a;   int m;   int n;   int p; };  int main(int argc, char **argv) {   struct arr3d a;   int m,n,p;   int i,j,k;    m = a.m = atoi(argv[1]);   n = a.n = atoi(argv[2]);   p = a.p = atoi(argv[3]);   a.a = malloc(m*n*p*sizeof(float));   (i=0; i<m; ++i) {     (j=0; j<n; ++j) {       (k=0; k<p; ++k) {         arr(a,i,j,k) = 100.*i + 10.*j + k;       }     }   }   (i=0; i<m; ++i) {     (j=0; j<n; ++j) {       (k=0; k<p; ++k) {         if (k>0) printf(",");         printf("%7.2f", arr(a,i,j,k));       }       printf("\n");     }     printf("\n");   }   free(a.a); } 

in both ways arrays locations contiguous in memory , can sent single mpi communication:

mpi_send(&a[0][0][0], m*n*p, mpi_float, ...       (c99) mpi_send(&arr(a,0,0,0), m*n*p, mpi_float, ...     (c89) 

or asked, send i-th subarray:

mpi_send(&a[i][0][0], n*p, mpi_float, ...         (c99) mpi_send(&arr(a,i,0,0), n*p, mpi_float, ...       (c89) 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -