parallel_histogram.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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()
30 
31 // Local includes
32 #include <vector>
33 #include <iterator>
34 
35 namespace libMesh
36 {
37 
38 namespace Parallel {
39 
44 template <typename KeyType, typename IdxType=unsigned int>
45 class Histogram : public ParallelObject
46 {
47  // The type of iterator we will be using is inferred from KeyType
48  typedef typename std::vector<KeyType>::const_iterator IterType;
49 
50 public:
51 
52  // Constructor
53  explicit
55  const std::vector<KeyType>& d);
56 
57  // The actual function which sorts the data into
58  // nbins. Currently based on the global min and
59  // max which you must provide e.g. by using MPI.
60  void make_histogram (const IdxType nbins,
61  KeyType max,
62  KeyType min);
63 
64  // Build the histogram across all processors and store the
65  // result in the input vector \p hist
66  void build_histogram ();
67 
68  // Return the raw histogram data to the user
69  const std::vector<IdxType>& get_histogram() const;
70 
71  // The number of bins in the histogram
72  IdxType n_bins () const;
73 
74  // Returns the size of local bin b
75  IdxType local_bin_size (const IdxType bin) const;
76 
77  // Returns the size of global bin b
78  // Requires that the user first call \p build_histogram()
79  IdxType global_bin_size (const IdxType bin) const;
80 
81  // Returns the lower boundary of bin \p bin
82  double lower_bound (const IdxType bin) const;
83 
84  // Returns the upper boundary of bin \p bin
85  double upper_bound (const IdxType bin) const;
86 
87 
88 private:
89 
90 
91  const std::vector<KeyType>& data;
92  std::vector<IdxType> hist; // The actual histogram
93  std::vector<double> bin_bounds; // The boundary values of each bin
94  std::vector<IterType> bin_iters; // Iterators to the bin boundaries
95  // in data
96 };
97 
98 
99 
100 
101 //--------------------------------------------------------------------------
102 template <typename KeyType, typename IdxType>
103 inline
104 const std::vector<IdxType>& Histogram<KeyType,IdxType>::get_histogram () const
105 {
106  return hist;
107 }
108 
109 
110 
111 template <typename KeyType, typename IdxType>
112 inline
114 {
115  if (bin_iters.empty())
116  return 0;
117 
118  return libmesh_cast_int<IdxType>(bin_iters.size()-1);
119 }
120 
121 
122 
123 template <typename KeyType, typename IdxType>
124 inline
125 IdxType Histogram<KeyType,IdxType>::local_bin_size (const IdxType bin) const
126 {
127  libmesh_assert_less ((bin+1), bin_iters.size());
128 
129  // The number of entries in the bin (locally)
130  return libmesh_cast_int<IdxType>
131  (std::distance (bin_iters[bin], bin_iters[bin+1]));
132 }
133 
134 
135 
136 template <typename KeyType, typename IdxType>
137 inline
138 IdxType Histogram<KeyType,IdxType>::global_bin_size (const IdxType bin) const
139 {
140  libmesh_assert_less (bin, hist.size());
141 
142  // The number of entries in the bin (globally)
143  return hist[bin];
144 }
145 
146 
147 
148 template <typename KeyType, typename IdxType>
149 inline
150 double Histogram<KeyType,IdxType>::lower_bound (const IdxType bin) const
151 {
152  libmesh_assert_less ((bin+1), bin_bounds.size());
153 
154  return bin_bounds[bin];
155 }
156 
157 
158 
159 template <typename KeyType, typename IdxType>
160 inline
161 double Histogram<KeyType,IdxType>::upper_bound (const IdxType bin) const
162 {
163  libmesh_assert_less ((bin+1), bin_bounds.size());
164 
165  return bin_bounds[bin+1];
166 }
167 
168 }
169 
170 } // namespace libMesh
171 
172 #endif // LIBMESH_PARALLEL_HISTOGRAM_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo