libMesh
parallel_histogram.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #ifndef LIBMESH_PARALLEL_HISTOGRAM_H
20 #define LIBMESH_PARALLEL_HISTOGRAM_H
21 
22 // This class contains all the functionality for bin sorting
23 // Templated on the type of keys you will be sorting and the
24 // type of iterator you will be using.
25 
26 
27 // C++ includes
28 #include "libmesh/libmesh_common.h" // for libmesh_assert()
29 #include "libmesh/parallel_object.h"
30 
31 // Local includes
32 #include <vector>
33 #include <iterator>
34 
35 namespace libMesh
36 {
37 
38 namespace Parallel
39 {
40 
50 template <typename KeyType, typename IdxType=unsigned int>
51 class Histogram : public ParallelObject
52 {
53  // The type of iterator we will be using is inferred from KeyType
54  typedef typename std::vector<KeyType>::const_iterator IterType;
55 
56 public:
57 
61  explicit
63  const std::vector<KeyType> & d);
64 
70  void make_histogram (const IdxType nbins,
71  KeyType max,
72  KeyType min);
73 
78  void build_histogram ();
79 
83  const std::vector<IdxType> & get_histogram() const;
84 
88  IdxType n_bins () const;
89 
93  IdxType local_bin_size (const IdxType bin) const;
94 
100  IdxType global_bin_size (const IdxType bin) const;
101 
105  double lower_bound (const IdxType bin) const;
106 
110  double upper_bound (const IdxType bin) const;
111 
112 
113 private:
114 
115  const std::vector<KeyType> & data;
116  std::vector<IdxType> hist; // The actual histogram
117  std::vector<double> bin_bounds; // The boundary values of each bin
118  std::vector<IterType> bin_iters; // Iterators to the bin boundaries in data
119 };
120 
121 
122 
123 template <typename KeyType, typename IdxType>
124 inline
125 const std::vector<IdxType> & Histogram<KeyType,IdxType>::get_histogram () const
126 {
127  return hist;
128 }
129 
130 
131 
132 template <typename KeyType, typename IdxType>
133 inline
135 {
136  if (bin_iters.empty())
137  return 0;
138 
139  return cast_int<IdxType>(bin_iters.size()-1);
140 }
141 
142 
143 
144 template <typename KeyType, typename IdxType>
145 inline
146 IdxType Histogram<KeyType,IdxType>::local_bin_size (const IdxType bin) const
147 {
148  libmesh_assert_less ((bin+1), bin_iters.size());
149 
150  // The number of entries in the bin (locally)
151  return cast_int<IdxType>
152  (std::distance (bin_iters[bin], bin_iters[bin+1]));
153 }
154 
155 
156 
157 template <typename KeyType, typename IdxType>
158 inline
159 IdxType Histogram<KeyType,IdxType>::global_bin_size (const IdxType bin) const
160 {
161  libmesh_assert_less (bin, hist.size());
162 
163  // The number of entries in the bin (globally)
164  return hist[bin];
165 }
166 
167 
168 
169 template <typename KeyType, typename IdxType>
170 inline
171 double Histogram<KeyType,IdxType>::lower_bound (const IdxType bin) const
172 {
173  libmesh_assert_less ((bin+1), bin_bounds.size());
174 
175  return bin_bounds[bin];
176 }
177 
178 
179 
180 template <typename KeyType, typename IdxType>
181 inline
182 double Histogram<KeyType,IdxType>::upper_bound (const IdxType bin) const
183 {
184  libmesh_assert_less ((bin+1), bin_bounds.size());
185 
186  return bin_bounds[bin+1];
187 }
188 
189 }
190 
191 } // namespace libMesh
192 
193 #endif // LIBMESH_PARALLEL_HISTOGRAM_H
IdxType n_bins() const
The number of bins in the histogram.
Encapsulates the MPI_Comm object.
Definition: parallel.h:657
double upper_bound(const IdxType bin) const
IdxType global_bin_size(const IdxType bin) const
double lower_bound(const IdxType bin) const
The libMesh namespace provides an interface to certain functionality in the library.
std::vector< IdxType > hist
std::vector< IterType > bin_iters
Real distance(const Point &p)
void build_histogram()
Build the histogram across all processors and store the result in the input vector hist...
const std::vector< KeyType > & data
Histogram(const Parallel::Communicator &comm, const std::vector< KeyType > &d)
Constructor.
void make_histogram(const IdxType nbins, KeyType max, KeyType min)
The actual function which sorts the data into nbins.
IdxType local_bin_size(const IdxType bin) const
This class forms the base class for all other classes that are expected to be implemented in parallel...
const std::vector< IdxType > & get_histogram() const
std::vector< KeyType >::const_iterator IterType
const Parallel::Communicator & comm() const
void max(T &r, const Communicator &comm=Communicator_World)
Defines a histogram to be used in parallel in conjunction with a BinSorter.
std::vector< double > bin_bounds
void min(T &r, const Communicator &comm=Communicator_World)