LAM/MPI logo

LAM/MPI General User's Mailing List Archives

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

From: Kumar, Ravi Ranjan (rrkuma0_at_[hidden])
Date: 2005-03-10 01:02:57


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];

        for(i=0;i<thk;i++)
        {
        A[i] = new int*[br];
        for(j=0;j<br;j++)
        A[i][j] = new int[ln];
        }

        if(myrank == 0)
        for(k=0;k<thk;k++)
        for(j=0;j<ln;j++)
        for(i=0;i<br;i++)
        {
        A[k][i][j] = count++;
        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;
        }

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

        delete [] A[i];
        }

        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