File: benchmark_rocrand_generate.cpp

package info (click to toggle)
rocrand 6.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 102,860 kB
  • sloc: cpp: 203,022; f90: 2,500; python: 1,417; sh: 359; xml: 212; asm: 90; makefile: 50
file content (522 lines) | stat: -rw-r--r-- 19,770 bytes parent folder | download
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (c) 2017-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

#include "cmdparser.hpp"

#include <hip/hip_runtime.h>
#include <rocrand/rocrand.h>

#define HIP_CHECK(condition)                                                           \
    {                                                                                  \
        hipError_t error = condition;                                                  \
        if(error != hipSuccess)                                                        \
        {                                                                              \
            std::cout << "HIP error: " << error << " line: " << __LINE__ << std::endl; \
            exit(error);                                                               \
        }                                                                              \
    }

#define ROCRAND_CHECK(condition)                                                             \
    {                                                                                        \
        rocrand_status _status = condition;                                                  \
        if(_status != ROCRAND_STATUS_SUCCESS)                                                \
        {                                                                                    \
            std::cout << "ROCRAND error: " << _status << " line: " << __LINE__ << std::endl; \
            exit(_status);                                                                   \
        }                                                                                    \
    }

#ifndef DEFAULT_RAND_N
const size_t DEFAULT_RAND_N = 1024 * 1024 * 128;
#endif

typedef rocrand_rng_type rng_type_t;

template<typename T>
using generate_func_type = std::function<rocrand_status(rocrand_generator, T*, size_t)>;

template<typename T>
void run_benchmark(const cli::Parser&    parser,
                   const rng_type_t      rng_type,
                   hipStream_t           stream,
                   generate_func_type<T> generate_func,
                   const std::string&    distribution,
                   const std::string&    engine,
                   const double          lambda = 0.f)
{
    const size_t      size0      = parser.get<size_t>("size");
    const size_t      trials     = parser.get<size_t>("trials");
    const size_t      dimensions = parser.get<size_t>("dimensions");
    const size_t      offset     = parser.get<size_t>("offset");
    const size_t      size       = (size0 / dimensions) * dimensions;
    const std::string format     = parser.get<std::string>("format");

    T* data;
    HIP_CHECK(hipMalloc(&data, size * sizeof(T)));

    rocrand_generator generator;
    ROCRAND_CHECK(rocrand_create_generator(&generator, rng_type));

    rocrand_status status = rocrand_set_quasi_random_generator_dimensions(generator, dimensions);
    if(status != ROCRAND_STATUS_TYPE_ERROR) // If the RNG is not quasi-random
    {
        ROCRAND_CHECK(status);
    }

    ROCRAND_CHECK(rocrand_set_stream(generator, stream));

    status = rocrand_set_offset(generator, offset);
    if(status != ROCRAND_STATUS_TYPE_ERROR) // If the RNG is not pseudo-random
    {
        ROCRAND_CHECK(status);
    }

    // Warm-up
    for(size_t i = 0; i < 15; i++)
    {
        ROCRAND_CHECK(generate_func(generator, data, size));
    }
    HIP_CHECK(hipDeviceSynchronize());

    // Measurement
    hipEvent_t start, stop;
    HIP_CHECK(hipEventCreate(&start));
    HIP_CHECK(hipEventCreate(&stop));
    HIP_CHECK(hipEventRecord(start, stream));
    for(size_t i = 0; i < trials; i++)
    {
        ROCRAND_CHECK(generate_func(generator, data, size));
    }
    HIP_CHECK(hipEventRecord(stop, stream));
    HIP_CHECK(hipEventSynchronize(stop));
    float elapsed;
    HIP_CHECK(hipEventElapsedTime(&elapsed, start, stop));
    HIP_CHECK(hipEventDestroy(start));
    HIP_CHECK(hipEventDestroy(stop));

    if(format.compare("csv") == 0)
    {
        std::cout << std::fixed << std::setprecision(3) << engine << "," << distribution << ","
                  << (trials * size * sizeof(T)) / (elapsed / 1e3 * (1 << 30)) << ","
                  << (trials * size) / (elapsed / 1e3 * (1 << 30)) << "," << elapsed / trials << ","
                  << elapsed << "," << size << ",";
        if(distribution.compare("poisson") == 0 || distribution.compare("discrete-poisson") == 0)
        {
            std::cout << lambda;
        }
        std::cout << std::endl;
    }
    else
    {
        if(format.compare("console") != 0)
        {
            std::cout << "Unknown format specified (must be either console or csv).  Defaulting to "
                         "console output."
                      << std::endl;
        }
        std::cout << std::fixed << std::setprecision(3) << "      "
                  << "Throughput = " << std::setw(8)
                  << (trials * size * sizeof(T)) / (elapsed / 1e3 * (1 << 30))
                  << " GB/s, Samples = " << std::setw(8)
                  << (trials * size) / (elapsed / 1e3 * (1 << 30))
                  << " GSample/s, AvgTime (1 trial) = " << std::setw(8) << elapsed / trials
                  << " ms, Time (all) = " << std::setw(8) << elapsed << " ms, Size = " << size
                  << std::endl;
    }

    ROCRAND_CHECK(rocrand_destroy_generator(generator));
    HIP_CHECK(hipFree(data));
}

void run_benchmarks(const cli::Parser& parser,
                    const rng_type_t   rng_type,
                    const std::string& distribution,
                    const std::string& engine,
                    hipStream_t        stream)
{
    const std::string format = parser.get<std::string>("format");
    if(distribution == "uniform-uint")
    {
        run_benchmark<unsigned int>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, unsigned int* data, size_t size)
            { return rocrand_generate(gen, data, size); },
            distribution,
            engine);
    }
    if(distribution == "uniform-uchar")
    {
        run_benchmark<unsigned char>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, unsigned char* data, size_t size)
            { return rocrand_generate_char(gen, data, size); },
            distribution,
            engine);
    }
    if(distribution == "uniform-ushort")
    {
        run_benchmark<unsigned short>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, unsigned short* data, size_t size)
            { return rocrand_generate_short(gen, data, size); },
            distribution,
            engine);
    }
    if(distribution == "uniform-half")
    {
        run_benchmark<__half>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, __half* data, size_t size)
            { return rocrand_generate_uniform_half(gen, data, size); },
            distribution,
            engine);
    }
    if(distribution == "uniform-float")
    {
        run_benchmark<float>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, float* data, size_t size)
            { return rocrand_generate_uniform(gen, data, size); },
            distribution,
            engine);
    }
    if(distribution == "uniform-double")
    {
        run_benchmark<double>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, double* data, size_t size)
            { return rocrand_generate_uniform_double(gen, data, size); },
            distribution,
            engine);
    }
    if(distribution == "normal-half")
    {
        run_benchmark<__half>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, __half* data, size_t size) {
                return rocrand_generate_normal_half(gen,
                                                    data,
                                                    size,
                                                    __float2half(0.0f),
                                                    __float2half(1.0f));
            },
            distribution,
            engine);
    }
    if(distribution == "normal-float")
    {
        run_benchmark<float>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, float* data, size_t size)
            { return rocrand_generate_normal(gen, data, size, 0.0f, 1.0f); },
            distribution,
            engine);
    }
    if(distribution == "normal-double")
    {
        run_benchmark<double>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, double* data, size_t size)
            { return rocrand_generate_normal_double(gen, data, size, 0.0, 1.0); },
            distribution,
            engine);
    }
    if(distribution == "log-normal-half")
    {
        run_benchmark<__half>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, __half* data, size_t size)
            {
                return rocrand_generate_log_normal_half(gen,
                                                        data,
                                                        size,
                                                        __float2half(0.0f),
                                                        __float2half(1.0f));
            },
            distribution,
            engine);
    }
    if(distribution == "log-normal-float")
    {
        run_benchmark<float>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, float* data, size_t size)
            { return rocrand_generate_log_normal(gen, data, size, 0.0f, 1.0f); },
            distribution,
            engine);
    }
    if(distribution == "log-normal-double")
    {
        run_benchmark<double>(
            parser,
            rng_type,
            stream,
            [](rocrand_generator gen, double* data, size_t size)
            { return rocrand_generate_log_normal_double(gen, data, size, 0.0, 1.0); },
            distribution,
            engine);
    }
    if(distribution == "poisson")
    {
        const auto lambdas = parser.get<std::vector<double>>("lambda");
        for(double lambda : lambdas)
        {
            if(format.compare("console") == 0)
            {
                std::cout << "    "
                          << "lambda " << std::fixed << std::setprecision(1) << lambda << std::endl;
            }
            run_benchmark<unsigned int>(
                parser,
                rng_type,
                stream,
                [lambda](rocrand_generator gen, unsigned int* data, size_t size)
                { return rocrand_generate_poisson(gen, data, size, lambda); },
                distribution,
                engine,
                lambda);
        }
    }
}

const std::vector<std::string> all_engines = {
    "xorwow",
    "mrg31k3p",
    "mrg32k3a",
    "mtgp32",
    "philox",
    "lfsr113",
    "mt19937",
    "threefry2x32",
    "threefry2x64",
    "threefry4x32",
    "threefry4x64",
    "sobol32",
    "scrambled_sobol32",
    "sobol64",
    "scrambled_sobol64",
};

const std::vector<std::string> all_distributions = {"uniform-uint",
                                                    "uniform-uchar",
                                                    "uniform-ushort",
                                                    "uniform-half",
                                                    // "uniform-long-long",
                                                    "uniform-float",
                                                    "uniform-double",
                                                    "normal-half",
                                                    "normal-float",
                                                    "normal-double",
                                                    "log-normal-half",
                                                    "log-normal-float",
                                                    "log-normal-double",
                                                    "poisson"};

int main(int argc, char* argv[])
{
    cli::Parser parser(argc, argv);

    const std::string distribution_desc
        = "space-separated list of distributions:"
          + std::accumulate(all_distributions.begin(),
                            all_distributions.end(),
                            std::string(),
                            [](const std::string& a, const std::string& b)
                            { return a + "\n      " + b; })
          + "\n      or all";
    const std::string engine_desc = "space-separated list of random number engines:"
                                    + std::accumulate(all_engines.begin(),
                                                      all_engines.end(),
                                                      std::string(),
                                                      [](const std::string& a, const std::string& b)
                                                      { return a + "\n      " + b; })
                                    + "\n      or all";

    parser.set_optional<size_t>("size", "size", DEFAULT_RAND_N, "number of values");
    parser.set_optional<size_t>("dimensions",
                                "dimensions",
                                1,
                                "number of dimensions of quasi-random values");
    parser.set_optional<size_t>("offset", "offset", 0, "offset of generated pseudo-random values");
    parser.set_optional<size_t>("trials", "trials", 20, "number of trials");
    parser.set_optional<std::vector<std::string>>("dis",
                                                  "dis",
                                                  {"uniform-uint"},
                                                  distribution_desc);
    parser.set_optional<std::vector<std::string>>("engine", "engine", {"philox"}, engine_desc);
    parser.set_optional<std::vector<double>>(
        "lambda",
        "lambda",
        {10.0},
        "space-separated list of lambdas of Poisson distribution");
    parser.set_optional<std::string>("format",
                                     "format",
                                     {"console"},
                                     "output format: console or csv");
    parser.run_and_exit_if_error();

    std::vector<std::string> engines;
    {
        auto es = parser.get<std::vector<std::string>>("engine");
        if(std::find(es.begin(), es.end(), "all") != es.end())
        {
            engines = all_engines;
        }
        else
        {
            for(auto e : all_engines)
            {
                if(std::find(es.begin(), es.end(), e) != es.end())
                    engines.push_back(e);
            }
        }
    }

    std::vector<std::string> distributions;
    {
        auto ds = parser.get<std::vector<std::string>>("dis");
        if(std::find(ds.begin(), ds.end(), "all") != ds.end())
        {
            distributions = all_distributions;
        }
        else
        {
            for(auto d : all_distributions)
            {
                if(std::find(ds.begin(), ds.end(), d) != ds.end())
                    distributions.push_back(d);
            }
        }
    }

    int version;
    ROCRAND_CHECK(rocrand_get_version(&version));
    int runtime_version;
    HIP_CHECK(hipRuntimeGetVersion(&runtime_version));
    int device_id;
    HIP_CHECK(hipGetDevice(&device_id));
    hipDeviceProp_t props;
    HIP_CHECK(hipGetDeviceProperties(&props, device_id));

    std::cout << "benchmark_rocrand_generate" << std::endl;
    std::cout << "rocRAND: " << version << " ";
    std::cout << "Runtime: " << runtime_version << " ";
    std::cout << "Device: " << props.name;
    std::cout << std::endl << std::endl;

    hipStream_t stream;
    HIP_CHECK(hipStreamCreate(&stream));

    std::string format         = parser.get<std::string>("format");
    bool        console_output = format.compare("console") == 0 ? true : false;

    if(!console_output)
    {
        std::cout
            << "Engine,Distribution,Throughput,Samples,AvgTime (1 Trial),Time(all),Size,Lambda"
            << std::endl;
        std::cout << ",,GB/s,GSample/s,ms),ms),values," << std::endl;
    }

    for(auto engine : engines)
    {
        rng_type_t rng_type = ROCRAND_RNG_PSEUDO_XORWOW;
        if(engine == "xorwow")
            rng_type = ROCRAND_RNG_PSEUDO_XORWOW;
        else if(engine == "mrg31k3p")
            rng_type = ROCRAND_RNG_PSEUDO_MRG31K3P;
        else if(engine == "mrg32k3a")
            rng_type = ROCRAND_RNG_PSEUDO_MRG32K3A;
        else if(engine == "philox")
            rng_type = ROCRAND_RNG_PSEUDO_PHILOX4_32_10;
        else if(engine == "threefry2x32")
            rng_type = ROCRAND_RNG_PSEUDO_THREEFRY2_32_20;
        else if(engine == "threefry2x64")
            rng_type = ROCRAND_RNG_PSEUDO_THREEFRY2_64_20;
        else if(engine == "threefry4x32")
            rng_type = ROCRAND_RNG_PSEUDO_THREEFRY4_32_20;
        else if(engine == "threefry4x64")
            rng_type = ROCRAND_RNG_PSEUDO_THREEFRY4_64_20;
        else if(engine == "sobol32")
            rng_type = ROCRAND_RNG_QUASI_SOBOL32;
        else if(engine == "scrambled_sobol32")
            rng_type = ROCRAND_RNG_QUASI_SCRAMBLED_SOBOL32;
        else if(engine == "sobol64")
            rng_type = ROCRAND_RNG_QUASI_SOBOL64;
        else if(engine == "scrambled_sobol64")
            rng_type = ROCRAND_RNG_QUASI_SCRAMBLED_SOBOL64;
        else if(engine == "mtgp32")
            rng_type = ROCRAND_RNG_PSEUDO_MTGP32;
        else if(engine == "lfsr113")
            rng_type = ROCRAND_RNG_PSEUDO_LFSR113;
        else if(engine == "mt19937")
            rng_type = ROCRAND_RNG_PSEUDO_MT19937;
        else
        {
            std::cout << "Wrong engine name" << std::endl;
            exit(1);
        }

        if(console_output)
            std::cout << engine << ":" << std::endl;

        for(auto distribution : distributions)
        {
            if(console_output)
                std::cout << "  " << distribution << ":" << std::endl;
            run_benchmarks(parser, rng_type, distribution, engine, stream);
        }
        std::cout << std::endl;
    }

    HIP_CHECK(hipStreamDestroy(stream));

    return 0;
}