Stxxl  1.4.0
include/stxxl/bits/mng/block_alloc.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/mng/block_alloc.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-2007 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2007-2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #ifndef STXXL_MNG__BLOCK_ALLOC_H
00015 #define STXXL_MNG__BLOCK_ALLOC_H
00016 
00017 #include <algorithm>
00018 #include <stxxl/bits/parallel.h>
00019 #include <stxxl/bits/common/rand.h>
00020 #include <stxxl/bits/mng/config.h>
00021 
00022 
00023 __STXXL_BEGIN_NAMESPACE
00024 
00025 //! \ingroup mnglayer
00026 
00027 //! \weakgroup alloc Allocation functors
00028 //! Standard allocation strategies encapsulated in functors
00029 //! \{
00030 
00031 //! \brief example disk allocation scheme functor
00032 //! \remarks model of \b allocation_strategy concept
00033 struct basic_allocation_strategy
00034 {
00035     basic_allocation_strategy(int disks_begin, int disks_end);
00036     basic_allocation_strategy();
00037     int operator () (int i) const;
00038     static const char * name();
00039 };
00040 
00041 //! \brief striping disk allocation scheme functor
00042 //! \remarks model of \b allocation_strategy concept
00043 struct striping
00044 {
00045     int begin, diff;
00046 
00047 public:
00048     striping(int b, int e) : begin(b), diff(e - b)
00049     { }
00050 
00051     striping() : begin(0)
00052     {
00053         diff = config::get_instance()->disks_number();
00054     }
00055 
00056     int operator () (int i) const
00057     {
00058         return begin + i % diff;
00059     }
00060 
00061     static const char * name()
00062     {
00063         return "striping";
00064     }
00065 };
00066 
00067 //! \brief fully randomized disk allocation scheme functor
00068 //! \remarks model of \b allocation_strategy concept
00069 struct FR : public striping
00070 {
00071 private:
00072     random_number<random_uniform_fast> rnd;
00073 
00074 public:
00075     FR(int b, int e) : striping(b, e)
00076     { }
00077 
00078     FR() : striping()
00079     { }
00080 
00081     int operator () (int /*i*/) const
00082     {
00083         return begin + rnd(diff);
00084     }
00085 
00086     static const char * name()
00087     {
00088         return "fully randomized striping";
00089     }
00090 };
00091 
00092 //! \brief simple randomized disk allocation scheme functor
00093 //! \remarks model of \b allocation_strategy concept
00094 struct SR : public striping
00095 {
00096 private:
00097     int offset;
00098 
00099     void init()
00100     {
00101         random_number<random_uniform_fast> rnd;
00102         offset = rnd(diff);
00103     }
00104 
00105 public:
00106     SR(int b, int e) : striping(b, e)
00107     {
00108         init();
00109     }
00110 
00111     SR() : striping()
00112     {
00113         init();
00114     }
00115 
00116     int operator () (int i) const
00117     {
00118         return begin + (i + offset) % diff;
00119     }
00120 
00121     static const char * name()
00122     {
00123         return "simple randomized striping";
00124     }
00125 };
00126 
00127 //! \brief randomized cycling disk allocation scheme functor
00128 //! \remarks model of \b allocation_strategy concept
00129 struct RC : public striping
00130 {
00131 private:
00132     std::vector<int> perm;
00133 
00134     void init()
00135     {
00136         for (int i = 0; i < diff; i++)
00137             perm[i] = i;
00138 
00139         stxxl::random_number<random_uniform_fast> rnd;
00140         std::random_shuffle(perm.begin(), perm.end(), rnd _STXXL_FORCE_SEQUENTIAL);
00141     }
00142 
00143 public:
00144     RC(int b, int e) : striping(b, e), perm(diff)
00145     {
00146         init();
00147     }
00148 
00149     RC() : striping(), perm(diff)
00150     {
00151         init();
00152     }
00153 
00154     int operator () (int i) const
00155     {
00156         return begin + perm[i % diff];
00157     }
00158 
00159     static const char * name()
00160     {
00161         return "randomized cycling striping";
00162     }
00163 };
00164 
00165 struct RC_disk : public RC
00166 {
00167     RC_disk(int b, int e) : RC(b, e)
00168     { }
00169 
00170     RC_disk() : RC(config::get_instance()->regular_disk_range().first, config::get_instance()->regular_disk_range().second)
00171     { }
00172 
00173     static const char * name()
00174     {
00175         return "Randomized cycling striping on regular disks";
00176     }
00177 };
00178 
00179 struct RC_flash : public RC
00180 {
00181     RC_flash(int b, int e) : RC(b, e)
00182     { }
00183 
00184     RC_flash() : RC(config::get_instance()->flash_range().first, config::get_instance()->flash_range().second)
00185     { }
00186 
00187     static const char * name()
00188     {
00189         return "Randomized cycling striping on flash devices";
00190     }
00191 };
00192 
00193 //! \brief 'single disk' disk allocation scheme functor
00194 //! \remarks model of \b allocation_strategy concept
00195 struct single_disk
00196 {
00197     const int disk;
00198     single_disk(int d, int = 0) : disk(d)
00199     { }
00200 
00201     single_disk() : disk(0)
00202     { }
00203 
00204     int operator () (int /*i*/) const
00205     {
00206         return disk;
00207     }
00208 
00209     static const char * name()
00210     {
00211         return "single disk";
00212     }
00213 };
00214 
00215 //! \brief Allocator functor adaptor
00216 
00217 //! Gives offset to disk number sequence defined in constructor
00218 template <class BaseAllocator_>
00219 struct offset_allocator
00220 {
00221     BaseAllocator_ base;
00222     int_type offset;
00223 
00224     //! \brief Creates functor based on instance of \c BaseAllocator_ functor
00225     //! with offset \c offset_
00226     //! \param offset_ offset
00227     //! \param base_ used to create a copy
00228     offset_allocator(int_type offset_, const BaseAllocator_ & base_) : base(base_), offset(offset_)
00229     { }
00230 
00231     //! \brief Creates functor based on instance of \c BaseAllocator_ functor
00232     //! \param base_ used to create a copy
00233     offset_allocator(const BaseAllocator_ & base_) : base(base_), offset(0)
00234     { }
00235 
00236     //! \brief Creates functor based on default \c BaseAllocator_ functor
00237     offset_allocator() : offset(0)
00238     { }
00239 
00240     int operator () (int_type i) const
00241     {
00242         return base(offset + i);
00243     }
00244 
00245     int_type get_offset() const
00246     {
00247         return offset;
00248     }
00249 
00250     void set_offset(int_type i)
00251     {
00252         offset = i;
00253     }
00254 };
00255 
00256 #ifndef STXXL_DEFAULT_ALLOC_STRATEGY
00257     #define STXXL_DEFAULT_ALLOC_STRATEGY stxxl::RC
00258 #endif
00259 
00260 //! \}
00261 
00262 __STXXL_END_NAMESPACE
00263 
00264 #endif // !STXXL_MNG__BLOCK_ALLOC_H
00265 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines