# MatSetValuesBlocked
Inserts or adds a block of values into a matrix. 
## Synopsis
```
#include "petscmat.h" 
PetscErrorCode MatSetValuesBlocked(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[], InsertMode addv)
```
Not Collective


## Input Parameters

- ***mat -*** the matrix
- ***v -*** a logically two-dimensional array of values
- ***m, idxm -*** the number of block rows and their global block indices
- ***n, idxn -*** the number of block columns and their global block indices
- ***addv -*** either `ADD_VALUES` to add values to any existing entries, or `INSERT_VALUES` replaces existing entries with new values



## Notes
If you create the matrix yourself (that is not with a call to `DMCreateMatrix()`) then you MUST call
MatXXXXSetPreallocation() or `MatSetUp()` before using this routine.

The m and n count the NUMBER of blocks in the row direction and column direction,
NOT the total number of rows/columns; for example, if the block size is 2 and
you are passing in values for rows 2,3,4,5  then m would be 2 (not 4).
The values in idxm would be 1 2; that is the first index for each block divided by
the block size.

Note that you must call `MatSetBlockSize()` when constructing this matrix (before
preallocating it).

By default the values, v, are row-oriented, so the layout of
v is the same as for `MatSetValues()`. See `MatSetOption()` for other options.

Calls to `MatSetValuesBlocked()` with the `INSERT_VALUES` and `ADD_VALUES`
options cannot be mixed without intervening calls to the assembly
routines.

`MatSetValuesBlocked()` uses 0-based row and column numbers in Fortran
as well as in C.

Negative indices may be passed in idxm and idxn, these rows and columns are
simply ignored. This allows easily inserting element stiffness matrices
with homogeneous Dirchlet boundary conditions that you don't want represented
in the matrix.

Each time an entry is set within a sparse matrix via `MatSetValues()`,
internal searching must be done to determine where to place the
data in the matrix storage space.  By instead inserting blocks of
entries via `MatSetValuesBlocked()`, the overhead of matrix assembly is
reduced.


## Example
```none
Suppose m=n=2 and block size(bs) = 2 The array is
```
```none
```
```none
1  2  | 3  4
```
```none
5  6  | 7  8
```
```none
- - - | - - -
```
```none
9  10 | 11 12
```
```none
13 14 | 15 16
```
```none
```
```none
v[] should be passed in like
```
```none
v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
```
```none
```
```none
If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then
```
```none
v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16]
```




## See Also
 `Mat`, `MatSetBlockSize()`, `MatSetOption()`, `MatAssemblyBegin()`, `MatAssemblyEnd()`, `MatSetValues()`, `MatSetValuesBlockedLocal()`

## Level
intermediate

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/interface/matrix.c.html#MatSetValuesBlocked">src/mat/interface/matrix.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex55.c.html">src/ksp/ksp/tutorials/ex55.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex56.c.html">src/ksp/ksp/tutorials/ex56.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex71.c.html">src/ksp/ksp/tutorials/ex71.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex74.c.html">src/ksp/ksp/tutorials/ex74.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex10.c.html">src/ts/tutorials/ex10.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex22.c.html">src/ts/tutorials/ex22.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex25.c.html">src/ts/tutorials/ex25.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex34.c.html">src/ts/tutorials/ex34.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex35.cxx.html">src/ts/tutorials/ex35.cxx.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex9.c.html">src/ts/tutorials/ex9.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/mpi/mpibaij.c.html#MatSetValuesBlocked_MPIBAIJ">MatSetValuesBlocked_MPIBAIJ in src/mat/impls/baij/mpi/mpibaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/seq/baij.c.html#MatSetValuesBlocked_SeqBAIJ">MatSetValuesBlocked_SeqBAIJ in src/mat/impls/baij/seq/baij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/is/matis.c.html#MatSetValuesBlocked_IS">MatSetValuesBlocked_IS in src/mat/impls/is/matis.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/mpi/mpisbaij.c.html#MatSetValuesBlocked_MPISBAIJ">MatSetValuesBlocked_MPISBAIJ in src/mat/impls/sbaij/mpi/mpisbaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/seq/sbaij.c.html#MatSetValuesBlocked_SeqSBAIJ">MatSetValuesBlocked_SeqSBAIJ in src/mat/impls/sbaij/seq/sbaij.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/interface/matrix.c)


[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)  
