If I could just wedge myself in here for a second... I recall fiddling with a 3d contiguous allocation problem a while back. My solution is essentially the same, but the pointers to pointers are allocated in the space before the array data... Nice thing is, you only need double ***M (or whatever type you want, I think I even have a templated version sitting around somewhere...) outside of the function.<br>
<br>double*** contiguous3(int icount, <br> int jcount, <br> int kcount)<br>// Generates a contiguously allocated (row major) array for 3 dimensions.<br>// Array can be accessed as M[i][j][k], and also taking <br>
// m = &M[0][0][0] (for pointer m) allows m[i]<br>// Allocated memory can be freed with a single call to free(M)<br>{<br> int ii, jj;<br> int typeSize = sizeof(double);<br> void*** iret = (void***)malloc(icount*sizeof(void***) + <br>
icount*jcount*sizeof(void**) + <br> icount*jcount*kcount*typeSize);<br> void** jret = (void**)(iret + icount);<br> char* kret = (char*)(jret + icount*jcount);<br> for(ii=0;ii<icount;++ii){<br>
iret[ii] = &jret[ii*jcount];<br> }<br> for(ii=0;ii<icount;++ii){<br> for(jj=0;jj<jcount;++jj){<br> jret[ii*jcount+jj] = &kret[ii*jcount*kcount*typeSize + <br> jj*kcount*typeSize];<br>
}<br> }<br> return (double***)iret;<br>}<br><br>Cheers,<br>Kevin<br>
<br><br><br>
<div class="gmail_quote">On Wed, Apr 18, 2012 at 3:52 PM, Alan Wei <span dir="ltr"><<a href="mailto:zhenglun.wei@gmail.com" target="_blank">zhenglun.wei@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear all,<div> I hope you're having a nice day. I have a further question on this issue in 3D.</div><div>1, Following the idea of Dr. Brown and Dr. Knepley, I finished a 2D test, which works very fine. Here, I did it in 3D by</div>
<div><div><div>"</div><div> TESTVAR ***a, ***b, ***c;</div><div> TESTVAR **aa, **bb, **cc; </div><div> TESTVAR *arraya, *arrayb, *arrayc;</div></div><div><br></div><div> arraya = (TESTVAR*) calloc(SIZE*SIZE*SIZE, sizeof(TESTVAR));</div>
<div> arrayb = (TESTVAR*) calloc(SIZE*SIZE*SIZE, sizeof(TESTVAR));</div><div> arrayc = (TESTVAR*) calloc(SIZE*SIZE*SIZE, sizeof(TESTVAR));</div><div><br></div><div> aa =(TESTVAR**) calloc(SIZE*SIZE, sizeof(TESTVAR*));</div>
<div> bb =(TESTVAR**) calloc(SIZE*SIZE, sizeof(TESTVAR*));</div><div> cc =(TESTVAR**) calloc(SIZE*SIZE, sizeof(TESTVAR*));</div><div> </div><div> for(i = 0; i < SIZE*SIZE; i++) {</div><div> aa[i] = &arraya[i*SIZE];</div>
<div> bb[i] = &arrayb[i*SIZE];</div><div> cc[i] = &arrayc[i*SIZE]; </div><div> }</div><div><br></div><div> a =(TESTVAR***) calloc(SIZE*SIZE, sizeof(TESTVAR**));</div><div> b =(TESTVAR***) calloc(SIZE*SIZE, sizeof(TESTVAR**));</div>
<div> c =(TESTVAR***) calloc(SIZE*SIZE, sizeof(TESTVAR**));</div><div><div> </div><div> for(i = 0; i < SIZE; i++) {</div></div><div> a[i] = &aa[i*SIZE];</div><div> b[i] = &bb[i*SIZE];</div>
<div> c[i] = &cc[i*SIZE];</div>
<div> }</div><div>"</div><div> It works. However, I wonder if there is any other good ideas for 3D problem other than this kinda of 'two-layer' approach.</div><div><br></div><div>2, I have a little question on PETSc about 3D processor ordering. Does PETSc have any function giving me the nodes/rank number of neighboring nodes/ranks? Are those 'Application Ordering' functions applicable for my case?</div>
<div><br></div><div>thanks,</div><div>Alan</div><div><div></div><div><br><div class="gmail_quote">On Fri, Apr 13, 2012 at 5:41 PM, Jed Brown <span dir="ltr"><<a href="mailto:jedbrown@mcs.anl.gov" target="_blank">jedbrown@mcs.anl.gov</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="gmail_quote">On Fri, Apr 13, 2012 at 17:38, Zhenglun (Alan) Wei <span dir="ltr"><<a href="mailto:zhenglun.wei@gmail.com" target="_blank">zhenglun.wei@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have a final question on it. Is it taken a lot of memory for
doing this? As I understand, pointers won't occupy many memories and
it works like an alias. It will not, to my limit knowledge, take
much extra memory by doing this. </blockquote></div><br></div><div>A pointer takes about as much space as a floating point value, so that array of pointers costs about 1*N compared to the N*N matrix.</div>
</blockquote></div><br></div></div></div>
</blockquote></div><br>