On 4/12/2012 11:52 AM, Jed Brown wrote: > On Thu, Apr 12, 2012 at 11:37, Zhenglun (Alan) Wei > <zhenglun.wei at gmail.com <mailto:zhenglun.wei at gmail.com>> wrote: > > Everything is fine in AlanFloatStruct.c. *However, when I was > trying to use 'calloc' to allocate memory to 'a[][]' rather than > directly define 'a[][]' as a structure matrix, the problem comes.* > (see AlanTEST1.c). > > > a = (TESTVAR**) calloc(SIZE, sizeof(TESTVAR*)); > b = (TESTVAR**) calloc(SIZE, sizeof(TESTVAR*)); > c = (TESTVAR**) calloc(SIZE, sizeof(TESTVAR*)); > for(i = 0; i < SIZE; i++) { > a[i] = (TESTVAR*) calloc(SIZE, sizeof(TESTVAR)); > b[i] = (TESTVAR*) calloc(SIZE, sizeof(TESTVAR)); > c[i] = (TESTVAR*) calloc(SIZE, sizeof(TESTVAR)); > } > > At this point, there is no relationship between a[i][j] and a[k][j]. > It is not strided memory. I suggest allocating the entire array in one > allocation (SIZE*SIZE*sizeof(TESTVAR)). You can set up pointers into > it if you like, but MPI will not use them. > > MPI_Type_vector(SIZE, 1, SIZE, structype, &columntype); > > This specifies that the array is strided. Dear Dr. Brown, Thank you for your reply. It gives me another way to solve this problem. However, I failed several times while I was trying it. Can I repeat your idea to make sure that I understand it. 1) should define a new one-dimensional pointer array, i.e. TESTVAR *array; 2) I allocate this array with array = (TESTVAR*) calloc(SIZE*SIZE, sizeof(TESTVAR)); 3) then, I setup pointer of 'a[i][j]' to array. Are they correct? BTW, does PETSc have any data structure can be an alternative so that I do not need to use MPI derived data type?
thanks, Alan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20120412/947d895c/attachment.htm>
