15 #ifndef STXXL_MNG_BLOCK_PREFETCHER_HEADER
16 #define STXXL_MNG_BLOCK_PREFETCHER_HEADER
40 : switch_(switch__), on_compl(on_compl)
54 template <
typename block_type,
typename b
id_iterator_type>
57 typedef typename block_type::bid_type
bid_type;
86 completed[iblock].wait_for_on();
88 STXXL_VERBOSE1(
"block_prefetcher: finished waiting block " << iblock);
89 int_type ibuffer = pref_buffer[iblock];
91 assert(ibuffer >= 0 && ibuffer < nreadblocks);
92 return (read_buffers + ibuffer);
104 bid_iterator_type _cons_begin,
105 bid_iterator_type _cons_end,
110 consume_seq_begin(_cons_begin),
111 consume_seq_end(_cons_end),
112 seq_length(_cons_end - _cons_begin),
113 prefetch_seq(_pref_seq),
116 nreadblocks(nextread),
117 do_after_fetch(do_after_fetch)
120 STXXL_VERBOSE1(
"block_prefetcher: _prefetch_buf_size=" << _prefetch_buf_size);
121 assert(seq_length > 0);
122 assert(_prefetch_buf_size > 0);
124 read_buffers =
new block_type[nreadblocks];
126 read_bids =
new bid_type[nreadblocks];
127 pref_buffer =
new int_type[seq_length];
129 std::fill(pref_buffer, pref_buffer + seq_length, -1);
133 for (i = 0; i < nreadblocks; ++i)
135 assert(prefetch_seq[i] <
int_type(seq_length));
136 assert(prefetch_seq[i] >= 0);
137 read_bids[i] = *(consume_seq_begin + prefetch_seq[i]);
139 " prefetch_seq[" << i <<
"]=" << prefetch_seq[i] <<
140 " @ " << &read_buffers[i] <<
141 " @ " << read_bids[i]);
142 read_reqs[i] = read_buffers[i].read(
145 pref_buffer[prefetch_seq[i]] = i;
153 return wait(nextconsume++);
162 int_type ibuffer = buffer - read_buffers;
163 STXXL_VERBOSE1(
"block_prefetcher: buffer " << ibuffer <<
" consumed");
164 if (read_reqs[ibuffer].valid())
165 read_reqs[ibuffer]->wait();
167 read_reqs[ibuffer] = NULL;
169 if (nextread < seq_length)
171 assert(ibuffer >= 0 && ibuffer < nreadblocks);
172 int_type next_2_prefetch = prefetch_seq[nextread++];
173 STXXL_VERBOSE1(
"block_prefetcher: prefetching block " << next_2_prefetch);
175 assert((next_2_prefetch <
int_type(seq_length)) && (next_2_prefetch >= 0));
176 assert(!completed[next_2_prefetch].is_on());
178 pref_buffer[next_2_prefetch] = ibuffer;
179 read_bids[ibuffer] = *(consume_seq_begin + next_2_prefetch);
180 read_reqs[ibuffer] = read_buffers[ibuffer].read(
186 if (nextconsume >= seq_length)
190 buffer = wait(nextconsume++);
199 return nextconsume >= seq_length;
211 for (
int_type i = 0; i < nreadblocks; ++i)
212 if (read_reqs[i].valid())
213 read_reqs[i]->wait();
219 delete[] pref_buffer;
220 delete[] read_buffers;
228 #endif // !STXXL_MNG_BLOCK_PREFETCHER_HEADER
const Tp & STXXL_MIN(const Tp &a, const Tp &b)
Default completion handler class.
block_type::bid_type bid_type
Completion handler class (Loki-style).
completion_handler do_after_fetch
bool empty() const
No more consumable blocks available, but can't delete the prefetcher, because not all blocks may have...
block_type * pull_block()
Pulls next unconsumed block from the consumption sequence.
block_type * wait(int_type iblock)
completion_handler on_compl
bool block_consumed(block_type *&buffer)
Exchanges buffers between prefetcher and application.
bid_iterator_type consume_seq_begin
const int_type nreadblocks
Encapsulates asynchronous prefetching engine.
choose_int_types< my_pointer_size >::int_type int_type
unsigned_type pos() const
Index of the next element in the consume sequence.
unsigned_type nextconsume
~block_prefetcher()
Frees used memory.
#define STXXL_BEGIN_NAMESPACE
#define STXXL_VERBOSE1(x)
block_prefetcher(bid_iterator_type _cons_begin, bid_iterator_type _cons_end, int_type *_pref_seq, int_type _prefetch_buf_size, completion_handler do_after_fetch=default_completion_handler())
Constructs an object and immediately starts prefetching.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Request with basic properties like file and offset.
block_type * read_buffers
#define STXXL_END_NAMESPACE
bid_iterator_type consume_seq_end
set_switch_handler(onoff_switch &switch__, const completion_handler &on_compl)