libMesh
Loading...
Searching...
No Matches
parallel_histogram.C
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// C++ includes
20#include <algorithm> // is_sorted, lower_bound
21
22// Local includes
23#include "libmesh/parallel_histogram.h"
24#include "libmesh/parallel.h"
25#include "libmesh/parallel_conversion_utils.h"
26#include "libmesh/parallel_hilbert.h"
27
28namespace libMesh
29{
30
31
32
33namespace Parallel {
34template <typename KeyType, typename IdxType>
36 const std::vector<KeyType> & d) :
37 ParallelObject(comm_in),
38 data(d)
39{
40 libmesh_assert (std::is_sorted (data.begin(), data.end()));
41}
42
43
44
45template <typename KeyType, typename IdxType>
47 KeyType max,
48 KeyType min)
49{
50 libmesh_assert_less (min, max);
51
52 // The width of each bin. Store this as a floating point value
53 double bin_width = (Parallel::Utils::to_double(max)-
54 Parallel::Utils::to_double(min))/static_cast<double>(nbins);
55
56
57 // The idea for 4 bins of size d is this:
58 //
59 // 0 1 2 3 4
60 // |----------|----------|-----------|----------|
61 // min 0 min+d 1 min+2d 2 min+3d 3 max
62
63
64
65 // Set the iterators corresponding to the boundaries
66 // as defined above. This takes nbins * O(log N) time.
67 bin_bounds.resize (nbins+1);
68 bin_iters.resize (nbins+1, data.begin());
69
70 // Set the minimum bin boundary iterator
71 bin_iters[0] = data.begin();
72 bin_bounds[0] = Parallel::Utils::to_double(min);
73
74 // Set the internal bin boundary iterators
75 for (IdxType b=1; b<nbins; ++b)
76 {
77 bin_bounds[b] = Parallel::Utils::to_double(min) + bin_width * b;
78
79 bin_iters[b] =
80 std::lower_bound (bin_iters[b-1], data.end(),
82 }
83
84 bin_iters[nbins] = data.end();
85 bin_bounds[nbins] = Parallel::Utils::to_double(max);
86}
87
88
89
90template <typename KeyType, typename IdxType>
92{
93 // Build a local histogram
94 std::vector<IdxType> local_hist (this->n_bins());
95
96 for (auto b : index_range(local_hist))
97 local_hist[b] = this->local_bin_size(b);
98
99 // Add all the local histograms to get the global histogram
100 hist = local_hist;
101 this->comm().sum(hist);
102
103 // All done!
104}
105
106}
107
108
109// Explicitly instantiate for int, double
110template class LIBMESH_EXPORT Parallel::Histogram<int, unsigned int>;
111template class LIBMESH_EXPORT Parallel::Histogram<double, unsigned int>;
112#ifdef LIBMESH_HAVE_LIBHILBERT
114#endif
115
116} // namespace libMesh
An object whose state is distributed along a set of processors.
Defines a histogram to be used in parallel in conjunction with a BinSorter.
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.
void build_histogram()
Build the histogram across all processors and store the result in the input vector hist.
const std::vector< KeyType > & data
static const Real b
double to_double(const KeyType &k)
A utility function which converts whatever KeyType is to a double for the histogram bounds.
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
libmesh_assert(ctx)
A utility to convert a double to some sort of KeyType, for interpreting how histogram bounds relate t...