Yeliang Zhang wrote:
> Hi,
>
>
>
> Is there a way to send a 2-D array at once instead of send one row by one
> row? I wrote a code with 2 processors, processor 0 generates 2-D array and
> send half of the rows to processor 1. I tested with 2x2 array, the result is
> fine. But when I tested with 4x4 array. The last two numbers received in
> processor 1 are 0.
>
>
>
>
> distr = n / size;
>
> in_set = (int **)malloc(n * sizeof(int *));
>
> for(x=0; x < n; x++) {
>
> in_set[x] = (int *)malloc(m * sizeof(int));
>
> }
>
>
> for (x=0;x<n;x++){
>
> for (y=0;y<m;y++){
>
> in_set[x][y] = 1+ rand()/(((double)RAND_MAX + 1) / 8);
>
> printf("[%d][%d] = %d\n",x,y, in_set[x][y]);
>
> }
>
> }
>
Hi,
that's an old problem discussed many times on this list: C language has support
only for statically sized multi-dimensional arrays, which is not satisfactory
for many computational applications, on the other side the "elegant" **p
approach of using a vector of pointers for addressing each dynamically allocated
row, besides being inefficient, does not guarantee that the data are consecutive
in memory (which is a necessary condition for a simple mpi_send to do its job),
so, in my view, the simple and efficient way to do this in C is allocating a
one-dimensional m*n array and addressing it with something like [x+y*m]. The
fact that the data allocated with the array of pointers are not necessarily
consecutive in memory is probably the reason for the wrong result in your program.
good luck, Davide
|