# [petsc-users] Multiplying a row-vector to each row in a dense matrix, sliced or full

Barry Smith bsmith at petsc.dev
Tue Dec 8 20:04:22 CST 2020

```   Roland,

You would need an algorithm to decide which entries of the vector you wish to use and then make sure those entries end up on the appropriate MPI process before the matrix vector product. You would use VecScatterCreate() and VecScatterBegin/End to move the vector entries to the correct location. See the manual page for MatCreateMPIAIJ() or the users manual for a discussion of the "right" vector used in matrix-vector products.

For example,

Say  v has 2 extra entries than the number of rows of A.

Vec right;
IS   is;
MatCreateVecs(A,&right,NULL);
// right will be used to store the truncated v vector
VecScatterCreate(PETSC_COMM_WORLD,v,is,right,NULL,&scatter);
VecScatterBegin(scatter,v,right,INSERT_VALUES,{SCATTER_FORWARD);
VecScatterEnd(scatter,v,right,INSERT_VALUES,{SCATTER_FORWARD);
MatDiagonalScale(A,NULL,right);

The "is" above that you need provide determines which entries of v you keep (or you could say which entries you do not keep).  In say a trivial case on one process , if v has 12 entries and you just want to keep the first 10 then you would use ISCreateStride(PETSC_COMM_SELF,10,0,1,&is); If you want to drop specific entries "in the middle" of the v then you would use ISCreateGeneral() to list the entries you wish to keep.

In parallel things are slightly trickier because each process needs to list in its part of  "is" the entries needed for its part of the right of the right vector.  So say v = [ w u ; q r]  where w, u, q, r are numbers and ; indicates the split between the first and second MPI rank and right has entries [w ; q r] then is on the first rank needs to have one entry (0) to grab the w and is on the second rank needs two entries (2,3) to grab the q and r. (Note the is entries are in global numbering across all the ranks).

Barry

> On Dec 8, 2020, at 4:16 PM, Roland Richter <roland.richter at ntnu.no> wrote:
>
> Yes, that would be exactly what I need, I have not thought about that possibility, thanks!
>
> Concerning slicing: Assumed my matrix A and vector v are defined by
>
> A = |a b|
>        |c d|
> v = |w x y z|
>
> After v is larger than the row size of A, I only can take some elements for multiplication and therefore I have to use only a slice of vector v:
>
> A*v[1:2] =  |xa yb|
>                     |xc yd|
>
> or
>
> A*v[0:1] =    |wa xb|
>                       |wc xd|
>
> How could I do that?
>
> Thanks,
>
> Roland
>
>
>
> Am 08.12.2020 um 19:26 schrieb Matthew Knepley:
>> On Tue, Dec 8, 2020 at 1:13 PM Roland Richter <roland.richter at ntnu.no <mailto:roland.richter at ntnu.no>> wrote:
>> Hei,
>>
>> I would like to multiply a row-vector to each row in a dense matrix,
>> either full or sliced (i.e. if the row-vector is larger than the row
>> length of the matrix). Armadillo offers a each_row()-function, where I
>> can iterate over all rows in a matrix and multiply the vector to them
>> (similar to the operation VecPointwiseMult()). Is there a similar
>> operation in PETSc? Ideally with the option of only multiplying a
>> part/slice of the row vector to each row, if the corresponding row of
>> the target matrix is shorter than the initial row vector.
>>
>> It helps to write in linear algebra notation so that we can be sure we are talking
>> about the same thing. Say we have the matrix A and vector v
>>
>>   A = / a b \  v = <m, n>
>>         \ c d /
>>
>> and you want
>>
>>   A * m = / ma nb \ = / a b \ / m 0 \ = A . diag(v)
>>                \ mc nd /    \ c d /  \ 0  n /
>>
>> which you can get using https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatDiagonalScale.html <https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatDiagonalScale.html>
>>
>> is that what you want? I do not have a clear picture of what you want slicing for.
>>
>>   Thanks,
>>
>>      Matt
>>
>> Thanks,
>>
>> Roland
>>
>>
>>
>> --
>> 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/ <http://www.cse.buffalo.edu/~knepley/>

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