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

Use container instead of allocator in dolfinx::la::Vector #2639

Merged
merged 25 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1967f5e
Use container not allocator as template
chrisrichardson Apr 25, 2023
4ccd29a
Update test
chrisrichardson Apr 25, 2023
38cd4e6
Updates for demos
chrisrichardson Apr 25, 2023
4d8665c
Also for MatrixCSR
chrisrichardson Apr 25, 2023
1cde283
Fix documentation
chrisrichardson Apr 25, 2023
5ac089f
Add index container
chrisrichardson Apr 25, 2023
d276f0a
Support scalar as template arg to la::Vector
garth-wells Apr 25, 2023
22abc6a
Remove commented code
garth-wells Apr 25, 2023
0f04e48
Fix bindings
garth-wells Apr 25, 2023
3e64ffd
Demo update
garth-wells Apr 25, 2023
d60a5b2
Simplify traits
garth-wells Apr 25, 2023
d9630a9
Make doxygen happy
chrisrichardson Apr 25, 2023
3646e8e
Interface change
garth-wells Apr 26, 2023
3698311
Updates
garth-wells Apr 26, 2023
d13da96
Merge branch 'main' into chris/vector-container
garth-wells Apr 26, 2023
bc54276
Tidy up and make consistent
chrisrichardson Apr 26, 2023
c8c4901
Remove commented code
chrisrichardson Apr 26, 2023
8fc8755
Add static type checks
garth-wells Apr 26, 2023
e007e34
Merge branch 'chris/vector-container' of github.com:FEniCS/dolfinx in…
garth-wells Apr 26, 2023
ca6b7e0
Change some naming
chrisrichardson Apr 26, 2023
d674724
Merge branch 'chris/vector-container' of ssh://github.com/fenics/dolf…
chrisrichardson Apr 26, 2023
61b8d90
Move implementation
garth-wells Apr 26, 2023
42467af
Merge branch 'chris/vector-container' of github.com:FEniCS/dolfinx in…
garth-wells Apr 26, 2023
832bbbb
Extend MatrixCSR container types
garth-wells Apr 26, 2023
55fb1bc
Doc update
garth-wells Apr 26, 2023
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
25 changes: 12 additions & 13 deletions cpp/demo/poisson_matrix_free/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ namespace linalg
/// @param[in] alpha
/// @param[in] x
/// @param[in] y
template <typename U>
void axpy(la::Vector<U>& r, U alpha, const la::Vector<U>& x,
const la::Vector<U>& y)
void axpy(auto& r, auto alpha, const auto& x, const auto& y)
{
std::transform(x.array().begin(), x.array().end(), y.array().begin(),
r.mutable_array().begin(),
Expand All @@ -61,19 +59,21 @@ void axpy(la::Vector<U>& r, U alpha, const la::Vector<U>& x,
/// @return The number if iterations
/// @pre It is required that the ghost values of `x` and `b` have been
/// updated before this function is called
template <typename U, typename ApplyFunction>
int cg(la::Vector<U>& x, const la::Vector<U>& b, ApplyFunction&& action,
int kmax = 50, double rtol = 1e-8)
template <typename ApplyFunction>
int cg(auto& x, auto& b, ApplyFunction&& action, int kmax = 50,
double rtol = 1e-8)
{
using T = typename std::decay_t<decltype(x)>::value_type;

// Create working vectors
la::Vector<U> r(b), y(b);
la::Vector r(b), y(b);

// Compute initial residual r0 = b - Ax0
action(x, y);
axpy(r, U(-1), y, b);
axpy(r, T(-1), y, b);

// Create p work vector
la::Vector<U> p(r);
la::Vector p(r);

// Iterations of CG
auto rnorm0 = la::squared_norm(r);
Expand All @@ -88,7 +88,7 @@ int cg(la::Vector<U>& x, const la::Vector<U>& b, ApplyFunction&& action,
action(p, y);

// Compute alpha = r.r/p.y
const U alpha = rnorm / la::inner_product(p, y);
const T alpha = rnorm / la::inner_product(p, y);

// Update x (x <- x + alpha*p)
axpy(x, alpha, p, x);
Expand All @@ -98,7 +98,7 @@ int cg(la::Vector<U>& x, const la::Vector<U>& b, ApplyFunction&& action,

// Update residual norm
const auto rnorm_new = la::squared_norm(r);
const U beta = rnorm_new / rnorm;
const T beta = rnorm_new / rnorm;
rnorm = rnorm_new;

if (rnorm / rnorm0 < rtol2)
Expand Down Expand Up @@ -182,8 +182,7 @@ int main(int argc, char* argv[])
const std::vector<T> constants = fem::pack_constants(*M);

// Create function for computing the action of A on x (y = Ax)
std::function<void(la::Vector<T>&, la::Vector<T>&)> action
= [&M, &ui, &bc, &coeff, &constants](la::Vector<T>& x, la::Vector<T>& y)
auto action = [&M, &ui, &bc, &coeff, &constants](auto& x, auto& y)
{
// Zero y
y.set(0.0);
Expand Down
13 changes: 7 additions & 6 deletions cpp/dolfinx/fem/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,13 @@ void pack(std::span<T> coeffs, std::int32_t cell, int bs, std::span<const T> v,
/// @private
/// @brief Concepts for function that returns cell index
template <typename F>
concept FetchCells = requires(F&& f, std::span<const std::int32_t> v) {
requires std::invocable<F, std::span<const std::int32_t>>;
{
f(v)
} -> std::convertible_to<std::int32_t>;
};
concept FetchCells
= requires(F&& f, std::span<const std::int32_t> v) {
requires std::invocable<F, std::span<const std::int32_t>>;
{
f(v)
} -> std::convertible_to<std::int32_t>;
};

/// @brief Pack a single coefficient for a set of active entities.
///
Expand Down
8 changes: 4 additions & 4 deletions cpp/dolfinx/graph/AdjacencyList.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ AdjacencyList(T, U) -> AdjacencyList<typename T::value_type>;
/// @return An adjacency list
template <typename U>
requires requires {
typename std::decay_t<U>::value_type;
requires std::convertible_to<
U, std::vector<typename std::decay_t<U>::value_type>>;
}
typename std::decay_t<U>::value_type;
requires std::convertible_to<
U, std::vector<typename std::decay_t<U>::value_type>>;
}
AdjacencyList<typename std::decay_t<U>::value_type>
regular_adjacency_list(U&& data, int degree)
{
Expand Down
Loading