# [petsc-users] Preallocation of sequential matrix

Smith, Barry F. bsmith at mcs.anl.gov
Tue Apr 23 23:51:18 CDT 2019

```  For a very small problem that produces the error add code that prints the values of row() to the screen and run with the PETSc options

-mat_new_nonzero_allocation_err false -mat_view

This will allow the matrix assembly to proceed and display the matrix, you can then compare the values in row() with the actual lengths of the matrix rows to get an idea of what is going wrong.  For example, for this size problem look at row 61 and compare row(61) with the length of that row in the matrix.

Barry

> On Apr 23, 2019, at 9:05 PM, Matthew Knepley via petsc-users <petsc-users at mcs.anl.gov> wrote:
>
> 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: #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?
>
> 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 experiments lead.
> -- Norbert Wiener
>
> https://www.cse.buffalo.edu/~knepley/

```