DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
SparsityPattern.h
1// Copyright (C) 2007-2026 Garth N. Wells
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include <cstddef>
10#include <cstdint>
11#include <dolfinx/common/MPI.h>
12#include <memory>
13#include <span>
14#include <utility>
15#include <vector>
16
17namespace dolfinx::common
18{
19class IndexMap;
20}
21
22namespace dolfinx::la
23{
28{
29public:
35 SparsityPattern(MPI_Comm comm,
36 std::array<std::shared_ptr<const common::IndexMap>, 2> maps,
37 std::array<int, 2> bs);
38
51 MPI_Comm comm,
52 const std::vector<std::vector<const SparsityPattern*>>& patterns,
53 const std::array<
54 std::vector<
55 std::pair<std::reference_wrapper<const common::IndexMap>, int>>,
56 2>& maps,
57 const std::array<std::vector<int>, 2>& bs);
58
59 SparsityPattern(const SparsityPattern& pattern) = delete;
60
62 SparsityPattern(SparsityPattern&& pattern) = default;
63
65 ~SparsityPattern() = default;
66
69
83 void reserve_blocks(std::size_t num_blocks, std::size_t num_rows,
84 std::size_t num_cols);
85
90 void insert(std::int32_t row, std::int32_t col);
91
104 void insert(std::span<const std::int32_t> rows,
105 std::span<const std::int32_t> cols);
106
110 void insert_diagonal(std::span<const std::int32_t> rows);
111
114 void finalize();
115
122 std::shared_ptr<const common::IndexMap> index_map(int dim) const;
123
136 std::vector<std::int64_t> column_indices() const;
137
139 int block_size(int dim) const;
140
143 std::int64_t num_nonzeros() const;
144
148 std::int32_t nnz_diag(std::int32_t row) const;
149
153 std::int32_t nnz_off_diag(std::int32_t row) const;
154
161 std::pair<std::span<const std::int32_t>, std::span<const std::int64_t>>
162 graph() const;
163
167 std::span<const std::int32_t> off_diagonal_offsets() const;
168
170 MPI_Comm comm() const;
171
172private:
173 // MPI communicator
174 dolfinx::MPI::Comm _comm;
175
176 // Index maps for each dimension
177 std::array<std::shared_ptr<const common::IndexMap>, 2> _index_maps;
178
179 // Block size
180 std::array<int, 2> _bs;
181
182 // Non-zero ghost columns in owned rows
183 std::vector<std::int64_t> _col_ghosts;
184
185 // Owning process of ghost columns in owned rows
186 std::vector<std::int32_t> _col_ghost_owners;
187
188 // Cache of unassembled entries on owned and unowned (ghost) rows,
189 // held until finalize().
190 //
191 // insert(rows, cols) inserts the outer product of `rows` and `cols`,
192 // so storing one (row, column) pair per entry repeats each index
193 // rows.size() or cols.size() times over. Blocks are instead kept in
194 // the form they arrive in and expanded only in finalize(): for a P1
195 // tetrahedron that is 4 + 4 indices per cell rather than 16 + 16.
196 std::vector<std::int32_t> _cache_brows, _cache_bcols;
197 std::vector<std::int64_t> _cache_boffs_r{0}, _cache_boffs_c{0};
198
199 // Cache of square blocks, i.e. those whose row and column index lists
200 // are the same span, which is the case whenever the test and trial
201 // dofmaps and the cells indexing them coincide. Only the row list is
202 // stored, halving both the copy in insert() and the cached bytes.
203 //
204 // _cache_sbs is the width shared by every cached square block, 0 if
205 // none are cached and -1 if they differ. Cell integrals give blocks
206 // of a single width, which are indexed by stride, so _cache_soffs
207 // stays empty until a block of a different width arrives.
208 std::vector<std::int32_t> _cache_srows;
209 std::int32_t _cache_sbs = 0;
210 std::vector<std::int64_t> _cache_soffs;
211
212 // Cache of individually inserted (row, column) pairs (row-major COO)
213 std::vector<std::int32_t> _cache_rows;
214 std::vector<std::int32_t> _cache_cols;
215
216 // Rows with a cached diagonal entry, from insert_diagonal
217 std::vector<std::int32_t> _cache_diag;
218
219 // Pending reserve_blocks request, as {number of blocks, total row
220 // indices, total column indices}. Which of the two block caches the
221 // blocks land in is only known once insert() sees the spans, and
222 // reserving both maps around 2.5x what is used, so the request is
223 // held here until then.
224 std::array<std::size_t, 3> _reserve{0, 0, 0};
225
228 void expand_square_offsets();
229
235 std::pair<std::vector<std::int64_t>, std::vector<std::int32_t>>
236 bucket_cache(std::int32_t num_rows, std::int32_t num_cols) const;
237
238 // Sparsity pattern adjacency data (computed once pattern is
239 // finalised). _edges holds the edges (connected dofs). The edges for
240 // node i are in the range [_offsets[i], _offsets[i + 1]).
241 std::vector<std::int32_t> _edges;
242 std::vector<std::int64_t> _offsets;
243
244 // Start of off-diagonal (unowned columns) on each row (row-wise)
245 std::vector<std::int32_t> _off_diagonal_offsets;
246};
247} // namespace dolfinx::la
A duplicate MPI communicator and manage lifetime of the communicator.
Definition MPI.h:47
Distribution of a global index range [0, N) across MPI ranks.
Definition IndexMap.h:114
SparsityPattern(SparsityPattern &&pattern)=default
Move constructor.
void reserve_blocks(std::size_t num_blocks, std::size_t num_rows, std::size_t num_cols)
Reserve storage for additional insert(rows, cols) calls.
Definition SparsityPattern.cpp:368
std::shared_ptr< const common::IndexMap > index_map(int dim) const
Index map for given dimension. Returns the index map for rows and columns that will be set by the cur...
Definition SparsityPattern.cpp:484
std::int32_t nnz_off_diag(std::int32_t row) const
Number of non-zeros in unowned columns (off-diagonal block) on a given row.
Definition SparsityPattern.cpp:835
void finalize()
Finalize sparsity pattern and communicate off-process entries.
Definition SparsityPattern.cpp:505
int block_size(int dim) const
Return index map block size for dimension dim.
Definition SparsityPattern.cpp:503
std::int32_t nnz_diag(std::int32_t row) const
Number of non-zeros in owned columns (diagonal block) on a given row.
Definition SparsityPattern.cpp:828
void insert(std::int32_t row, std::int32_t col)
Insert non-zero locations using local (process-wise) indices.
Definition SparsityPattern.cpp:396
void insert_diagonal(std::span< const std::int32_t > rows)
Insert non-zero locations on the diagonal.
Definition SparsityPattern.cpp:471
std::int64_t num_nonzeros() const
Number of nonzeros on this rank after assembly, including ghost rows.
Definition SparsityPattern.cpp:821
std::span< const std::int32_t > off_diagonal_offsets() const
Row-wise start of off-diagonals (unowned columns) for each row.
Definition SparsityPattern.cpp:850
std::vector< std::int64_t > column_indices() const
Global column indices corresponding to the local column indices used by SparsityPattern::graph.
Definition SparsityPattern.cpp:489
SparsityPattern(MPI_Comm comm, std::array< std::shared_ptr< const common::IndexMap >, 2 > maps, std::array< int, 2 > bs)
Create an empty sparsity pattern with specified dimensions.
Definition SparsityPattern.cpp:232
MPI_Comm comm() const
Return MPI communicator.
Definition SparsityPattern.cpp:857
SparsityPattern & operator=(SparsityPattern &&pattern)=default
Move assignment.
~SparsityPattern()=default
Destructor.
std::pair< std::span< const std::int32_t >, std::span< const std::int64_t > > graph() const
Sparsity pattern graph after assembly. Uses local indices for the columns.
Definition SparsityPattern.cpp:843
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
Linear algebra interface.
Definition dolfinx_la.h:7