[petsc-dev] Segfault in DMPlexVecGetClosure
Lawrence Mitchell
wence at gmx.li
Tue Oct 31 12:38:50 CDT 2017
> On 31 Oct 2017, at 15:36, Blaise A Bourdin <bourdin at lsu.edu> wrote:
>
> Hi,
>
> I am losing my mind over segfaults in DMPlexVecGetClosure.
>
> In the example below, I get a segfault whenever cval is not set to NULL before calling DMPlexVecGetClosure. Is this expected? The documentations says nothing about this
> Also, after calling DMPlexVecRestoreClosure, the values of cval are discarded and neither global nor coord are changed.
>
> I am obviously doing something wrong but cannot figure it out...
The interface is similar to DMPlexGetClosure.
DMPlexVecGetClosure fills the array you provide it with values. It can allocate a work array for you if you don't provide one, but you must indicate so explicitly by setting it to NULL:
so
PetscScalar *values = NULL;
DMPlexVecGetClosure(...., &values);
Do things with values
Now you need to release the work array, you do that with the matching restore closure call.
DMPlexVecRestoreClosure(...., &values);
Now, these arrays you get are *not* pointers into the global Vec array, instead they are local buffers. If you want to update the Vec with the values you've put in values, you need to call DMPlexVecSetClosure:
e.g.
DMPlexVecSetClosure(..., values, INSERT_VALUES);
So the calling sequence should be something like:
PetscScalar *values = NULL;
for ( int p = 0; p < ...; p++ ) {
DMPlexVecGetClosure(..., p, ..., &values);
// do stuff with values.
DMPlexVecSetClosure(..., p, ..., values, INSERT_VALUES);
}
// Finally, restore work array
DMPlexVecRestoreClosure(..., 0, ..., &values);
Cheers,
Lawrence
More information about the petsc-dev
mailing list