# [petsc-users] Preallocation of sequential matrix

Matthew Knepley knepley at gmail.com
Tue Apr 23 21:05:04 CDT 2019

```On Tue, Apr 23, 2019 at 9:57 PM Maahi Talukder via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> Dear All,
>
>
> I am trying to preallocate the no of nonzeros in my matrix using the
> parameter 'nnz'. Here 'row' is the parameter 'nnz'. The part of the code
> that does that is the following-
>
>
> ..........................................................................................................................................................................................
> *Do j = 2,xmax-1*
>
> *Do i = 2,ymax-1*
>
> *a = (ymax-2)*(j-2)+i-1-1*
>
> *If(j.eq.2 .and. i .ge. 3 .and. i .le. (ymax-2))then*
> *row(a) = 6*
>
> *else if (j.eq.(xmax-1) .and. i.ge.3 .and. i .le. (ymax-2)) then*
> *row(a) = 6*
>
> *else if(i.eq.2 .and. j.ge.3 .and. j.le.(xmax-2))then*
> *row(a) = 6*
>
> *else if(i.eq.(ymax-1) .and. j.ge.3 .and. j.le.(xmax-2)) then*
> *row(a) = 6*
>
> *else if(i.eq.2 .and. j.eq.2) then*
> *row(a) = 4*
>
> *else if (i.eq.2 .and. j .eq. (xmax-1)) then*
> *row(a)= 4*
>
> *else if (i.eq.(ymax-1) .and. j .eq. 2) then*
> *row(a) = 4*
>
> *else if (i .eq. (ymax-1) .and. j .eq. (xmax-1)) then*
> *row(a) = 4*
>
> *else*
> *row(a) = 9*
>
> *end if*
>
>
> *end do *
>
> *end do *
>
>
> *call MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,ze,row,Mp,ierr)*
>
>
> .................................................................................................................................................................................................
>
> But I get the following error message :
>
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: Argument out of range
> [0]PETSC ERROR: New nonzero at (61,124) caused a malloc
> Use MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE) to turn
> off this check
> [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html
> for trouble shooting.
> [0]PETSC ERROR: Petsc Release Version 3.10.2, unknown
> [0]PETSC ERROR: ./Test5 on a arch-opt named CB272PP-THINK1 by maahi Tue
> Apr 23 21:39:26 2019
> [0]PETSC ERROR: Configure options --with-debugging=0
> [0]PETSC ERROR: #1 MatSetValues_SeqAIJ() line 481 in
> /home/maahi/petsc/src/mat/impls/aij/seq/aij.c
> [0]PETSC ERROR: #2 MatSetValues() line 1349 in
> /home/maahi/petsc/src/mat/interface/matrix.c
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: Argument out of range
> [0]PETSC ERROR: New nonzero at (124,186) caused a malloc
> Use MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE) to turn
> off this check
> [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html
> for trouble shooting.
> [0]PETSC ERROR: Petsc Release Version 3.10.2, unknown
> [0]PETSC ERROR: ./Test5 on a arch-opt named CB272PP-THINK1 by maahi Tue
> Apr 23 21:39:26 2019
> [0]PETSC ERROR: Configure options --with-debugging=0
> [0]PETSC ERROR: #3 MatSetValues_SeqAIJ() line 481 in
> /home/maahi/petsc/src/mat/impls/aij/seq/aij.c
> [0]PETSC ERROR: #4 MatSetValues() line 1349 in
> /home/maahi/petsc/src/mat/interface/matrix.c
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
>
> .................................................................................................................................................................................
>
> But instead of using 'nnz', if I put a upper bound for 'nz',  the code
> works fine.
>
> Any idea what went wrong?
>

I am guessing you are not converting to the global row index the same as
PETSc.

Matt

> Thanks,
> Maahi Talukder
>
>

--
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their