# [petsc-users] Preallocation of sequential matrix

Zhang, Junchao jczhang at mcs.anl.gov
Tue Apr 23 22:40:40 CDT 2019

```The error message has
[0]PETSC ERROR: New nonzero at (61,124) caused a malloc
[0]PETSC ERROR: New nonzero at (124,186) caused a malloc
You can check your code to see if you allocated spots for these nonzeros.

--Junchao Zhang

On Tue, Apr 23, 2019 at 8:57 PM Maahi Talukder via petsc-users <petsc-users at mcs.anl.gov<mailto: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: #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: #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?

Thanks,
Maahi Talukder

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20190424/e28831a3/attachment-0001.html>
```