LAM/MPI logo

LAM/MPI General User's Mailing List Archives

  |   Home   |   Download   |   Documentation   |   FAQ   |   all just in this list

From: Neil Storer (Neil.Storer_at_[hidden])
Date: 2005-03-10 07:28:24


Kumar,

Correct me if I'm wrong (C++ is not my strong point) but you are mixing
up your i, j and k.

Try the changes I've suggested in your code below:

Kumar, Ravi Ranjan wrote:

>Hello,
>
>I wish to allocate a 3D array A[Nz][Nx][Ny] using new & delete in C++ and send
>Nx*Ny number contiguous data to another 1D array (say) B[Nx*Ny]. I have written
>a code to send first plane of Nx*Ny data (i.e. A[0][1...Nx][1...Ny]) but there
>seems to be some problem in understanding how data are stored in contiguos
>fashion. I am not getting the desired result. I think there is some mistake in
>order of indices. Any help will be greatley appreciated. Here is my code &
>result:
>
>#include<iostream.h>
>#include<math.h>
>#include<fstream.h>
>#define SIZE 10
>#include<time.h>
>#include<iomanip.h>
>#include<mpi.h>
>#include<stdio.h>
>#include<stdlib.h>
>
>int main( int argc, char **argv )
>{
> //char message[20];
> int myrank, count = 0;
> int ***A, *B,i,j,k,ln,br,thk;
> MPI_Status status;
> MPI_Init( &argc, &argv );
> MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
>
>
> //cin>>ln>>br>>thk;
>
> ln = 5;
> br = 4;
> thk = 3;
>
> B = new int[ln*br];
>
> A = new int**[thk];
>
>//Neil for(i=0;i<thk;i++)
>//Neil {
>//Neil A[i] = new int*[br];
>//Neil for(j=0;j<br;j++)
>//Neil A[i][j] = new int[ln];
>//Neil }
>
     // This reads better (it doesn't alter result)

> for(k=0;k<thk;k++)
> {
> A[k] = new int*[br];
> for(i=0;i<br;i++)
> A[k][i] = new int[ln];
> }
>
>
> if(myrank == 0)
> for(k=0;k<thk;k++)
>
  // I think these next two "for" statements are the problem.
  // "i" should go from 0 to ln-1, "j" from 0 to br-1.

  //Neil for(j=0;j<ln;j++)

>//Neil for(i=0;i<br;i++)
>

> for(i=0;i<ln;i++)
>
> for(j=0;j<br;j++)
>
> {
> A[k][i][j] = count++;
>//Neil cout<<A[k][i][j]<<" ";
> }
>
   // Print out the array here, to see exactly what you have
   // Try this with your orginal code, you'll probably see overwriting.

          for (k+0;k<thk;k++)
          for (i=0;i<br;i++)
          for (j=0;j<ln;j++)
          {
             cout<<A[k][i][j];
          }

>
> cout<<"start Send"<<endl;
> if (myrank == 0) // code for process zero //
> MPI_Send(&A[0][0][0], 20, MPI_INT, 1, 111, MPI_COMM_WORLD);
>
>
> cout<<"end Send"<<endl;
>
> if (myrank == 1) // code for process 1 //
> MPI_Recv(&B[0], 20, MPI_INT, 0, 111, MPI_COMM_WORLD, &status);
>
> cout<<"end Recv"<<endl;
>
> if(myrank == 1)
> {
> cout<<endl<<"Rank = "<<myrank<<endl;
> for(i=0;i<ln*br;i++)
> cout<<"B["<<i<<"] = "<<B[i]<<endl;
> }
>
>
   //More readability mods

> //Neil for(i=0;i<thk;i++)
> //Neil {
> //Neil for(j=0;j<br;j++)
> //Neil delete [] A[i][j];
>
> //Neil delete [] A[i];
> //Neil }
>

> for(k=0;k<thk;k++)
> {
> for(i=0;i<br;i++)
> delete [] A[k][i];
>
> delete [] A[k];
> }
>
> delete [] A;
> delete [] B;
>
> MPI_Finalize();
> return 0;
>}
>
>
>0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
>30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
>56 57 58 59 start Send
>end Send
>start Send
>end Send
>end Recv
>
>Rank = 1
>B[0] = 0
>B[1] = 4
>B[2] = 8
>B[3] = 12
>B[4] = 16
>B[5] = 25
>B[6] = 1
>B[7] = 5
>B[8] = 9
>B[9] = 13
>B[10] = 17
>B[11] = 25
>B[12] = 2
>B[13] = 6
>B[14] = 10
>B[15] = 14
>B[16] = 18
>B[17] = 25
>B[18] = 3
>B[19] = 7
>end Recv
>
>
>Thanks!
>Ravi R. Kumar
>
>
>
>
>
>
>_______________________________________________
>This list is archived at http://www.lam-mpi.org/MailArchives/lam/
>

-- 
+-----------------+---------------------------------+------------------+
| Neil Storer     |    Head: Systems S/W Section    | Operations Dept. |
+-----------------+---------------------------------+------------------+
| ECMWF,          | email: neil.storer_at_[hidden]    |    //=\\  //=\\  |
| Shinfield Park, | Tel:   (+44 118) 9499353        |   //   \\//   \\ |
| Reading,        |        (+44 118) 9499000 x 2353 | ECMWF            |
| Berkshire,      | Fax:   (+44 118) 9869450        | ECMWF            |
| RG2 9AX,        |                                 |   \\   //\\   // |
| UK              | URL:   http://www.ecmwf.int/    |    \\=//  \\=//  |
+--+--------------+---------------------------------+----------------+-+
   | ECMWF is the European Centre for Medium-Range Weather Forecasts |
   +-----------------------------------------------------------------+