DOLFIN-X
DOLFIN-X C++ interface
utils.h
1 // Copyright (C) 2009-2010 Anders Logg
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 <cstring>
12 #include <dolfinx/common/MPI.h>
13 #include <limits>
14 #include <mpi.h>
15 #include <sstream>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace dolfinx::common
21 {
22 
29 template <typename U, typename V>
30 std::pair<U, V> sort_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  std::vector<std::pair<typename U::value_type, typename V::value_type>> data(
36  indices.size());
37  for (std::size_t i = 0; i < indices.size(); ++i)
38  data[i] = {indices[i], values[i]};
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  U indices_new;
46  V values_new;
47  indices_new.reserve(data.size());
48  values_new.reserve(data.size());
49  for (auto d = data.begin(); d != it; ++d)
50  {
51  indices_new.push_back(d->first);
52  values_new.push_back(d->second);
53  }
54 
55  return {std::move(indices_new), std::move(values_new)};
56 }
57 
59 std::string indent(std::string block);
60 
62 template <typename T>
63 std::string container_to_string(const T& x, const int precision,
64  const int linebreak)
65 {
66  std::stringstream s;
67  s.precision(precision);
68 
69  for (std::size_t i = 0; i < (std::size_t)x.size(); ++i)
70  {
71  if ((i + 1) % linebreak == 0 && linebreak != 0)
72  s << x.data()[i] << std::endl;
73  else
74  s << x.data()[i] << " ";
75  }
76  return s.str();
77 }
78 
80 template <class T>
81 std::size_t hash_local(const T& x)
82 {
83  boost::hash<T> hash;
84  return hash(x);
85 }
86 
90 template <class T>
91 std::int64_t hash_global(const MPI_Comm comm, const T& x)
92 {
93  // Compute local hash
94  int64_t local_hash = hash_local(x);
95 
96  // Gather hash keys on root process
97  std::vector<int64_t> all_hashes(dolfinx::MPI::size(comm));
98  MPI_Gather(&local_hash, 1, MPI_INT64_T, all_hashes.data(), 1, MPI_INT64_T, 0,
99  comm);
100 
101  // Hash the received hash keys
102  boost::hash<std::vector<int64_t>> hash;
103  int64_t global_hash = hash(all_hashes);
104 
105  // Broadcast hash key to all processes
106  MPI_Bcast(&global_hash, 1, MPI_INT64_T, 0, comm);
107 
108  return global_hash;
109 }
110 } // namespace dolfinx::common
static int size(MPI_Comm comm)
Return size of the group (number of processes) associated with the communicator.
Definition: MPI.cpp:87
Miscellaneous classes, functions and types.
std::string container_to_string(const T &x, const int precision, const int linebreak)
Convert a container to string.
Definition: utils.h:63
std::string indent(std::string block)
Indent string block.
Definition: utils.cpp:12
std::size_t hash_local(const T &x)
Return a hash of a given object.
Definition: utils.h:81
std::pair< U, V > 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:30
std::int64_t hash_global(const MPI_Comm comm, const T &x)
Return a hash for a distributed (MPI) object. A hash is computed on each process, and the hash of the...
Definition: utils.h:91