STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
block_alloc.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/mng/block_alloc.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2007 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007-2009 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_MNG_BLOCK_ALLOC_HEADER
15 #define STXXL_MNG_BLOCK_ALLOC_HEADER
16 
17 #include <algorithm>
18 #include <stxxl/bits/parallel.h>
19 #include <stxxl/bits/common/rand.h>
20 #include <stxxl/bits/mng/config.h>
21 
22 
24 
25 //! \defgroup alloc Allocation Functors
26 //! \ingroup mnglayer
27 //! Standard allocation strategies encapsulated in functors.
28 //! \{
29 
30 //! Example disk allocation scheme functor.
31 //! \remarks model of \b allocation_strategy concept
33 {
34  basic_allocation_strategy(int disks_begin, int disks_end);
36  int operator () (int i) const;
37  static const char * name();
38 };
39 
40 //! Striping disk allocation scheme functor.
41 //! \remarks model of \b allocation_strategy concept
42 struct striping
43 {
45 
46 public:
47  striping(unsigned_type b, unsigned_type e) : begin(b), diff(e - b)
48  { }
49 
50  striping() : begin(0)
51  {
52  diff = config::get_instance()->disks_number();
53  }
54 
55  unsigned_type operator () (unsigned_type i) const
56  {
57  return begin + i % diff;
58  }
59 
60  static const char * name()
61  {
62  return "striping";
63  }
64 };
65 
66 //! Fully randomized disk allocation scheme functor.
67 //! \remarks model of \b allocation_strategy concept
68 struct FR : public striping
69 {
70 private:
73 
74 public:
76  { }
77 
78  FR() : striping()
79  { }
80 
81  unsigned_type operator () (unsigned_type /*i*/) const
82  {
83  return begin + rnd(rnd_type::value_type(diff));
84  }
85 
86  static const char * name()
87  {
88  return "fully randomized striping";
89  }
90 };
91 
92 //! Simple randomized disk allocation scheme functor.
93 //! \remarks model of \b allocation_strategy concept
94 struct SR : public striping
95 {
96 private:
98 
100 
101  void init()
102  {
103  rnd_type rnd;
104  offset = rnd(rnd_type::value_type(diff));
105  }
106 
107 public:
109  {
110  init();
111  }
112 
113  SR() : striping()
114  {
115  init();
116  }
117 
118  unsigned_type operator () (unsigned_type i) const
119  {
120  return begin + (i + offset) % diff;
121  }
122 
123  static const char * name()
124  {
125  return "simple randomized striping";
126  }
127 };
128 
129 //! Randomized cycling disk allocation scheme functor.
130 //! \remarks model of \b allocation_strategy concept
131 struct RC : public striping
132 {
133 private:
134  std::vector<unsigned_type> perm;
135 
136  void init()
137  {
138  for (unsigned_type i = 0; i < diff; i++)
139  perm[i] = i;
140 
142  std::random_shuffle(perm.begin(), perm.end(), rnd _STXXL_FORCE_SEQUENTIAL);
143  }
144 
145 public:
146  RC(unsigned_type b, unsigned_type e) : striping(b, e), perm(diff)
147  {
148  init();
149  }
150 
151  RC() : striping(), perm(diff)
152  {
153  init();
154  }
155 
156  unsigned_type operator () (unsigned_type i) const
157  {
158  return begin + perm[i % diff];
159  }
160 
161  static const char * name()
162  {
163  return "randomized cycling striping";
164  }
165 };
166 
167 struct RC_disk : public RC
168 {
170  { }
171 
172  RC_disk() : RC(config::get_instance()->regular_disk_range().first, config::get_instance()->regular_disk_range().second)
173  { }
174 
175  static const char * name()
176  {
177  return "Randomized cycling striping on regular disks";
178  }
179 };
180 
181 struct RC_flash : public RC
182 {
184  { }
185 
186  RC_flash() : RC(config::get_instance()->flash_range().first, config::get_instance()->flash_range().second)
187  { }
188 
189  static const char * name()
190  {
191  return "Randomized cycling striping on flash devices";
192  }
193 };
194 
195 //! 'Single disk' disk allocation scheme functor.
196 //! \remarks model of \b allocation_strategy concept
198 {
201  { }
202 
203  single_disk() : disk(0)
204  { }
205 
206  unsigned_type operator () (unsigned_type /*i*/) const
207  {
208  return disk;
209  }
210 
211  static const char * name()
212  {
213  return "single disk";
214  }
215 };
216 
217 //! Allocator functor adaptor.
218 //!
219 //! Gives offset to disk number sequence defined in constructor
220 template <class BaseAllocator_>
222 {
223  BaseAllocator_ base;
225 
226  //! Creates functor based on instance of \c BaseAllocator_ functor
227  //! with offset \c offset_.
228  //! \param offset_ offset
229  //! \param base_ used to create a copy
230  offset_allocator(int_type offset_, const BaseAllocator_& base_) : base(base_), offset(offset_)
231  { }
232 
233  //! Creates functor based on instance of \c BaseAllocator_ functor.
234  //! \param base_ used to create a copy
235  offset_allocator(const BaseAllocator_& base_) : base(base_), offset(0)
236  { }
237 
238  //! Creates functor based on default \c BaseAllocator_ functor.
239  offset_allocator() : offset(0)
240  { }
241 
242  unsigned_type operator () (unsigned_type i) const
243  {
244  return base(offset + i);
245  }
246 
248  {
249  return offset;
250  }
251 
253  {
254  offset = i;
255  }
256 };
257 
258 #ifndef STXXL_DEFAULT_ALLOC_STRATEGY
259  #define STXXL_DEFAULT_ALLOC_STRATEGY stxxl::RC
260 #endif
261 
262 //! \}
263 
265 
266 #endif // !STXXL_MNG_BLOCK_ALLOC_HEADER
267 // vim: et:ts=4:sw=4
striping(unsigned_type b, unsigned_type e)
Definition: block_alloc.h:47
random_number< random_uniform_fast > rnd_type
Definition: block_alloc.h:71
Fully randomized disk allocation scheme functor.
Definition: block_alloc.h:68
void random_shuffle(ExtIterator_ first, ExtIterator_ last, RandomNumberGenerator_ &rand, unsigned_type M, AllocStrategy_ AS=STXXL_DEFAULT_ALLOC_STRATEGY())
External equivalent of std::random_shuffle.
SR(unsigned_type b, unsigned_type e)
Definition: block_alloc.h:108
Allocator functor adaptor.
Definition: block_alloc.h:221
random_number< random_uniform_fast > rnd_type
Definition: block_alloc.h:99
Example disk allocation scheme functor.
Definition: block_alloc.h:32
&#39;Single disk&#39; disk allocation scheme functor.
Definition: block_alloc.h:197
static const char * name()
Definition: block_alloc.h:211
unsigned_type disk
Definition: block_alloc.h:199
RC_flash(unsigned_type b, unsigned_type e)
Definition: block_alloc.h:183
void init()
Definition: block_alloc.h:136
unsigned_type offset
Definition: block_alloc.h:97
static const char * name()
Definition: block_alloc.h:161
unsigned_type diff
Definition: block_alloc.h:44
static const char * name()
Definition: block_alloc.h:123
BaseAllocator_ base
Definition: block_alloc.h:223
Randomized cycling disk allocation scheme functor.
Definition: block_alloc.h:131
choose_int_types< my_pointer_size >::int_type int_type
Definition: types.h:66
static const char * name()
Definition: block_alloc.h:60
rnd_type rnd
Definition: block_alloc.h:72
Striping disk allocation scheme functor.
Definition: block_alloc.h:42
std::vector< unsigned_type > perm
Definition: block_alloc.h:134
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
offset_allocator(const BaseAllocator_ &base_)
Creates functor based on instance of BaseAllocator_ functor.
Definition: block_alloc.h:235
int_type get_offset() const
Definition: block_alloc.h:247
offset_allocator(int_type offset_, const BaseAllocator_ &base_)
Creates functor based on instance of BaseAllocator_ functor with offset offset_.
Definition: block_alloc.h:230
Simple randomized disk allocation scheme functor.
Definition: block_alloc.h:94
void set_offset(int_type i)
Definition: block_alloc.h:252
static const char * name()
Definition: block_alloc.h:86
FR(unsigned_type b, unsigned_type e)
Definition: block_alloc.h:75
void init()
Definition: block_alloc.h:101
static const char * name()
Definition: block_alloc.h:175
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
Access point to disks properties. Since 1.4.0: no config files are read automatically! ...
Definition: config.h:106
RC_disk(unsigned_type b, unsigned_type e)
Definition: block_alloc.h:169
RC(unsigned_type b, unsigned_type e)
Definition: block_alloc.h:146
#define _STXXL_FORCE_SEQUENTIAL
Definition: parallel.h:51
static const char * name()
Definition: block_alloc.h:189
offset_allocator()
Creates functor based on default BaseAllocator_ functor.
Definition: block_alloc.h:239
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
single_disk(unsigned_type d, unsigned_type=0)
Definition: block_alloc.h:200