[petsc-users] petsc malloc multidimensional array

Barry Smith bsmith at mcs.anl.gov
Tue Mar 11 12:51:11 CDT 2014


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];

     …….
     Use element and even pass element to MatSetValues().

   In C99 you can even have the element size be a runtime value and do

    mysubroutine(int N,…..)
    double element[N][N];

     ……

      Use element and even pass element to MatSetValues().

   C will automatically handle the allocation of the needed space and it will be efficient.  Note that C will allocate the space as needed inside the routine, it is not kept around outside the routine.

   No need to use PETSc’s routines to allocate the space.


   Barry

> So i want to use PetscMalloc to bulid 2d arrays to store element stiffness matrixs' values. And I don't know how to do, could do tell me?
> 
> AGAIN, these are C questions. You could
> 
>   a) malloc() each row
> 
>   b) malloc() the whole thing, and make pointers to each row
> 
> Notice that this is enormous for an element matrix. This is likely not optimal for performance.
> 
>    Matt
>  
>      then use another 1d array to abstract the nonzero values from 2d arrays above-mentioned. could do you please tell me  some other methods much more convenient and faster?
> 
> 
> 
> -----原始邮件-----
> 发件人: "Matthew Knepley" <knepley at gmail.com>
> 发送时间: 2014年3月11日 星期二
> 收件人: "吕超" <luchao at mail.iggcas.ac.cn>
> 抄送: petsc-users <petsc-users at mcs.anl.gov>
> 主题: Re: [petsc-users] petsc malloc multidimensional array
> 
> On Tue, Mar 11, 2014 at 8:37 AM, 吕超 <luchao at mail.iggcas.ac.cn> wrote:
>   Hi, recently,when I use PETSc to bulid 2d arrays such as PetscScalar A[512][512],B[512][512],C,D,E,F,..., program always has error of Segmentation Violation. So I want to use PetscMalloc to bulid 2d array, and I hope that I can also use these 2d array A[i][j] by subscripts as before. Could do please tell me how can I do? Thank you.
> 
> 1) This is a C question, not a PETSc question
> 
> 2) If you are using 2D arrays for fields, you should be using the DMDA, which has a section in the manual
> 
>   Thanks,
> 
>      Matt
>  
> LV CHAO
> 
> 2014/3/11
> 
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.
> -- Norbert Wiener
> 
> 
> 
> 
> 
> 
> 
> -- 
> What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.
> -- Norbert Wiener



More information about the petsc-users mailing list