Krnl_LS6.cl 10.2 KB
Newer Older
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
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
__kernel __attribute__ ((max_global_work_dim(0)))
void Krnl_LS6(
		unsigned short            DockConst_max_num_of_iters,		
		#if defined (FIXED_POINT_LS6)
		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_LS6)
		fixedpt                   DockConst_base_dang_mul_sqrt3,
		#else
   		float                     DockConst_base_dang_mul_sqrt3,
		#endif

		unsigned char             DockConst_cons_limit
)
{
	bool valid = true;

while(valid) {

	bool active;
	bool valid_active = false;

	float current_energy;
	bool valid_energy = false;

	while( (valid_active == false) && (valid_energy == false)) {
Leonardo Solis's avatar
Leonardo Solis committed
34
		active         = read_channel_nb_intel(chan_GA2LS_Off_active[5], &valid_active);
35
		current_energy = read_channel_nb_intel(chan_GA2LS_energy[5],  &valid_energy);
36
37
38
39
40
41
42
43
44
45
46
47
48
	}
	valid = active || valid_energy;

	if (valid) {

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

		for (uchar i=0; i<DockConst_num_of_genes; i++) {
			#if defined (FIXED_POINT_LS6)
49
			float tmp_gene = read_channel_intel(chan_GA2LS_genotype[5]);
50
51
			genotype [i] = fixedpt_fromfloat(tmp_gene);
			#else
52
			genotype [i] = read_channel_intel(chan_GA2LS_genotype[5]);
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
			#endif
		}
	
		#if defined (DEBUG_KRNL_LS6)
		printf("In of while iter LS6\n");
		#endif

		#if defined (FIXED_POINT_LS6)
		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
		#if defined (FIXED_POINT_LS6)
		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_LS6)
					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_LS6)
					fixpt_rho = fixpt_rho >> 1;
					#else
					rho = LS_CONT_FACTOR*rho;
					#endif
					cons_fail = 0;
					cons_succ = 0;
				}
				iteration_cnt++;
			}

			#if defined (DEBUG_KRNL_LS6)
			printf("LS6 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_LS6)
			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, LS6 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_LS6)
Leonardo Solis's avatar
Leonardo Solis committed
122
			write_channel_intel(chan_LS2Arbiter_end[5], (fixpt_rho < DockConst_rho_lower_bound)?true:false);
123
			#else
Leonardo Solis's avatar
Leonardo Solis committed
124
			write_channel_intel(chan_LS2Arbiter_end[5], (rho < DockConst_rho_lower_bound)?true:false);
125
126
			#endif
			mem_fence(CLK_CHANNEL_MEM_FENCE);
127
128
129
130
131
132
133
134
135
136
137

			// read pnrgs from prng_kernel using channels.
			// these and following loop were initially merged for deeper pipelining.
			// however the prng channel-read created a bottleneck II=15.
			// splitted loops have each II=1.
			float float_prng [ACTUAL_GENOTYPE_LENGTH];

			for (uchar i=0; i<DockConst_num_of_genes; i++) {
				float_prng [i] = read_channel_intel(chan_PRNG2LS_float_prng[5]);
			}
			mem_fence(CLK_CHANNEL_MEM_FENCE);
138
139
140
141
		
			// new random deviate
			// rho is the deviation of the uniform distribution
			for (uchar i=0; i<DockConst_num_of_genes; i++) {
142
				/*
Leonardo Solis's avatar
Leonardo Solis committed
143
				float tmp_prng = read_channel_intel(chan_PRNG2LS_float_prng[5]);
144
				mem_fence(CLK_CHANNEL_MEM_FENCE);
145
146
				*/
				float tmp_prng = float_prng[i];
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

				#if defined (FIXED_POINT_LS6)
				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);
Leonardo Solis's avatar
Leonardo Solis committed
170

171
172
				if (i>2) {if (i==4) { fixpt_tmp3 = fixedpt_map_angle_180(fixpt_tmp3);}
					  else      { fixpt_tmp3 = fixedpt_map_angle_360(fixpt_tmp3);}}
173
174

				entity_possible_new_genotype [i] = fixpt_tmp3;
Leonardo Solis's avatar
Leonardo Solis committed
175
				write_channel_intel(chan_LS2Conf_genotype[5], fixedpt_tofloat(fixpt_tmp3));
176
177
178
179
180
181
182
183
184
185
186
187
188

				#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];
189
				float bias = 0.6f * tmp_bias;
190
191
192
193
194
195

				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);
Leonardo Solis's avatar
Leonardo Solis committed
196

197
198
				if (i>2) {if (i==4) { tmp3 = map_angle_180(tmp3);}
					  else      { tmp3 = map_angle_360(tmp3);}}
199
200

				entity_possible_new_genotype [i] = tmp3;
Leonardo Solis's avatar
Leonardo Solis committed
201
				write_channel_intel(chan_LS2Conf_genotype[5], tmp3);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
				#endif

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

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

			float energyIA_LS_rx;
			float energyIE_LS_rx;
			bool intra_valid = false;
			bool inter_valid = false;
			while( (intra_valid == false) || (inter_valid == false)) {
				if (intra_valid == false) {
Leonardo Solis's avatar
Leonardo Solis committed
217
					energyIA_LS_rx = read_channel_nb_intel(chan_Intrae2StoreLS_intrae[5], &intra_valid);
218
219
				}
				else if (inter_valid == false) {
Leonardo Solis's avatar
Leonardo Solis committed
220
					energyIE_LS_rx = read_channel_nb_intel(chan_Intere2StoreLS_intere[5], &inter_valid);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
				}
			}

			float candidate_energy = energyIA_LS_rx + energyIE_LS_rx;

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

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

			#if defined (FIXED_POINT_LS6)
			if (candidate_energy < current_energy) {
				// updating offspring_genotype
				// updating genotype_bias

				//#pragma unroll 16
				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

				//#pragma unroll 16
				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

				//#pragma unroll 16
				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

				//#pragma unroll 16
				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_LS6)
		printf("Out of while iter LS6\n");
		#endif

		// write back data to GA
		for (uchar i=0; i<DockConst_num_of_genes; i++) {
			if (i == 0) {
				float2 evalenergy  = {*(float*)&LS_eval, current_energy};
309
				write_channel_intel(chan_LS2GA_evalenergy[5], evalenergy);
310
311
312
313
			}
			mem_fence(CLK_CHANNEL_MEM_FENCE);

			#if defined (FIXED_POINT_LS6)
314
			write_channel_intel(chan_LS2GA_genotype[5], fixedpt_tofloat(genotype [i]));
315
			#else
316
			write_channel_intel(chan_LS2GA_genotype[5], genotype [i]);
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
			#endif
		}

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

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

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