1 | // -*- Mode: C -*- |
---|
2 | // |
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo |
---|
4 | // |
---|
5 | // PRNG.c -- |
---|
6 | // |
---|
7 | // Author : Peter A. Buhr |
---|
8 | // Created On : Wed Dec 29 09:38:12 2021 |
---|
9 | // Last Modified By : Peter A. Buhr |
---|
10 | // Last Modified On : Sun Apr 23 22:02:09 2023 |
---|
11 | // Update Count : 420 |
---|
12 | // |
---|
13 | |
---|
14 | #include <fstream.hfa> // sout |
---|
15 | #include <stdlib.hfa> // PRNG |
---|
16 | #include <clock.hfa> |
---|
17 | #include <limits.hfa> // MAX |
---|
18 | #include <math.hfa> // sqrt |
---|
19 | #include <malloc.h> // malloc_stats |
---|
20 | #include <locale.h> // setlocale |
---|
21 | #include <thread.hfa> |
---|
22 | #include <mutex_stmt.hfa> |
---|
23 | |
---|
24 | #define xstr(s) str(s) |
---|
25 | #define str(s) #s |
---|
26 | |
---|
27 | #if defined( __x86_64__ ) || defined( __aarch64__ ) // 64-bit architecture |
---|
28 | #define PRNG PRNG64 |
---|
29 | #else // 32-bit architecture |
---|
30 | #define PRNG PRNG32 |
---|
31 | #endif // __x86_64__ |
---|
32 | |
---|
33 | //#define TIME |
---|
34 | |
---|
35 | #ifdef TIME // use -O2 -nodebug |
---|
36 | #define STARTTIME start = timeHiRes() |
---|
37 | #define ENDTIME( extra ) sout | wd(0,1, (timeHiRes() - start)`ms / 1000.) | extra "seconds" |
---|
38 | enum { BUCKETS = 100_000, TRIALS = 1_000_000_000 }; |
---|
39 | #else |
---|
40 | #define STARTTIME |
---|
41 | #define ENDTIME( extra ) |
---|
42 | enum { BUCKETS = 100_000, TRIALS = 100_000_000 }; |
---|
43 | #endif // TIME |
---|
44 | |
---|
45 | static void avgstd( size_t trials, size_t buckets[] ) { |
---|
46 | size_t min = MAX, max = 0; |
---|
47 | double sum = 0.0, diff; |
---|
48 | for ( i; BUCKETS ) { |
---|
49 | if ( buckets[i] < min ) min = buckets[i]; |
---|
50 | if ( buckets[i] > max ) max = buckets[i]; |
---|
51 | sum += buckets[i]; |
---|
52 | } // for |
---|
53 | |
---|
54 | double avg = sum / BUCKETS; // average |
---|
55 | sum = 0.0; |
---|
56 | for ( i; BUCKETS ) { // sum squared differences from average |
---|
57 | diff = buckets[i] - avg; |
---|
58 | sum += diff * diff; |
---|
59 | } // for |
---|
60 | double std = sqrt( sum / BUCKETS ); |
---|
61 | mutex( sout ) sout | "trials" | trials | "buckets" | BUCKETS |
---|
62 | | "min" | min | "max" | max |
---|
63 | | "avg" | wd(0,1, avg) | "std" | wd(0,1, std) | "rstd" | wd(0,1, (avg == 0 ? 0.0 : std / avg * 100)) | "%"; |
---|
64 | } // avgstd |
---|
65 | |
---|
66 | |
---|
67 | size_t seed = 1009; |
---|
68 | |
---|
69 | thread T1 {}; |
---|
70 | void main( T1 & ) { |
---|
71 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
72 | for ( TRIALS / 50 ) { |
---|
73 | buckets[rand() % BUCKETS] += 1; // concurrent |
---|
74 | } // for |
---|
75 | avgstd( TRIALS / 50, buckets ); |
---|
76 | free( buckets ); |
---|
77 | } // main |
---|
78 | |
---|
79 | thread T2 {}; |
---|
80 | void main( T2 & ) { |
---|
81 | PRNG prng; |
---|
82 | if ( seed != 0 ) set_seed( prng, seed ); |
---|
83 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
84 | for ( TRIALS ) { |
---|
85 | buckets[prng( prng ) % BUCKETS] += 1; // concurrent |
---|
86 | } // for |
---|
87 | avgstd( TRIALS, buckets ); |
---|
88 | free( buckets ); |
---|
89 | } // main |
---|
90 | |
---|
91 | thread T3 {}; |
---|
92 | void main( T3 & th ) { |
---|
93 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
94 | for ( TRIALS / 5 ) { |
---|
95 | buckets[prng() % BUCKETS] += 1; // concurrent |
---|
96 | } // for |
---|
97 | avgstd( TRIALS / 5, buckets ); |
---|
98 | free( buckets ); |
---|
99 | } // main |
---|
100 | |
---|
101 | thread T4 {}; |
---|
102 | void main( T4 & th ) { |
---|
103 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
104 | for ( TRIALS ) { |
---|
105 | buckets[prng( th ) % BUCKETS] += 1; // concurrent |
---|
106 | } // for |
---|
107 | avgstd( TRIALS, buckets ); |
---|
108 | free( buckets ); |
---|
109 | } // main |
---|
110 | |
---|
111 | // Compiler bug requires hiding declaration of th from the bucket access, otherwise the compiler thinks th is aliased |
---|
112 | // and continually reloads it from memory, which doubles the cost. |
---|
113 | static void dummy( thread$ & th ) __attribute__(( noinline )); |
---|
114 | static void dummy( thread$ & th ) { |
---|
115 | size_t * buckets = (size_t *)calloc( BUCKETS, sizeof(size_t) ); // too big for task stack |
---|
116 | for ( size_t i = 0; i < TRIALS; i += 1 ) { |
---|
117 | buckets[prng( th ) % BUCKETS] += 1; // sequential |
---|
118 | } // for |
---|
119 | avgstd( TRIALS, buckets ); |
---|
120 | free( buckets ); |
---|
121 | } // dummy |
---|
122 | |
---|
123 | |
---|
124 | int main() { |
---|
125 | // setlocale( LC_NUMERIC, getenv( "LANG" ) ); // causes leaked storage message |
---|
126 | |
---|
127 | // only works on the current pthread thread |
---|
128 | // locale_t loc = newlocale( LC_NUMERIC_MASK, getenv( "LANG" ), (locale_t)0p ); |
---|
129 | // if ( loc == (locale_t)0p ) abort( "newlocale" ); |
---|
130 | // uselocale( loc ); |
---|
131 | |
---|
132 | enum { TASKS = 4 }; |
---|
133 | Time start; |
---|
134 | |
---|
135 | #ifdef TIME // too slow for test and generates non-repeatable results |
---|
136 | #if 1 |
---|
137 | sout | "glib rand" | nl | nl; |
---|
138 | |
---|
139 | size_t rseed; |
---|
140 | if ( seed != 0 ) rseed = seed; |
---|
141 | else rseed = rdtscl(); |
---|
142 | srand( rseed ); |
---|
143 | |
---|
144 | sout | sepDisable; |
---|
145 | sout | nl | wd(26, "rand()" ) | wd(12, "rand(5)") | wd(12, "rand(0,5)" ); |
---|
146 | for ( 20 ) { |
---|
147 | sout | wd(26, rand()) | nonl; |
---|
148 | sout | wd(12, rand() % 5) | nonl; |
---|
149 | sout | wd(12, rand() % (5 - 0 + 1) + 0); |
---|
150 | } // for |
---|
151 | sout | sepEnable; |
---|
152 | sout | "seed" | rseed; |
---|
153 | |
---|
154 | sout | nl | "Sequential"; |
---|
155 | STARTTIME; |
---|
156 | { |
---|
157 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
158 | for ( i; TRIALS / 5 ) { |
---|
159 | buckets[rand() % BUCKETS] += 1; // sequential |
---|
160 | } // for |
---|
161 | avgstd( TRIALS / 5, buckets ); |
---|
162 | free( buckets ); |
---|
163 | } |
---|
164 | ENDTIME( " x 5 " ); |
---|
165 | |
---|
166 | sout | nl | "Concurrent"; |
---|
167 | STARTTIME; |
---|
168 | { |
---|
169 | processor p[TASKS - 1]; // already 1 processor |
---|
170 | { |
---|
171 | T1 t[TASKS]; |
---|
172 | } // wait for threads to complete |
---|
173 | } |
---|
174 | ENDTIME( " x 50 " ); |
---|
175 | #endif // 0 |
---|
176 | #endif // TIME |
---|
177 | |
---|
178 | sout | nl | "CFA " xstr(PRNG_NAME); |
---|
179 | |
---|
180 | #if 1 |
---|
181 | PRNG prng; |
---|
182 | |
---|
183 | if ( seed != 0 ) set_seed( prng, seed ); |
---|
184 | |
---|
185 | sout | sepDisable; |
---|
186 | sout | nl | wd(26, "PRNG()" ) | wd(12, "PRNG(5)") | wd(12, "PRNG(0,5)" ); |
---|
187 | for ( 20 ) { |
---|
188 | sout | wd(26, prng( prng )) | nonl; // cascading => side-effect functions called in arbitary order |
---|
189 | sout | wd(12, prng( prng, 5 )) | nonl; |
---|
190 | sout | wd(12, prng( prng, 0, 5 )); |
---|
191 | } // for |
---|
192 | sout | sepEnable; |
---|
193 | sout | "seed" | get_seed( prng ); |
---|
194 | |
---|
195 | sout | nl | "Sequential"; |
---|
196 | STARTTIME; |
---|
197 | { |
---|
198 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
199 | for ( TRIALS ) { |
---|
200 | buckets[prng( prng ) % BUCKETS] += 1; // sequential |
---|
201 | } // for |
---|
202 | avgstd( TRIALS, buckets ); |
---|
203 | free( buckets ); |
---|
204 | } |
---|
205 | ENDTIME(); |
---|
206 | |
---|
207 | sout | nl | "Concurrent"; |
---|
208 | STARTTIME; |
---|
209 | { |
---|
210 | processor p[TASKS - 1]; // already 1 processor |
---|
211 | { |
---|
212 | T2 t[TASKS]; |
---|
213 | } // wait for threads to complete |
---|
214 | } |
---|
215 | ENDTIME(); |
---|
216 | #endif // 0 |
---|
217 | #if 1 |
---|
218 | if ( seed != 0 ) set_seed( seed ); |
---|
219 | |
---|
220 | sout | sepDisable; |
---|
221 | sout | nl | wd(26, "prng()" ) | wd(12, "prng(5)") | wd(12, "prng(0,5)" ); |
---|
222 | for ( 20 ) { |
---|
223 | sout | wd(26, prng()) | nonl; // cascading => side-effect functions called in arbitary order |
---|
224 | sout | wd(12, prng( 5 )) | nonl; |
---|
225 | sout | wd(12, prng( 0, 5 )); |
---|
226 | } // for |
---|
227 | sout | sepEnable; |
---|
228 | sout | "seed" | get_seed( prng ); |
---|
229 | |
---|
230 | sout | nl | "Sequential"; |
---|
231 | STARTTIME; |
---|
232 | { |
---|
233 | size_t * buckets = calloc( BUCKETS ); // too big for task stack |
---|
234 | for ( TRIALS / 5 ) { |
---|
235 | buckets[prng() % BUCKETS] += 1; |
---|
236 | } // for |
---|
237 | avgstd( TRIALS / 5, buckets ); |
---|
238 | free( buckets ); |
---|
239 | } |
---|
240 | ENDTIME( " x 5 " ); |
---|
241 | |
---|
242 | sout | nl | "Concurrent"; |
---|
243 | STARTTIME; |
---|
244 | { |
---|
245 | processor p[TASKS - 1]; // already 1 processor |
---|
246 | { |
---|
247 | T3 t[TASKS]; |
---|
248 | } // wait for threads to complete |
---|
249 | } |
---|
250 | ENDTIME( " x 5 " ); |
---|
251 | #endif // 0 |
---|
252 | #if 1 |
---|
253 | if ( seed != 0 ) set_seed( seed ); |
---|
254 | thread$ & th = *active_thread(); |
---|
255 | |
---|
256 | sout | sepDisable; |
---|
257 | sout | nl | wd(26, "prng(t)" ) | wd(12, "prng(t,5)") | wd(12, "prng(t,0,5)" ); |
---|
258 | for ( 20 ) { |
---|
259 | sout | wd(26, prng( th )) | nonl; // cascading => side-effect functions called in arbitary order |
---|
260 | sout | wd(12, prng( th, 5 )) | nonl; |
---|
261 | sout | wd(12, prng( th, 0, 5 )); |
---|
262 | } // for |
---|
263 | sout | sepEnable; |
---|
264 | sout | "seed" | get_seed( prng ); |
---|
265 | |
---|
266 | sout | nl | "Sequential"; |
---|
267 | STARTTIME; |
---|
268 | { |
---|
269 | dummy( th ); |
---|
270 | } |
---|
271 | ENDTIME(); |
---|
272 | |
---|
273 | sout | nl | "Concurrent"; |
---|
274 | STARTTIME; |
---|
275 | { |
---|
276 | processor p[TASKS - 1]; // already 1 processor |
---|
277 | { |
---|
278 | T4 t[TASKS]; |
---|
279 | } // wait for threads to complete |
---|
280 | } |
---|
281 | ENDTIME(); |
---|
282 | #endif // 0 |
---|
283 | // malloc_stats(); |
---|
284 | // freelocale( loc ); |
---|
285 | } // main |
---|
286 | |
---|
287 | |
---|
288 | // Local Variables: // |
---|
289 | // compile-command: "cfa -DTIME -O2 -nodebug PRNG.cfa" // |
---|
290 | // End: // |
---|