libMesh
parallel_histogram.C
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 // C++ includes
20 #include <algorithm> // std::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 
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 {
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();
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();
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
IdxType n_bins() const
The number of bins in the histogram.
Encapsulates the MPI_Comm object.
Definition: parallel.h:657
The libMesh namespace provides an interface to certain functionality in the library.
std::vector< IdxType > hist
std::vector< IterType > bin_iters
void build_histogram()
Build the histogram across all processors and store the result in the input vector hist...
libmesh_assert(j)
bool is_sorted(const std::vector< KeyType > &v)
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...
static KeyType to_key_type(const double f)
const Parallel::Communicator & comm() const
void max(T &r, const Communicator &comm=Communicator_World)
IterBase * data
Ideally this private member data should have protected access.
Defines a histogram to be used in parallel in conjunction with a BinSorter.
void sum(T &r) const
Take a local variable and replace it with the sum of it&#39;s values on all processors.
std::vector< double > bin_bounds
void min(T &r, const Communicator &comm=Communicator_World)
double to_double(const KeyType &k)
A utility function which converts whatever KeyType is to a double for the histogram bounds...