I try to loop through big 3D array of structures and it works so slowly. Then I used 1D array instead of 3D, but without success.
I use structure below to describe parameters of one cell of 3D mesh:
struct cellStruct
{
double v1;
// more variables here
double v15;
double v16;
double v17;
double v18;
};
Please take a look to two used approaches.
3D arrays
#define Nx 500 #define Ny 500 #define Nz 500 cellStruct ***cell; cell = new cellStruct **[Nx]; for(int i=0;i<Nx;i++) { cell[i]=new cellStruct *[Ny]; for(int j=0;j<Ny;j++) cell[i][j]=new cellStruct [Nz]; } for (i = 0; i< Nx; ++i) for (j = 0; j< Ny; ++j) for (k = 0; k< Nz; ++k) { // big algorithm that uses array like in string below cell[i][j][k+1].v1 = cell[i][j+1][k-1].v2 * cell[i+1][Ny-1][k+1].v5; }
1D array
#define cell(i,j,k) (cells[(i)*Ny*Nz + (j)*Ny + (k)]) cellStruct *cells = new cellStruct [Nx*Ny*Nz]; for (i = 1; i< Nx-1; ++i) for (j = 1; j< Ny-1; ++j) for (k = 1; k< Nz-1; ++k) { cell(i,j,k+1).v1 = cell(i,j+1,k-1).v2 * cell(i+1,Ny-1,k+1).v5; }
Program works more slowly in case 2. How else I can improve approach of working with big 3D array? Using float variables speed up calculations twice, but I want to have more accuracy. Maybe is better to use structure with pointers to variables inside like below?
struct cells
{
double ***v1;
// ...
double ***v15;
double ***v16;
double ***v17;
double ***v18;
};