# MatPreallocateBegin
Begins the block of code that will count the number of nonzeros per row in a matrix providing the data that one can use to correctly preallocate the matrix. 
## Synopsis
```
#include <petscmat.h>
PetscErrorCode MatPreallocateBegin(MPI_Comm comm, PetscInt nrows, PetscInt ncols, PetscInt *dnz, PetscInt *onz)
```
Collective


## Input Parameters

- ***comm -*** the communicator that will share the eventually allocated matrix
- ***nrows -*** the number of LOCAL rows in the matrix
- ***ncols -*** the number of LOCAL columns in the matrix



## Output Parameters

- ***dnz -*** the array that will be passed to the matrix preallocation routines
- ***onz -*** the other array passed to the matrix preallocation routines





## Notes
This is a macro that handles its own error checking, it does not return an error code.

See Users-Manual: ch_performance for more details.

Do not malloc or free dnz and onz, that is handled internally by these routines


## Developer Notes
This is a MACRO not a function because it has a leading { that is closed by PetscPreallocateFinalize().


## See Also
 `MatPreallocateEnd()`, `MatPreallocateSet()`, `MatPreallocateSymmetricSetBlock()`, `MatPreallocateSetLocal()`,
`MatPreallocateSymmetricSetLocalBlock()`

## Level
intermediate

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscmat.h.html#MatPreallocateBegin">include/petscmat.h</A>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/include/petscmat.h)


[Index of all Mat routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
