# Re: [petsc-users] Write Non-Zero Values of MPI Matrix on an MPI Vector

OK, here is the thing. I have a 2D cartesian regular grid. I am working on wavelet method collocation method, which creates an irregular adaptive grid by turning grid points on an off on the previously mentioned cartesian grid. I store the grid and the values as sparse Mat objects, where each entry to the matrix denotes the x and y location of the value (x:row, y:column). However, to feed the values into PETSc's solver contexts, I have to turn them into vectors.
```
```
By the way, I believe I have solved the problem. For future reference who looks for this, the algorithm is as follows;
```
For each processor,

1.)Get the local number of nonzero entries on the matrix using MatGetInfo

```
2.)Call MPI_Allgather so that every process will know exactly how many nonzero entries each other has
```
```
3.)Create the Vector and set its size using the data from MPI_Allgather from step 2 (sum of all local nonzero sizes)
```
```
4.)Call MatMPIAIJGetLocalMat to get the local portion of the matrix, then call MatSeqAIJGetArray on the local portion to extract its nonzero values as an array
```
```
5.)Using the info from step 2 and 4, set the according values on the vector (e.g. if process 0 has 4 nonzeros, process 1 will set the values on Vector's row 4 onwards)
```

I am always open to ideas for improvements.

Ali

On 13-02-2018 16:46, Jed Brown wrote:
```
```Ali Kahraman <aliberkkahra...@yahoo.com> writes:

```
Dear All, My problem definition is as follows, I  have an MPI matrix with a random sparsity pattern i.e. I do not know how many nonzeros there are on any row unless I call MatGetRow to learn it. There are possibly unequal numbers of nonzeros on every row. I want to write all the nonzero values of this matrix onto a parallel vector. An example can be as follows. Imagine I have a 4x4 matrix (; denotes next row, . denotes sparse "zeros") [3 . 2 . ; .  1 .  . ; 4 5 3 2; . . . .]. I want to obtain the vector [3 2 1 4 5 3 2]. I could not find any function that does this. Any idea is appreciated.
```This seems like an odd thing to want.  What are you trying to do?
```
```
```