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
123 std::shared_ptr<const common::IndexMap> index_map(int dim) const;
124
133 std::shared_ptr<const common::IndexMap> input_index_map(int dim) const;
134
147 std::vector<std::int64_t> column_indices() const;
148
150 int block_size(int dim) const;
151
154 std::int64_t num_nonzeros() const;
155
159 std::int32_t nnz_diag(std::int32_t row) const;
160
164 std::int32_t nnz_off_diag(std::int32_t row) const;
165
172 std::pair<std::span<const std::int32_t>, std::span<const std::int64_t>>
173 graph() const;
174
178 std::span<const std::int32_t> off_diagonal_offsets() const;
179
181 MPI_Comm comm() const;
182
183private:
184 // MPI communicator
185 dolfinx::MPI::Comm _comm;
186
187 // Input index maps for each dimension
188 std::array<std::shared_ptr<const common::IndexMap>, 2> _index_maps;
189
190 // Column map spanning the finalised pattern's local column indices.
191 // Aliases _index_maps[1] unless finalize() added ghosts.
192 std::shared_ptr<const common::IndexMap> _col_index_map;
193
194 // Block size
195 std::array<int, 2> _bs;
196
197 // Non-zero ghost columns in owned rows
198 std::vector<std::int64_t> _col_ghosts;
199
200 // Owning process of ghost columns in owned rows
201 std::vector<std::int32_t> _col_ghost_owners;
202
203 // Cache of unassembled entries on owned and unowned (ghost) rows,
204 // held until finalize().
205 //
206 // insert(rows, cols) inserts the outer product of `rows` and `cols`,
207 // so storing one (row, column) pair per entry repeats each index
208 // rows.size() or cols.size() times over. Blocks are instead kept in
209 // the form they arrive in and expanded only in finalize(): for a P1
210 // tetrahedron that is 4 + 4 indices per cell rather than 16 + 16.
211 std::vector<std::int32_t> _cache_brows, _cache_bcols;
212 std::vector<std::int64_t> _cache_boffs_r{0}, _cache_boffs_c{0};
213
214 // Cache of square blocks, i.e. those whose row and column index lists
215 // are the same span, which is the case whenever the test and trial
216 // dofmaps and the cells indexing them coincide. Only the row list is
217 // stored, halving both the copy in insert() and the cached bytes.
218 //
219 // _cache_sbs is the width shared by every cached square block, 0 if
220 // none are cached and -1 if they differ. Cell integrals give blocks
221 // of a single width, which are indexed by stride, so _cache_soffs
222 // stays empty until a block of a different width arrives.
223 std::vector<std::int32_t> _cache_srows;
224 std::int32_t _cache_sbs = 0;
225 std::vector<std::int64_t> _cache_soffs;
226
227 // Cache of individually inserted (row, column) pairs (row-major COO)
228 std::vector<std::int32_t> _cache_rows;
229 std::vector<std::int32_t> _cache_cols;
230
231 // Rows with a cached diagonal entry, from insert_diagonal
232 std::vector<std::int32_t> _cache_diag;
233
234 // Pending reserve_blocks request, as {number of blocks, total row
235 // indices, total column indices}. Which of the two block caches the
236 // blocks land in is only known once insert() sees the spans, and
237 // reserving both maps around 2.5x what is used, so the request is
238 // held here until then.
239 std::array<std::size_t, 3> _reserve{0, 0, 0};
240
243 void expand_square_offsets();
244
250 std::pair<std::vector<std::int64_t>, std::vector<std::int32_t>>
251 bucket_cache(std::int32_t num_rows, std::int32_t num_cols) const;
252
253 // Sparsity pattern adjacency data (computed once pattern is
254 // finalised). _edges holds the edges (connected dofs). The edges for
255 // node i are in the range [_offsets[i], _offsets[i + 1]).
256 std::vector<std::int32_t> _edges;
257 std::vector<std::int64_t> _offsets;
258
259 // Start of off-diagonal (unowned columns) on each row (row-wise)
260 std::vector<std::int32_t> _off_diagonal_offsets;
261};
262} // 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:191
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:370
std::shared_ptr< const common::IndexMap > index_map(int dim) const
Index map for the rows and columns that will be set by the current MPI rank.
Definition SparsityPattern.cpp:486
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:844
void finalize()
Finalize sparsity pattern and communicate off-process entries.
Definition SparsityPattern.cpp:513
int block_size(int dim) const
Return index map block size for dimension dim.
Definition SparsityPattern.cpp:511
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:837
void insert(std::int32_t row, std::int32_t col)
Insert non-zero locations using local (process-wise) indices.
Definition SparsityPattern.cpp:398
void insert_diagonal(std::span< const std::int32_t > rows)
Insert non-zero locations on the diagonal.
Definition SparsityPattern.cpp:473
std::int64_t num_nonzeros() const
Number of nonzeros on this rank after assembly, including ghost rows.
Definition SparsityPattern.cpp:830
std::span< const std::int32_t > off_diagonal_offsets() const
Row-wise start of off-diagonals (unowned columns) for each row.
Definition SparsityPattern.cpp:859
std::vector< std::int64_t > column_indices() const
Global column indices corresponding to the local column indices used by SparsityPattern::graph.
Definition SparsityPattern.cpp:497
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:866
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:852
std::shared_ptr< const common::IndexMap > input_index_map(int dim) const
Input index map for the rows and columns used to construct the pattern.
Definition SparsityPattern.cpp:492
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
Linear algebra interface.
Definition dolfinx_la.h:7