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

Sparsity pattern insert #3338

Merged
merged 15 commits into from
Aug 27, 2024

Conversation

schnellerhase
Copy link
Contributor

Introduces a new sparsity pattern insert functionality for a single entry - which is the method of choice used in multiple locations of the code base.

Additionally the documentation of the insert(span<>, span<>) call on a sparsity pattern is extended to make clear how the inputs work together, and now hints that the interpretation of a pairwise operation is not correct to the caller.

@schnellerhase schnellerhase force-pushed the sparsity_pattern_insert branch from 8f4c8e2 to c1b5746 Compare August 7, 2024 08:23
@@ -140,14 +140,29 @@ SparsityPattern::SparsityPattern(
}
}
//-----------------------------------------------------------------------------
void SparsityPattern::insert(std::int32_t row, std::int32_t col)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this covered by passing rows and cols of length 1?

Copy link
Contributor Author

@schnellerhase schnellerhase Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It it, but the interface of the caller becomes cleaner. Instead of needing to call

sp.insert(std::vector<std::int32_t>{row}, std::vector<std::int32_t>{col});

we can now simply call

sp.insert(row, col);

same holds for the python interface with numpy arrays.

Also, in combination with the span based version it makes clearer how the sparsity pattern is to be used, i.e. loop over entries and dense matrices and do not pre-compute the entries and then pass it in bulk. Sparsity pattern already performs the caching and associated memory management.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's clear from the improved docstring. I don't see the point of this new method, the clarity of overloading functions is debatable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add, the interface is vectorised from the Python end to encourage users to pass in multiple entries, avoiding the overhead of the function call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How/where is the interface vectorized? I can not find that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisrichardson mentioned the other day, we might instead of the integer version consider to create a non owning const std::vector<std::int32_t>& version, this should allow for the interface to be insert({row}, {col}), i.e. it allows for conversion from an initializer list.

/// This routine inserts non-zero locations at the outer product of rows and
/// cols to the sparsity pattern, i.e. adds the matrix entries at
/// A[row[i], col[j]] for all i, j
/// Enabling easy setting of dense sub-blocks, for example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the word dense. What if I pass e.g. 1, 3, 4 1, 3, 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduced entries then form a dense 3x3 entries again, not locally dense in the sparse matrix, but a dense matrix worth of entries - not possible to only specify a sub set of these with this function.

@schnellerhase
Copy link
Contributor Author

Same interface present for MatrixCSR.set.

@schnellerhase
Copy link
Contributor Author

schnellerhase commented Aug 19, 2024

A related problem of span based interface is present in the global to local mappings of the IndexMap, converting a single value is not straight forward, see here

Copy link
Member

@jpdean jpdean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping single values in std::vector doesn't bother me too much, but I like the improvements to the docs; it's now much clearer what the function is doing!

@chrisrichardson chrisrichardson added this pull request to the merge queue Aug 27, 2024
Merged via the queue into FEniCS:main with commit 4d4a864 Aug 27, 2024
14 of 15 checks passed
schnellerhase added a commit to schnellerhase/fenics-dolfinx that referenced this pull request Sep 11, 2024
* Add single index insert to sparsity pattern.

* Remove some superfluous {}

* export new insert function

* Make use of new insert functionality to simplify code

* Extend insert doc to make clear how the block insertion works

* Fix doc

* to -> into

* Remove duplicate comment

* Remove dense matrix example

* {} style adapted

* Missed some

* Update doctring

---------

Co-authored-by: Chris Richardson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants