libMesh
Loading...
Searching...
No Matches
parallel_histogram.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
35namespace libMesh
36{
37
38namespace Parallel
39{
40
50template <typename KeyType, typename IdxType=unsigned int>
52{
53 // The type of iterator we will be using is inferred from KeyType
54 typedef typename std::vector<KeyType>::const_iterator IterType;
55
56public:
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
113private:
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
123template <typename KeyType, typename IdxType>
124inline
125const std::vector<IdxType> & Histogram<KeyType,IdxType>::get_histogram () const
126{
127 return hist;
128}
129
130
131
132template <typename KeyType, typename IdxType>
133inline
135{
136 if (bin_iters.empty())
137 return 0;
138
139 return cast_int<IdxType>(bin_iters.size()-1);
140}
141
142
143
144template <typename KeyType, typename IdxType>
145inline
146IdxType 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
157template <typename KeyType, typename IdxType>
158inline
159IdxType 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
169template <typename KeyType, typename IdxType>
170inline
171double 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
180template <typename KeyType, typename IdxType>
181inline
182double 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
An object whose state is distributed along a set of processors.
const Parallel::Communicator & comm() const
Defines a histogram to be used in parallel in conjunction with a BinSorter.
std::vector< IterType > bin_iters
std::vector< KeyType >::const_iterator IterType
IdxType local_bin_size(const IdxType bin) const
double lower_bound(const IdxType bin) const
const std::vector< IdxType > & get_histogram() const
void make_histogram(const IdxType nbins, KeyType max, KeyType min)
The actual function which sorts the data into nbins.
double upper_bound(const IdxType bin) const
IdxType n_bins() const
The number of bins in the histogram.
void build_histogram()
Build the histogram across all processors and store the result in the input vector hist.
IdxType global_bin_size(const IdxType bin) const
std::vector< double > bin_bounds
const std::vector< KeyType > & data
The libMesh namespace provides an interface to certain functionality in the library.