DOLFIN
DOLFIN C++ interface
utils.h
1 // Copyright (C) 2009-2010 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Joachim B. Haga, 2012.
19 // Modified by Garth N. Wells, 2013.
20 //
21 // First added: 2009-08-09
22 // Last changed: 2013-01-03
23 
24 #ifndef __DOLFIN_UTILS_H
25 #define __DOLFIN_UTILS_H
26 
27 #include <cstring>
28 #include <limits>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32 #include <boost/functional/hash.hpp>
33 #include <dolfin/common/MPI.h>
34 
35 namespace dolfin
36 {
37 
39  std::string indent(std::string block);
40 
43  template<typename T>
44  std::string container_to_string(const T& x, std::string delimiter,
45  int precision, int linebreak=0)
46  {
47  std::stringstream s;
48  s.precision(precision);
49  if (!x.empty())
50  {
51  if (linebreak == 0)
52  {
53  s << *x.begin();
54  for (auto it = x.begin() + 1; it != x.end(); ++it)
55  s << delimiter << *it;
56  }
57  else
58  {
59  for (unsigned int i = 0 ; i != x.size(); ++i)
60  {
61  if ((i + 1)%linebreak == 0)
62  s << x[i] << std::endl;
63  else
64  s << x[i] << delimiter;
65  }
66  }
67  }
68  return s.str();
69  }
70 
72  std::string to_string(const double* x, std::size_t n);
73 
75  template <class T>
76  std::size_t hash_local(const T& x)
77  {
78  boost::hash<T> hash;
79  return hash(x);
80  }
81 
85  template <class T>
86  std::size_t hash_global(const MPI_Comm mpi_comm, const T& x)
87  {
88  // Compute local hash
89  std::size_t local_hash = hash_local(x);
90 
91  // Gather hash keys on root process
92  std::vector<std::size_t> all_hashes;
93  std::vector<std::size_t> local_hash_tmp(1, local_hash);
94  MPI::gather(mpi_comm, local_hash_tmp, all_hashes);
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  MPI::broadcast(mpi_comm, global_hash);
102  return global_hash;
103  }
104 
105 }
106 
107 #endif
std::string to_string(const double *x, std::size_t n)
Return string representation of given array.
Definition: utils.cpp:42
std::size_t hash_local(const T &x)
Return a hash of a given object.
Definition: utils.h:76
std::string container_to_string(const T &x, std::string delimiter, int precision, int linebreak=0)
Definition: utils.h:44
Definition: adapt.h:29
std::string indent(std::string block)
Indent string block.
Definition: utils.cpp:26
static void broadcast(MPI_Comm comm, std::vector< T > &value, unsigned int broadcaster=0)
Broadcast vector of value from broadcaster to all processes.
Definition: MPI.h:320
std::size_t hash_global(const MPI_Comm mpi_comm, const T &x)
Definition: utils.h:86
static void gather(MPI_Comm comm, const std::vector< T > &in_values, std::vector< T > &out_values, unsigned int receiving_process=0)
Gather values on one process.
Definition: MPI.h:598