14 #ifndef STXXL_MNG_PREFETCH_POOL_HEADER
15 #define STXXL_MNG_PREFETCH_POOL_HEADER
28 template <
class BlockType>
33 typedef typename block_type::bid_type
bid_type;
40 size_t result = size_t(bid.storage) +
41 size_t(bid.offset & 0xffffffff) +
42 size_t(bid.offset >> 32);
48 return (a.storage < b.storage) || (a.storage == b.storage && a.offset < b.offset);
75 : free_blocks_size(init_size)
78 for ( ; i < init_size; ++i)
92 while (!free_blocks.empty())
94 delete free_blocks.back();
95 free_blocks.pop_back();
101 for ( ; i2 != busy_blocks.end(); ++i2)
103 i2->second.second->wait();
104 delete i2->second.first;
114 return free_blocks_size + busy_blocks.size();
120 return free_blocks_size;
126 return busy_blocks.size();
132 free_blocks.push_back(block);
145 free_blocks.pop_back();
165 if (in_prefetching(bid)) {
166 STXXL_VERBOSE2(
"prefetch_pool::hint2 bid=" << bid <<
" was already cached");
170 if (free_blocks_size)
174 free_blocks.pop_back();
175 STXXL_VERBOSE2(
"prefetch_pool::hint bid=" << bid <<
" => prefetching");
180 STXXL_VERBOSE2(
"prefetch_pool::hint bid=" << bid <<
" => no free blocks for prefetching");
200 if (in_prefetching(bid)) {
201 STXXL_VERBOSE2(
"prefetch_pool::hint2 bid=" << bid <<
" was already cached");
205 if (free_blocks_size)
209 free_blocks.pop_back();
213 STXXL_VERBOSE1(
"prefetch_pool::hint2 bid=" << bid <<
" was in write cache at " << wp_request.first);
214 assert(wp_request.first != 0);
216 busy_blocks[bid] = wp_request;
219 STXXL_VERBOSE2(
"prefetch_pool::hint2 bid=" << bid <<
" => prefetching");
224 STXXL_VERBOSE2(
"prefetch_pool::hint2 bid=" << bid <<
" => no free blocks for prefetching");
232 if (cache_el == busy_blocks.end())
237 if (cache_el->second.second->get_type() == request::READ)
238 cache_el->second.second->cancel();
240 cache_el->second.second->wait();
242 free_blocks.push_back(cache_el->second.first);
243 busy_blocks.erase(cache_el);
250 return (busy_blocks.find(bid) != busy_blocks.end());
259 if (cache_el == busy_blocks.end())
262 return cache_el->second.second;
269 return req.
valid() ? req->
poll() :
false;
289 if (cache_el == busy_blocks.end())
292 STXXL_VERBOSE1(
"prefetch_pool::read bid=" << bid <<
" => no copy in cache, retrieving to " << block);
293 return block->read(bid);
297 STXXL_VERBOSE1(
"prefetch_pool::read bid=" << bid <<
" => copy in cache exists");
299 free_blocks.push_back(block);
300 block = cache_el->second.first;
302 busy_blocks.erase(cache_el);
310 if (cache_el != busy_blocks.end())
313 STXXL_VERBOSE1(
"prefetch_pool::read bid=" << bid <<
" => copy in cache exists");
315 free_blocks.push_back(block);
316 block = cache_el->second.first;
318 busy_blocks.erase(cache_el);
326 STXXL_VERBOSE1(
"prefetch_pool::read bid=" << bid <<
" was in write cache at " << wp_request.first);
327 assert(wp_request.first != 0);
329 block = wp_request.first;
330 return wp_request.second;
334 STXXL_VERBOSE1(
"prefetch_pool::read bid=" << bid <<
" => no copy in cache, retrieving to " << block);
335 return block->read(bid);
349 free_blocks_size += diff;
356 while (diff < 0 && free_blocks_size > 0)
360 delete free_blocks.back();
361 free_blocks.pop_back();
373 template <
class BlockType>
382 #endif // !STXXL_MNG_PREFETCH_POOL_HEADER
bool has_request(bid_type bid)
hash_map_type busy_blocks
blocks that are in reading or already read but not retrieved by user
bool hint(bid_type bid, write_pool< block_type > &w_pool)
Gives a hint for prefetching a block, the block may or may not be read into a prefetch buffer...
request_ptr read(block_type *&block, bid_type bid, write_pool< block_type > &w_pool)
void swap(prefetch_pool &obj)
bool in_prefetching(bid_type bid)
Checks if a block is in the hinted block set.
void add(block_type *&block)
std::list< block_type * > free_blocks
contains free prefetch blocks
request_ptr find(bid_type bid)
Returns the request pointer for a hinted block, or an invalid NULL request in case it was not request...
void add(block_type *&block)
Add a new block to prefetch pool, enlarges size of pool.
block_type::bid_type bid_type
bool invalidate(bid_type bid)
Cancel a hint request in case the block is no longer desired.
virtual ~prefetch_pool()
Waits for completion of all ongoing read requests and frees memory.
hash_map_type::iterator busy_blocks_iterator
bool hint(bid_type bid)
Gives a hint for prefetching a block, the block may or may not be read into a prefetch buffer...
#define STXXL_VERBOSE2(x)
bool valid() const
test for a non-NULL pointer
std::pair< block_type *, request_ptr > steal_request(bid_type bid)
#define STXXL_CHECK(condition)
STXXL_CHECK is an assertion macro for unit tests, which contrarily to assert() also works in release ...
Implements dynamically resizable buffered writing pool.
unsigned_type free_blocks_size
count number of free blocks, since traversing the std::list is slow.
Implements dynamically resizable prefetching pool.
counting_ptr< request > request_ptr
A reference counting pointer for request.
choose_int_types< my_pointer_size >::int_type int_type
#define STXXL_BEGIN_NAMESPACE
#define STXXL_VERBOSE1(x)
unsigned_type free_size() const
Returns the number of free prefetching blocks.
std::list< block_type * >::iterator free_blocks_iterator
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
unsigned_type size() const
Returns number of owned blocks.
unsigned_type busy_size() const
Returns the number of busy prefetching blocks.
block_type * steal()
Take out a block from the pool, one unhinted free block must be available.
unsigned_type resize(unsigned_type new_size)
Resizes size of the pool.
bool poll(bid_type bid)
Returns true if the blocks was hinted and the request is finished.
virtual bool poll()=0
Polls the status of the request.
std::pair< block_type *, request_ptr > busy_entry
request_ptr read(block_type *&block, bid_type bid)
Reads block.
ExtIterator find(ExtIterator begin, ExtIterator end, const EqualityComparable &value, int_type nbuffers=0)
External equivalent of std::find, see stxxl::find.
prefetch_pool(unsigned_type init_size=1)
Constructs pool.
#define STXXL_END_NAMESPACE
compat_hash_map< bid_type, busy_entry, bid_hash >::result hash_map_type