DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
IndexMap.h
1// Copyright (C) 2015-2026 Chris Richardson, Garth N. Wells, Igor Baratta
2// and Jørgen S. Dokken
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "MPI.h"
11#include <array>
12#include <cstdint>
13#include <functional>
14#include <memory>
15#include <span>
16#include <tuple>
17#include <utility>
18#include <vector>
19
20namespace dolfinx::common
21{
22// Forward declaration
23class IndexMap;
24
26enum class IndexMapOrder : bool
27{
28 preserve = true,
29 any = false
30};
31
55std::vector<std::int32_t>
56compute_owned_indices(std::span<const std::int32_t> indices,
57 const IndexMap& map);
58
72std::tuple<std::int64_t, std::vector<std::int32_t>,
73 std::vector<std::vector<std::int64_t>>,
74 std::vector<std::vector<int>>>
76 const std::vector<std::pair<std::reference_wrapper<const IndexMap>, int>>&
77 maps);
78
99std::vector<std::int32_t>
100num_sharing_ranks(const IndexMap& map, std::span<const std::int32_t> indices,
101 int bs);
102
127std::tuple<IndexMap, std::vector<std::int32_t>, bool>
128create_sub_index_map(const IndexMap& imap,
129 std::span<const std::int32_t> indices,
131
181std::tuple<std::vector<int>, std::vector<int>, std::vector<std::int32_t>>
182compute_sharing_neighbourhood(const IndexMap& imap);
183
191{
192public:
213 IndexMap(MPI_Comm comm, std::int32_t local_size);
214
254 IndexMap(MPI_Comm comm, std::int32_t local_size,
255 std::span<const std::int64_t> ghosts, std::span<const int> owners,
256 int tag = static_cast<int>(dolfinx::MPI::tag::consensus_nbx));
257
299 IndexMap(MPI_Comm comm, std::int32_t local_size,
300 const std::array<std::vector<int>, 2>& src_dest,
301 std::span<const std::int64_t> ghosts, std::span<const int> owners);
302
303 // Copy constructor (deleted)
304 IndexMap(const IndexMap& map) = delete;
305
307 IndexMap(IndexMap&& map) = default;
308
310 ~IndexMap() = default;
311
312 // Copy assignment (deleted)
313 IndexMap& operator=(const IndexMap& map) = delete;
314
316 IndexMap& operator=(IndexMap&& map) = default;
317
319 std::array<std::int64_t, 2> local_range() const noexcept;
320
322 std::int32_t num_ghosts() const noexcept;
323
325 std::int32_t size_local() const noexcept;
326
328 std::int64_t size_global() const noexcept;
329
331 std::span<const std::int64_t> ghosts() const noexcept;
332
335 MPI_Comm comm() const;
336
346 void local_to_global(std::span<const std::int32_t> local,
347 std::span<std::int64_t> global) const;
348
355 void global_to_local(std::span<const std::int64_t> global,
356 std::span<std::int32_t> local) const;
357
360 std::vector<std::int64_t> global_indices() const;
361
364 std::span<const int> owners() const noexcept { return _owners; }
365
376 std::pair<std::vector<int>, std::vector<std::int32_t>>
377 index_to_dest_ranks() const;
378
384 std::vector<std::int32_t> shared_indices() const;
385
387 std::span<const int> src() const noexcept;
388
391 std::span<const int> dest() const noexcept;
392
399 std::vector<std::int32_t> weights_src() const;
400
410 std::vector<std::int32_t> weights_dest() const;
411
428 std::array<std::vector<int>, 2> rank_type(int split_type) const;
429
430private:
431 // Global range of owned indices
432 std::array<std::int64_t, 2> _local_range;
433
434 // Global number of indices
435 std::int64_t _size_global;
436
437 // Map communicator
438 dolfinx::MPI::Comm _comm;
439
440 // Global ghost indices
441 std::vector<std::int64_t> _ghosts;
442
443 // Owner ranks for ghosts
444 std::vector<int> _owners;
445
446 // Ranks that own ghosts
447 std::vector<int> _src;
448
449 // Ranks that ghost owned entries
450 std::vector<int> _dest;
451};
452
453} // namespace dolfinx::common
Distribution of a global index range [0, N) across MPI ranks.
Definition IndexMap.h:191
std::vector< std::int32_t > weights_dest() const
Count the caller's owned entries ghosted by each destination rank.
Definition IndexMap.cpp:1398
~IndexMap()=default
Destructor.
std::int32_t num_ghosts() const noexcept
Return the number of ghost indices.
Definition IndexMap.cpp:1076
std::span< const int > dest() const noexcept
Return sorted unique ranks that ghost entries owned by the caller.
Definition IndexMap.cpp:1381
std::vector< std::int32_t > weights_src() const
Count the caller's ghosts owned by each source rank.
Definition IndexMap.cpp:1383
std::array< std::int64_t, 2 > local_range() const noexcept
Return the global range of owned indices.
Definition IndexMap.cpp:1071
std::span< const int > owners() const noexcept
Return ranks that own ghost entries.
Definition IndexMap.h:364
std::int32_t size_local() const noexcept
Return the number of owned indices.
Definition IndexMap.cpp:1081
std::int64_t size_global() const noexcept
Return the total number of indices across the communicator.
Definition IndexMap.cpp:1086
IndexMap(IndexMap &&map)=default
Move constructor.
IndexMap & operator=(IndexMap &&map)=default
Move assignment.
std::span< const int > src() const noexcept
Return sorted unique ranks that own the caller's ghosts.
Definition IndexMap.cpp:1379
void local_to_global(std::span< const std::int32_t > local, std::span< std::int64_t > global) const
Compute global indices for local indices.
Definition IndexMap.cpp:1093
void global_to_local(std::span< const std::int64_t > global, std::span< std::int32_t > local) const
Compute local indices for global indices.
Definition IndexMap.cpp:1121
IndexMap(MPI_Comm comm, std::int32_t local_size)
Create a non-overlapping index map.
Definition IndexMap.cpp:1010
std::span< const std::int64_t > ghosts() const noexcept
Return global indices of ghosts in local ghost-index order.
Definition IndexMap.cpp:1088
std::vector< std::int32_t > shared_indices() const
Return owned indices ghosted by another rank.
Definition IndexMap.cpp:1305
MPI_Comm comm() const
Return the communicator that the map is defined on.
Definition IndexMap.cpp:1170
std::pair< std::vector< int >, std::vector< std::int32_t > > index_to_dest_ranks() const
Compute sharing ranks for each local index.
Definition IndexMap.cpp:1173
std::vector< std::int64_t > global_indices() const
Return global indices for all local entries, including ghosts.
Definition IndexMap.cpp:1158
std::array< std::vector< int >, 2 > rank_type(int split_type) const
Return neighbours in the caller's MPI split-type group.
Definition IndexMap.cpp:1422
MPI support functionality.
Definition MPI.h:36
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
std::vector< std::int32_t > compute_owned_indices(std::span< const std::int32_t > indices, const IndexMap &map)
Return selected indices owned by the calling rank.
Definition IndexMap.cpp:718
IndexMapOrder
Control ghost-index ordering in sub-index maps.
Definition IndexMap.h:27
@ any
Allow arbitrary ghost-index ordering.
Definition IndexMap.h:29
@ preserve
Preserve the ordering of ghost indices.
Definition IndexMap.h:28
std::vector< std::int32_t > num_sharing_ranks(const IndexMap &map, std::span< const std::int32_t > indices, int bs)
Number of ranks that share each of the given local indices.
Definition IndexMap.cpp:933
std::tuple< std::vector< int >, std::vector< int >, std::vector< std::int32_t > > compute_sharing_neighbourhood(const IndexMap &imap)
Compute the symmetric neighbourhood of ranks that share an owned or ghost index with the caller.
Definition IndexMap.cpp:984
std::tuple< IndexMap, std::vector< std::int32_t >, bool > create_sub_index_map(const IndexMap &imap, std::span< const std::int32_t > indices, IndexMapOrder order=IndexMapOrder::any)
Create an index map from a subset of an existing map.
Definition IndexMap.cpp:941
std::tuple< std::int64_t, std::vector< std::int32_t >, std::vector< std::vector< std::int64_t > >, std::vector< std::vector< int > > > stack_index_maps(const std::vector< std::pair< std::reference_wrapper< const IndexMap >, int > > &maps)
Compute layout data for a concatenated index map.
Definition IndexMap.cpp:767
Top-level namespace.
Definition defines.h:12