Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/dolfinx/v0.9.0/cpp/doxygen/dd/d09/common_2utils_8h_source.html
DOLFINx 0.7.3
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 <algorithm>
10#include <boost/functional/hash.hpp>
11#include <dolfinx/common/MPI.h>
12#include <mpi.h>
13#include <utility>
14#include <vector>
15
17namespace dolfinx::common
18{
19
26template <typename U, typename V>
27std::pair<std::vector<typename U::value_type>,
28 std::vector<typename V::value_type>>
29sort_unique(const U& indices, const V& values)
30{
31 if (indices.size() != values.size())
32 throw std::runtime_error("Cannot sort two arrays of different lengths");
33
34 using T = typename std::pair<typename U::value_type, typename V::value_type>;
35 std::vector<T> data(indices.size());
36 std::transform(indices.begin(), indices.end(), values.begin(), data.begin(),
37 [](auto& idx, auto& v) -> T {
38 return {idx, v};
39 });
40
41 // Sort make unique
42 std::sort(data.begin(), data.end());
43 auto it = std::unique(data.begin(), data.end(),
44 [](auto& a, auto& b) { return a.first == b.first; });
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,
93 dolfinx::MPI::mpi_type<std::size_t>(), 0, comm);
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)
Return size of the group (number of processes) associated with the communicator.
Definition MPI.cpp:72
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. Any duplicate indices and the corresponding val...
Definition utils.h:29
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