[petsc-users] petsc malloc multidimensional array

Jed Brown jed at jedbrown.org
Tue Mar 11 13:28:57 CDT 2014


Barry Smith <bsmith at mcs.anl.gov> writes:

> On Mar 11, 2014, at 9:22 AM, Matthew Knepley <knepley at gmail.com> wrote:
>
>> On Tue, Mar 11, 2014 at 9:05 AM, 吕超 <luchao at mail.iggcas.ac.cn> wrote:
>> Hi, Matthew:
>> 
>>      Thank you for your reply so fast! but I also have some questions:
>> 
>>     2d arrays I used is just intermediate variable, not for fields, and the fields is used Vector. In Finite element method, when I use element stiffness matrix to assemble global stiffness matrix, I always first compute the 2d element stiffness matrixs whose size is 512*512(inner points in element),so big for static arrays.
>
>    If the 512 is a fixed number, not different for different elements you can in C 89 standard use 
>
>     mysubroutine(….)
>     double element[512][512];

This is fairly "big" -- 2 MiB of what is typically an 8 MiB stack size,
so if you allocate several of these, you may get a stack overflow.  You
can increase the stack size on most systems, but that makes it more
complicated to run your program.  I would allocate dynamically if you
are worried about this.

If you want to avoid PETSc array functions, you can allocate dynamically
and access via a pointer:

  double *mem = malloc(M*N*sizeof(double));
  double (*p)[N] = (double (*)[N])mem;
  // access p[i][j]
  free(mem);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140311/ff194477/attachment.pgp>


More information about the petsc-users mailing list