[go: up one dir, main page]

Menu

[8bf46b]: / algo / random_shuffle.h  Maximize  Restore  History

Download this file

414 lines (326 with data), 12.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#ifndef STXXL_RANDOM_SHUFFLE_HEADER
#define STXXL_RANDOM_SHUFFLE_HEADER
/***************************************************************************
* random_shuffle.h
*
* Tue May 29 14:48:04 2007
* Copyright 2007 Manuel Krings, Markus Westphal
*
*
****************************************************************************/
// TODO: improve main memory consumption in recursion
// (free stacks buffers)
#include "../stream/stream.h"
#include "../algo/scan.h"
#include "../containers/stack.h"
__STXXL_BEGIN_NAMESPACE
namespace random_shuffle_local
{
// efficiently writes data into an stxxl::vector with overlapping of I/O and
// computation
template <class ExtIterator>
class write_vector
{
write_vector(); // forbidden
typedef typename ExtIterator::size_type size_type;
typedef typename ExtIterator::value_type value_type;
typedef typename ExtIterator::block_type block_type;
typedef typename ExtIterator::const_iterator ConstExtIterator;
typedef stxxl::buf_ostream<block_type,typename ExtIterator::bids_container_iterator> buf_ostream_type;
ExtIterator it;
unsigned_type nbuffers;
buf_ostream_type * outstream;
public:
write_vector( ExtIterator begin,
unsigned nbuffers_=2 // buffers to use for overlapping (>=2 recommended)
): it(begin),nbuffers(nbuffers_)
{
outstream = new buf_ostream_type(it.bid(),nbuffers);
}
value_type & operator * ()
{
if(it.block_offset() ==0 ) it.touch(); // tells the vector that the block was modified
return **outstream;
}
write_vector & operator ++()
{
++it;
++(*outstream);
return *this;
}
void flush()
{
ConstExtIterator const_out = it;
while(const_out.block_offset())
{
**outstream = * const_out; // might cause I/Os for loading the page that
++const_out; // contains data beyond out
++(*outstream);
}
it.flush();
delete outstream;
outstream = NULL;
}
virtual ~write_vector()
{
if(outstream) flush();
}
};
};
//! \addtogroup stlalgo
//! \{
template < typename Tp_, typename AllocStrategy_, typename SzTp_,typename DiffTp_,
unsigned BlockSize_, typename PgTp_, unsigned PageSize_, typename RandomNumberGenerator_>
void random_shuffle( stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> first,
stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> beyond,
RandomNumberGenerator_ & rand,
unsigned_type M);
//! \brief External equivalent of std::random_shuffle
//! \param first begin of the range to shuffle
//! \param beyond end of the range to shuffle
//! \param rand random number generator object (functor)
//! \param M number of bytes for internal use
//! \param RC paralel disk allocation strategy
//!
//! - BlockSize_ size of the block to use for external memory data structures
//! - PageSize_ page size in blocks to use for external memory data structures
template < typename ExtIterator_,
typename RandomNumberGenerator_,
unsigned BlockSize_,
unsigned PageSize_,
typename AllocStrategy_>
void random_shuffle( ExtIterator_ first,
ExtIterator_ beyond,
RandomNumberGenerator_ & rand,
unsigned_type M,
AllocStrategy_ AS = stxxl::RC())
{
typedef typename ExtIterator_::value_type value_type;
typedef typename stxxl::STACK_GENERATOR<value_type, stxxl::external,
stxxl::grow_shrink2, PageSize_,
BlockSize_,void,0,AllocStrategy_>::result stack_type;
typedef typename stack_type::block_type block_type;
STXXL_VERBOSE1("random_shuffle: Plain Version")
stxxl::int64 n = beyond - first; // the number of input elements
// make sure we have at least 6 blocks + 1 page
if(M < 6*BlockSize_+PageSize_*BlockSize_)
M = 6*BlockSize_+PageSize_*BlockSize_;
int_type k = M/(3*BlockSize_); // number of buckets
stxxl::int64 i,j,size = 0;
value_type * temp_array;
typedef typename stxxl::VECTOR_GENERATOR<value_type,
PageSize_, 4, BlockSize_, AllocStrategy_>::result temp_vector_type;
temp_vector_type * temp_vector;
stxxl::prefetch_pool<block_type> p_pool(0); // no read buffers
STXXL_VERBOSE1("random_shuffle: "<<M/BlockSize_-k<<" write buffers for "<<k<<" buckets")
stxxl::write_pool<block_type> w_pool(M/BlockSize_-k); // M/B-k write buffers
stack_type **buckets;
// create and put buckets into container
buckets = new stack_type * [k];
for (j = 0; j < k; j++)
buckets[j] = new stack_type(p_pool, w_pool, 0);
///// Reading input /////////////////////
typedef typename stream::streamify_traits<ExtIterator_>::stream_type input_stream;
input_stream in = stxxl::stream::streamify(first,beyond);
// distribute input into random buckets
int_type random_bucket = 0;
for(i=0; i<n; ++i){
random_bucket = rand(k);
buckets[random_bucket]->push(*in); // reading the current input element
++in; // go to the next input element
}
///// Processing //////////////////////
// resize buffers
w_pool.resize(0);
p_pool.resize(PageSize_);
// Set prefetch aggr to PageSize_
for (int_type j=0; j<k; j++){
buckets[j]->set_prefetch_aggr(PageSize_);
}
unsigned_type space_left = M - k*BlockSize_ -
PageSize_*BlockSize_;// remaining int space
ExtIterator_ Writer = first;
ExtIterator_ it = first;
for (i=0;i<k;i++){
STXXL_VERBOSE1("random_shuffle: bucket no " << i << " contains " << buckets[i]->size() << " elements");
}
// shuffle each bucket
for (i=0;i<k;i++){
size = buckets[i]->size();
// does the bucket fit into memory?
if (size * sizeof(value_type) < space_left){
STXXL_VERBOSE1("random_shuffle: no recursion");
// copy bucket into temp. array
temp_array = new value_type[size];
for (j=0; j<size; j++){
temp_array[j] = buckets[i]->top();
buckets[i]->pop();
}
// shuffle
std::random_shuffle(temp_array, temp_array+size,rand);
// write back
for (j=0; j<size; j++){
*Writer = temp_array[j];
++Writer;
}
// free memory
delete[] temp_array;
}
else {
STXXL_VERBOSE1("random_shuffle: recursion");
// copy bucket into temp. stxxl::vector
temp_vector = new temp_vector_type(size);
for (j = 0; j < size; j++){
(*temp_vector)[j]=buckets[i]->top();
buckets[i]->pop();
}
p_pool.resize(0);
space_left += PageSize_*BlockSize_;
STXXL_VERBOSE1("random_shuffle: Space left: " << space_left);
// recursive shuffle
stxxl::random_shuffle(temp_vector->begin(),
temp_vector->end(), rand, space_left);
p_pool.resize(PageSize_);
// write back
for (j=0; j<size; j++){
*Writer = (*temp_vector)[j];
++Writer;
}
// free memory
delete temp_vector;
}
}
// free buckets
for (int j = 0; j < k; j++)
delete buckets[j];
delete [] buckets;
}
//! \brief External equivalent of std::random_shuffle (specialization for stxxl::vector)
//! \param first begin of the range to shuffle
//! \param beyond end of the range to shuffle
//! \param rand random number generator object (functor)
//! \param M number of bytes for internal use
template < typename Tp_, typename AllocStrategy_, typename SzTp_,typename DiffTp_,
unsigned BlockSize_, typename PgTp_, unsigned PageSize_, typename RandomNumberGenerator_>
void random_shuffle( stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> first,
stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> beyond,
RandomNumberGenerator_ & rand,
unsigned_type M)
{
typedef stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> ExtIterator_;
typedef typename ExtIterator_::value_type value_type;
typedef typename stxxl::STACK_GENERATOR<value_type, stxxl::external,
stxxl::grow_shrink2, PageSize_, BlockSize_>::result stack_type;
typedef typename stack_type::block_type block_type;
STXXL_VERBOSE1("random_shuffle: Vector Version")
// make sure we have at least 6 blocks + 1 page
if(M < 6*BlockSize_+PageSize_*BlockSize_)
M = 6*BlockSize_+PageSize_*BlockSize_;
stxxl::int64 n = beyond - first; // the number of input elements
int_type k = M/(3*BlockSize_); // number of buckets
stxxl::int64 i,j,size = 0;
value_type * temp_array;
typedef typename stxxl::VECTOR_GENERATOR<value_type,
PageSize_, 4, BlockSize_, AllocStrategy_>::result temp_vector_type;
temp_vector_type * temp_vector;
stxxl::prefetch_pool<block_type> p_pool(0); // no read buffers
stxxl::write_pool<block_type> w_pool(M/BlockSize_-k); // M/B-k write buffers
stack_type **buckets;
// create and put buckets into container
buckets = new stack_type * [k];
for (j = 0; j < k; j++)
buckets[j] = new stack_type(p_pool, w_pool, 0);
///// Reading input /////////////////////
typedef typename stream::streamify_traits<ExtIterator_>::stream_type input_stream;
input_stream in = stxxl::stream::streamify(first,beyond);
// distribute input into random buckets
int_type random_bucket = 0;
for(i=0; i<n; ++i){
random_bucket = rand(k);
buckets[random_bucket]->push(*in); // reading the current input element
++in; // go to the next input element
}
///// Processing //////////////////////
// resize buffers
w_pool.resize(0);
p_pool.resize(PageSize_);
// Set prefetch aggr to PageSize_
for (int_type j=0; j<k; j++){
buckets[j]->set_prefetch_aggr(PageSize_);
}
unsigned_type space_left = M - k*BlockSize_ -
PageSize_*BlockSize_;// remaining int space
random_shuffle_local::write_vector<ExtIterator_>
Writer(first,2*PageSize_);
ExtIterator_ it = first;
for (i=0;i<k;i++){
STXXL_VERBOSE1("random_shuffle: bucket no " << i << " contains " << buckets[i]->size() << " elements");
}
// shuffle each bucket
for (i=0;i<k;i++){
size = buckets[i]->size();
// does the bucket fit into memory?
if (size * sizeof(value_type) < space_left){
STXXL_VERBOSE1("random_shuffle: no recursion");
// copy bucket into temp. array
temp_array = new value_type[size];
for (j=0; j<size; j++){
temp_array[j] = buckets[i]->top();
buckets[i]->pop();
}
// shuffle
std::random_shuffle(temp_array, temp_array+size,rand);
// write back
for (j=0; j<size; j++){
*Writer = temp_array[j];
++Writer;
}
// free memory
delete[] temp_array;
}
else {
STXXL_VERBOSE1("random_shuffle: recursion");
// copy bucket into temp. stxxl::vector
temp_vector = new temp_vector_type(size);
for (j = 0; j < size; j++){
(*temp_vector)[j]=buckets[i]->top();
buckets[i]->pop();
}
p_pool.resize(0);
space_left += PageSize_*BlockSize_;
STXXL_VERBOSE1("random_shuffle: Space left: " << space_left);
// recursive shuffle
stxxl::random_shuffle(temp_vector->begin(),
temp_vector->end(), rand, space_left);
p_pool.resize(PageSize_);
// write back
for (j=0; j<size; j++){
*Writer = (*temp_vector)[j];
++Writer;
}
// free memory
delete temp_vector;
}
}
// free buckets
for (int j = 0; j < k; j++)
delete buckets[j];
delete [] buckets;
Writer.flush(); // flush buffers
}
//! \brief External equivalent of std::random_shuffle (specialization for stxxl::vector)
//! \param first begin of the range to shuffle
//! \param beyond end of the range to shuffle
//! \param M number of bytes for internal use
template < typename Tp_, typename AllocStrategy_, typename SzTp_,typename DiffTp_,
unsigned BlockSize_, typename PgTp_, unsigned PageSize_>
void random_shuffle( stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> first,
stxxl::vector_iterator<Tp_,AllocStrategy_,SzTp_,DiffTp_,BlockSize_,PgTp_,PageSize_> beyond,
unsigned_type M)
{
stxxl::random_number<> rand;
stxxl::random_shuffle(first,beyond,rand,M);
}
//! \}
__STXXL_END_NAMESPACE
#endif