Stxxl  1.3.2
block_alloc.h
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_H
15 #define STXXL_MNG__BLOCK_ALLOC_H
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 
23 __STXXL_BEGIN_NAMESPACE
24 
26 
30 
34 {
35  basic_allocation_strategy(int disks_begin, int disks_end);
37  int operator () (int i) const;
38  static const char * name();
39 };
40 
43 struct striping
44 {
45  int begin, diff;
46 
47 public:
48  striping(int b, int e) : begin(b), diff(e - b)
49  { }
50 
51  striping() : begin(0)
52  {
53  diff = config::get_instance()->disks_number();
54  }
55 
56  int operator () (int i) const
57  {
58  return begin + i % diff;
59  }
60 
61  static const char * name()
62  {
63  return "striping";
64  }
65 };
66 
69 struct FR : public striping
70 {
71 private:
73 
74 public:
75  FR(int b, int e) : striping(b, e)
76  { }
77 
78  FR() : striping()
79  { }
80 
81  int operator () (int /*i*/) const
82  {
83  return begin + rnd(diff);
84  }
85 
86  static const char * name()
87  {
88  return "fully randomized striping";
89  }
90 };
91 
94 struct SR : public striping
95 {
96 private:
97  int offset;
98 
99  void init()
100  {
102  offset = rnd(diff);
103  }
104 
105 public:
106  SR(int b, int e) : striping(b, e)
107  {
108  init();
109  }
110 
111  SR() : striping()
112  {
113  init();
114  }
115 
116  int operator () (int i) const
117  {
118  return begin + (i + offset) % diff;
119  }
120 
121  static const char * name()
122  {
123  return "simple randomized striping";
124  }
125 };
126 
129 struct RC : public striping
130 {
131 private:
132  std::vector<int> perm;
133 
134  void init()
135  {
136  for (int i = 0; i < diff; i++)
137  perm[i] = i;
138 
139  stxxl::random_number<random_uniform_fast> rnd;
140  std::random_shuffle(perm.begin(), perm.end(), rnd _STXXL_FORCE_SEQUENTIAL);
141  }
142 
143 public:
144  RC(int b, int e) : striping(b, e), perm(diff)
145  {
146  init();
147  }
148 
149  RC() : striping(), perm(diff)
150  {
151  init();
152  }
153 
154  int operator () (int i) const
155  {
156  return begin + perm[i % diff];
157  }
158 
159  static const char * name()
160  {
161  return "randomized cycling striping";
162  }
163 };
164 
165 struct RC_disk : public RC
166 {
167  RC_disk(int b, int e) : RC(b, e)
168  { }
169 
170  RC_disk() : RC(config::get_instance()->regular_disk_range().first, config::get_instance()->regular_disk_range().second)
171  { }
172 
173  static const char * name()
174  {
175  return "Randomized cycling striping on regular disks";
176  }
177 };
178 
179 struct RC_flash : public RC
180 {
181  RC_flash(int b, int e) : RC(b, e)
182  { }
183 
184  RC_flash() : RC(config::get_instance()->flash_range().first, config::get_instance()->flash_range().second)
185  { }
186 
187  static const char * name()
188  {
189  return "Randomized cycling striping on flash devices";
190  }
191 };
192 
196 {
197  const int disk;
198  single_disk(int d, int = 0) : disk(d)
199  { }
200 
201  single_disk() : disk(0)
202  { }
203 
204  int operator () (int /*i*/) const
205  {
206  return disk;
207  }
208 
209  static const char * name()
210  {
211  return "single disk";
212  }
213 };
214 
216 
218 template <class BaseAllocator_>
220 {
221  BaseAllocator_ base;
222  int_type offset;
223 
228  offset_allocator(int_type offset_, const BaseAllocator_ & base_) : base(base_), offset(offset_)
229  { }
230 
233  offset_allocator(const BaseAllocator_ & base_) : base(base_), offset(0)
234  { }
235 
237  offset_allocator() : offset(0)
238  { }
239 
240  int operator () (int_type i) const
241  {
242  return base(offset + i);
243  }
244 
245  int_type get_offset() const
246  {
247  return offset;
248  }
249 
250  void set_offset(int_type i)
251  {
252  offset = i;
253  }
254 };
255 
256 #ifndef STXXL_DEFAULT_ALLOC_STRATEGY
257  #define STXXL_DEFAULT_ALLOC_STRATEGY stxxl::RC
258 #endif
259 
261 
262 __STXXL_END_NAMESPACE
263 
264 #endif // !STXXL_MNG__BLOCK_ALLOC_H
265 // vim: et:ts=4:sw=4
fully randomized disk allocation scheme functor
Definition: block_alloc.h:69
striping disk allocation scheme functor
Definition: block_alloc.h:43
offset_allocator(const BaseAllocator_ &base_)
Creates functor based on instance of BaseAllocator_ functor.
Definition: block_alloc.h:233
example disk allocation scheme functor
Definition: block_alloc.h:33
void random_shuffle(ExtIterator_ first, ExtIterator_ last, RandomNumberGenerator_ &rand, unsigned_type M, AllocStrategy_ AS=STXXL_DEFAULT_ALLOC_STRATEGY())
External equivalent of std::random_shuffle.
Definition: random_shuffle.h:49
offset_allocator(int_type offset_, const BaseAllocator_ &base_)
Creates functor based on instance of BaseAllocator_ functor with offset offset_.
Definition: block_alloc.h:228
simple randomized disk allocation scheme functor
Definition: block_alloc.h:94
Allocator functor adaptor.
Definition: block_alloc.h:219
&#39;single disk&#39; disk allocation scheme functor
Definition: block_alloc.h:195
offset_allocator()
Creates functor based on default BaseAllocator_ functor.
Definition: block_alloc.h:237
Access point to disks properties.
Definition: config.h:31
randomized cycling disk allocation scheme functor
Definition: block_alloc.h:129