Skip to content
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

Allow creating matrices based on hash tables #3983

Draft
wants to merge 29 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d557acc
Space between function name and parens
lindsayad Oct 30, 2024
1e02946
Add init_hash methods to SparseMatrix
lindsayad Oct 30, 2024
2ead745
Try using hash table in system of eq ex7
lindsayad Oct 30, 2024
274d57e
Add a destroy_on_exit parameter to constructor-from-Mat
lindsayad Oct 30, 2024
0fc8c94
Create copy_from_hash method
lindsayad Oct 30, 2024
63451fe
Revert "Space between function name and parens"
lindsayad Nov 3, 2024
3989719
Refactoring: add use_hash_table methods to SparseMatrix
lindsayad Nov 4, 2024
4228676
reset -> reset_memory
lindsayad Nov 5, 2024
d7b4e6e
We don't prefer hash table matrix assembly by default
lindsayad Nov 22, 2024
d2b80e0
Don't break after return type in clang-format
lindsayad Nov 22, 2024
3d5e4b2
Move setting of new nonzero error into new finish_initialization routine
lindsayad Nov 22, 2024
b368217
PETSc didn't support hash table assembly until 3.19
lindsayad Nov 25, 2024
e617d40
Adjust reset_memory error message
lindsayad Nov 25, 2024
705627d
WIP: Address Roy review
lindsayad Dec 18, 2024
f102d7c
Add VSCode related files to gitignore
lindsayad Dec 18, 2024
2bb581b
Create miscellaneous example 17
lindsayad Dec 19, 2024
6f81eae
Changes to make our example unique
lindsayad Dec 19, 2024
47e999e
Add example to autotools files
lindsayad Dec 19, 2024
00a3ac7
bootstrap
lindsayad Dec 19, 2024
50abf05
Make example smaller
lindsayad Dec 19, 2024
bdc9e26
do repeat solves, resetting memory in between
lindsayad Dec 19, 2024
a3d8bf2
Always honor `use_hash_table` if `true`
lindsayad Dec 19, 2024
300ac39
Add monitor programmatically
lindsayad Dec 19, 2024
a3bed02
Add html for John
lindsayad Dec 19, 2024
17092ad
Skip resetting preallocation for now
lindsayad Dec 20, 2024
3829c12
Put PETSc version guards around code that calls MatResetHash
lindsayad Dec 21, 2024
5559c3f
Guard petscksp include
lindsayad Dec 23, 2024
a7df39b
More guards
lindsayad Dec 23, 2024
4a8a5e3
Keep fighting CI to make example workable without PETSc
lindsayad Dec 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ AllowShortLoopsOnASingleLine: false
SortIncludes: false
IndentCaseLabels: true
ConstructorInitializerIndentWidth: 2
AlwaysBreakAfterDefinitionReturnType: TopLevel
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: true

FixNamespaceComments: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ int main (int argc, char ** argv)

NonlinearImplicitSystem & system =
equation_systems.add_system<NonlinearImplicitSystem> ("NonlinearElasticity");
system.prefer_hash_table_matrix_assembly(true);

unsigned int u_var =
system.add_variable("u",
Expand Down
46 changes: 44 additions & 2 deletions include/numerics/petsc_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ class PetscMatrix final : public PetscMatrixBase<T>

/**
* Constructor. Creates a PetscMatrix assuming you already have a
* valid Mat object. In this case, m is NOT destroyed by the
* valid Mat object. In this case, m may not be destroyed by the
* PetscMatrix destructor when this object goes out of scope. This
* allows ownership of m to remain with the original creator, and to
* simply provide additional functionality with the PetscMatrix.
*/
explicit
PetscMatrix (Mat m,
const Parallel::Communicator & comm_in);
const Parallel::Communicator & comm_in,
bool destroy_on_exit = false);

/**
* Constructor. Creates and initializes a PetscMatrix with the given
Expand Down Expand Up @@ -284,7 +285,48 @@ class PetscMatrix final : public PetscMatrixBase<T>

virtual void scale(const T scale) override;

#if PETSC_RELEASE_GREATER_EQUALS(3,23,0)
std::unique_ptr<PetscMatrix<T>> copy_from_hash();
roystgnr marked this conversation as resolved.
Show resolved Hide resolved
#endif

virtual bool supports_hash_table() const override;

virtual void reset_memory() override;

protected:
/**
* Perform matrix initialization steps sans preallocation
* @param m The global number of rows
* @param n The global number of columns
* @param m_l The local number of rows
* @param n_l The local number of columns
* @param blocksize The matrix block size
*/
void init_without_preallocation (numeric_index_type m,
numeric_index_type n,
numeric_index_type m_l,
numeric_index_type n_l,
numeric_index_type blocksize);

/*
* Performs matrix preallcation
* \param m_l The local number of rows.
* \param n_nz array containing the number of nonzeros in each row of the DIAGONAL portion of the local submatrix.
* \param n_oz Array containing the number of nonzeros in each row of the OFF-DIAGONAL portion of the local submatrix.
* \param blocksize Optional value indicating dense coupled blocks for systems with multiple variables all of the same */
void preallocate(numeric_index_type m_l,
const std::vector<numeric_index_type> & n_nz,
const std::vector<numeric_index_type> & n_oz,
numeric_index_type blocksize);

/**
* Finish up the initialization process. This method does a few things which include
* - Setting the option to make new nonzeroes an error (otherwise users will just have a silent
(often huge) performance penalty
* - Marking the matrix as initialized
* - Zeroing the matrix
*/
void finish_initialization();

/**
* This function either creates or re-initializes a matrix called \p
Expand Down
5 changes: 3 additions & 2 deletions include/numerics/petsc_matrix_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ class PetscMatrixBase : public SparseMatrix<T>

/**
* Constructor. Creates a PetscMatrixBase assuming you already have a
* valid Mat object. In this case, m is NOT destroyed by the
* valid Mat object. In this case, m may not be destroyed by the
* PetscMatrixBase destructor when this object goes out of scope. This
* allows ownership of m to remain with the original creator, and to
* simply provide additional functionality with the PetscMatrixBase.
*/
explicit
PetscMatrixBase (Mat m,
const Parallel::Communicator & comm_in);
const Parallel::Communicator & comm_in,
bool destroy_on_exit = false);

/**
* This class manages a C-style struct (Mat) manually, so we
Expand Down
47 changes: 47 additions & 0 deletions include/numerics/sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,32 @@ class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
*/
virtual void scale(const T scale);

/**
* @returns Whether the matrix supports hash table assembly
*/
virtual bool supports_hash_table() const { return false; }

/**
* @returns Whether a preference for hash table vs. preallocation has been set
*/
bool assembly_preference_set() const { return _assembly_preference_set; }
roystgnr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets whether to use hash table assembly. This will error if the passed-in value is true and the
* matrix type does not support hash tables
roystgnr marked this conversation as resolved.
Show resolved Hide resolved
*/
void use_hash_table(bool use_hash);

/**
* @returns Whether this matrix is using hash table assembly
*/
bool use_hash_table() const { return _use_hash_table; }

/**
* Reset the memory storage of the matrix
roystgnr marked this conversation as resolved.
Show resolved Hide resolved
*/
virtual void reset_memory() { libmesh_not_implemented(); }

protected:
/**
* Protected implementation of the create_submatrix and reinit_submatrix
Expand Down Expand Up @@ -616,12 +642,33 @@ class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
* Flag indicating whether or not the matrix has been initialized.
*/
bool _is_initialized;

/**
* Flag indicating whether the matrix is assembled using a hash table
*/
bool _use_hash_table;

/**
* Whether a preference for hash table vs. preallocation has been set
*/
bool _assembly_preference_set;
};



//-----------------------------------------------------------------------
// SparseMatrix inline members
template <typename T>
void
SparseMatrix<T>::use_hash_table(const bool use_hash)
{
libmesh_error_msg_if(use_hash && !this->supports_hash_table(),
"This matrix class does not support hash table assembly");
libmesh_error_msg_if(this->_assembly_preference_set,
"The use_hash_table API should only be called once");
this->_use_hash_table = use_hash;
this->_assembly_preference_set = true;
}

// For SGI MIPSpro this implementation must occur after
// the full specialization of the print() member.
Expand Down
22 changes: 22 additions & 0 deletions include/systems/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,10 @@ class System : public ReferenceCountedObject<System>,
*/
SparseMatrix<Number> & get_matrix (std::string_view mat_name);

/**
* Sets whether to use hash table matrix assembly if the matrix sub-classes support it
*/
void prefer_hash_table_matrix_assembly(bool preference);

protected:

Expand Down Expand Up @@ -2293,6 +2297,16 @@ class System : public ReferenceCountedObject<System>,
* Do we want to apply constraints while projecting vectors ?
*/
bool project_with_constraints;

/**
* Whether to use hash table matrix assembly if the matrix sub-classes support it
*/
bool _prefer_hash_table_matrix_assembly;

/**
* Whether any of our matrices require an initial sparsity pattern computation in order to determine preallocation
*/
bool _require_sparsity_pattern;
};


Expand Down Expand Up @@ -2685,6 +2699,14 @@ System::add_matrix (std::string_view mat_name,
return mat;
}

inline void
System::prefer_hash_table_matrix_assembly(const bool preference)
{
libmesh_error_msg_if(
_matrices_initialized,
"System::prefer_hash_table_matrix_assembly() should be called before matrices are initialized");
_prefer_hash_table_matrix_assembly = preference;
}

} // namespace libMesh

Expand Down
Loading