[petsc-users] ghost values

Barry Smith bsmith at mcs.anl.gov
Tue Apr 14 19:54:56 CDT 2015


> On Apr 14, 2015, at 7:43 PM, Gideon Simpson <gideon.simpson at gmail.com> wrote:
> 
> So in your example of the form function, is something happening where, when I leave the scope of FormFunction, the invocation of DMGetLocalVector on the DM keeps a note that it had been called previously and has that memory already allocated?

   Yes, it just keeps a lists of available vectors and gives you the next one on the list. When you call RestoreLocalVector() it returns the vector to the list. If it has handed out all the vectors on the list to you (and you have not restored them) it creates a new one and gives it to you.

>  Does the design distinguish between different calls to DMGetLocalVector, like if I called it in one subroutine and then in another subroutine, or twice in a single subroutine?
> 

   Not sure what you mean by this but if you call it twice it gives you two different vectors (unless of course you restored the first before requesting the second) It does't matter from what function your request the vector or restore it (but passing around a vector you obtained with GetVector through a complicated set of function calls is generally bad because you will forget to restore it). Hence it is better to Get; Use; Restore

  Here is the entire routine.

PetscErrorCode  DMGetLocalVector(DM dm,Vec *g)
{
  PetscErrorCode ierr,i;

  PetscFunctionBegin;
  PetscValidHeaderSpecific(dm,DM_CLASSID,1);
  PetscValidPointer(g,2);
  for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
    if (dm->localin[i]) {
      *g             = dm->localin[i];
      dm->localin[i] = NULL;
      goto alldone;
    }
  }
  ierr = DMCreateLocalVector(dm,g);CHKERRQ(ierr);

alldone:
  for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
    if (!dm->localout[i]) {
      dm->localout[i] = *g;
      break;
    }
  }
  PetscFunctionReturn(0);
}

> -gideon
> 
>> On Apr 14, 2015, at 8:23 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:
>> 
>> 
>>> On Apr 14, 2015, at 6:25 PM, Gideon Simpson <gideon.simpson at gmail.com> wrote:
>>> 
>>> So other than the minor point of it zeroing out the data, is there any conceivable reason to use Create/Destroy over Get/Restore?
>> 
>> The design intends that Create/Destroy are for long lived objects. For example in main you might call Create to make a vector you will store the solution for the entire simulation run in then just before you exit the program you destroy it.
>> 
>>  The Get/Restore are for work vectors that are used within one routine. The most classic example is in FormFunction where you often have something like  DMGetLocalVector() ;  DMGlobalToLocalBegin/End(global, local, ...) DMDAVecGetArray(local) ..../  DMRestoreLocalVector(). The idea is that rather creating work vectors in main and passing them around to use when needed, DM manages a "pool" of work vectors that you can grab whenever you need them and are low overhead to access.
>> 
>> There is nothing that enforces you follow this style, but if you don't you need to be careful to know which vectors to call VecDestroy() on and which ones to call DMRestoreLocalVector() on plus it will be easier for other people to follow your code.
>> 
>> Barry
>> 
>>> 
>>> -gideon
>>> 
>>>> On Apr 14, 2015, at 7:16 PM, Matthew Knepley <knepley at gmail.com> wrote:
>>>> 
>>>> On Tue, Apr 14, 2015 at 6:15 PM, Gideon Simpson <gideon.simpson at gmail.com> wrote:
>>>> Other than zeroing things out, is there any substantive difference between DMCreateLocalVector and DMGetLocalVector?
>>>> 
>>>> The Get version caches vectors, so you are not continually creating/destroying
>>>> 
>>>>  Matt
>>>> 
>>>> -gideon
>>>> 
>>>>> On Apr 14, 2015, at 7:02 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:
>>>>> 
>>>>> 
>>>>> When PETSc vectors are created initially they always have 0 everywhere. So if you use DMCreateLocalVector() it will have zero in all those ghost places (as well as everywhere else).
>>>>> 
>>>>> But if you use DMGetLocalVector() it returns vectors that maybe dirty so you need to fill in any locations you want to have a known value with that known value. Or call VecSet() to clear the entire vector.
>>>>> 
>>>>> Barry
>>>>> 
>>>>>> On Apr 14, 2015, at 5:52 PM, Gideon Simpson <gideon.simpson at gmail.com> wrote:
>>>>>> 
>>>>>> If i use the DM_BOUNDARY_GHOSTED flag in the creation of a DMDA array, are the ghosted values automatically set to zero, or should they be manually set to zero if that’s the desired ghost value?
>>>>>> 
>>>>>> -gideon
>>>>>> 
>>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> 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
>>> 
>> 
> 



More information about the petsc-users mailing list