Thrill  0.1
read_write_pool.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/mng/read_write_pool.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2009 Andreas Beckmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef FOXXLL_MNG_READ_WRITE_POOL_HEADER
14 #define FOXXLL_MNG_READ_WRITE_POOL_HEADER
15 
16 #include <algorithm>
17 #include <utility>
18 
19 #include <tlx/define.hpp>
20 
23 
24 namespace foxxll {
25 
26 //! \addtogroup foxxll_schedlayer
27 //! \{
28 
29 //! Implements dynamically resizable buffered writing and prefetched reading pool.
30 template <typename BlockType>
32 {
33 public:
34  using block_type = BlockType;
35  using bid_type = typename block_type::bid_type;
36  using size_type = size_t;
37 
38 protected:
41 
45 
46 public:
47  //! Constructs pool.
48  //! \param init_size_prefetch initial number of blocks in the prefetch pool
49  //! \param init_size_write initial number of blocks in the write pool
50  explicit read_write_pool(size_type init_size_prefetch = 1, size_type init_size_write = 1)
51  : delete_pools(true)
52  {
53  w_pool = new write_pool_type(init_size_write);
54  p_pool = new prefetch_pool_type(init_size_prefetch);
55  }
56 
57  //! non-copyable: delete copy-constructor
58  read_write_pool(const read_write_pool&) = delete;
59  //! non-copyable: delete assignment operator
61 
62  void swap(read_write_pool& obj)
63  {
64  std::swap(w_pool, obj.w_pool);
65  std::swap(p_pool, obj.p_pool);
66  std::swap(delete_pools, obj.delete_pools);
67  }
68 
69  //! Waits for completion of all ongoing requests and frees memory.
71  {
72  if (delete_pools) {
73  delete w_pool;
74  delete p_pool;
75  }
76  }
77 
78  //! Returns number of blocks owned by the write_pool.
79  size_type size_write() const { return w_pool->size(); }
80 
81  //! Returns number of blocks owned by the prefetch_pool.
82  size_type size_prefetch() const { return p_pool->size(); }
83 
84  //! Resizes size of the pool.
85  //! \param new_size new size of the pool after the call
86  void resize_write(size_type new_size)
87  {
88  w_pool->resize(new_size);
89  }
90 
91  //! Resizes size of the pool.
92  //! \param new_size new size of the pool after the call
93  void resize_prefetch(size_type new_size)
94  {
95  p_pool->resize(new_size);
96  }
97 
98  // WRITE POOL METHODS
99 
100  //! Passes a block to the pool for writing.
101  //! \param block block to write. Ownership of the block goes to the pool.
102  //! \c block must be allocated dynamically with using \c new .
103  //! \param bid location, where to write
104  //! \warning \c block must be allocated dynamically with using \c new .
105  //! \return request object of the write operation
107  {
108  request_ptr result = w_pool->write(block, bid);
109 
110  // if there is a copy of this block in the prefetch pool,
111  // it is now a stale copy, so invalidate it and re-hint the block
112  if (p_pool->invalidate(bid))
113  p_pool->hint(bid, *w_pool);
114 
115  return result;
116  }
117 
118  //! Take out a block from the pool.
119  //! \return pointer to the block. Ownership of the block goes to the caller.
121  {
122  return w_pool->steal();
123  }
124 
125  //! Add block to write pool
126  void add(block_type*& block)
127  {
128  w_pool->add(block);
129  }
130 
131  // PREFETCH POOL METHODS
132 
133  //! Gives a hint for prefetching a block.
134  //! \param bid address of a block to be prefetched
135  //! \return \c true if there was a free block to do prefetch and prefetching
136  //! was scheduled, \c false otherwise
137  //! \note If there are no free blocks available (all blocks
138  //! are already in reading or read but not retrieved by user calling \c read
139  //! method) calling \c hint function has no effect
140  bool hint(bid_type bid)
141  {
142  return p_pool->hint(bid, *w_pool);
143  }
144 
145  //! Cancel a hint request in case the block is no longer desired.
147  {
148  return p_pool->invalidate(bid);
149  }
150 
151  /*!
152  * Reads block. If this block is cached block is not read but passed from
153  * the cache.
154  *
155  * \param block block object, where data to be read to. If block was cached
156  * \c block 's ownership goes to the pool and block from cache is returned
157  * in \c block value.
158  *
159  * \param bid address of the block
160  * \warning \c block parameter must be allocated dynamically using \c new .
161  * \return request pointer object of read operation
162  */
164  {
165  return p_pool->read(block, bid, *w_pool);
166  }
167 
168  //! Returns the request pointer for a hinted block, or an invalid nullptr
169  //! request in case it was not requested due to lack of prefetch buffers.
171  {
172  return p_pool->find(bid);
173  }
174 
175  //! Returns true if the blocks was hinted and the request is finished.
176  bool poll_hint(bid_type bid)
177  {
178  return p_pool->poll(bid);
179  }
180 
181  //! Add block to prefetch pool
182  void add_prefetch(block_type*& block)
183  {
184  p_pool->add(block);
185  }
186 
187  //! Take out a block from the prefetch pool, one unhinted free block must
188  //! be available.
189  //! \return pointer to the block. Ownership of the block goes to the caller.
191  {
192  return p_pool->steal();
193  }
194 
195  //! Checks if a block is in the hinted block set.
197  {
198  return p_pool->in_prefetching(bid);
199  }
200 
201  //! Returns the number of free prefetching blocks.
202  size_t free_size_prefetch() const
203  {
204  return p_pool->free_size();
205  }
206 
207  //! Returns the number of busy prefetching blocks.
208  size_t busy_size_prefetch() const
209  {
210  return p_pool->busy_size();
211  }
212 };
213 
214 //! \}
215 
216 } // namespace foxxll
217 
218 namespace std {
219 
220 template <class BlockType>
223 {
224  a.swap(b);
225 }
226 
227 } // namespace std
228 
229 #endif // !FOXXLL_MNG_READ_WRITE_POOL_HEADER
230 
231 /**************************************************************************/
Implements dynamically resizable prefetching pool.
~read_write_pool()
Waits for completion of all ongoing requests and frees memory.
bool invalidate(bid_type bid)
Cancel a hint request in case the block is no longer desired.
request_ptr read(block_type *&block, bid_type bid)
Reads block.
size_t size() const
Returns number of owned blocks.
request_ptr read(block_type *&block, bid_type bid)
Reads block.
request_ptr find(bid_type bid)
Implements dynamically resizable buffered writing pool.
Definition: write_pool.hpp:38
void resize(size_t new_size)
Definition: write_pool.hpp:177
write_pool_type * w_pool
bool invalidate(bid_type bid)
Cancel a hint request in case the block is no longer desired.
void add(block_type *&block)
Add a new block to prefetch pool, enlarges size of pool.
STL namespace.
void resize_prefetch(size_type new_size)
Implements dynamically resizable buffered writing and prefetched reading pool.
request_ptr write(block_type *&block, bid_type bid)
void add_prefetch(block_type *&block)
Add block to prefetch pool.
prefetch_pool_type * p_pool
void resize_write(size_type new_size)
read_write_pool(size_type init_size_prefetch=1, size_type init_size_write=1)
size_t resize(size_t new_size)
read_write_pool & operator=(const read_write_pool &)=delete
non-copyable: delete assignment operator
write_pool< block_type > write_pool_type
bool in_prefetching(bid_type bid)
Checks if a block is in the hinted block set.
FOXXLL library namespace
size_type size_write() const
Returns number of blocks owned by the write_pool.
size_t busy_size() const
Returns the number of busy prefetching blocks.
High-performance smart pointer used as a wrapping reference counting pointer.
size_t free_size_prefetch() const
Returns the number of free prefetching blocks.
size_t busy_size_prefetch() const
Returns the number of busy prefetching blocks.
prefetch_pool< block_type > prefetch_pool_type
size_type size_prefetch() const
Returns number of blocks owned by the prefetch_pool.
void swap(read_write_pool &obj)
void add(block_type *&block)
Definition: write_pool.hpp:227
bool poll(bid_type bid)
Returns true if the blocks was hinted and the request is finished.
typename block_type::bid_type bid_type
bool hint(bid_type bid)
size_t size() const
Returns number of owned blocks.
Definition: write_pool.hpp:123
bool poll_hint(bid_type bid)
Returns true if the blocks was hinted and the request is finished.
size_t free_size() const
Returns the number of free prefetching blocks.
bool in_prefetching(bid_type bid)
Checks if a block is in the hinted block set.
void add(block_type *&block)
Add block to write pool.
request_ptr write(block_type *&block, bid_type bid)
Definition: write_pool.hpp:131
bool hint(bid_type bid)
Gives a hint for prefetching a block, the block may or may not be read into a prefetch buffer...
block_type * steal()
Definition: write_pool.hpp:154
request_ptr find_hint(bid_type bid)