Handle PETSc signature change for FormFunctionForColoring#3116
Handle PETSc signature change for FormFunctionForColoring#3116
Conversation
…smatch Using reinterpret_cast no longer works. Expected signature is PetscErrorCode (*)() but is PetscErrorCode (*)(SNES, Vec, Vec, void*) on main branch. For the newer version the current solver instance must be passed as the 'cxt' argument, so store 'this' instance in a global variable (before calling MatFDColoringSetFunction). Have to do it this way because the function passed as the (second) argument to MatFDColoringSetFunction must be a free function, so cannot make the function wrapper an instance method and pass 'this' from there.
…*, Vec x, Vec y, void* ctx)
Use a wrapper around solver_f for passing as the second argument to MatFDColoringSetFunction.
| } | ||
|
|
||
| static PetscErrorCode imexbdf2PCapply(PC pc, Vec x, Vec y) { | ||
| // Global context to store IMEXBDF2 instance |
There was a problem hiding this comment.
warning: variable 'imexbdf2_ctx' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace]
static void* imexbdf2_ctx = nullptr;
^| } | ||
|
|
||
| static PetscErrorCode imexbdf2PCapply(PC pc, Vec x, Vec y) { | ||
| // Global context to store IMEXBDF2 instance |
There was a problem hiding this comment.
warning: variable 'imexbdf2_ctx' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
static void* imexbdf2_ctx = nullptr;
^| } | ||
|
|
||
| static PetscErrorCode imexbdf2PCapply(PC pc, Vec x, Vec y) { | ||
| // Global context to store IMEXBDF2 instance |
There was a problem hiding this comment.
warning: variable 'imexbdf2_ctx' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
static void* imexbdf2_ctx = nullptr;
^| } | ||
|
|
||
| // Global context to store SNESSolver instance | ||
| static void* snes_ctx = nullptr; |
There was a problem hiding this comment.
warning: variable 'snes_ctx' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
static void* snes_ctx = nullptr;
^| } | ||
| #else | ||
| // Wrapper for PETSc < 3.20 (signature: PetscErrorCode (*)(void)) | ||
| static PetscErrorCode FormFunctionForColoringWrapper() { |
There was a problem hiding this comment.
warning: function 'FormFunctionForColoringWrapper' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace]
static PetscErrorCode FormFunctionForColoringWrapper() {
^|
Thanks @tomc271, unfortunately this approach won't work -- although |
|
Ok. It got all the tests to pass, so I guess there is no test covering that scenario. |
|
Hmm, I did think we had tests on this, maybe they're not on by default |
Use a wrapper function to handle FormFunctionForColoring signature mismatch, as using reinterpret_cast no longer works.
Expected signature is
PetscErrorCode (*)()but is
PetscErrorCode (*)(SNES, Vec, Vec, void*)in newer PETSc versions (on main branch, but no releases yet).For the newer version the current solver instance must be passed as the 'cxt' argument,
so store 'this' instance in a global variable (before calling
MatFDColoringSetFunction).Have to do it this way because the function passed as the (second) argument to
MatFDColoringSetFunctionmust be a free function, so cannot make the function wrapper an instance method and pass 'this' from there.