parallel_histogram.C
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 // C++ includes 00020 #include <algorithm> // std::lower_bound 00021 00022 // Local includes 00023 #include "libmesh/parallel_histogram.h" 00024 #ifdef LIBMESH_HAVE_LIBHILBERT 00025 # include "hilbert.h" 00026 #endif 00027 #include "libmesh/parallel.h" 00028 #include "libmesh/parallel_conversion_utils.h" 00029 00030 namespace libMesh 00031 { 00032 00033 00034 00035 namespace Parallel { 00036 template <typename KeyType, typename IdxType> 00037 Histogram<KeyType,IdxType>::Histogram (const std::vector<KeyType>& d) : 00038 data(d) 00039 { 00040 libmesh_assert (Parallel::Utils::is_sorted (data)); 00041 } 00042 00043 00044 00045 template <typename KeyType, typename IdxType> 00046 void Histogram<KeyType,IdxType>::make_histogram (const IdxType nbins, 00047 KeyType max, 00048 KeyType min) 00049 { 00050 libmesh_assert_less (min, max); 00051 00052 // The width of each bin. Store this as a floating point value 00053 double bin_width = (Parallel::Utils::to_double(max)- 00054 Parallel::Utils::to_double(min))/static_cast<double>(nbins); 00055 00056 00057 // The idea for 4 bins of size d is this: 00058 // 00059 // 0 1 2 3 4 00060 // |----------|----------|-----------|----------| 00061 // min 0 min+d 1 min+2d 2 min+3d 3 max 00062 00063 00064 00065 // Set the iterators corresponding to the boundaries 00066 // as defined above. This takes nbins * O(log N) time. 00067 bin_bounds.resize (nbins+1); 00068 bin_iters.resize (nbins+1, data.begin()); 00069 00070 // Set the minimum bin boundary iterator 00071 bin_iters[0] = data.begin(); 00072 bin_bounds[0] = Parallel::Utils::to_double(min); 00073 00074 // Set the internal bin boundary iterators 00075 for (IdxType b=1; b<nbins; ++b) 00076 { 00077 bin_bounds[b] = Parallel::Utils::to_double(min) + bin_width * b; 00078 00079 bin_iters[b] = std::lower_bound (bin_iters[b-1], data.end(), 00080 Parallel::Utils::to_key_type<KeyType>(bin_bounds[b])); 00081 } 00082 00083 bin_iters[nbins] = data.end(); 00084 bin_bounds[nbins] = Parallel::Utils::to_double(max); 00085 } 00086 00087 00088 00089 template <typename KeyType, typename IdxType> 00090 void Histogram<KeyType,IdxType>::build_histogram () 00091 { 00092 // Build a local histogram 00093 std::vector<IdxType> local_hist (this->n_bins()); 00094 00095 for (IdxType b=0; b<this->n_bins(); b++) 00096 local_hist[b] = this->local_bin_size(b); 00097 00098 // Add all the local histograms to get the global histogram 00099 hist = local_hist; 00100 CommWorld.sum(hist); 00101 00102 // All done! 00103 } 00104 00105 } 00106 00107 00108 // Explicitly instantiate for int, double 00109 template class Parallel::Histogram<int, unsigned int>; 00110 template class Parallel::Histogram<double, unsigned int>; 00111 #ifdef LIBMESH_HAVE_LIBHILBERT 00112 template class Parallel::Histogram<Hilbert::HilbertIndices, unsigned int>; 00113 #endif 00114 00115 } // namespace libMesh 00116
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:48 UTC
Hosted By: