Krnl_LS7.cl 11.7 KB
Newer Older
1
2
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
3
/*
4
__kernel __attribute__ ((max_global_work_dim(0)))
lvs's avatar
lvs committed
5
6
*/
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Krnl_LS7(
		unsigned short            DockConst_max_num_of_iters,		
		#if defined (FIXED_POINT_LS7)
		fixedpt                   DockConst_rho_lower_bound,
		fixedpt                   DockConst_base_dmov_mul_sqrt3,
		#else
		float                     DockConst_rho_lower_bound,
		float                     DockConst_base_dmov_mul_sqrt3,
		#endif
		unsigned char             DockConst_num_of_genes,
		#if defined (FIXED_POINT_LS7)
		fixedpt                   DockConst_base_dang_mul_sqrt3,
		#else
   		float                     DockConst_base_dang_mul_sqrt3,
		#endif

		unsigned char             DockConst_cons_limit
24
25

#if !defined(SW_EMU)
26
27
		// IMPORTANT: enable this dummy global argument only for "hw" build.
		// Check ../common_xilinx/utility/boards.mk
28
29
30
31
		// https://forums.xilinx.com/t5/SDAccel/ERROR-KernelCheck-83-114-in-sdx-2017-4/td-p/818135
		,
		__global int *dummy
#endif
32
33
)
{
34
35
36
37
38
39
40
41
42
	#if 0
	printf("\nLS7: DockConst_max_num_of_iters: %u\n",   DockConst_max_num_of_iters);
	printf("LS7: DockConst_rho_lower_bound: %f\n",      DockConst_rho_lower_bound);
	printf("LS7: DockConst_base_dmov_mul_sqrt3: %f\n",  DockConst_base_dmov_mul_sqrt3);
	printf("LS7: DockConst_num_of_genes: %u\n",  	    DockConst_num_of_genes);
	printf("LS7: DockConst_base_dang_mul_sqrt3: %f\n",  DockConst_base_dang_mul_sqrt3);
	printf("LS7: DockConst_cons_limit: %u\n",           DockConst_cons_limit);
	#endif

43
44
	bool valid = true;

45
46
__attribute__((xcl_pipeline_loop))
LOOP_WHILE_LS7_MAIN:
47
while(valid) {
48
/*
49
	bool active;
50
51
*/
	int active;
lvs's avatar
lvs committed
52
/*
53
	bool valid_active = false;
lvs's avatar
lvs committed
54
55
*/
	int valid_active= 1;
56
57

	float current_energy;
lvs's avatar
lvs committed
58
/*
59
	bool valid_energy = false;
lvs's avatar
lvs committed
60
61
*/
	int valid_energy = 1;
62

lvs's avatar
lvs committed
63
/*
64
	while( (valid_active == false) && (valid_energy == false)) {
lvs's avatar
lvs committed
65
*/
66
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
67
	LOOP_WHILE_LS7_ACTIVE:
lvs's avatar
lvs committed
68
	while( (valid_active != 0) && (valid_energy != 0)) {
lvs's avatar
lvs committed
69
70
		valid_active = read_pipe(chan_GA2LS_Off7_active, &active);
		valid_energy = read_pipe(chan_GA2LS_LS7_energy,  &current_energy);
71
	}
lvs's avatar
lvs committed
72
/*
73
	valid = active || valid_energy;
lvs's avatar
lvs committed
74
*/
75
/*
lvs's avatar
lvs committed
76
	valid = active || (valid_energy == 0);
77
78
79
*/
	// (active == 1) means stop LS
	valid = (active != 1) || (valid_energy == 0);
80
81
82
83
84
85
86
87
88

	if (valid) {

		#if defined (FIXED_POINT_LS7)
		fixedpt genotype [ACTUAL_GENOTYPE_LENGTH];
		#else
		float   genotype [ACTUAL_GENOTYPE_LENGTH];
		#endif

89
		__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
90
		LOOP_FOR_LS7_READ_INPUT_GENOTYPE:
91
92
		for (uchar i=0; i<DockConst_num_of_genes; i++) {
			#if defined (FIXED_POINT_LS7)
lvs's avatar
lvs committed
93
94
			float tmp_gene;
			read_pipe_block(chan_GA2LS_LS7_genotype, &tmp_gene);
95
96
			genotype [i] = fixedpt_fromfloat(tmp_gene);
			#else
lvs's avatar
lvs committed
97
			read_pipe_block(chan_GA2LS_LS7_genotype, &genotype [i] );
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
			#endif
		}
	
		#if defined (DEBUG_KRNL_LS7)
		printf("In of while iter LS7\n");
		#endif

		#if defined (FIXED_POINT_LS7)
		fixedpt fixpt_rho = FIXEDPT_ONE;
		#else
		float rho = 1.0f;
		#endif
		ushort iteration_cnt = 0;
		uchar  cons_succ     = 0;
		uchar  cons_fail     = 0;
		uint   LS_eval       = 0;
		bool   positive_direction = true;

		// performing local search
117
		__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
118
		LOOP_WHILE_LS7_ITERATION_RHO:
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
		#if defined (FIXED_POINT_LS7)
		while ((iteration_cnt < DockConst_max_num_of_iters) && (fixpt_rho > DockConst_rho_lower_bound)) {
		#else
		while ((iteration_cnt < DockConst_max_num_of_iters) && (rho > DockConst_rho_lower_bound)) {	
		#endif
			// -----------------------------------------------
			// Exit condition is groups here. It allows pipelining
			if (positive_direction == true) { 
				if (cons_succ >= DockConst_cons_limit) {
					#if defined (FIXED_POINT_LS7)
					fixpt_rho = fixpt_rho << 1;
					#else
					rho = LS_EXP_FACTOR*rho;
					#endif
					cons_fail = 0;
					cons_succ = 0;
				}
				else if (cons_fail >= DockConst_cons_limit) {
					#if defined (FIXED_POINT_LS7)
					fixpt_rho = fixpt_rho >> 1;
					#else
					rho = LS_CONT_FACTOR*rho;
					#endif
					cons_fail = 0;
					cons_succ = 0;
				}
				iteration_cnt++;
			}

			#if defined (DEBUG_KRNL_LS7)
			printf("LS7 positive?: %u, iteration_cnt: %u, rho: %f, limit rho: %f\n", positive_direction, iteration_cnt, rho, DockConst_rho_lower_bound);
			#endif
			// -----------------------------------------------

			#if defined (FIXED_POINT_LS7)
			fixedpt entity_possible_new_genotype [ACTUAL_GENOTYPE_LENGTH];
			fixedpt genotype_bias                [ACTUAL_GENOTYPE_LENGTH];
			fixedpt deviate_plus_bias            [ACTUAL_GENOTYPE_LENGTH];
			fixedpt deviate_minus_bias           [ACTUAL_GENOTYPE_LENGTH];
			#else
			float entity_possible_new_genotype   [ACTUAL_GENOTYPE_LENGTH];
			float genotype_bias                  [ACTUAL_GENOTYPE_LENGTH];
			float deviate_plus_bias              [ACTUAL_GENOTYPE_LENGTH];
			float deviate_minus_bias             [ACTUAL_GENOTYPE_LENGTH];
			#endif

			// Tell Krnl_Conf_Arbiter, LS7 is done
			// Not completely strict as the (iteration_cnt < DockConst_max_num_of_iters) is ignored
			// In practice, rho condition dominates most of the cases
			#if defined (FIXED_POINT_LS7)
169
170
			int tmp_int = (fixpt_rho < DockConst_rho_lower_bound)?0:1;
			write_pipe_block(chan_LS2Arbiter_LS7_end, &tmp_int);
171
			#else
172
173
			int tmp_int = (rho < DockConst_rho_lower_bound)?0:1;
			write_pipe_block(chan_LS2Arbiter_LS7_end, &tmp_int);
174
			#endif
lvs's avatar
lvs committed
175
/*
176
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
177
*/
178
179
180
		
			// new random deviate
			// rho is the deviation of the uniform distribution
181
			__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
182
			LOOP_FOR_LS7_WRITE_GENOTYPE:
183
			for (uchar i=0; i<DockConst_num_of_genes; i++) {
lvs's avatar
lvs committed
184
185
186
				float tmp_prng;
				read_pipe_block(chan_PRNG2GA_LS7_float_prng, &tmp_prng);
/*
187
				mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
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

				#if defined (FIXED_POINT_LS7)
				fixedpt fixpt_tmp_prng = *(fixedpt*) &tmp_prng;

				// tmp1 is genotype_deviate
				fixedpt fixpt_tmp1 = fixedpt_mul(fixpt_rho, ((fixpt_tmp_prng << 1) - FIXEDPT_ONE));

				if (i<3) { fixpt_tmp1 = fixedpt_mul(fixpt_tmp1, DockConst_base_dmov_mul_sqrt3); }
				else     { fixpt_tmp1 = fixedpt_mul(fixpt_tmp1, DockConst_base_dang_mul_sqrt3); }

				fixedpt deviate = fixedpt_mul(0x6666, fixpt_tmp1);

				// tmp2 is the addition: genotype_deviate + genotype_bias
				// tmp3 is entity_possible_new_genotype
				fixedpt tmp_bias = (iteration_cnt == 1)? 0:genotype_bias [i];
				fixedpt bias = fixedpt_mul(0x9999, tmp_bias);

				deviate_plus_bias  [i] = deviate + bias;
				deviate_minus_bias [i] = deviate - bias;

				fixedpt fixpt_tmp2 = fixpt_tmp1 + tmp_bias;
				fixedpt fixpt_tmp3 = (positive_direction == true)? (genotype [i] + fixpt_tmp2): 
								                   (genotype [i] - fixpt_tmp2);

213
				if (i>2) {if (i==4) { fixpt_tmp3 = fixedpt_map_angle_180(fixpt_tmp3);}
214
215
216
					  else      { fixpt_tmp3 = fixedpt_map_angle_360(fixpt_tmp3);}}

				entity_possible_new_genotype [i] = fixpt_tmp3;
lvs's avatar
lvs committed
217
218
				float tmp_float = fixedpt_tofloat(fixpt_tmp3);
				write_pipe_block(chan_LS2Conf_LS7_genotype, &tmp_float);
219
220
221
222
223
224
225
226
227
228
229
230
231

				#else
				// tmp1 is genotype_deviate
				float tmp1 = rho * (2.0f*tmp_prng - 1.0f);

				if (i<3) { tmp1 = tmp1 * DockConst_base_dmov_mul_sqrt3; }
				else 	 { tmp1 = tmp1 * DockConst_base_dang_mul_sqrt3; }

				float deviate = 0.4f*tmp1;

				// tmp2 is the addition: genotype_deviate + genotype_bias
				// tmp3 is entity_possible_new_genotype
				float tmp_bias = (iteration_cnt == 1)? 0.0f:genotype_bias [i];
lvs's avatar
lvs committed
232
				float bias = 0.6f * tmp_bias;
233
234
235
236
237
238
239

				deviate_plus_bias  [i] = deviate + bias;
				deviate_minus_bias [i] = deviate - bias;

				float tmp2 = tmp1 + tmp_bias;
				float tmp3 = (positive_direction == true)? (genotype [i] + tmp2): (genotype [i] - tmp2);

240
				if (i>2) {if (i==4) { tmp3 = map_angle_180(tmp3);}
241
242
243
					  else      { tmp3 = map_angle_360(tmp3);}}

				entity_possible_new_genotype [i] = tmp3;
lvs's avatar
lvs committed
244
				write_pipe_block(chan_LS2Conf_LS7_genotype, &tmp3);
245
246
247
248
249
250
251
252
253
254
255
				#endif

				#if defined (DEBUG_KRNL_LS7)
				printf("LS7_genotype sent: %u\n", i);
				#endif
			}

			//printf("Energy to calculate sent from LS7 ... ");

			float energyIA_LS_rx;
			float energyIE_LS_rx;
lvs's avatar
lvs committed
256
/*
257
258
			bool intra_valid = false;
			bool inter_valid = false;
lvs's avatar
lvs committed
259
260
261
262
263
*/
			int intra_valid = 1;
			int inter_valid = 1;

/*
264
			while( (intra_valid == false) || (inter_valid == false)) {
lvs's avatar
lvs committed
265
*/
266
			__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
267
			LOOP_WHILE_LS7_READ_ENERGIES:
lvs's avatar
lvs committed
268
269
			while( (intra_valid != 0) || (inter_valid != 0)) {
/*
270
				if (intra_valid == false) {
lvs's avatar
lvs committed
271
272
*/
				if (intra_valid != 0) {
lvs's avatar
lvs committed
273
					intra_valid = read_pipe(chan_Intrae2StoreLS_LS7_intrae, &energyIA_LS_rx );
274
				}
lvs's avatar
lvs committed
275
/*
276
				else if (inter_valid == false) {
lvs's avatar
lvs committed
277
278
*/
				else if (inter_valid != 0) {
lvs's avatar
lvs committed
279
					inter_valid = read_pipe(chan_Intere2StoreLS_LS7_intere, &energyIE_LS_rx);
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
				}
			}

			float candidate_energy = energyIA_LS_rx + energyIE_LS_rx;

			// update LS energy-evaluation count
			LS_eval++;

			#if defined (DEBUG_KRNL_LS7)
			printf("INTERE received in LS7: %u\n", LS_eval);
			#endif

			#if defined (FIXED_POINT_LS7)
			if (candidate_energy < current_energy) {
				// updating offspring_genotype
				// updating genotype_bias
296
				__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
297
				LOOP_FOR_LS7_FIXEDPT_UPDATE_POS_GENOTYPE:
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
				for (uchar i=0; i<DockConst_num_of_genes; i++) {
					genotype_bias [i] = (positive_direction == true) ? deviate_plus_bias  [i]: 
											   deviate_minus_bias [i]; 

					genotype [i] = entity_possible_new_genotype [i];
				}

				current_energy = candidate_energy;
				cons_succ++;
				cons_fail = 0;
				positive_direction = true;

			}
			else {
				// updating (halving) genotype_bias
313
				__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
314
				LOOP_FOR_LS7_FIXEDPT_UPDATE_NEG_GENOTYPE:
315
316
317
318
319
320
321
322
323
324
325
326
327
328
				for (uchar i=0; i<DockConst_num_of_genes; i++) {
					genotype_bias [i] = (iteration_cnt == 1)? 0: (genotype_bias [i] >> 1);
				}

				if (positive_direction == false) {
					cons_fail++;
					cons_succ = 0;
				}
				positive_direction = !positive_direction;
			}
			#else
			if (candidate_energy < current_energy) {
				// updating offspring_genotype
				// updating genotype_bias
329
				__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
330
				LOOP_FOR_LS7_FLOATPT_UPDATE_POS_GENOTYPE:
331
332
333
334
335
336
337
338
339
340
341
342
343
				for (uchar i=0; i<DockConst_num_of_genes; i++) {
					genotype_bias [i] = (positive_direction == true) ?  deviate_plus_bias  [i] : 
											    deviate_minus_bias [i] ;
					genotype [i] = entity_possible_new_genotype [i];
				}	

				current_energy = candidate_energy;
				cons_succ++;
				cons_fail = 0;
				positive_direction = true;				
			}
			else {
				// updating (halving) genotype_bias
344
				__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
345
				LOOP_FOR_LS7_FLOATPT_UPDATE_NEG_GENOTYPE:
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
				for (uchar i=0; i<DockConst_num_of_genes; i++) {
					genotype_bias [i] = (iteration_cnt == 1)? 0.0f: (0.5f*genotype_bias [i]);
				}

				if (positive_direction == false) {
					cons_fail++;
					cons_succ = 0;
				}
				positive_direction = !positive_direction;
			}
			#endif

		} // end of while (iteration_cnt) && (rho)
	
		#if defined (DEBUG_KRNL_LS7)
		printf("Out of while iter LS7\n");
		#endif

		// write back data to GA
365
		__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
366
		LOOP_FOR_LS7_WRITEBACK2GA:
367
368
369
		for (uchar i=0; i<DockConst_num_of_genes; i++) {
			if (i == 0) {
				float2 evalenergy  = {*(float*)&LS_eval, current_energy};
lvs's avatar
lvs committed
370
				write_pipe_block(chan_LS2GA_LS7_evalenergy, &evalenergy);
371
			}
lvs's avatar
lvs committed
372
/*
373
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
374
*/
375
376

			#if defined (FIXED_POINT_LS7)
lvs's avatar
lvs committed
377
378
			float tmp_float = fixedpt_tofloat(genotype [i]);
			write_pipe_block(chan_LS2GA_LS7_genotype, &tmp_float);
379
			#else
lvs's avatar
lvs committed
380
			write_pipe_block(chan_LS2GA_LS7_genotype, &genotype [i]);
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
			#endif
		}

	} // End of if (valid)
	
} // End of while (valid)		

#if defined (DEBUG_ACTIVE_KERNEL)
printf("	%-20s: %s\n", "Krnl_LS7", "disabled");		
#endif
	
}

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