$extrastylesheet
parallel_histogram.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 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 #ifndef LIBMESH_PARALLEL_HISTOGRAM_H
00020 #define LIBMESH_PARALLEL_HISTOGRAM_H
00021 
00022 // This class contains all the functionality for bin sorting
00023 // Templated on the type of keys you will be sorting and the
00024 // type of iterator you will be using.
00025 
00026 
00027 // C++ includes
00028 #include "libmesh/libmesh_common.h" // for libmesh_assert()
00029 #include "libmesh/parallel_object.h"
00030 
00031 // Local includes
00032 #include <vector>
00033 #include <iterator>
00034 
00035 namespace libMesh
00036 {
00037 
00038 namespace Parallel {
00039 
00044 template <typename KeyType, typename IdxType=unsigned int>
00045 class Histogram : public ParallelObject
00046 {
00047   // The type of iterator we will be using is inferred from KeyType
00048   typedef typename std::vector<KeyType>::const_iterator IterType;
00049 
00050 public:
00051 
00052   // Constructor
00053   explicit
00054   Histogram (const Parallel::Communicator &comm,
00055              const std::vector<KeyType>& d);
00056 
00057   // The actual function which sorts the data into
00058   // nbins.  Currently based on the global min and
00059   // max which you must provide e.g. by using MPI.
00060   void make_histogram (const IdxType nbins,
00061                        KeyType max,
00062                        KeyType min);
00063 
00064   // Build the histogram across all processors and store the
00065   // result in the input vector \p hist
00066   void build_histogram ();
00067 
00068   // Return the raw histogram data to the user
00069   const std::vector<IdxType>& get_histogram() const;
00070 
00071   // The number of bins in the histogram
00072   IdxType n_bins () const;
00073 
00074   // Returns the size of local bin b
00075   IdxType local_bin_size (const IdxType bin) const;
00076 
00077   // Returns the size of global bin b
00078   // Requires that the user first call \p build_histogram()
00079   IdxType global_bin_size (const IdxType bin) const;
00080 
00081   // Returns the lower boundary of bin \p bin
00082   double lower_bound (const IdxType bin) const;
00083 
00084   // Returns the upper boundary of bin \p bin
00085   double upper_bound (const IdxType bin) const;
00086 
00087 
00088 private:
00089 
00090 
00091   const std::vector<KeyType>& data;
00092   std::vector<IdxType>        hist;        // The actual histogram
00093   std::vector<double>         bin_bounds;  // The boundary values of each bin
00094   std::vector<IterType>       bin_iters;   // Iterators to the bin boundaries
00095                                            //  in data
00096 };
00097 
00098 
00099 
00100 
00101 //--------------------------------------------------------------------------
00102 template <typename KeyType, typename IdxType>
00103 inline
00104 const std::vector<IdxType>& Histogram<KeyType,IdxType>::get_histogram () const
00105 {
00106   return hist;
00107 }
00108 
00109 
00110 
00111 template <typename KeyType, typename IdxType>
00112 inline
00113 IdxType Histogram<KeyType,IdxType>::n_bins () const
00114 {
00115   if (bin_iters.empty())
00116     return 0;
00117 
00118   return cast_int<IdxType>(bin_iters.size()-1);
00119 }
00120 
00121 
00122 
00123 template <typename KeyType, typename IdxType>
00124 inline
00125 IdxType Histogram<KeyType,IdxType>::local_bin_size (const IdxType bin) const
00126 {
00127   libmesh_assert_less ((bin+1), bin_iters.size());
00128 
00129   // The number of entries in the bin (locally)
00130   return cast_int<IdxType>
00131     (std::distance (bin_iters[bin], bin_iters[bin+1]));
00132 }
00133 
00134 
00135 
00136 template <typename KeyType, typename IdxType>
00137 inline
00138 IdxType Histogram<KeyType,IdxType>::global_bin_size (const IdxType bin) const
00139 {
00140   libmesh_assert_less (bin, hist.size());
00141 
00142   // The number of entries in the bin (globally)
00143   return hist[bin];
00144 }
00145 
00146 
00147 
00148 template <typename KeyType, typename IdxType>
00149 inline
00150 double Histogram<KeyType,IdxType>::lower_bound (const IdxType bin) const
00151 {
00152   libmesh_assert_less ((bin+1), bin_bounds.size());
00153 
00154   return bin_bounds[bin];
00155 }
00156 
00157 
00158 
00159 template <typename KeyType, typename IdxType>
00160 inline
00161 double Histogram<KeyType,IdxType>::upper_bound (const IdxType bin) const
00162 {
00163   libmesh_assert_less ((bin+1), bin_bounds.size());
00164 
00165   return bin_bounds[bin+1];
00166 }
00167 
00168 }
00169 
00170 } // namespace libMesh
00171 
00172 #endif // LIBMESH_PARALLEL_HISTOGRAM_H