DOLFINx
0.11.0.0
DOLFINx C++
Toggle main menu visibility
Loading...
Searching...
No Matches
dolfinx
common
utils.h
1
// Copyright (C) 2009-2025 Anders Logg and 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 "MPI.h"
10
#include <algorithm>
11
#include <boost/functional/hash.hpp>
12
#include <dolfinx/graph/AdjacencyList.h>
13
#include <mpi.h>
14
#include <string>
15
#include <tuple>
16
#include <utility>
17
#include <vector>
18
20
namespace
dolfinx::common
21
{
31
template
<
typename
U,
typename
V>
32
std::pair<std::vector<typename U::value_type>,
33
std::vector<typename V::value_type>>
34
sort_unique
(
const
U& indices,
const
V& values)
35
{
36
if
(indices.size() != values.size())
37
throw
std::runtime_error(
"Cannot sort two arrays of different lengths"
);
38
39
using
T =
typename
std::pair<typename U::value_type, typename V::value_type>;
40
std::vector<T> data(indices.size());
41
std::ranges::transform(indices, values, data.begin(),
42
[](
auto
& idx,
auto
& v) -> T { return {idx, v}; });
43
44
// Sort make unique
45
std::ranges::sort(data);
46
auto
it = std::ranges::unique(data, [](
auto
& a,
auto
& b)
47
{
return
a.first == b.first; })
48
.begin();
49
50
std::vector<typename U::value_type> indices_new;
51
std::vector<typename V::value_type> values_new;
52
indices_new.reserve(data.size());
53
values_new.reserve(data.size());
54
std::transform(data.begin(), it, std::back_inserter(indices_new),
55
[](
auto
& d) { return d.first; });
56
std::transform(data.begin(), it, std::back_inserter(values_new),
57
[](
auto
& d) { return d.second; });
58
59
return
{std::move(indices_new), std::move(values_new)};
60
}
61
69
template
<
class
T>
70
std::size_t
hash_local
(
const
T& x)
71
{
72
boost::hash<T> hash;
73
return
hash(x);
74
}
75
87
template
<
class
T>
88
std::size_t
hash_global
(MPI_Comm comm,
const
T& x)
89
{
90
// Compute local hash
91
std::size_t local_hash =
hash_local
(x);
92
93
// Gather hash keys on root process
94
std::vector<std::size_t> all_hashes(
dolfinx::MPI::size
(comm));
95
int
err = MPI_Gather(&local_hash, 1,
dolfinx::MPI::mpi_t<std::size_t>
,
96
all_hashes.data(), 1,
dolfinx::MPI::mpi_t<std::size_t>
,
97
0, comm);
98
dolfinx::MPI::check_error
(comm, err);
99
100
// Hash the received hash keys
101
boost::hash<std::vector<std::size_t>> hash;
102
std::size_t global_hash = hash(all_hashes);
103
104
// Broadcast hash key to all processes
105
err = MPI_Bcast(&global_hash, 1,
dolfinx::MPI::mpi_t<std::size_t>
, 0, comm);
106
dolfinx::MPI::check_error
(comm, err);
107
108
return
global_hash;
109
}
110
124
std::string
comm_to_json
(
125
const
graph::AdjacencyList
<std::tuple<int, std::size_t, std::int8_t>,
126
std::pair<std::int32_t, std::int32_t>>& g);
127
}
// namespace dolfinx::common
dolfinx::graph::AdjacencyList
This class provides a static adjacency list data structure.
Definition
AdjacencyList.h:38
dolfinx::MPI::mpi_t
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition
MPI.h:280
dolfinx::MPI::check_error
void check_error(MPI_Comm comm, int code)
Check MPI error code. If the error code is not equal to MPI_SUCCESS, then std::abort is called.
Definition
MPI.cpp:80
dolfinx::MPI::size
int size(MPI_Comm comm)
Definition
MPI.cpp:72
dolfinx::common
Miscellaneous classes, functions and types.
Definition
dolfinx_common.h:8
dolfinx::common::sort_unique
std::pair< std::vector< typename U::value_type >, std::vector< typename V::value_type > > sort_unique(const U &indices, const V &values)
Sort two arrays based on the values in array indices.
Definition
utils.h:34
dolfinx::common::hash_global
std::size_t hash_global(MPI_Comm comm, const T &x)
Compute a hash for a distributed (MPI) object.
Definition
utils.h:88
dolfinx::common::hash_local
std::size_t hash_local(const T &x)
Compute a hash of a given object.
Definition
utils.h:70
dolfinx::common::comm_to_json
std::string comm_to_json(const graph::AdjacencyList< std::tuple< int, std::size_t, std::int8_t >, std::pair< std::int32_t, std::int32_t > > &g)
Build communication graph data as a JSON string.
Generated by
1.17.0