[petsc-users] VecSet and VecAssemblyBegin

Junchao Zhang junchao.zhang at gmail.com
Sat Apr 26 10:41:25 CDT 2025


Yes, that is correct.

--Junchao Zhang


On Sat, Apr 26, 2025 at 10:35 AM Derek Teaney <derek.teaney at stonybrook.edu>
wrote:

> Ok -- got it -- thanks so just do the RestoreArray before the final step,
> e.g.
>
> Loop over cases
>       VecSet(dn_local, 0.)
>       GetArray(dn_local, &dn)  // RIGHT
>       Fill up dn
>       Restore Array
>       LocalToGlobal
>
>
> On Sat, Apr 26, 2025 at 9:50 AM Junchao Zhang <junchao.zhang at gmail.com>
> wrote:
>
>> On Sat, Apr 26, 2025 at 8:27 AM Derek Teaney via petsc-users <
>> petsc-users at mcs.anl.gov> wrote:
>>
>>> Thanks Barry -- this solved the issue.
>>>
>>> "probably will be fine" was fine with 3.17 and maybe 3.19,  but
>>> definitely not fine with 3.20.
>>>
>>> For others the faulty logic is:
>>>
>>> GetArray(dn_local, &dn)   //WRONG
>>> Loop over cases
>>>      VecSet(dn_local, 0.)
>>>       Fill up dn
>>>       LocalToGlobal
>>> RestoreArray
>>>
>>> Where as one should do:
>>>
>>> Loop over cases
>>>       VecSet(dn_local, 0.)
>>>       GetArray(dn_local, &dn)  // RIGHT
>>>       Fill up dn
>>>       LocalToGlobal
>>>       RestoreArray
>>>
>> The above two pieces of code are both wrong, in my view.
>>
>>>
>>>
>> So, while nothing is copied, if I think of dn as a copy (and not a view)
>>> the logic will always be correct.
>>>
>>> Now I have a related question about  "Technically you should not be
>>> calling VecSet() with any outstanding arrays but it will probably be fine."
>>> What about  GlobalToLocal?  should I always GetArray for the local array
>>> after the GlobalToLocal
>>>
>>> So,  is this  also bad logic:
>>>
>>> GetArray(n_local, &n)
>>> Loop over cases:
>>>      GlobalToLocal(n_global, &n_local)
>>>      do stuff with n
>>>      LocalToGlobal(n_local, n_global)
>>>  RestoreArray
>>>
>>> as opposed to
>>>
>>> Loop over cases:
>>>      GlobalToLocal(n_global, &n_local)
>>>      GetArray(n_local, &n)
>>>      do stuff with n
>>>      LocalToGlobal(n_local, n_global)
>>>      RestoreArray
>>>
>> This is also wrong.
>> I think the rule here is: GetArray() puts the vector in an interim state.
>> One shall not call any vector routines (ex. LocalToGlobal/GlobalToLocal)
>> before RestoreArray().  You can only operate on the array instead.
>>
>>
>>>
>> Thanks again for all your help,
>>>
>>> Derek
>>>
>>>
>>>
>>> On Fri, Apr 25, 2025 at 5:30 PM Barry Smith <bsmith at petsc.dev> wrote:
>>>
>>>>
>>>>    Technically you should not be calling VecSet() with any outstanding
>>>> arrays but it will probably be fine.
>>>>
>>>>     Even though GetArray() does not copy the array values; both
>>>> GetArray/RestoreArray and Set track the current "state" of the vector and
>>>> that count might get confused if they are used improperly.
>>>>
>>>>
>>>>
>>>> On Apr 25, 2025, at 4:42 PM, Derek Teaney <derek.teaney at stonybrook.edu>
>>>> wrote:
>>>>
>>>> Thanks,   I am working on providing a standalone code. A related
>>>> question is -  if I did have a view of a local vector provided by:
>>>>
>>>> data_node ***dn_array;
>>>> DMDAVecGetArray(domain, dn_local, &dn_array);
>>>>
>>>> Can I assume through multiple calls to VecSet that the view  dn_array
>>>> is valid, or should this be restored, between calls.
>>>>
>>>> Thanks,
>>>>
>>>> Derek
>>>>
>>>> On Fri, Apr 25, 2025 at 1:26 PM Barry Smith <bsmith at petsc.dev> wrote:
>>>>
>>>>>
>>>>>    You absolutely should not need to do an assembly after a VecSet.
>>>>> Please post a full reproducer that demonstrates the problem.
>>>>>
>>>>>   Barry
>>>>>
>>>>>
>>>>> On Apr 25, 2025, at 1:23 PM, Derek Teaney via petsc-users <
>>>>> petsc-users at mcs.anl.gov> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I was under the (mistaken) impression that one does not need to due a
>>>>> VecAssemblyBegin etc following a VecSet, e.g.
>>>>>
>>>>>       VecSet(dn_local, 0.);
>>>>>       VecAssemblyBegin(dn_local) ;
>>>>>       VecAssemblyEnd(dn_local) ;
>>>>>
>>>>> Seems to give different results without the Assembly.
>>>>>
>>>>> Thanks for clarifying,
>>>>>
>>>>> Derek
>>>>>
>>>>> --
>>>>>
>>>>> ------------------------------------------------------------------------
>>>>> Derek Teaney
>>>>> Professor and Graduate Program Director
>>>>> Dept. of Physics & Astronomy
>>>>> Stony Brook University
>>>>> Stony Brook, NY 11794-3800
>>>>> Tel: (631) 632-4489
>>>>> Fax: (631) 632-9718
>>>>> e-mail: Derek.Teaney at stonybrook.edu
>>>>>
>>>>> ------------------------------------------------------------------------
>>>>>
>>>>>
>>>>>
>>>>
>>>> --
>>>> ------------------------------------------------------------------------
>>>> Derek Teaney
>>>> Professor and Graduate Program Director
>>>> Dept. of Physics & Astronomy
>>>> Stony Brook University
>>>> Stony Brook, NY 11794-3800
>>>> Tel: (631) 632-4489
>>>> Fax: (631) 632-9718
>>>> e-mail: Derek.Teaney at stonybrook.edu
>>>> ------------------------------------------------------------------------
>>>>
>>>>
>>>>
>>>
>>> --
>>> ------------------------------------------------------------------------
>>> Derek Teaney
>>> Professor and Graduate Program Director
>>> Dept. of Physics & Astronomy
>>> Stony Brook University
>>> Stony Brook, NY 11794-3800
>>> Tel: (631) 632-4489
>>> Fax: (631) 632-9718
>>> e-mail: Derek.Teaney at stonybrook.edu
>>> ------------------------------------------------------------------------
>>>
>>>
>
> --
> ------------------------------------------------------------------------
> Derek Teaney
> Professor and Graduate Program Director
> Dept. of Physics & Astronomy
> Stony Brook University
> Stony Brook, NY 11794-3800
> Tel: (631) 632-4489
> Fax: (631) 632-9718
> e-mail: Derek.Teaney at stonybrook.edu
> ------------------------------------------------------------------------
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250426/92754692/attachment.html>


More information about the petsc-users mailing list