-
Notifications
You must be signed in to change notification settings - Fork 106
WIP: Calculate Jacobian coloring using PetscOperator matrices #3350
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
base: petsc-operators
Are you sure you want to change the base?
Changes from all commits
b48c40b
325019a
b433faf
65185d0
4920c86
9feb293
7fadf06
729f86e
ba6996e
9de5da3
631dcdd
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #pragma once | ||
|
|
||
| #ifndef BOUT_PETSC_JACOBIAN_H | ||
| #define BOUT_PETSC_JACOBIAN_H | ||
|
|
||
| #include "bout/build_defines.hxx" | ||
|
|
||
| #if BOUT_HAS_PETSC | ||
|
|
||
| #include <petscmat.h> | ||
|
|
||
| /// Insert the nonzero pattern of @p sub into the variable block | ||
| /// (@p out_var, @p in_var) of the Jacobian @p Jfd. | ||
| /// | ||
| /// @p Jfd is a square matrix of size (n_evolving * nvars) where nvars is | ||
| /// inferred as Jfd_global_size / sub_global_size. Each nonzero (r, c) in | ||
| /// @p sub produces an entry at (r * nvars + out_var, c * nvars + in_var) | ||
| /// in @p Jfd. | ||
| /// | ||
| /// @p Jfd must already be preallocated. Entries are inserted with | ||
| /// INSERT_VALUES; MatAssemblyBegin/End must be called by the caller after | ||
| /// all insertions are complete. | ||
| /// | ||
| /// @param Jfd The Jacobian matrix to populate. Must be preallocated. | ||
| /// @param sub Evolving-cell submatrix providing the nonzero pattern. | ||
| /// @param out_var Row variable index in [0, nvars). | ||
| /// @param in_var Column variable index in [0, nvars). | ||
| void addOperatorSparsity(Mat Jfd, Mat sub, int out_var, int in_var); | ||
|
|
||
| /// @brief Insert the nonzero pattern of @p sub into every variable block of | ||
| /// @p Jfd. | ||
| /// | ||
| /// Equivalent to calling addOperatorSparsity(Jfd, sub, out_var, in_var) for | ||
| /// every combination of @p out_var and @p in_var in [0, nvars), where | ||
| /// @c nvars is inferred as @c Jfd_global / @c sub_global. | ||
| /// | ||
| /// @param Jfd The Jacobian matrix to populate. Must be preallocated. | ||
| /// @param sub Evolving-cell submatrix providing the nonzero pattern. | ||
| void addOperatorSparsity(Mat Jfd, Mat sub); | ||
|
|
||
| #endif // BOUT_HAS_PETSC | ||
|
|
||
| #endif // BOUT_PETSC_JACOBIAN_H |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||
| #include "bout/build_defines.hxx" | ||||||||
|
|
||||||||
| #if BOUT_HAS_PETSC | ||||||||
|
|
||||||||
| #include "bout/assert.hxx" | ||||||||
| #include "bout/petsc_jacobian.hxx" | ||||||||
| #include "bout/petsclib.hxx" | ||||||||
|
|
||||||||
| #include <petscmat.h> | ||||||||
|
|
||||||||
| void addOperatorSparsity(Mat Jfd, Mat sub, int out_var, int in_var) { | ||||||||
| // Infer nvars from global sizes | ||||||||
| PetscInt jfd_global{0}, sub_global{0}; | ||||||||
| BOUT_DO_PETSC(MatGetSize(Jfd, &jfd_global, nullptr)); | ||||||||
| BOUT_DO_PETSC(MatGetSize(sub, &sub_global, nullptr)); | ||||||||
|
|
||||||||
| ASSERT1(sub_global > 0); | ||||||||
| ASSERT1(jfd_global % sub_global == 0); | ||||||||
|
|
||||||||
| const PetscInt nvars = jfd_global / sub_global; | ||||||||
|
|
||||||||
| ASSERT1(out_var >= 0 && out_var < static_cast<int>(nvars)); | ||||||||
| ASSERT1(in_var >= 0 && in_var < static_cast<int>(nvars)); | ||||||||
|
|
||||||||
| // Iterate over locally owned rows of sub and insert into Jfd | ||||||||
| PetscInt rstart{0}, rend{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: multiple declarations in a single statement reduces readability [readability-isolate-declaration]
Suggested change
|
||||||||
| BOUT_DO_PETSC(MatGetOwnershipRange(sub, &rstart, &rend)); | ||||||||
|
|
||||||||
| const PetscScalar one = 1.0; | ||||||||
|
|
||||||||
| for (PetscInt sub_row = rstart; sub_row < rend; ++sub_row) { | ||||||||
| PetscInt ncols{0}; | ||||||||
| const PetscInt* sub_cols{nullptr}; | ||||||||
| const PetscScalar* vals{nullptr}; | ||||||||
| BOUT_DO_PETSC(MatGetRow(sub, sub_row, &ncols, &sub_cols, &vals)); | ||||||||
|
|
||||||||
| const PetscInt jfd_row = (sub_row * nvars) + out_var; | ||||||||
|
|
||||||||
| for (PetscInt k = 0; k < ncols; ++k) { | ||||||||
| const PetscInt jfd_col = (sub_cols[k] * nvars) + in_var; | ||||||||
| BOUT_DO_PETSC(MatSetValues(Jfd, 1, &jfd_row, 1, &jfd_col, &one, INSERT_VALUES)); | ||||||||
| } | ||||||||
|
|
||||||||
| BOUT_DO_PETSC(MatRestoreRow(sub, sub_row, &ncols, &sub_cols, &vals)); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| void addOperatorSparsity(Mat Jfd, Mat sub) { | ||||||||
| PetscInt jfd_global{0}, sub_global{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: multiple declarations in a single statement reduces readability [readability-isolate-declaration]
Suggested change
|
||||||||
| MatGetSize(Jfd, &jfd_global, nullptr); | ||||||||
| MatGetSize(sub, &sub_global, nullptr); | ||||||||
|
|
||||||||
| ASSERT1(sub_global > 0); | ||||||||
| ASSERT1(jfd_global % sub_global == 0); | ||||||||
|
|
||||||||
| const int nvars = static_cast<int>(jfd_global / sub_global); | ||||||||
|
|
||||||||
| for (int out_var = 0; out_var < nvars; ++out_var) { | ||||||||
| for (int in_var = 0; in_var < nvars; ++in_var) { | ||||||||
| addOperatorSparsity(Jfd, sub, out_var, in_var); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #endif // BOUT_HAS_PETSC | ||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |||||
| #include "bout/bout_types.hxx" | ||||||
| #include "bout/boutexception.hxx" | ||||||
| #include "bout/field3d.hxx" | ||||||
| #include "bout/output.hxx" | ||||||
| #include "bout/output_bout_types.hxx" | ||||||
| #include "bout/petsc_operators.hxx" | ||||||
| #include "bout/region.hxx" | ||||||
|
|
@@ -127,6 +126,33 @@ PetscCellMapping::PetscCellMapping(const Field3D& cell_number, | |||||
| local_indices); | ||||||
| } | ||||||
|
|
||||||
| IS PetscCellMapping::makeEvolvingIS() const { | ||||||
| // Collect global PETSc indices in mapOwnedInteriorCells order. | ||||||
| // Reserve the known count up front to avoid reallocation. | ||||||
| std::vector<PetscInt> indices; | ||||||
| indices.reserve(static_cast<std::size_t>(evolving_region.size())); | ||||||
|
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: no header providing "std::size_t" is directly included [misc-include-cleaner] src/mesh/petsc_operators.cxx:1: + #include <cstddef> |
||||||
|
|
||||||
| mapOwnedInteriorCells( | ||||||
| [&](PetscInt row, const Ind3D& /*i*/, int /*stored*/) { indices.push_back(row); }); | ||||||
|
|
||||||
| IS is; | ||||||
|
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 'is' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
| BOUT_DO_PETSC(ISCreateGeneral(BoutComm::get(), static_cast<PetscInt>(indices.size()), | ||||||
| indices.data(), PETSC_COPY_VALUES, &is)); | ||||||
| return is; | ||||||
| } | ||||||
|
|
||||||
| Mat PetscCellMapping::extractEvolvingSubmatrix( | ||||||
| const PetscOperator<CellSpaceTag, CellSpaceTag>& op) const { | ||||||
| IS is = makeEvolvingIS(); | ||||||
|
|
||||||
| Mat sub; | ||||||
|
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 'sub' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
| BOUT_DO_PETSC(MatCreateSubMatrix(op.raw(), is, is, MAT_INITIAL_MATRIX, &sub)); | ||||||
|
|
||||||
| BOUT_DO_PETSC(ISDestroy(&is)); | ||||||
|
|
||||||
| return sub; | ||||||
| } | ||||||
|
|
||||||
| PetscLegMapping::PetscLegMapping(int total_legs, std::vector<int> local_leg_indices) { | ||||||
| std::sort(local_leg_indices.begin(), local_leg_indices.end()); | ||||||
| local_leg_indices.erase(std::unique(local_leg_indices.begin(), local_leg_indices.end()), | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| bout_add_integrated_test( | ||
| test-petsc-ordering | ||
| SOURCES test_petsc_ordering.cxx | ||
| REQUIRES BOUT_HAS_PETSC | ||
| USE_RUNTEST USE_DATA_BOUT_INP | ||
| PROCESSORS 4 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Input file for test_petsc_ordering integrated test. | ||
| # Uses a small grid large enough to exercise MPI decomposition on 4 ranks | ||
| # (nx=10 gives 2 interior x-points per rank when NXPE=4; ny and nz similarly). | ||
|
|
||
| [mesh] | ||
| nx = 10 # 8 interior + 2 guard (mxg=1 each side) | ||
| ny = 8 | ||
| nz = 4 | ||
|
|
||
| ixseps1 = nx | ||
| ixseps2 = nx | ||
|
|
||
| [output] | ||
| enabled = false # Suppress data file output; we only need stdout |
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: multiple declarations in a single statement reduces readability [readability-isolate-declaration]