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