Krnl_PRNG.cl 13.2 KB
Newer Older
Leonardo Solis's avatar
Leonardo Solis committed
1
// --------------------------------------------------------------------------
2
3
// PRNG generators are implemented as Linear Feedback Shift Registers (LFSR)
// All are 32-bit LFRS, feedback taps: 30, 20, 26, 25
Leonardo Solis's avatar
Leonardo Solis committed
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
__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_BT_ushort_float(unsigned int Host_seed1,
			       unsigned int Host_seed2,
			       unsigned int DockConst_pop_size){

	uint2 lfsr;
	lfsr.x = Host_seed1;
	lfsr.y = Host_seed2;
	bool valid  = false;
	
	while(!valid) {	
		bool active = true;
		active = read_channel_nb_altera(chan_Arbiter_BT_ushort_float_off, &valid);

		uint   u_tmp[4]; // used as short in GA
		float  f_tmp[4];	

		#pragma unroll
		for(uchar i=0; i<4; i++) {
			uchar2 lsb;

			lsb.x = lfsr.x & 0x01u;
			lsb.y = lfsr.y & 0x01u;

			lfsr.x >>= 1;
			lfsr.y >>= 1;

			lfsr.x ^= (-lsb.x) & 0xA3000000u;
			lfsr.y ^= (-lsb.y) & 0xA3000000u;

			u_tmp[i] = (lfsr.x/MAX_UINT)*DockConst_pop_size;
			f_tmp[i] = (0.999999f/MAX_UINT)*lfsr.y;
		}

		bool success = false;
		if(!valid) {
			float8 tmp = {*(float*)&u_tmp[0], f_tmp[0],
				      *(float*)&u_tmp[1], f_tmp[1],
				      *(float*)&u_tmp[2], f_tmp[2],
				      *(float*)&u_tmp[3], f_tmp[3]};
			success = write_channel_nb_altera(chan_PRNG2GA_BT_ushort_float_prng, tmp);
		}
	} // End of while(!valid)
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------

Leonardo Solis's avatar
Leonardo Solis committed
54
__kernel __attribute__ ((max_global_work_dim(0)))
55
void Krnl_Prng_GG_uchar(unsigned int  Host_seed, 
56
		        unsigned char DockConst_num_of_genes){
Leonardo Solis's avatar
Leonardo Solis committed
57

58
	uint lfsr = Host_seed;
59
	bool valid = false;
Leonardo Solis's avatar
Leonardo Solis committed
60

61
62
63
64
65
	while(!valid) {
		bool active = true;
		active = read_channel_nb_altera(chan_Arbiter_GG_uchar_off, &valid);
		
		uchar tmp[2];
66

67
68
		#pragma unroll
		for(uchar i=0; i<2; i++) {
69
70
71
72
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
73
			tmp[i] = (lfsr/MAX_UINT)*DockConst_num_of_genes;
74

75
76
77
78
79
80
81
82
83
		}

		bool success = false;
		uchar2 utmp;
		utmp.x = tmp[0];
		utmp.y = tmp[1];

		if(!valid) {
			success = write_channel_nb_altera(chan_PRNG2GA_GG_uchar_prng, utmp);
Leonardo Solis's avatar
Leonardo Solis committed
84
		}
85
	} // End of while(active)
Leonardo Solis's avatar
Leonardo Solis committed
86
}
Leonardo Solis's avatar
Leonardo Solis committed
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

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_GG_float(unsigned int  Host_seed,
		        unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active = read_channel_nb_altera(chan_Arbiter_GG_float_off, &valid);

		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;

			bool success = false;
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_GG_float_prng, tmp);
			}
		}
	} // End of while(active)
}
Leonardo Solis's avatar
Leonardo Solis committed
114
115
116

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
117

Leonardo Solis's avatar
Leonardo Solis committed
118
119
120
121
122
123
__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS123_ushort(unsigned int Host_seed1,
			    unsigned int Host_seed2, 
			    unsigned int Host_seed3,
			    unsigned int Host_seed4,
			    unsigned int Host_seed5, 
124
125
126
127
			    unsigned int Host_seed6, 
			    unsigned int Host_seed7,
			    unsigned int Host_seed8,
			    unsigned int Host_seed9, 
Leonardo Solis's avatar
Leonardo Solis committed
128
129
130
		            unsigned int DockConst_pop_size){


131
	uint lfsr[9];
Leonardo Solis's avatar
Leonardo Solis committed
132
133
134
135
136
	lfsr[0] = Host_seed1;
	lfsr[1] = Host_seed2;
	lfsr[2] = Host_seed3;
	lfsr[3] = Host_seed4;
	lfsr[4] = Host_seed5;
137
138
139
140
	lfsr[5] = Host_seed6;
	lfsr[6] = Host_seed7;
	lfsr[7] = Host_seed8;
	lfsr[8] = Host_seed9;
Leonardo Solis's avatar
Leonardo Solis committed
141
142
143
144
145
146
147

	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS123_ushort_off, &valid);

148
		ushort tmp[9];
Leonardo Solis's avatar
Leonardo Solis committed
149
150
		
		#pragma unroll
151
152
		for (uint i=0; i<9; i++){
			uchar  lsb[9];
Leonardo Solis's avatar
Leonardo Solis committed
153
154
155
156
157
158
			lsb [i] = lfsr[i] & 0x01u;
			lfsr[i] >>= 1;
			lfsr[i] ^= (-lsb[i]) & 0xA3000000u;
			tmp [i] = (DockConst_pop_size/MAX_UINT)*lfsr[i];
		}

159
		// To avoid having same entities undergoing LS simultaneously
Leonardo Solis's avatar
Leonardo Solis committed
160
		if (
161
162
163
164
165
166
167
168
			(tmp[0] == tmp[1]) || (tmp[0] == tmp[2]) || (tmp[0] == tmp[3]) || (tmp[0] == tmp[4]) || (tmp[0] == tmp[5]) || (tmp[0] == tmp[6]) || (tmp[0] == tmp[7]) || (tmp[0] == tmp[8]) ||
 			(tmp[1] == tmp[2]) || (tmp[1] == tmp[3]) || (tmp[1] == tmp[4]) || (tmp[1] == tmp[5]) || (tmp[1] == tmp[6]) || (tmp[1] == tmp[7]) || (tmp[1] == tmp[8]) ||
			(tmp[2] == tmp[3]) || (tmp[2] == tmp[4]) || (tmp[2] == tmp[5]) || (tmp[2] == tmp[6]) || (tmp[2] == tmp[7]) || (tmp[2] == tmp[8]) ||
			(tmp[3] == tmp[4]) || (tmp[3] == tmp[5]) || (tmp[3] == tmp[6]) || (tmp[3] == tmp[7]) || (tmp[3] == tmp[8]) ||
			(tmp[4] == tmp[5]) || (tmp[4] == tmp[6]) || (tmp[4] == tmp[7]) || (tmp[4] == tmp[8]) ||
			(tmp[5] == tmp[6]) || (tmp[5] == tmp[7]) || (tmp[5] == tmp[8]) ||
			(tmp[6] == tmp[7]) || (tmp[6] == tmp[8]) ||
			(tmp[7] == tmp[8])
Leonardo Solis's avatar
Leonardo Solis committed
169
170
171
172
173
		) {
			tmp[1] = tmp[0] + 1;
			tmp[2] = tmp[1] + 2;
			tmp[3] = tmp[2] + 3;
			tmp[4] = tmp[3] + 4;
174
175
176
177
			tmp[5] = tmp[4] + 5;
			tmp[6] = tmp[5] + 6;
			tmp[7] = tmp[6] + 7;
			tmp[8] = tmp[7] + 8;
Leonardo Solis's avatar
Leonardo Solis committed
178
179
180
		}

		bool success = false;
181
		ushort16 tmp123;
Leonardo Solis's avatar
Leonardo Solis committed
182
183
184
185
186
		tmp123.s0 = tmp[0];
		tmp123.s1 = tmp[1];
		tmp123.s2 = tmp[2];
		tmp123.s3 = tmp[3];
		tmp123.s4 = tmp[4];
187
188
189
190
		tmp123.s5 = tmp[5];
		tmp123.s6 = tmp[6];
		tmp123.s7 = tmp[7];
		tmp123.s8 = tmp[8];
Leonardo Solis's avatar
Leonardo Solis committed
191
192
193
194
195
196
197

		if(!valid) {
			success = write_channel_nb_altera(chan_PRNG2GA_LS123_ushort_prng, tmp123);
		}

	} // End of while(active)
}
198
199
200

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS_float(unsigned int  Host_seed,
		        unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;
Leonardo Solis's avatar
Leonardo Solis committed
221
222
223
224
225
226
227

			#if defined (FIXED_POINT_LS1)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS_float_prng, *(float*) &fixpt_tmp);
			}
			#else
Leonardo Solis's avatar
Leonardo Solis committed
228
229
230
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS_float_prng, tmp);
			}
Leonardo Solis's avatar
Leonardo Solis committed
231
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
232
233
234
		}
	} // End of while(active)
}
Leonardo Solis's avatar
Leonardo Solis committed
235

Leonardo Solis's avatar
Leonardo Solis committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS2_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS2_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;
Leonardo Solis's avatar
Leonardo Solis committed
255
256
257
258
259
260
261

			#if defined (FIXED_POINT_LS2)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS2_float_prng, *(float*) &fixpt_tmp);
			}
			#else
Leonardo Solis's avatar
Leonardo Solis committed
262
263
264
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS2_float_prng, tmp);
			}
Leonardo Solis's avatar
Leonardo Solis committed
265
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
266
267
268
		}
	} // End of while(active)
}
Leonardo Solis's avatar
Leonardo Solis committed
269

Leonardo Solis's avatar
Leonardo Solis committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS3_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS3_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;
Leonardo Solis's avatar
Leonardo Solis committed
289
290
291
292
293
294
295
296

			#if defined (FIXED_POINT_LS3)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS3_float_prng, *(float*) &fixpt_tmp);
			}

			#else
Leonardo Solis's avatar
Leonardo Solis committed
297
298
299
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS3_float_prng, tmp);
			}
Leonardo Solis's avatar
Leonardo Solis committed
300
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
301
302
303
		}
	} // End of while(active)
}
Leonardo Solis's avatar
Leonardo Solis committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS4_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS4_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;

325
			#if defined (FIXED_POINT_LS4)
Leonardo Solis's avatar
Leonardo Solis committed
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
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS4_float_prng, *(float*) &fixpt_tmp);
			}
			#else
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS4_float_prng, tmp);
			}
			#endif
		}
	} // End of while(active)
}

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS5_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS5_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;

359
			#if defined (FIXED_POINT_LS5)
Leonardo Solis's avatar
Leonardo Solis committed
360
361
362
363
364
365
366
367
368
369
370
371
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS5_float_prng, *(float*) &fixpt_tmp);
			}
			#else
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS5_float_prng, tmp);
			}
			#endif
		}
	} // End of while(active)
}
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

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS6_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS6_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;

			#if defined (FIXED_POINT_LS6)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS6_float_prng, *(float*) &fixpt_tmp);
			}
			#else
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS6_float_prng, tmp);
			}
			#endif
		}
	} // End of while(active)
}

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS7_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS7_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;

			#if defined (FIXED_POINT_LS7)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS7_float_prng, *(float*) &fixpt_tmp);
			}
			#else
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS7_float_prng, tmp);
			}
			#endif
		}
	} // End of while(active)
}

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS8_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS8_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;

			#if defined (FIXED_POINT_LS8)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS8_float_prng, *(float*) &fixpt_tmp);
			}
			#else
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS8_float_prng, tmp);
			}
			#endif
		}
	} // End of while(active)
}

__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_Prng_LS9_float(unsigned int  Host_seed,
			 unsigned char DockConst_num_of_genes){

	uint lfsr = Host_seed;
	bool valid = false;

	while(!valid) {
		bool active = true;
		active  = read_channel_nb_altera(chan_Arbiter_LS9_float_off, &valid);
	
		for(uchar i=0; i<DockConst_num_of_genes; i++) {
			float tmp;
			uchar lsb;
			lsb = lfsr & 0x01u;
			lfsr >>= 1;
			lfsr ^= (-lsb) & 0xA3000000u;
			tmp = (0.999999f/MAX_UINT)*lfsr;
			bool success = false;

			#if defined (FIXED_POINT_LS9)
			fixedpt fixpt_tmp = fixedpt_fromfloat(tmp);
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS9_float_prng, *(float*) &fixpt_tmp);
			}
			#else
			if(!valid) {
				success = write_channel_nb_altera(chan_PRNG2GA_LS9_float_prng, tmp);
			}
			#endif
		}
	} // End of while(active)
}
Leonardo Solis's avatar
Leonardo Solis committed
508
509
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------