DOLFINx 0.9.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
utils.h
1// Copyright (C) 2009-2022 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 <mpi.h>
13#include <utility>
14#include <vector>
15
17namespace dolfinx::common
18{
27template <typename U, typename V>
28std::pair<std::vector<typename U::value_type>,
29 std::vector<typename V::value_type>>
30sort_unique(const U& indices, const V& values)
31{
32 if (indices.size() != values.size())
33 throw std::runtime_error("Cannot sort two arrays of different lengths");
34
35 using T = typename std::pair<typename U::value_type, typename V::value_type>;
36 std::vector<T> data(indices.size());
37 std::ranges::transform(indices, values, data.begin(),
38 [](auto& idx, auto& v) -> T { return {idx, v}; });
39
40 // Sort make unique
41 std::ranges::sort(data);
42 auto it = std::ranges::unique(data, [](auto& a, auto& b)
43 { return a.first == b.first; })
44 .begin();
45
46 std::vector<typename U::value_type> indices_new;
47 std::vector<typename V::value_type> values_new;
48 indices_new.reserve(data.size());
49 values_new.reserve(data.size());
50 std::transform(data.begin(), it, std::back_inserter(indices_new),
51 [](auto& d) { return d.first; });
52 std::transform(data.begin(), it, std::back_inserter(values_new),
53 [](auto& d) { return d.second; });
54
55 return {std::move(indices_new), std::move(values_new)};
56}
57
65template <class T>
66std::size_t hash_local(const T& x)
67{
68 boost::hash<T> hash;
69 return hash(x);
70}
71
83template <class T>
84std::size_t hash_global(MPI_Comm comm, const T& x)
85{
86 // Compute local hash
87 std::size_t local_hash = hash_local(x);
88
89 // Gather hash keys on root process
90 std::vector<std::size_t> all_hashes(dolfinx::MPI::size(comm));
91 int err = MPI_Gather(&local_hash, 1, dolfinx::MPI::mpi_type<std::size_t>(),
92 all_hashes.data(), 1,
95
96 // Hash the received hash keys
97 boost::hash<std::vector<std::size_t>> hash;
98 std::size_t global_hash = hash(all_hashes);
99
100 // Broadcast hash key to all processes
101 err = MPI_Bcast(&global_hash, 1, dolfinx::MPI::mpi_type<std::size_t>(), 0,
102 comm);
103 dolfinx::MPI::check_error(comm, err);
104
105 return global_hash;
106}
107
108} // namespace dolfinx::common
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
int size(MPI_Comm comm)
Definition MPI.cpp:72
constexpr MPI_Datatype mpi_type()
MPI Type.
Definition MPI.h:273
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
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:30
std::size_t hash_global(MPI_Comm comm, const T &x)
Compute a hash for a distributed (MPI) object.
Definition utils.h:84
std::size_t hash_local(const T &x)
Compute a hash of a given object.
Definition utils.h:66