Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Allocation of huge 3D arrays
    primarykey
    data
    text
    <p>I am working on a project where I need to create a 3D array, some 2D and 1D arrays. The 3D array represents discrete co-ordinates in space and I need lots of points for my problem. The array size will be around 2000*2000*2000. I need store 'double' values in these arrays. Can anyone suggest an efficient scheme to implement this in C?</p> <p>Thanks in advance </p> <pre><code>/*********************************************************** * Copyright Univ. of Texas M.D. Anderson Cancer Center * 1992. * * Some routines modified from Numerical Recipes in C, * including error report, array or matrix declaration * and releasing. ****/ #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;math.h&gt; #include &lt;malloc.h&gt; /*********************************************************** * Report error message to stderr, then exit the program * with signal 1. ****/ void nrerror(char error_text[]) { fprintf(stderr,"%s\n",error_text); fprintf(stderr,"...now exiting to system...\n"); exit(1); } /*********************************************************** * Allocate an array with index from nl to nh inclusive. * * Original matrix and vector from Numerical Recipes in C * don't initialize the elements to zero. This will * be accomplished by the following functions. ****/ double *AllocVector(short nl, short nh) { double *v; short i; v=(double *)malloc((unsigned) (nh-nl+1)*sizeof(double)); if (!v) nrerror("allocation failure in vector()"); v -= nl; for(i=nl;i&lt;=nh;i++) v[i] = 0.0; /* init. */ return v; } /*********************************************************** * Allocate a matrix with row index from nrl to nrh * inclusive, and column index from ncl to nch * inclusive. ****/ double **AllocMatrix(short nrl,short nrh, short ncl,short nch) { short i,j; double **m; m=(double **) malloc((unsigned) (nrh-nrl+1) *sizeof(double*)); if (!m) nrerror("allocation failure 1 in matrix()"); m -= nrl; for(i=nrl;i&lt;=nrh;i++) { m[i]=(double *) malloc((unsigned) (nch-ncl+1) *sizeof(double)); if (!m[i]) nrerror("allocation failure 2 in matrix()"); m[i] -= ncl; } for(i=nrl;i&lt;=nrh;i++) for(j=ncl;j&lt;=nch;j++) m[i][j] = 0.0; return m; } /*********************************************************** * Allocate a 3D array with x index from nxl to nxh * inclusive, y index from nyl to nyh and z index from nzl to nzh * inclusive. ****/ double ***Alloc3D(short nxl,short nxh, short nyl,short nyh, short nzl, short nzh) { double ***t; short i,j,k; t=(double ***) malloc((unsigned) (nxh-nxl+1)*sizeof(double **)); if (!t) nrerror("allocation failure 1 in matrix()"); t -= nxl; for(i=nxl;i&lt;=nxh;i++) { t[i]=(double **) malloc((unsigned) (nyh-nyl+1)*sizeof(double *)); if (!t[i]) nrerror("allocation failure 2 in matrix()"); t[i] -= nyl; for(j=nyl;j&lt;=nyh;j++) { t[i][j]=(double *) malloc((unsigned) (nzh-nzl+1)*sizeof(double)); if (!t[i][j]) nrerror("allocation failure 3 in matrix()"); t[i][j] -= nzl;} } for(i=nxl;i&lt;=nxh;i++) for(j=nyl;j&lt;=nyh;j++) for(k=nzl; k&lt;=nzh;k++) t[i][j][k] = 0.0; return t; } /*********************************************************** *Index to 3D array. ****/ long index(int x, int y, int z, int Size) { return (Size*Size*x + Size*y + z); } /*********************************************************** * Release the memory. ****/ void FreeVector(double *v,short nl,short nh) { free((char*) (v+nl)); } /*********************************************************** * Release the memory. ****/ void FreeMatrix(double **m,short nrl,short nrh, short ncl,short nch) { short i; for(i=nrh;i&gt;=nrl;i--) free((char*) (m[i]+ncl)); free((char*) (m+nrl)); } /*********************************************************** * Release the memory. ****/ void Free3D(double ***t,short nxl,short nxh, short nyl,short nyh, short nzl, short nzh) { short i,j; for(i=nxh;i&gt;=nxl;i--) {for(j=nyl;j&gt;=nyl;j--) free((char*) (t[i][j]+nzl)); free((char*) (t[i]+nyl)); } free((char*) (t+nxl)); } *********************************************************************************** void InitOutputData(InputStruct In_Parm, OutStruct * Out_Ptr) { short nz = In_Parm.nz; short nr = In_Parm.nr; short na = In_Parm.na; short nl = In_Parm.num_layers; short size = nr/2*nr/2*nz; /* remember to use nl+2 because of 2 for ambient. */ if(nz&lt;=0 || nr&lt;=0 || na&lt;=0 || nl&lt;=0) nrerror("Wrong grid parameters.\n"); /* Init pure numbers. */ Out_Ptr-&gt;Rsp = 0.0; Out_Ptr-&gt;Rd = 0.0; Out_Ptr-&gt;A = 0.0; Out_Ptr-&gt;Tt = 0.0; /* Allocate the arrays and the matrices. */ //Out_Ptr-&gt;Rd_ra = AllocMatrix(0,nr-1,0,na-1); //Out_Ptr-&gt;Rd_r = AllocVector(0,nr-1); //Out_Ptr-&gt;Rd_a = AllocVector(0,na-1); Out_Ptr-&gt;A_xyz1 = AllocVector(0,size-1); Out_Ptr-&gt;A_xyz2 = AllocVector(0,size-1); Out_Ptr-&gt;A_xyz3 = AllocVector(0,size-1); Out_Ptr-&gt;A_xyz4 = AllocVector(0,size-1); Out_Ptr-&gt;A_xz = AllocMatrix(0,nr-1,0,nz-1); Out_Ptr-&gt;A_z = AllocVector(0,nz-1); Out_Ptr-&gt;A_l = AllocVector(0,nl+1); Out_Ptr-&gt;Tt_ra = AllocMatrix(0,nr-1,0,na-1); Out_Ptr-&gt;Tt_r = AllocVector(0,nr-1); Out_Ptr-&gt;Tt_a = AllocVector(0,na-1); } </code></pre> <p>Above is the code for allocating the arrays and the function to call them. The call which fails is 'Out_Ptr->A_xyz1 = AllocVector(0,size-1);' when size is more than approx. 7000.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload