File: time_serial.c

package info (click to toggle)
librandom123 1.09%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,284 kB
  • sloc: cpp: 4,849; ansic: 4,407; perl: 108; sh: 37; makefile: 3
file content (154 lines) | stat: -rw-r--r-- 5,405 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
/*
Copyright 2010-2011, D. E. Shaw Research.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions, and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions, and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of D. E. Shaw Research nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
 * Single-core CPU test and timing harness for Random123 RNGs.
 * Uses macros and util_expandtpl.h to "templatize" over all the
 * different permutations of RNGs and NxW and R.
 */
#include "util.h"

#include "Random123/philox.h"
#include "Random123/threefry.h"
#include "Random123/ars.h"
#include "Random123/aes.h"

#include "time_misc.h"
#include "util_print.h"

#include "util_cpu.h"
#define KERNEL R123_STATIC_INLINE
#define get_global_id(i)    (i)
#include "time_random123.h"

#define TEST_TPL(NAME, N, W, R) \
void NAME##N##x##W##_##R(NAME##N##x##W##_ukey_t ukey, NAME##N##x##W##_ctr_t ctr, NAME##N##x##W##_ctr_t kactr, uint count, CPUInfo *tp) \
{ \
    const char *kernelname = #NAME #N "x" #W "_" #R; \
    double cur_time; \
    int n, niterations = numtrials; /* we make niterations + 2 (warmup, overhead) calls to the kernel */ \
    uint kcount = 0; \
    double basetime = 0., dt = 0., mindt = 0.; \
    NAME##N##x##W##_ctr_t C, *hC = &C; \
    \
    for (n = -2; n < niterations; n++) { \
	if (n == -2) { \
	    if (count == 0) { \
		/* try to set a good guess for count */ \
		count = (uint)(tp->hz ? tp->hz * 1e-5 : 100000); \
		dprintf(("starting with count = %u\n", count)); \
	    } \
	    kcount = count; \
	} else if (n == -1) { \
	    /* use first iteration time to calibrate count to get approximately sec_per_trial */ \
	    if (count > 1) { \
		count = (uint)(count * sec_per_trial / dt); \
		dprintf(("scaled count = %u\n", count)); \
	    } \
	    /* second iteration is to calculate overhead after warmup */ \
	    kcount = 1; \
	} else if (n == 0) { \
	    int xj; \
	    /* Check that we got the expected value */ \
	    for (xj = 0; xj < N; xj++) { \
		if (kactr.v[xj] != hC[0].v[xj]) { \
		    printf("%s mismatch: xj = %d, expected\n", kernelname, xj); \
		    printline_##NAME##N##x##W##_##R(ukey, ctr, &kactr, 1); \
		    printf("    but got\n"); \
		    printline_##NAME##N##x##W##_##R(ukey, ctr, hC, 1); \
		    if(!debug) exit(1);                                 \
                    else break;                                         \
		} else { \
		    dprintf(("%s matched word %d\n", kernelname, xj)); \
		} \
	    } \
	    basetime = dt; \
	    if (debug||verbose) { \
		dprintf(("%s %.3f secs\n", kernelname, basetime)); \
		printline_##NAME##N##x##W##_##R(ukey, ctr, hC, 1); \
	    } \
	    kcount = count + 1; \
	} \
	dprintf(("call function %s\n", kernelname)); \
	(void)timer(&cur_time); \
	test_##NAME##N##x##W##_##R(kcount, ukey, ctr, hC); \
	dt = timer(&cur_time); \
	dprintf(("iteration %d took %.3f secs\n", n, dt)); \
	ALLZEROS(hC, 1, N); \
	if (n == 0 || dt < mindt) mindt = dt; \
    } \
    if (count > 1) { \
	double tpB = (mindt - basetime) / ( (kcount - 1.) * (N * W / 8.) ); \
	printf("%-17s %#5.3g cpB %#5.3g GB/s %u B granularity (best %u in %.3f s - %.6f s)\n", \
	       kernelname, tpB * tp->hz, 1e-9/tpB, \
	       (uint)(N*W/8), kcount, mindt, basetime ); \
	fflush(stdout); \
    } \
}

#include "util_expandtpl.h"

int main(int argc, char **argv)
{
    char *cp;
    uint count = 0;
    CPUInfo *infop;
    int keyctroffset = 0;
    
    progname = argv[0];
    if (argc > 3|| (argv[1] && argv[1][0] == '-')) {
	fprintf(stderr, "Usage: %s [COUNT]]\n", progname);
	exit(1);
    }
    if (argc > 1)
	count = atoi(argv[1]);
    if ((cp = getenv("TIME_SERIAL_VERBOSE")) != NULL) {
	verbose = atoi(cp);
    }
    if ((cp = getenv("TIME_SERIAL_DEBUG")) != NULL) {
	debug = atoi(cp);
    }
    if ((cp = getenv("TIME_SERIAL_OFFSET")) != NULL) {
	keyctroffset = atoi(cp);
    }
    if ((cp = getenv("TIME_SERIAL_NUMTRIALS")) != NULL) {
        numtrials = atoi(cp);
    }
    if ((cp = getenv("TIME_SERIAL_SEC_PER_TRIAL")) != NULL) {
        sec_per_trial = atof(cp);
    }
    infop = cpu_init("1"); /* This is time_serial.c.  Use only one core. */
    {
#   include "time_initkeyctr.h"
    cpu_done(infop);
    }
    return 0;
}