-
Notifications
You must be signed in to change notification settings - Fork 107
Handle PETSc signature change for FormFunctionForColoring #3116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
58588d4
a51fa0f
fc2e6bd
607fffe
fdaeb45
6d6be6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -118,7 +118,25 @@ static PetscErrorCode FormFunctionForColoring(SNES UNUSED(snes), Vec x, Vec f, | |||||
| return static_cast<IMEXBDF2*>(ctx)->snes_function(x, f, true); | ||||||
| } | ||||||
|
|
||||||
| static PetscErrorCode imexbdf2PCapply(PC pc, Vec x, Vec y) { | ||||||
| // Global context to store IMEXBDF2 instance | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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;
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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;
^ |
||||||
| static void* imexbdf2_ctx = nullptr; | ||||||
|
|
||||||
|
ZedThree marked this conversation as resolved.
ZedThree marked this conversation as resolved.
|
||||||
| #if PETSC_VERSION_GE(3, 24, 0) || PETSC_VERSION_RELEASE == 0 | ||||||
| // Wrapper for PETSc 3.24 and later (signature: PetscErrorCode (*)(void*, Vec, Vec, void*)) | ||||||
| static PetscErrorCode FormFunctionForColoringWrapper(void*, Vec x, Vec y, void* ctx) { | ||||||
| SNES dummy_snes = nullptr; | ||||||
| return FormFunctionForColoring(dummy_snes, x, y, ctx); | ||||||
| } | ||||||
| #else | ||||||
| // Wrapper for PETSc < 3.20 (signature: PetscErrorCode (*)(void)) | ||||||
|
ZedThree marked this conversation as resolved.
|
||||||
| static PetscErrorCode FormFunctionForColoringWrapper() { | ||||||
| SNES dummy_snes = nullptr; | ||||||
| Vec dummy_vec = nullptr; | ||||||
| return FormFunctionForColoring(dummy_snes, dummy_vec, dummy_vec, imexbdf2_ctx); | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
|
ZedThree marked this conversation as resolved.
|
||||||
| PetscErrorCode imexbdf2PCapply(PC pc, Vec x, Vec y) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'ierr' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
| int ierr; | ||||||
|
|
||||||
| // Get the context | ||||||
|
|
@@ -651,9 +669,8 @@ void IMEXBDF2::constructSNES(SNES* snesIn) { | |||||
| // Create data structure for SNESComputeJacobianDefaultColor | ||||||
| MatFDColoringCreate(Jmf, iscoloring, &fdcoloring); | ||||||
| // Set the function to difference | ||||||
| MatFDColoringSetFunction( | ||||||
| fdcoloring, reinterpret_cast<PetscErrorCode (*)()>(FormFunctionForColoring), | ||||||
| this); | ||||||
| imexbdf2_ctx = this; | ||||||
| MatFDColoringSetFunction(fdcoloring, FormFunctionForColoringWrapper, this); | ||||||
| MatFDColoringSetFromOptions(fdcoloring); | ||||||
| MatFDColoringSetUp(Jmf, iscoloring, fdcoloring); | ||||||
| ISColoringDestroy(&iscoloring); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -61,6 +61,26 @@ extern PetscErrorCode PhysicsPCApply(PC, Vec x, Vec y); | |||||
| extern PetscErrorCode PhysicsJacobianApply(Mat J, Vec x, Vec y); | ||||||
| extern PetscErrorCode PhysicsSNESApply(SNES, Vec); | ||||||
|
|
||||||
| // Global context to store PetscSolver instance | ||||||
| static void* petsc_ctx = nullptr; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'petsc_ctx' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace] static void* petsc_ctx = nullptr;
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'petsc_ctx' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables] static void* petsc_ctx = nullptr;
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'petsc_ctx' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables] static void* petsc_ctx = nullptr;
^ |
||||||
|
|
||||||
| #if PETSC_VERSION_GE(3, 24, 0) || PETSC_VERSION_RELEASE == 0 | ||||||
|
ZedThree marked this conversation as resolved.
|
||||||
| // Wrapper for PETSc 3.24 and later (signature: PetscErrorCode (*)(void*, Vec, Vec, void*)) | ||||||
| static PetscErrorCode FormFunctionForColoringWrapper(void*, Vec x, Vec y, void* ctx) { | ||||||
| TS dummy_ts = nullptr; | ||||||
| BoutReal dummy_time = 0.0; | ||||||
| return solver_f(dummy_ts, dummy_time, x, y, ctx); | ||||||
| } | ||||||
| #else | ||||||
| // Wrapper for PETSc < 3.20 (signature: PetscErrorCode (*)(void)) | ||||||
| static PetscErrorCode FormFunctionForColoringWrapper() { | ||||||
|
ZedThree marked this conversation as resolved.
|
||||||
| TS dummy_ts = nullptr; | ||||||
| BoutReal dummy_time = 0.0; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'dummy_time' of type 'BoutReal' (aka 'double') can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
| Vec dummy_vec = nullptr; | ||||||
| return solver_f(dummy_ts, dummy_time, dummy_vec, dummy_vec, petsc_ctx); | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| PetscSolver::PetscSolver(Options* opts) | ||||||
| : Solver(opts), | ||||||
| diagnose( | ||||||
|
|
@@ -514,8 +534,8 @@ int PetscSolver::init() { | |||||
| CHKERRQ(ierr); | ||||||
| ierr = ISColoringDestroy(&iscoloring); | ||||||
| CHKERRQ(ierr); | ||||||
| ierr = MatFDColoringSetFunction(matfdcoloring, | ||||||
| reinterpret_cast<PetscErrorCode (*)()>(solver_f), this); | ||||||
| petsc_ctx = this; | ||||||
| ierr = MatFDColoringSetFunction(matfdcoloring, FormFunctionForColoringWrapper, this); | ||||||
|
ZedThree marked this conversation as resolved.
|
||||||
| CHKERRQ(ierr); | ||||||
| ierr = SNESSetJacobian(snes, J, J, SNESComputeJacobianDefaultColor, matfdcoloring); | ||||||
| CHKERRQ(ierr); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,24 @@ static PetscErrorCode FormFunctionForColoring(SNES UNUSED(snes), Vec x, Vec f, | |
| return static_cast<SNESSolver*>(ctx)->snes_function(x, f, true); | ||
| } | ||
|
|
||
| // Global context to store SNESSolver instance | ||
| static void* snes_ctx = nullptr; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'snes_ctx' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace] static void* snes_ctx = nullptr;
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'snes_ctx' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables] static void* snes_ctx = nullptr;
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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;
^ |
||
|
|
||
| #if PETSC_VERSION_GE(3, 24, 0) || PETSC_VERSION_RELEASE == 0 | ||
|
ZedThree marked this conversation as resolved.
ZedThree marked this conversation as resolved.
|
||
| // Wrapper for PETSc 3.24 and later (signature: PetscErrorCode (*)(void*, Vec, Vec, void*)) | ||
| static PetscErrorCode FormFunctionForColoringWrapper(void*, Vec x, Vec y, void* ctx) { | ||
| SNES dummy_snes = nullptr; | ||
| return FormFunctionForColoring(dummy_snes, x, y, ctx); | ||
| } | ||
| #else | ||
| // Wrapper for PETSc < 3.20 (signature: PetscErrorCode (*)(void)) | ||
| static PetscErrorCode FormFunctionForColoringWrapper() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: function 'FormFunctionForColoringWrapper' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace] static PetscErrorCode FormFunctionForColoringWrapper() {
^ |
||
| SNES dummy_snes = nullptr; | ||
| Vec dummy_vec = nullptr; | ||
| return FormFunctionForColoring(dummy_snes, dummy_vec, dummy_vec, snes_ctx); | ||
| } | ||
| #endif | ||
|
|
||
| static PetscErrorCode snesPCapply(PC pc, Vec x, Vec y) { | ||
| int ierr; | ||
|
|
||
|
|
@@ -1291,8 +1309,8 @@ void SNESSolver::updateColoring() { | |
| // Replace the old coloring with the new one | ||
| MatFDColoringDestroy(&fdcoloring); | ||
| MatFDColoringCreate(Jfd, iscoloring, &fdcoloring); | ||
| MatFDColoringSetFunction( | ||
| fdcoloring, reinterpret_cast<PetscErrorCode (*)()>(FormFunctionForColoring), this); | ||
| snes_ctx = this; | ||
| MatFDColoringSetFunction(fdcoloring, FormFunctionForColoringWrapper, this); | ||
|
ZedThree marked this conversation as resolved.
|
||
| MatFDColoringSetFromOptions(fdcoloring); | ||
| MatFDColoringSetUp(Jfd, iscoloring, fdcoloring); | ||
| ISColoringDestroy(&iscoloring); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: variable 'imexbdf2_ctx' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace]