[petsc-users] a question about MatSetValue

Dave May dave.mayhem23 at gmail.com
Thu May 21 05:23:07 CDT 2020


On Thu 21. May 2020 at 12:17, Yang Bo (Asst Prof) <yang.bo at ntu.edu.sg>
wrote:

> Hi Dave,
>
> Yes it is parallel so the preallocation calls are not lowered by the
> allocation.
>
> I am trying to use MatXAIJSetPreallocation, but not sure how, since the
> following link does not give an example:
>
>
> https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatXAIJSetPreallocation.html
>
> If I have the following matrix:
>
> 0 1 2 0
> 1 0 0 0
> 2 0 1 3
> 0 0 3 2
>
> How should I put in the parameters of MatXAIJSetPreallocation?
>

Please read this page to understand the info required

https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatMPIAIJSetPreallocation.html


Compute everything as described above and give the results to
MatXAIJSetPreallocation(). MatXAIJSetPreallocation() is just a helper
function to hide all the implementation specific setters.

Thanks
Dave






> Thanks!
>
> Cheers,
>
> Yang Bo
>
>
> On 21 May 2020, at 5:42 PM, Dave May <dave.mayhem23 at gmail.com> wrote:
>
>
>
> On Thu 21. May 2020 at 10:49, Yang Bo (Asst Prof) <yang.bo at ntu.edu.sg>
> wrote:
>
>> Hi Dave,
>>
>> Thank you very much for your reply. That is indeed the problem. I have
>> been working with matrices in Slepc but I don’t really understand it. I
>> tried to preallocate but it still does not work.
>>
>
> Meaning the number of reported mallocs is still non-zero?
> Is the number reported with you preallocation calls lower than what you
> originally saw?
>
> If you look at my code below:
>>
>> ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
>> ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,h_dim,h_dim);
>>          // h_dim is the dimension of the square matrix A
>> ierr = MatSetFromOptions(A);CHKERRQ(ierr);
>> ierr = MatSetUp(A);CHKERRQ(ierr);
>> ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
>>
>> MatSeqAIJSetPreallocation(A,0,nnz);
>>  // I try to preallocate here, where nnz is the array containing the
>> number of non-zero entries each row
>>
>> for (int i=0;i<row.size();i++) {
>> MatSetValue(A,row[i],column[i],h[i],INSERT_VALUES);
>> }
>>
>> I am not sure what other information I need to give for the
>> pre-allocation…
>>
>
> This looks fine. However MatSeqAIJSetPreallocation() has no effect if the
> Mat type is not SEQAIJ.
>
> Are you running in parallel? If yes then the Mat type will be MATMPIAIJ
> and you either have to call the MPI specific preallocator or use the
> generic one I pointed you too.
>
> Thanks
> Dave
>
>
>
>> Cheers,
>>
>> Yang Bo
>>
>>
>>
>> On 21 May 2020, at 4:08 PM, Dave May <dave.mayhem23 at gmail.com> wrote:
>>
>> *-info | grep malloc*
>>
>>
>> ------------------------------
>>
>> CONFIDENTIALITY: This email is intended solely for the person(s) named
>> and may be confidential and/or privileged. If you are not the intended
>> recipient, please delete it, notify us and do not copy, use, or disclose
>> its contents.
>> Towards a sustainable earth: Print only when necessary. Thank you.
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20200521/632e1417/attachment.html>


More information about the petsc-users mailing list