|
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | 5 | import numpy as np |
6 | | -try: |
7 | | - import mkl_random as rnd |
8 | | - mkl = True |
9 | | -except (ImportError, ModuleNotFoundError): |
10 | | - import numpy.random as rnd |
11 | | - mkl = False |
12 | 6 | import timeit |
| 7 | +import sys |
13 | 8 |
|
14 | 9 | def sample_uniform(rs, sz): |
15 | 10 | rs.uniform(-1, 1, size=sz) |
@@ -46,31 +41,56 @@ def sample_hypergeom(rs, sz): |
46 | 41 |
|
47 | 42 | OUTER_REPS=6 |
48 | 43 | INNER_REPS=512 |
| 44 | +SEED=123 |
49 | 45 |
|
50 | 46 | def main(): |
51 | 47 | import itertools |
| 48 | + import argparse |
| 49 | + parser = argparse.ArgumentParser() |
| 50 | + parser.add_argument('--prefix', required=False, default="IntelPython", help="Print with each result") |
| 51 | + parser.add_argument('--impl', required=False, default="mkl_random", choices=['mkl_random', 'numpy'], help='RNG implementation\n' |
| 52 | + 'choices:\n' |
| 53 | + 'mkl_random: mkl_random.RandomState to be used\n' |
| 54 | + 'numpy: numpy.Generator to be used') |
| 55 | + |
| 56 | + args = parser.parse_args() |
| 57 | + |
| 58 | + if args.impl == 'mkl_random': |
| 59 | + try: |
| 60 | + import mkl_random as rnd |
| 61 | + mkl = True |
| 62 | + except (ImportError, ModuleNotFoundError) as e: |
| 63 | + print(str(e)) |
| 64 | + print('mkl_random is chosen for benchmark, however it is not found in current environemnt') |
| 65 | + sys.exit(1) |
| 66 | + else: |
| 67 | + import numpy.random as rnd |
| 68 | + mkl = False |
| 69 | + |
52 | 70 | if mkl: |
53 | 71 | brngs = ['WH', 'PHILOX4X32X10', 'MT2203', 'MCG59', 'MCG31', 'MT19937', 'MRG32K3A', 'SFMT19937', 'R250'] |
54 | 72 | else: |
55 | 73 | brngs = [np.random.MT19937, np.random.Philox] |
| 74 | + |
56 | 75 | samplers = {'uniform': sample_uniform, 'normal': sample_normal, 'gamma': sample_gamma, 'beta': sample_beta, |
57 | 76 | 'randint': sample_randint, 'poisson': sample_poisson, 'hypergeom': sample_hypergeom} |
58 | 77 | multipliers = {'uniform': 10, 'normal': 2, 'gamma': 1, 'beta': 1, 'randint': 10, 'poisson': 5, 'hypergeom': 1} |
| 78 | + |
59 | 79 | for brng_name, sfn in itertools.product(brngs, samplers.keys()): |
60 | 80 | func = samplers[sfn] |
61 | 81 | m = multipliers[sfn] |
62 | 82 | times_list = [] |
63 | 83 | for __ in range(OUTER_REPS): |
64 | 84 | if mkl: |
65 | | - rs = rnd.RandomState(123, brng=brng_name) |
| 85 | + rs = rnd.RandomState(SEED, brng=brng_name) |
66 | 86 | else: |
67 | | - rs = rnd.Generator(brng_name(seed=123)) |
| 87 | + rs = rnd.Generator(brng_name(seed=SEED)) |
68 | 88 | t0 = timeit.default_timer() |
69 | 89 | for __ in range(INNER_REPS): |
70 | 90 | func(rs, (m*100, 1000)) |
71 | 91 | t1 = timeit.default_timer() |
72 | 92 | times_list.append(t1-t0) |
73 | | - print("IntelPython,{brng_name},{dist_name},{time:.5f}".format(brng_name=brng_name, dist_name=sfn, time=min(times_list))) |
| 93 | + print(f"{args.prefix},{m*100*1000},{brng_name if mkl else brng_name().__class__.__name__},{sfn},{min(times_list):.5f}") |
74 | 94 |
|
75 | 95 |
|
76 | 96 | if __name__ == '__main__': |
|
0 commit comments