parallel_histogram.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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> // std::lower_bound
21 
22 // Local includes
24 #include "libmesh/parallel.h"
27 
28 namespace libMesh
29 {
30 
31 
32 
33 namespace Parallel {
34 template <typename KeyType, typename IdxType>
36  const std::vector<KeyType> & d) :
37  ParallelObject(comm_in),
38  data(d)
39 {
40  libmesh_assert (Parallel::Utils::is_sorted (data));
41 }
42 
43 
44 
45 template <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 
90 template <typename KeyType, typename IdxType>
92 {
93  // Build a local histogram
94  std::vector<IdxType> local_hist (this->n_bins());
95 
96  for (IdxType b=0; b<this->n_bins(); b++)
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
112 #ifdef LIBMESH_HAVE_LIBHILBERT
114 #endif
115 
116 } // namespace libMesh
long double max(long double a, double b)
bool is_sorted(const std::vector< KeyType > &v)
const std::vector< KeyType > & data
Histogram(const Parallel::Communicator &comm, const std::vector< KeyType > &d)
void make_histogram(const IdxType nbins, KeyType max, KeyType min)
An object whose state is distributed along a set of processors.
static KeyType to_key_type(const double f)
IterBase * data
Used in conjunction with a BinSorter for parallel sorting.
long double min(long double a, double b)
double to_double(const KeyType &k)