Parallel::BinSorter< KeyType > Class Template Reference

#include <parallel_bin_sorter.h>

List of all members.

Public Member Functions

 BinSorter (const std::vector< KeyType > &d)
void binsort (const unsigned int nbins, KeyType max, KeyType min)
unsigned int sizeof_bin (const unsigned int bin) const

Private Types

typedef std::vector< KeyType >
::const_iterator 
IterType

Private Attributes

const std::vector< KeyType > & data
std::vector< IterTypebin_iters


Detailed Description

template<typename KeyType>
class Parallel::BinSorter< KeyType >

Perform a parallel sort using a bin-sort method.

Definition at line 38 of file parallel_bin_sorter.h.


Member Typedef Documentation

template<typename KeyType >
typedef std::vector<KeyType>::const_iterator Parallel::BinSorter< KeyType >::IterType [private]

Definition at line 41 of file parallel_bin_sorter.h.


Constructor & Destructor Documentation

template<typename KeyType >
Parallel::BinSorter< KeyType >::BinSorter ( const std::vector< KeyType > &  d  )  [inline]

Definition at line 41 of file parallel_bin_sorter.C.

References Parallel::BinSorter< KeyType >::data, and Parallel::Utils::is_sorted().

00041                                                           :
00042   data(d)
00043 {
00044   // Assume (& libmesh_assert) we are working with a sorted range
00045 
00046   // Ah...  is_sorted is an STL extension!
00047   //libmesh_assert (std::is_sorted (data.begin(), data.end()));
00048 
00049   // Home-grown is_sorted
00050   libmesh_assert (Parallel::Utils::is_sorted (data));
00051 }


Member Function Documentation

template<typename KeyType >
void Parallel::BinSorter< KeyType >::binsort ( const unsigned int  nbins,
KeyType  max,
KeyType  min 
) [inline]

Definition at line 56 of file parallel_bin_sorter.C.

References Parallel::BinSorter< KeyType >::bin_iters, Parallel::Histogram< KeyType >::build_histogram(), Parallel::BinSorter< KeyType >::data, Parallel::Histogram< KeyType >::get_histogram(), Parallel::Histogram< KeyType >::make_histogram(), Parallel::Histogram< KeyType >::n_bins(), Parallel::Utils::to_double(), and Parallel::Histogram< KeyType >::upper_bound().

Referenced by Parallel::Sort< KeyType >::binsort().

00059 {
00060   libmesh_assert (min < max);
00061   
00062   // Build a histogram in parallel from our data.
00063   // Use this to create quasi-uniform bins.
00064   Parallel::Histogram<KeyType> phist (data);
00065   phist.make_histogram (nbins*50, max, min);
00066   phist.build_histogram ();
00067 
00068   const std::vector<unsigned int>& histogram =
00069     phist.get_histogram();
00070 
00071 
00072   // Now we will locate the bin boundaries so
00073   // that each bin is roughly equal size
00074   {
00075     // Find the total size of the data set
00076     unsigned int local_data_size = data.size();
00077     unsigned int global_data_size = local_data_size;
00078 
00079     Parallel::sum(global_data_size);
00080     
00081     std::vector<unsigned int> target_bin_size (nbins, global_data_size / nbins);
00082     
00083     // Equally distribute the remainder
00084     for (unsigned int i=0; i<(global_data_size % nbins); i++)
00085       ++target_bin_size[i];
00086     
00087     // Set the iterators corresponding to the bin boundaries
00088     {
00089       std::vector<double> bin_bounds (nbins+1);
00090       bin_iters.resize  (nbins+1, data.begin());
00091       
00092       // Set the minimum bin boundary iterator
00093       bin_iters[0]  = data.begin();
00094       bin_bounds[0] = Parallel::Utils::to_double(min);
00095       
00096       // The current location in the histogram
00097       unsigned int current_histogram_bin = 0;
00098 
00099       // How much above (+) or below (-) we are from the
00100       // target size for the last bin.
00101       // Note that when delta is (-) we will
00102       // accept a slightly larger size for the next bin,
00103       // the goal being to keep the whole mess average
00104       int delta = 0;
00105       
00106       // Set the internal bin boundary iterators
00107       for (unsigned int b=0; b<nbins; ++b)
00108         {
00109           // The size of bin b.  We want this to
00110           // be ~= target_bin_size[b]
00111           int current_bin_size = 0;
00112           
00113           // Step through the histogram until we have the
00114           // desired bin size     
00115           while ((current_bin_size + histogram[current_histogram_bin] + delta) <= target_bin_size[b])
00116             {
00117               // Don't index out of the histogram!
00118               if ((current_histogram_bin+1) == phist.n_bins())
00119                 break;
00120               
00121               current_bin_size += histogram[current_histogram_bin++];
00122             }
00123           
00124           delta += current_bin_size - target_bin_size[b];
00125           
00126           // Set the upper bound of the bin
00127           bin_bounds[b+1] = phist.upper_bound (current_histogram_bin);    
00128           bin_iters[b+1]  = std::lower_bound(bin_iters[b], data.end(), 
00129                                              Parallel::Utils::to_key_type<KeyType>(bin_bounds[b+1]));
00130         }
00131 
00132       // Just be sure the last boundaries point to the right place
00133       bin_iters[nbins]  = data.end();
00134       bin_bounds[nbins] = Parallel::Utils::to_double(max);
00135     }
00136   }
00137 }

template<typename KeyType >
unsigned int Parallel::BinSorter< KeyType >::sizeof_bin ( const unsigned int  bin  )  const [inline]

Definition at line 71 of file parallel_bin_sorter.h.

References Parallel::BinSorter< KeyType >::bin_iters.

Referenced by Parallel::Sort< KeyType >::binsort().

00072 {
00073   libmesh_assert ((bin+1) < bin_iters.size());
00074 
00075   // The size of the bin is defined by the distance between
00076   // its bounding iterators
00077   return std::distance (bin_iters[bin], bin_iters[bin+1]);
00078 }


Member Data Documentation

template<typename KeyType >
std::vector<IterType> Parallel::BinSorter< KeyType >::bin_iters [private]

template<typename KeyType >
const std::vector<KeyType>& Parallel::BinSorter< KeyType >::data [private]


The documentation for this class was generated from the following files:

Site Created By: libMesh Developers
Last modified: November 25 2009 03:45:16.

Hosted By:
SourceForge.net Logo