File: benchmark_pqueue.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (335 lines) | stat: -rw-r--r-- 10,823 bytes parent folder | download | duplicates (4)
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
/***************************************************************************
 *  tools/benchmark_pqueue.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de>
 *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
 *  Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

static const char* description =
    "Benchmark the priority queue implementation using a sequence of "
    "operations. The PQ contains pairs of 32- or 64-bit integers, or a "
    "24 byte struct. The operation sequence is either a simple fill/delete "
    "cycle or fill/intermixed inserts/deletes. Because the memory parameters "
    "of the PQ must be set a compile-time, the benchmark provides only "
    "three PQ sizes: for 256 MiB, 1 GiB and 8 GiB of RAM, with the maximum "
    "number of items set accordingly.";

#include <limits>
#include <iomanip>
#include <stxxl/priority_queue>
#include <stxxl/timer>
#include <stxxl/random>
#include <stxxl/cmdline>
#include <stxxl/bits/common/tuple.h>

using stxxl::uint32;
using stxxl::uint64;
using stxxl::internal_size_type;

#define MiB (1024 * 1024)
#define PRINTMOD (16 * MiB)

// *** Integer Pair Types

typedef stxxl::tuple<uint32, uint32> uint32_pair_type;

typedef stxxl::tuple<uint64, uint64> uint64_pair_type;

// *** Larger Structure Type

#define MY_TYPE_SIZE 24

struct my_type : public uint32_pair_type
{
    typedef uint32 key_type;

    char data[MY_TYPE_SIZE - sizeof(uint32_pair_type)];
    my_type() { }
    my_type(const key_type& k1, const key_type& k2)
        : uint32_pair_type(k1, k2)
    {
#if STXXL_WITH_VALGRIND
        memset(data, 0, sizeof(data));
#endif
    }

    static my_type max_value()
    {
        return my_type(std::numeric_limits<key_type>::max(),
                       std::numeric_limits<key_type>::max());
    }
};

template <typename ValueType>
struct my_cmp : public std::binary_function<ValueType, ValueType, bool>
{
    bool operator () (const ValueType& a, const ValueType& b) const
    {
        // PQ is a max priority queue, thus compare greater
        return a.first > b.first;
    }

    ValueType min_value() const
    {
        return ValueType::max_value();
    }
};

static inline void progress(const char* text, uint64 i, uint64 nelements)
{
    if ((i % PRINTMOD) == 0)
        STXXL_MSG(text << " " << i << " ("
                       << std::setprecision(5)
                       << ((double)i * 100.0 / (double)nelements) << " %)");
}

template <typename PQType>
void run_pqueue_insert_delete(uint64 nelements, internal_size_type mem_for_pools)
{
    typedef typename PQType::value_type ValueType;

    // construct priority queue
    PQType pq(mem_for_pools / 2, mem_for_pools / 2);

    pq.dump_sizes();

    STXXL_MSG("Internal memory consumption of the priority queue: " << pq.mem_cons() << " B");
    stxxl::stats_data stats_begin(*stxxl::stats::get_instance());

    {
        stxxl::scoped_print_timer timer("Filling PQ", nelements * sizeof(ValueType));

        for (stxxl::uint64 i = 0; i < nelements; i++)
        {
            progress("Inserting element", i, nelements);

            pq.push(ValueType((int)(nelements - i), 0));
        }
    }

    STXXL_CHECK(pq.size() == nelements);

    STXXL_MSG("Internal memory consumption of the priority queue: " << pq.mem_cons() << " B");

    pq.dump_sizes();

    std::cout << stxxl::stats_data(*stxxl::stats::get_instance()) - stats_begin;
    stats_begin = *stxxl::stats::get_instance();

    {
        stxxl::scoped_print_timer timer("Reading PQ", nelements * sizeof(ValueType));

        for (stxxl::uint64 i = 0; i < nelements; ++i)
        {
            STXXL_CHECK(!pq.empty());
            STXXL_CHECK(pq.top().first == i + 1);

            pq.pop();

            progress("Popped element", i, nelements);
        }
    }

    STXXL_MSG("Internal memory consumption of the priority queue: " << pq.mem_cons() << " B");
    std::cout << stxxl::stats_data(*stxxl::stats::get_instance()) - stats_begin;
}

template <typename PQType>
void run_pqueue_insert_intermixed(uint64 nelements, internal_size_type mem_for_pools)
{
    typedef typename PQType::value_type ValueType;

    // construct priority queue
    PQType pq(mem_for_pools / 2, mem_for_pools / 2);

    pq.dump_sizes();

    STXXL_MSG("Internal memory consumption of the priority queue: " << pq.mem_cons() << " B");
    stxxl::stats_data stats_begin(*stxxl::stats::get_instance());

    {
        stxxl::scoped_print_timer timer("Filling PQ", nelements * sizeof(ValueType));

        for (stxxl::uint64 i = 0; i < nelements; i++)
        {
            progress("Inserting element", i, nelements);

            pq.push(ValueType((int)(nelements - i), 0));
        }
    }

    STXXL_CHECK(pq.size() == nelements);

    STXXL_MSG("Internal memory consumption of the priority queue: " << pq.mem_cons() << " B");

    pq.dump_sizes();

    std::cout << stxxl::stats_data(*stxxl::stats::get_instance()) - stats_begin;
    stats_begin = *stxxl::stats::get_instance();

    stxxl::random_number32 rand;

    {
        stxxl::scoped_print_timer timer("Intermixed Insert/Delete", nelements * sizeof(ValueType));

        for (stxxl::uint64 i = 0; i < nelements; ++i)
        {
            int o = rand() % 3;
            if (o == 0)
            {
                pq.push(ValueType((int)(nelements - i), 0));
            }
            else
            {
                STXXL_CHECK(!pq.empty());
                pq.pop();
            }

            progress("Intermixed element", i, nelements);
        }
    }

    STXXL_MSG("Internal memory consumption of the priority queue: " << pq.mem_cons() << " B");

    pq.dump_sizes();

    std::cout << stxxl::stats_data(*stxxl::stats::get_instance()) - stats_begin;
}

template <typename ValueType,
          internal_size_type mib_for_queue, internal_size_type mib_for_pools,
          uint64 maxvolume>
int do_benchmark_pqueue(uint64 volume, int opseq)
{
    const internal_size_type mem_for_queue = mib_for_queue * MiB;
    const internal_size_type mem_for_pools = mib_for_pools * MiB;

    typedef typename stxxl::PRIORITY_QUEUE_GENERATOR<
            ValueType, my_cmp<ValueType>,
            mem_for_queue,
            maxvolume* MiB / sizeof(ValueType)> gen;

    typedef typename gen::result pq_type;

    STXXL_MSG("Given PQ parameters: " << mib_for_queue << " MiB for queue, "
                                      << mib_for_pools << " MiB for pools, " << maxvolume << " GiB maximum volume.");

    STXXL_MSG("Selected PQ parameters:");
    STXXL_MSG("element size: " << sizeof(ValueType));
    STXXL_MSG("block size: " << pq_type::BlockSize);
    STXXL_MSG("insertion buffer size (N): " << pq_type::N << " items ("
                                            << pq_type::N * sizeof(ValueType) << " B)");
    STXXL_MSG("delete buffer size: " << pq_type::delete_buffer_size);
    STXXL_MSG("maximal arity for internal mergers (AI): " << pq_type::IntKMAX);
    STXXL_MSG("maximal arity for external mergers (AE): " << pq_type::ExtKMAX);
    STXXL_MSG("internal groups: " << pq_type::num_int_groups);
    STXXL_MSG("external groups: " << pq_type::num_ext_groups);
    STXXL_MSG("X : " << gen::X);

    if (volume == 0) volume = 2 * (mem_for_queue + mem_for_pools);

    stxxl::uint64 nelements = volume / sizeof(ValueType);

    STXXL_MSG("Number of elements: " << nelements);

    if (opseq == 0)
    {
        run_pqueue_insert_delete<pq_type>(nelements, mem_for_pools);
        run_pqueue_insert_intermixed<pq_type>(nelements, mem_for_pools);
    }
    else if (opseq == 1)
        run_pqueue_insert_delete<pq_type>(nelements, mem_for_pools);
    else if (opseq == 2)
        run_pqueue_insert_intermixed<pq_type>(nelements, mem_for_pools);
    else
        STXXL_ERRMSG("Invalid operation sequence.");

    return 1;
}

template <typename ValueType>
int do_benchmark_pqueue_config(unsigned pqconfig, uint64 size, unsigned opseq)
{
    if (pqconfig == 0)
    {
        do_benchmark_pqueue_config<ValueType>(1, size, opseq);
        do_benchmark_pqueue_config<ValueType>(2, size, opseq);
        do_benchmark_pqueue_config<ValueType>(3, size, opseq);
        return 1;
    }
    else if (pqconfig == 1)
        return do_benchmark_pqueue<ValueType, 128, 128, 16>(size, opseq);
    else if (pqconfig == 2)
        return do_benchmark_pqueue<ValueType, 512, 512, 64>(size, opseq);
#if __x86_64__ || __LP64__ || (__WORDSIZE == 64)
    else if (pqconfig == 3)
        return do_benchmark_pqueue<ValueType, 4096, 4096, 512>(size, opseq);
#endif
    else
        return 0;
}

int do_benchmark_pqueue_type(unsigned type, unsigned pqconfig, uint64 size, unsigned opseq)
{
    if (type == 0)
    {
        do_benchmark_pqueue_type(1, pqconfig, size, opseq);
        do_benchmark_pqueue_type(2, pqconfig, size, opseq);
        do_benchmark_pqueue_type(3, pqconfig, size, opseq);
        return 1;
    }
    else if (type == 1)
        return do_benchmark_pqueue_config<uint32_pair_type>(pqconfig, size, opseq);
    else if (type == 2)
        return do_benchmark_pqueue_config<uint64_pair_type>(pqconfig, size, opseq);
    else if (type == 3)
        return do_benchmark_pqueue_config<my_type>(pqconfig, size, opseq);
    else
        return 0;
}

int benchmark_pqueue(int argc, char* argv[])
{
    // parse command line
    stxxl::cmdline_parser cp;

    cp.set_description(description);

    uint64 size = 0;
    cp.add_opt_param_bytes("size", "Amount of data to insert (e.g. 1GiB)", size);

    unsigned type = 2;
    cp.add_uint('t', "type", "Value type of tested priority queue:\n 1 = pair of uint32,\n 2 = pair of uint64 (default),\n 3 = 24 byte struct\n 0 = all of the above", type);

    unsigned pqconfig = 2;
    cp.add_uint('p', "pq",
                "Priority queue configuration to test:\n"
                "1 = small (256 MiB RAM, 4 GiB elements)\n"
                "2 = medium (1 GiB RAM, 16 GiB elements) (default)\n"
#if __x86_64__ || __LP64__ || (__WORDSIZE == 64)
                "3 = big (8 GiB RAM, 64 GiB elements)\n"
#endif
                "0 = all of the above", pqconfig);

    unsigned opseq = 1;
    cp.add_uint('o', "opseq", "Operation sequence to perform:\n 1 = insert all, delete all (default)\n 2 = insert all, intermixed insert/delete\n 0 = all of the above", opseq);

    if (!cp.process(argc, argv))
        return -1;

    stxxl::config::get_instance();

    if (!do_benchmark_pqueue_type(type, pqconfig, size, opseq))
    {
        STXXL_ERRMSG("Invalid (type,pqconfig) combination.");
    }

    return 0;
}