[go: up one dir, main page]

Menu

[dc7c28]: / algo / test_sort.cpp  Maximize  Restore  History

Download this file

212 lines (178 with data), 6.4 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
/***************************************************************************
* test_ksort.cpp
*
* Fri Oct 4 19:41:25 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
//#define COUNT_WAIT_TIME
//#define PLAY_WITH_OPT_PREF
int n_prefetch_buffers;
int n_opt_prefetch_buffers;
#include "stxxl/mng"
#include "stxxl/sort"
#include "stxxl/scan"
#include "stxxl/vector"
#include "stxxl/random"
#ifndef RECORD_SIZE
#define RECORD_SIZE 4
#endif
struct my_type
{
typedef unsigned key_type;
key_type _key;
// char _data[RECORD_SIZE - sizeof(key_type)];
my_type() { };
my_type(key_type __key) : _key(__key) { };
};
std::ostream & operator << (std::ostream & o, const my_type obj)
{
o << obj._key;
return o;
}
bool operator < (const my_type & a, const my_type & b)
{
return a._key < b._key;
}
bool operator != (const my_type & a, const my_type & b)
{
return a._key != b._key;
}
struct Cmp
{
bool operator () (const my_type & a, const my_type & b) const
{
return a._key < b._key;
}
static my_type min_value()
{
return my_type(0);
}
static my_type max_value()
{
return my_type(0xffffffff);
}
};
#define MB (1024 * 1024)
struct zero
{
unsigned operator () ()
{
return 0;
}
};
template <typename alloc_strategy_type, unsigned block_size>
void test(stxxl::int64 records_to_sort, unsigned memory_to_use, unsigned n_prefetch_blocks)
{
typedef stxxl::vector < my_type, 2, stxxl::lru_pager < 8 >, block_size, alloc_strategy_type > vector_type;
vector_type v(records_to_sort);
unsigned ndisks = stxxl::config::get_instance()->disks_number();
STXXL_MSG("Sorting " << records_to_sort << " records of size " << sizeof(my_type) );
STXXL_MSG("Total volume " << (records_to_sort * sizeof(my_type)) / MB << " MB");
STXXL_MSG("Using " << memory_to_use / MB << " MB");
STXXL_MSG("Using " << ndisks << " disks");
STXXL_MSG("Using " << alloc_strategy_type::name() << " allocation strategy ");
STXXL_MSG("Block size " << vector_type::block_type::raw_size / 1024 << " KB");
STXXL_MSG("Prefetch buffers " << n_prefetch_blocks << " = " << (double (n_prefetch_blocks) / ndisks) << "*D");
n_prefetch_buffers = n_prefetch_blocks;
STXXL_MSG("OPT Prefetch buffers " << n_opt_prefetch_buffers << " = " << (double (n_opt_prefetch_buffers) / ndisks) << "*D");
const int n_write_blocks = STXXL_MAX( 2 * ndisks,
int (memory_to_use / vector_type::block_type::raw_size) -
int (2 * (records_to_sort * sizeof(my_type)) / memory_to_use) - n_prefetch_blocks );
STXXL_MSG("Write buffers " << (n_write_blocks) << " = " << (double (n_write_blocks) / ndisks) << "*D");
STXXL_MSG("Seed " << stxxl::ran32State );
STXXL_MSG("Filling vector...");
stxxl::generate(v.begin(), v.end(), stxxl::random_number32(), 32);
stxxl::wait_time_counter = 0.0;
STXXL_MSG("Sorting vector...");
reset_io_wait_time();
stxxl::sort(v.begin(), v.end(), Cmp(), memory_to_use);
// STXXL_MSG("Checking order...");
// STXXL_MSG( ((stxxl::is_sorted(v.begin(),v.end()))?"OK":"WRONG" ));
}
template <unsigned block_size>
void test_all_strategies(
stxxl::int64 records_to_sort,
unsigned memory_to_use,
unsigned n_prefetch_blocks,
int strategy)
{
switch (strategy)
{
case 0:
test<stxxl::striping, block_size>(records_to_sort, memory_to_use, n_prefetch_blocks);
break;
case 1:
test<stxxl::SR, block_size>(records_to_sort, memory_to_use, n_prefetch_blocks);
break;
case 2:
test<stxxl::FR, block_size>(records_to_sort, memory_to_use, n_prefetch_blocks);
break;
case 3:
test<stxxl::RC, block_size>(records_to_sort, memory_to_use, n_prefetch_blocks);
break;
default:
STXXL_ERRMSG("Unknown allocation strategy: " << strategy << ", aborting");
abort();
}
}
int main(int argc, char * argv[])
{
if (argc < 8)
{
STXXL_ERRMSG("Usage: " << argv[0] <<
" <MB to sort> <MB to use> <alloc_strategy> <blk_size> <prefetch_buffers> <opt_pref_b> <seed>");
return -1;
}
#ifdef BOOST_MSVC
stxxl::int64 n_records = (_atoi64(argv[1]) * MB) / sizeof(my_type);
#else
stxxl::int64 n_records = (atoll(argv[1]) * MB) / sizeof(my_type);
#endif
int sort_mem = atoi(argv[2]) * MB;
int strategy = atoi(argv[3]);
int block_size = atoi(argv[4]);
int n_prefetch_buffers = atoi(argv[5]);
n_opt_prefetch_buffers = atoi(argv[6]);
stxxl::ran32State = strtoul(argv[7], NULL, 10 );
switch (block_size)
{
case 0:
test_all_strategies < (128 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 1:
test_all_strategies < (256 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 2:
test_all_strategies < (512 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 3:
test_all_strategies < (1024 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 4:
test_all_strategies < (2 * 1024 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 5:
test_all_strategies < (4 * 1024 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 6:
test_all_strategies < (8 * 1024 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 7:
test_all_strategies < (16 * 1024 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 8:
test_all_strategies < (640 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 9:
test_all_strategies < (768 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
case 10:
test_all_strategies < (896 * 1024) > (n_records, sort_mem, n_prefetch_buffers, strategy);
break;
default:
STXXL_ERRMSG("Unknown block size: " << block_size << ", aborting");
abort();
}
return 0;
}