libMesh
parallel_bin_sorter.h
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 #ifndef LIBMESH_PARALLEL_BIN_SORTER_H
20 #define LIBMESH_PARALLEL_BIN_SORTER_H
21 
22 // This class contains all the functionality for bin sorting
23 // Templated on the type of keys you will be sorting and the
24 // type of iterator you will be using.
25 
26 // libMesh includes
27 #include "libmesh/libmesh_common.h" // cast_int
28 #include "libmesh/parallel_object.h"
29 
30 // C++ includes
31 #include <vector>
32 #include <iterator>
33 
34 namespace libMesh
35 {
36 
37 namespace Parallel
38 {
39 
48 template <typename KeyType, typename IdxType=unsigned int>
49 class BinSorter : public ParallelObject
50 {
51  // the type of iterator we will be using is inferred from KeyType
52  typedef typename std::vector<KeyType>::const_iterator IterType;
53 
54 public:
55 
56  // Constructor
57  explicit
59  const std::vector<KeyType> & d);
60 
66  void binsort (const IdxType nbins,
67  KeyType max,
68  KeyType min);
69 
73  IdxType sizeof_bin (const IdxType bin) const;
74 
75 
76 private:
77 
78  const std::vector<KeyType> & data;
79  std::vector<IterType> bin_iters; // Iterators to the bin boundaries
80  // in data
81 };
82 
83 
84 
85 //--------------------------------------------------------------------------
86 template <typename KeyType, typename IdxType>
87 inline
88 IdxType BinSorter<KeyType,IdxType>::sizeof_bin (const IdxType bin) const
89 {
90  libmesh_assert_less ((bin+1), bin_iters.size());
91 
92  // The size of the bin is defined by the distance between
93  // its bounding iterators
94  return cast_int<IdxType>
95  (std::distance (bin_iters[bin], bin_iters[bin+1]));
96 }
97 
98 }
99 
100 } // namespace libMesh
101 
102 #endif // LIBMESH_PARALLEL_BIN_SORTER_H
std::vector< IterType > bin_iters
Encapsulates the MPI_Comm object.
Definition: parallel.h:657
BinSorter(const Parallel::Communicator &comm, const std::vector< KeyType > &d)
const std::vector< KeyType > & data
The libMesh namespace provides an interface to certain functionality in the library.
Real distance(const Point &p)
Perform a parallel sort using a bin-sort method.
This class forms the base class for all other classes that are expected to be implemented in parallel...
std::vector< KeyType >::const_iterator IterType
const Parallel::Communicator & comm() const
void max(T &r, const Communicator &comm=Communicator_World)
IdxType sizeof_bin(const IdxType bin) const
void min(T &r, const Communicator &comm=Communicator_World)
void binsort(const IdxType nbins, KeyType max, KeyType min)
The actual function which sorts the data into nbins.