[petsc-dev] Efficient shifting vectors one position in array of vectors.

Wim Vanroose wim at vanroo.se
Wed Aug 30 08:24:49 CDT 2017


Thanks!  It works.

Wim

On Wed, Aug 30, 2017 at 3:21 PM, Stefano Zampini <stefano.zampini at gmail.com>
wrote:

> sorry, forgot the last line of pointers swapping
> Vec tmp = Z_Vec[pipe_l];
> for(int i = pipe_l;  i>0; i--){
>    Z_Vec[i] = Z_Vec[i-1];
>    }
> Z_Vec[0] = tmp;
>
> 2017-08-30 16:19 GMT+03:00 Wim Vanroose <wim at vanroo.se>:
>
>> I've tried that in an earlier version.  But it does not work. Then my
>> MatMult complains that
>>
>> [0]PETSC ERROR: Object is in wrong state
>>
>> [0]PETSC ERROR: x and y must be different vectors
>>
>> Because Vec[1] is then referring to  Vec[0] where I am going to store the
>> result of the MatMult.
>> Also later on there are some AXPY that are complaining that that x and y
>> are the same vector.
>>
>> Wim
>>
>>
>> On Wed, Aug 30, 2017 at 2:55 PM, Stefano Zampini <
>> stefano.zampini at gmail.com> wrote:
>>
>>> if you don't use KSPCreateVecs, but just a sequence of VecDuplicates,
>>> then you can swap pointers
>>>
>>> Vec tmp = Z_Vec[pipe_l];
>>> for(int i = pipe_l;  i>0; i--){
>>>    Z_Vec[i] = Z_Vec[i-1];
>>>    }
>>>
>>> if you still want to use KSPCreateVecs, you need to keep track of what
>>> was the first vector of the sequence at the time of creation, because its
>>> address in memory points to the malloced memory for all the Vec pointers.
>>>
>>>
>>>
>>> 2017-08-30 15:45 GMT+03:00 Wim Vanroose <wim at vanroo.se>:
>>>
>>>> Dear Petsc Developers,
>>>>
>>>> We are writing a pipelined CG with deep pipelines.    For this
>>>> algorithm I need to
>>>> store   "pipe_l +1"  auxiliary variables  Z_Vec.    I create these
>>>> vectors as follows:
>>>>
>>>>  Vec *Z_VEC;
>>>>  ierr = KSPCreateVecs(ksp,pipe_l+1,&Z_VEC,0,NULL);CHKERRQ(ierr);
>>>>
>>>>
>>>> Then we have a loop of iterations.   However at some point I have to
>>>> drop the last
>>>> the auxiliary vector and shift the remaining vectors one position, and
>>>> replace the first vector with the result of a matvec.
>>>>
>>>> This is in the current prototype implemented as
>>>>
>>>>       for(int i = pipe_l;  i>0; i--){
>>>>         VecCopy( Z_VEC[i-1], Z_VEC[i] );
>>>>       }
>>>>
>>>>       ierr = MatMult(A, Z_VEC[1], Z_VEC[0]);CHKERRQ(ierr);
>>>>
>>>> It looks very inefficient to me to copy the vector, if we only want to
>>>> shift the data.
>>>>
>>>> Do you have examples code where this is done more efficiently?  Or is
>>>> this already a good way to do it?
>>>> Do you have suggestions?
>>>>
>>>>
>>>> Wim
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Stefano
>>>
>>
>>
>
>
> --
> Stefano
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20170830/b18d8ea2/attachment-0001.html>


More information about the petsc-dev mailing list