Krnl_GA.cl 20.3 KB
Newer Older
1
2
3
//IC:  initial calculation of energy of populations
//GG:  genetic generation 
//LS:  local search
4
//OFF: turn off 
Leonardo Solis's avatar
Leonardo Solis committed
5
6

#include "../defines.h"
Leonardo Solis's avatar
Leonardo Solis committed
7

8
9
10
11
#define PIPE_DEPTH_16  16
#define PIPE_DEPTH_64  64
#define PIPE_DEPTH_512 512

12
13
14
15
16
17
18
19
20
21
22
// Status of pipe operation
// Success: 0
// Failure: negative value, e.g.: -1, -2, etc

// Important: the evaluation of failure of "pipe-expr" 
// must be done: (pipe-expr != PIPE_STATUS_SUCCESS),
// as a failure is characterize by any negative integer number.
typedef int nb_pipe_status;
#define PIPE_STATUS_SUCCESS      0
#define PIPE_STATUS_FAILURE	-1

23
// Send active signal to IGL_Arbiter
lvs's avatar
lvs committed
24
// Resized to valid SDAccel depths: 16, 32, ...
25
26
pipe int    chan_GA2IGL_IC_active	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2IGL_GG_active	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
27

28
// Send genotypes from producers (IC, GG, LSs) to Conform
29
30
pipe float  chan_IC2Conf_genotype          __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_GG2Conf_genotype          __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
Leonardo Solis's avatar
Leonardo Solis committed
31

32
// Send ligand-atom positions from Conform to InterE & IntraE
lvs's avatar
lvs committed
33
// Resized to valid SDAccel depths: 16, 32, ...
34
35
pipe float8  chan_Conf2Intere_xyz           __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe char    chan_Conf2Intere_actmode	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
36

37
38
pipe float8  chan_Conf2Intrae_xyz           __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe char    chan_Conf2Intrae_actmode       __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
39

40
// Send energy values from InterE & IntraE to genotype-senders (IC, GG, LSs)
lvs's avatar
lvs committed
41
// Resized to valid SDAccel depths: 16, 32, ...
42
43
pipe float  chan_Intere2StoreIC_intere     __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreGG_intere     __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
44

45
46
pipe float  chan_Intrae2StoreIC_intrae     __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreGG_intrae     __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
Leonardo Solis's avatar
Leonardo Solis committed
47

48
// Send PRNG outputs from generators to consumers
lvs's avatar
lvs committed
49
// Resized to valid SDAccel depths: 16, 32, ...
50
51
52
pipe float8   chan_PRNG2GA_BT_ushort_float_prng	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe uchar2   chan_PRNG2GA_GG_uchar_prng	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float    chan_PRNG2GA_GG_float_prng     	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
53
54

// Turn-off signals to PRNG generators
lvs's avatar
lvs committed
55
// Resized to valid SDAccel depths: 16, 32, ...
Leonardo Solis's avatar
Leonardo Solis committed
56
57
58
pipe int    chan_GA2PRNG_BT_ushort_float_off	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_GG_uchar_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_GG_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
lvs's avatar
lvs committed
59
60

// Send genotype-producer-pipe selector and genotype 
61
// from IGL_Arbiter to Conform
lvs's avatar
lvs committed
62
// Resized to valid SDAccel depths: 16, 32, ...
63
64
pipe char   chan_IGL2Conform_actmode	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16))); // active, mode
pipe float  chan_IGL2Conform_genotype       __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_512)));
Leonardo Solis's avatar
Leonardo Solis committed
65

66
// Turn-off signal to IGL_Arbiter, Conform, InterE, IntraE
lvs's avatar
lvs committed
67
// Resized to valid SDAccel depths: 16, 32, ...
68
pipe int   chan_IGLArbiter_Off		    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
Leonardo Solis's avatar
Leonardo Solis committed
69

Leonardo Solis's avatar
Leonardo Solis committed
70
// --------------------------------------------------------------------------
71
// Map the argument into the interval 0 - 180, or 0 - 360
Leonardo Solis's avatar
Leonardo Solis committed
72
73
74
// by adding/subtracting n*ang_max to/from it.
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------
75
__attribute__((always_inline))
Leonardo Solis's avatar
Leonardo Solis committed
76
77
78
float map_angle_180(float angle)
{
	float x = angle;
79
80
81
82
83
84
	//while (x < 0.0f)
	if (x < 0.0f)   
	{ x += 180.0f; }
	//while (x > 180.0f)
	if (x > 180.0f) 
	{ x -= 180.0f; }
Leonardo Solis's avatar
Leonardo Solis committed
85
86
87
	return x;
}

88
__attribute__((always_inline))
Leonardo Solis's avatar
Leonardo Solis committed
89
90
91
float map_angle_360(float angle)
{
	float x = angle;
92
93
94
95
96
97
	//while (x < 0.0f)
	if (x < 0.0f)
	{ x += 360.0f; }
	//while (x > 360.0f)
	if (x > 360.0f)
	{ x -= 360.0f;}
Leonardo Solis's avatar
Leonardo Solis committed
98
99
100
	return x;
}

101
// --------------------------------------------------------------------------
102
// Lamarckian Genetic-Algorithm (GA): GA + LS (Local Search) 
103
104
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
105
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
lvs's avatar
lvs committed
106
void Krnl_GA(
107
108
	     __global const float*           restrict GlobPopulationCurrentInitial,
	     __global       float*           restrict GlobPopulationCurrentFinal,
109
	     __global       float*           restrict GlobEnergyCurrent,
110
111
   	     __global       unsigned int*    restrict GlobEvals_performed,
             __global       unsigned int*    restrict GlobGens_performed,
Leonardo Solis's avatar
Leonardo Solis committed
112
113
114
115
116
117
118
			    unsigned int              DockConst_pop_size,
		     	    unsigned int              DockConst_num_of_energy_evals,
			    unsigned int              DockConst_num_of_generations,
		      	    float                     DockConst_tournament_rate,
			    float                     DockConst_mutation_rate,
		    	    float                     DockConst_abs_max_dmov,
			    float                     DockConst_abs_max_dang,
Leonardo Solis's avatar
Leonardo Solis committed
119
120
		    	    float                     Host_two_absmaxdmov,
			    float                     Host_two_absmaxdang,
Leonardo Solis's avatar
Leonardo Solis committed
121
122
			    float                     DockConst_crossover_rate,
			    unsigned int              DockConst_num_of_lsentities,
123
			    unsigned char             DockConst_num_of_genes,
124
125
126
127
	                    unsigned short            Host_RunId,
			    unsigned int 	      Host_Offset_Pop,
			    unsigned int	      Host_Offset_Ene
	     )
128
{
129
	#if defined (DEBUG_KRNL_GA)
130
	printf("\n");
Leonardo Solis's avatar
Leonardo Solis committed
131
	printf("%-40s %u\n", "DockConst_pop_size: ",        		DockConst_pop_size);
Leonardo Solis's avatar
Leonardo Solis committed
132
133
134
135
136
137
	printf("%-40s %u\n", "DockConst_num_of_energy_evals: ",  	DockConst_num_of_energy_evals);
	printf("%-40s %u\n", "DockConst_num_of_generations: ",  	DockConst_num_of_generations);
	printf("%-40s %f\n", "DockConst_tournament_rate: ", 		DockConst_tournament_rate);
	printf("%-40s %f\n", "DockConst_mutation_rate: ", 		DockConst_mutation_rate);
	printf("%-40s +/-%fA\n", "DockConst_abs_max_dmov: ",		DockConst_abs_max_dmov);
	printf("%-40s +/-%f°\n", "DockConst_abs_max_dang: ",  		DockConst_abs_max_dang);
Leonardo Solis's avatar
Leonardo Solis committed
138
139
	printf("%-40s +/-%fA\n", "Host_two_absmaxdmov: ",		Host_two_absmaxdmov);
	printf("%-40s +/-%f°\n", "Host_two_absmaxdang: ",  		Host_two_absmaxdang);
Leonardo Solis's avatar
Leonardo Solis committed
140
	printf("%-40s %f\n", "DockConst_crossover_rate: ", 		DockConst_crossover_rate);
Leonardo Solis's avatar
Leonardo Solis committed
141
	printf("%-40s %u\n", "DockConst_num_of_lsentities: ",   	DockConst_num_of_lsentities);
Leonardo Solis's avatar
Leonardo Solis committed
142
	printf("%-40s %u\n", "DockConst_num_of_genes: ",        	DockConst_num_of_genes);
143
144
	#endif

145
	// Other banking configuration (see PopNext, eneNext) might reduce logic
146
	// but makes PopCurr stallable
Leonardo Solis's avatar
Leonardo Solis committed
147
148
149
	__local float LocalPopCurr[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
	__local float LocalEneCurr[MAX_POPSIZE];

150
151
152
	__global const float* GlobPopCurrInitial = & GlobPopulationCurrentInitial [Host_Offset_Pop];
	__global       float* GlobPopCurrFinal   = & GlobPopulationCurrentFinal   [Host_Offset_Pop];
	__global       float* GlobEneCurr        = & GlobEnergyCurrent     	  [Host_Offset_Ene];
Leonardo Solis's avatar
Leonardo Solis committed
153

154
	// ------------------------------------------------------------------
155
	// Initial Calculation (IC) of scores
156
	// ------------------------------------------------------------------
157
	__attribute__((xcl_pipeline_loop))
158
	LOOP_FOR_GA_IC_OUTER:
Leonardo Solis's avatar
Leonardo Solis committed
159
	for (ushort pop_cnt = 0; pop_cnt < DockConst_pop_size; pop_cnt++) {
160
		// Calculate energy
161
162
		const int tmp_int_zero = 0;
		write_pipe_block(chan_GA2IGL_IC_active, &tmp_int_zero);
lvs's avatar
lvs committed
163
164
165
/*
		mem_fence(CLK_CHANNEL_MEM_FENCE);
*/
166
		__attribute__((xcl_pipeline_loop))
167
		LOOP_FOR_GA_IC_INNER_WRITE_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
168
		for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
169
			float tmp_ic;
170
			tmp_ic = GlobPopCurrInitial[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt];
Leonardo Solis's avatar
Leonardo Solis committed
171

Leonardo Solis's avatar
Leonardo Solis committed
172
			LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_ic;
Leonardo Solis's avatar
Leonardo Solis committed
173
			write_pipe_block(chan_IC2Conf_genotype, &tmp_ic);	
174
		}
175

Leonardo Solis's avatar
Leonardo Solis committed
176
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
177
		printf("\nIC - tx pop: %u", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
178
		#endif
179

180
		// Read energy
181
182
		float energyIA_IC_rx;
		float energyIE_IC_rx;
183
184
185
186

		nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
		nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;	

187
		__attribute__((xcl_pipeline_loop))
188
		LOOP_WHILE_GA_IC_INNER_READ_ENERGY:
189
		while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {
lvs's avatar
lvs committed
190

191
			if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
192
				intra_valid = read_pipe(chan_Intrae2StoreIC_intrae, &energyIA_IC_rx);
193
			}
194
			else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
195
				inter_valid = read_pipe(chan_Intere2StoreIC_intere, &energyIE_IC_rx);
196
197
198
			}
		}

Leonardo Solis's avatar
Leonardo Solis committed
199
		LocalEneCurr[pop_cnt] = energyIA_IC_rx + energyIE_IC_rx;
200

201
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
202
		printf(", IC - rx pop: %u\n", pop_cnt); 		
203
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
204
	}
205
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
206

Leonardo Solis's avatar
Leonardo Solis committed
207
	uint eval_cnt = DockConst_pop_size; // takes into account the IC evals
208

Leonardo Solis's avatar
Leonardo Solis committed
209
	uint generation_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
210

211
212
	__attribute__((xcl_pipeline_loop))
	LOOP_WHILE_GA_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
213
	while ((eval_cnt < DockConst_num_of_energy_evals) && (generation_cnt < DockConst_num_of_generations)) {
Leonardo Solis's avatar
Leonardo Solis committed
214

215
216
217
		//float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
		//float LocalEneNext[MAX_POPSIZE];

218
		// This configuration reduces logic and does not increase block RAM usage
lvs's avatar
lvs committed
219
/*
220
221
222
223
224
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(32),
			              )) LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
lvs's avatar
lvs committed
225
226
*/
		float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
227

lvs's avatar
lvs committed
228
/*
229
230
231
232
233
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(4),
			              )) LocalEneNext[MAX_POPSIZE];
lvs's avatar
lvs committed
234
235
*/
		float LocalEneNext[MAX_POPSIZE];
236

237
		// ------------------------------------------------------------------
238
		// Genetic Generation (GG)
239
		// ------------------------------------------------------------------
lvs's avatar
lvs committed
240
/*
Leonardo Solis's avatar
Leonardo Solis committed
241
242
243
244
245
		float __attribute__ ((
				       memory,
		   		       numbanks(1),
			               bankwidth(64),
			               singlepump,
246
 			               numreadports(6),
Leonardo Solis's avatar
Leonardo Solis committed
247
248
			               numwriteports(1)
			              )) loc_energies[MAX_POPSIZE];
lvs's avatar
lvs committed
249
250
*/
		float loc_energies[MAX_POPSIZE];
Leonardo Solis's avatar
Leonardo Solis committed
251
252
253

		ushort best_entity = 0;

254
255
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_SHIFT: 
Leonardo Solis's avatar
Leonardo Solis committed
256
257
258
259
260
//		for (ushort pop_cnt=1; pop_cnt<DockConst_pop_size; pop_cnt++) {
		for (ushort pop_cnt=0; pop_cnt<DockConst_pop_size; pop_cnt++) {
			// copy energy to local memory
			loc_energies[pop_cnt] = LocalEneCurr[pop_cnt];

261
262
263
264
265
266
267
268
			#if defined (DEBUG_KRNL_GA)
			if (pop_cnt==0) {printf("\n");}
			printf("%3u %20.6f\n", pop_cnt, loc_energies[pop_cnt]);
			#endif

			if (loc_energies[pop_cnt] < loc_energies[best_entity]) {
				best_entity = pop_cnt;
			}
Leonardo Solis's avatar
Leonardo Solis committed
269
		}
270

271
272
273
274
		#if defined (DEBUG_KRNL_GA)
		printf("best_entity: %3u, energy: %20.6f\n", best_entity, loc_energies[best_entity]);
		#endif

275
		/*
276
277
		#pragma ivdep array (LocalPopNext)
		#pragma ivdep array (LocalEneNext)
278
		*/
279
280
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_OUTER_GLOBAL: 
Leonardo Solis's avatar
Leonardo Solis committed
281
		for (ushort new_pop_cnt = 1; new_pop_cnt < DockConst_pop_size; new_pop_cnt++) {
282
283

			// ---------------------------------------------------
284
			// Elitism: copying the best entity to new population
285
286
			// ---------------------------------------------------
			if (new_pop_cnt == 1) {
287
				__attribute__((xcl_pipeline_loop))
288
				LOOP_FOR_GA_INNER_ELITISM:
289
				for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
290
					LocalPopNext[0][gene_cnt & MASK_GENOTYPE] = LocalPopCurr[best_entity][gene_cnt & MASK_GENOTYPE]; 	
291
292
293
294
				} 		
				LocalEneNext[0] = loc_energies[best_entity];
			}

295
			#if defined (DEBUG_KRNL_GA)
296
			printf("Krnl_GA: %u\n", new_pop_cnt);
297
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
298

Leonardo Solis's avatar
Leonardo Solis committed
299
300
			float local_entity_1 [ACTUAL_GENOTYPE_LENGTH];
			float local_entity_2 [ACTUAL_GENOTYPE_LENGTH]; 
Leonardo Solis's avatar
Leonardo Solis committed
301
		
302
			// ---------------------------------------------------
303
			// Binary-Tournament (BT) selection
304
			// ---------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
305

306
307
			// Get ushort binary_tournament selection prngs (parent index)
			// Get float binary_tournament selection prngs (tournament rate)
lvs's avatar
lvs committed
308
309
310
			float8 bt_tmp;
			read_pipe_block(chan_PRNG2GA_BT_ushort_float_prng, &bt_tmp);
/*
Leonardo Solis's avatar
Leonardo Solis committed
311
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
312
*/
Leonardo Solis's avatar
Leonardo Solis committed
313
//printf("test point 1\n");
314
			// Convert: float prng that must be still converted to short
315
316
317
318
319
			float bt_tmp_uf0 = bt_tmp.s0;
			float bt_tmp_uf1 = bt_tmp.s2;
			float bt_tmp_uf2 = bt_tmp.s4;
			float bt_tmp_uf3 = bt_tmp.s6;

Leonardo Solis's avatar
Leonardo Solis committed
320
			// short prng ready to be used, replace ushort prng_BT_U[4];
321
/*
322
323
324
325
			ushort bt_tmp_u0 = *(uint*)&bt_tmp_uf0;
			ushort bt_tmp_u1 = *(uint*)&bt_tmp_uf1;
			ushort bt_tmp_u2 = *(uint*)&bt_tmp_uf2;
			ushort bt_tmp_u3 = *(uint*)&bt_tmp_uf3;
326
327
328
329
330
331
332
*/
			// Check "Krnl_Prng_BT_ushort_float"
			// To surpass error in hw_emu		
			ushort bt_tmp_u0 = bt_tmp_uf0;
			ushort bt_tmp_u1 = bt_tmp_uf1;
			ushort bt_tmp_u2 = bt_tmp_uf2;
			ushort bt_tmp_u3 = bt_tmp_uf3;
333

Leonardo Solis's avatar
Leonardo Solis committed
334
			// float prng ready to used, replace float prng_BT_F[4];
335
336
337
338
339
			float bt_tmp_f0 = bt_tmp.s1;
			float bt_tmp_f1 = bt_tmp.s3;
			float bt_tmp_f2 = bt_tmp.s5;
			float bt_tmp_f3 = bt_tmp.s7;

Leonardo Solis's avatar
Leonardo Solis committed
340
341
			ushort parent1;
			ushort parent2; 
Leonardo Solis's avatar
Leonardo Solis committed
342

343
			// First parent
344
345
346
347
348
349
350
			if (loc_energies[bt_tmp_u0] < loc_energies[bt_tmp_u1]) {
				if (bt_tmp_f0 < DockConst_tournament_rate) {parent1 = bt_tmp_u0;}
				else				           {parent1 = bt_tmp_u1;}}
			else {
				if (bt_tmp_f1 < DockConst_tournament_rate) {parent1 = bt_tmp_u1;}
				else				           {parent1 = bt_tmp_u0;}}

351
			// The better will be the second parent
352
353
354
355
356
357
			if (loc_energies[bt_tmp_u2] < loc_energies[bt_tmp_u3]) {
				if (bt_tmp_f2 < DockConst_tournament_rate) {parent2 = bt_tmp_u2;}
				else		          	           {parent2 = bt_tmp_u3;}}
			else {
				if (bt_tmp_f3 < DockConst_tournament_rate) {parent2 = bt_tmp_u3;}
				else			                   {parent2 = bt_tmp_u2;}}
Leonardo Solis's avatar
Leonardo Solis committed
358

359
360
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_INNER_BT:
Leonardo Solis's avatar
Leonardo Solis committed
361
			// local_entity_1 and local_entity_2 are population-parent1, population-parent2
Leonardo Solis's avatar
Leonardo Solis committed
362
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
363
364
				local_entity_1[gene_cnt & MASK_GENOTYPE] = LocalPopCurr[parent1][gene_cnt & MASK_GENOTYPE];
				local_entity_2[gene_cnt & MASK_GENOTYPE] = LocalPopCurr[parent2][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
365
			}
366

367
			// ---------------------------------------------------
368
			// Mating parents
369
			// ---------------------------------------------------	
Leonardo Solis's avatar
Leonardo Solis committed
370

371
372
			// get uchar genetic_generation prngs (gene index)
			// get float genetic_generation prngs (mutation rate)
lvs's avatar
lvs committed
373
374
375
			uchar2 prng_GG_C;
			read_pipe_block(chan_PRNG2GA_GG_uchar_prng, &prng_GG_C);
/*
Leonardo Solis's avatar
Leonardo Solis committed
376
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
377
*/
Leonardo Solis's avatar
Leonardo Solis committed
378
//printf("test point 2\n");
Leonardo Solis's avatar
Leonardo Solis committed
379
380
381
382

			uchar covr_point_low;
			uchar covr_point_high;
			bool twopoint_cross_yes = false;
Leonardo Solis's avatar
Leonardo Solis committed
383

384
385
386
			if (prng_GG_C.x == prng_GG_C.y) {covr_point_low = prng_GG_C.x;}
			else {
				twopoint_cross_yes = true;
Leonardo Solis's avatar
Leonardo Solis committed
387
388
389
390
				if (prng_GG_C.x < prng_GG_C.y) { covr_point_low  = prng_GG_C.x;
					                         covr_point_high = prng_GG_C.y; }
				else {		      		 covr_point_low  = prng_GG_C.y;
   								 covr_point_high = prng_GG_C.x; }
391
392
			}
			
393
			// Reuse of bt prng float as crossover-rate
Leonardo Solis's avatar
Leonardo Solis committed
394
			bool crossover_yes = (DockConst_crossover_rate > bt_tmp_f0);
Leonardo Solis's avatar
Leonardo Solis committed
395

396
397
			const int tmp_int_zero = 0;
			write_pipe_block(chan_GA2IGL_GG_active, &tmp_int_zero);
lvs's avatar
lvs committed
398
/*
Leonardo Solis's avatar
Leonardo Solis committed
399
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
400
*/
Leonardo Solis's avatar
Leonardo Solis committed
401
//printf("test point 3\n");
Leonardo Solis's avatar
Leonardo Solis committed
402

403
404
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_INNER_CROSS_MUT:
Leonardo Solis's avatar
Leonardo Solis committed
405
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
lvs's avatar
lvs committed
406
407
408
				float prngGG;
				read_pipe_block(chan_PRNG2GA_GG_float_prng, &prngGG);
/*
Leonardo Solis's avatar
Leonardo Solis committed
409
				mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
410
*/
Leonardo Solis's avatar
Leonardo Solis committed
411
//printf("test point 4\n");
Leonardo Solis's avatar
Leonardo Solis committed
412

Leonardo Solis's avatar
Leonardo Solis committed
413
				float tmp_offspring;
Leonardo Solis's avatar
Leonardo Solis committed
414

415
				// Performing crossover
Leonardo Solis's avatar
Leonardo Solis committed
416
417
418
419
420
421
422
				if (   	(
					crossover_yes && (										// crossover
					( (twopoint_cross_yes == true)  && ((gene_cnt <= covr_point_low) || (gene_cnt > covr_point_high)) )  ||	// two-point crossover 			 		
					( (twopoint_cross_yes == false) && (gene_cnt <= covr_point_low))  					// one-point crossover
					)) || 
					(!crossover_yes)	// no crossover
				   ) {
423
					tmp_offspring = local_entity_1[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
424
425
				}
				else {
426
					tmp_offspring = local_entity_2[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
427
428
				}

429
				// Performing mutation
Leonardo Solis's avatar
Leonardo Solis committed
430
				if (DockConst_mutation_rate > prngGG) {
Leonardo Solis's avatar
Leonardo Solis committed
431
					if(gene_cnt<3) {
Leonardo Solis's avatar
Leonardo Solis committed
432
						tmp_offspring = tmp_offspring + Host_two_absmaxdmov*prngGG-DockConst_abs_max_dmov;
Leonardo Solis's avatar
Leonardo Solis committed
433
434
435
					}
					else {
						float tmp;
Leonardo Solis's avatar
Leonardo Solis committed
436
437
438
						tmp = tmp_offspring + Host_two_absmaxdang*prngGG-DockConst_abs_max_dang;
						if (gene_cnt==4) { tmp_offspring = map_angle_180(tmp); }
						else             { tmp_offspring = map_angle_360(tmp); }
Leonardo Solis's avatar
Leonardo Solis committed
439
440
441
					}
				}

442
				// Calculate energy
Leonardo Solis's avatar
Leonardo Solis committed
443
				LocalPopNext [new_pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_offspring;
lvs's avatar
lvs committed
444
				write_pipe_block(chan_GG2Conf_genotype, &tmp_offspring);
Leonardo Solis's avatar
Leonardo Solis committed
445
//printf("test point 5\n");
Leonardo Solis's avatar
Leonardo Solis committed
446
			}
447

448
			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
449
			printf("GG - tx pop: %u", new_pop_cnt); 		
450
			#endif	
451

452
			// Read energy
453
454
			float energyIA_GG_rx;
			float energyIE_GG_rx;
lvs's avatar
lvs committed
455

456
457
458
			nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
			nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;

459
460
			__attribute__((xcl_pipeline_loop))
			LOOP_WHILE_GA_INNER_READ_ENERGIES:
461
462
463
			while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {

				if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
464
					intra_valid = read_pipe(chan_Intrae2StoreGG_intrae, &energyIA_GG_rx);
465
				}
466
				else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
467
					inter_valid = read_pipe(chan_Intere2StoreGG_intere, &energyIE_GG_rx);
468
				}
469

Leonardo Solis's avatar
Leonardo Solis committed
470
//printf("intra_valid: %i, inter_valid: %i\n", intra_valid, inter_valid);
471
			}
Leonardo Solis's avatar
Leonardo Solis committed
472
//printf("test point 5\n");			
Leonardo Solis's avatar
Leonardo Solis committed
473
			LocalEneNext[new_pop_cnt] = energyIA_GG_rx + energyIE_GG_rx;
Leonardo Solis's avatar
Leonardo Solis committed
474

475
			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
476
			printf(", GG - rx pop: %u\n", new_pop_cnt); 		
477
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
478
		} 
479
480
		// ------------------------------------------------------------------
		// LS: Local Search
481
		// Subject num_of_entity_for_ls pieces of offsprings to LS 
Leonardo Solis's avatar
Leonardo Solis committed
482
		// ------------------------------------------------------------------
483

484
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
485

486
		// Update current pops & energies
487
488
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_UPDATEPOP_OUTER:
489
		for (ushort pop_cnt=0; pop_cnt<DockConst_pop_size; pop_cnt++) {
490
491
492

			__attribute__((xcl_pipeline_loop))
			LOOP_GA_UPDATEPOP_INNER:
Leonardo Solis's avatar
Leonardo Solis committed
493
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
494
				LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = LocalPopNext[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
495
496
497
498
499
			}

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

500
		// Update energy evaluations count: count LS and GG evals
501
		eval_cnt += DockConst_pop_size; 
Leonardo Solis's avatar
Leonardo Solis committed
502

503
		// Update generation count
Leonardo Solis's avatar
Leonardo Solis committed
504
505
		generation_cnt++;

506
507
508
		#if defined (DEBUG_KRNL_GA)
		printf("eval_cnt: %u, generation_cnt: %u\n", eval_cnt, generation_cnt);
		#endif
509
	} // End while eval_cnt & generation_cnt
510

511
	// ------------------------------------------------------------------
512
	// Off: turn off all other kernels
513
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
514

515
	// Turn off PRNG kernels
516
	const int tmp_int_one = 1;
Leonardo Solis's avatar
Leonardo Solis committed
517
518
519
	write_pipe_block(chan_GA2PRNG_BT_ushort_float_off,  	&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_GG_uchar_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_GG_float_off, 		&tmp_int_one);
lvs's avatar
lvs committed
520
/*
521
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
522
*/
523
524


525
	// Turn off IGL_Arbiter, Conform, InterE, IntraE kernerls
526
	write_pipe_block(chan_IGLArbiter_Off,     		&tmp_int_one);
lvs's avatar
lvs committed
527
/*
528
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
529
*/
Leonardo Solis's avatar
Leonardo Solis committed
530

531
	// Write final pop & energies back to FPGA-board DDRs
532
533
	__attribute__((xcl_pipeline_loop))
	LOOP_GA_WRITEPOP2DDR_OUTER:
Leonardo Solis's avatar
Leonardo Solis committed
534
	for (ushort pop_cnt=0;pop_cnt<DockConst_pop_size; pop_cnt++) { 	
Leonardo Solis's avatar
Leonardo Solis committed
535

536
537
		__attribute__((xcl_pipeline_loop))
		LOOP_GA_WRITEPOP2DDR_INNER:
Leonardo Solis's avatar
Leonardo Solis committed
538
		for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
539
			GlobPopCurrFinal[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt] = LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
540
		}
541

Leonardo Solis's avatar
Leonardo Solis committed
542
		GlobEneCurr[pop_cnt] = LocalEneCurr[pop_cnt];
Leonardo Solis's avatar
Leonardo Solis committed
543
544
	}

545
	#if defined (DEBUG_KRNL_GA)
Leonardo Solis's avatar
Leonardo Solis committed
546
	printf("GA: %u %u\n", active, DockConst_pop_size -1);
547
548
	#endif

549
550
	#if defined (DEBUG_ACTIVE_KERNEL)
	printf("	%-20s: %s\n", "Krnl_GA", "disabled");
551
	#endif
Leonardo Solis's avatar
Leonardo Solis committed
552

553
	// Write final evals & generation counts to FPGA-board DDRs
554
555
	GlobEvals_performed[Host_RunId] = eval_cnt;
	GlobGens_performed [Host_RunId] = generation_cnt;
556
557

	/*printf("Host_RunId: %u, eval_cnt: %u, generation_cnt: %u\n", Host_RunId, eval_cnt, generation_cnt);*/
558
559
560
561
562
}

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

Leonardo Solis's avatar
Leonardo Solis committed
563
#include "Krnl_PRNG.cl"
564

565
566
//#include "Krnl_IGL_Arbiter.cl"
#include "Krnl_IGL_SimplifArbiter.cl"
567

568
569
#include "Krnl_Conform.cl"
#include "Krnl_InterE.cl"
570
#include "Krnl_IntraE.cl"
lvs's avatar
lvs committed
571