With a 3d matrix, in the general case you will have to use a vector of vectors (because two steps are involved), which is possible, but it is much easier to use MPI_Type_create_subarray () , which allows you to cut the plate of the multidimensional array that you want.
Update . One of the problems in the code above is that the 3D array you selected is not contiguous; this collection of IE * JE allocates 1d arrays that may or may not be next to each other. Thus, there is no reliable way to extract the data plane from it.
You need to do something like this:
double ***alloc3d(int l, int m, int n) { double *data = new double [l*m*n]; double ***array = new double **[l]; for (int i=0; i<l; i++) { array[i] = new double *[m]; for (int j=0; j<m; j++) { array[i][j] = &(data[(i*m+j)*n]); } } return array; }
Then the data is in one large cube, as you would expect, with an array of pointers pointing to it. It is the fact that C does not have real multidimensional arrays - it constantly appears with C + MPI.
source share