parallel_histogram.C
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 // C++ includes
20 #include <algorithm> // std::lower_bound
21 
22 // Local includes
24 #ifdef LIBMESH_HAVE_LIBHILBERT
25 # include "hilbert.h"
26 #endif
27 #include "libmesh/parallel.h"
29 
30 namespace libMesh
31 {
32 
33 
34 
35 namespace Parallel {
36 template <typename KeyType, typename IdxType>
38  const std::vector<KeyType>& d) :
39  ParallelObject(comm_in),
40  data(d)
41 {
43 }
44 
45 
46 
47 template <typename KeyType, typename IdxType>
49  KeyType max,
50  KeyType min)
51 {
52  libmesh_assert_less (min, max);
53 
54  // The width of each bin. Store this as a floating point value
55  double bin_width = (Parallel::Utils::to_double(max)-
56  Parallel::Utils::to_double(min))/static_cast<double>(nbins);
57 
58 
59  // The idea for 4 bins of size d is this:
60  //
61  // 0 1 2 3 4
62  // |----------|----------|-----------|----------|
63  // min 0 min+d 1 min+2d 2 min+3d 3 max
64 
65 
66 
67  // Set the iterators corresponding to the boundaries
68  // as defined above. This takes nbins * O(log N) time.
69  bin_bounds.resize (nbins+1);
70  bin_iters.resize (nbins+1, data.begin());
71 
72  // Set the minimum bin boundary iterator
73  bin_iters[0] = data.begin();
74  bin_bounds[0] = Parallel::Utils::to_double(min);
75 
76  // Set the internal bin boundary iterators
77  for (IdxType b=1; b<nbins; ++b)
78  {
79  bin_bounds[b] = Parallel::Utils::to_double(min) + bin_width * b;
80 
81  bin_iters[b] = std::lower_bound (bin_iters[b-1], data.end(),
82  Parallel::Utils::to_key_type<KeyType>(bin_bounds[b]));
83  }
84 
85  bin_iters[nbins] = data.end();
86  bin_bounds[nbins] = Parallel::Utils::to_double(max);
87 }
88 
89 
90 
91 template <typename KeyType, typename IdxType>
93 {
94  // Build a local histogram
95  std::vector<IdxType> local_hist (this->n_bins());
96 
97  for (IdxType b=0; b<this->n_bins(); b++)
98  local_hist[b] = this->local_bin_size(b);
99 
100  // Add all the local histograms to get the global histogram
101  hist = local_hist;
102  this->comm().sum(hist);
103 
104  // All done!
105 }
106 
107 }
108 
109 
110 // Explicitly instantiate for int, double
113 #ifdef LIBMESH_HAVE_LIBHILBERT
115 #endif
116 
117 } // namespace libMesh

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

Hosted By:
SourceForge.net Logo