Krnl_GA.cl 37.2 KB
Newer Older
1
2
3
// Enable the channels extension
#pragma OPENCL EXTENSION cl_altera_channels : enable

4
5
6
//IC:  initial calculation of energy of populations
//GG:  genetic generation 
//LS:  local search
7
//OFF: turn off 
Leonardo Solis's avatar
Leonardo Solis committed
8
9

#include "../defines.h"
Leonardo Solis's avatar
Leonardo Solis committed
10
11
12
13

#define CHAN_DEPTH_ATOMXYZ   (MAX_NUM_OF_ATOMS/2)
#define CHAN_DEPTH_GENOTYPE  ACTUAL_GENOTYPE_LENGTH

14
15
16
// Send active signal to IGL_Arbiter
channel bool    chan_GA2IGL_IC_active;
channel bool    chan_GA2IGL_GG_active;
17

18
// Send genotypes from producers (IC, GG, LSs) to Conform
Leonardo Solis's avatar
Leonardo Solis committed
19
20
21
22
23
channel float  	chan_IC2Conf_genotype          __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GG2Conf_genotype          __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS1_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS2_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS3_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
24
25
channel float  	chan_LS2Conf_LS4_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS5_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
26
27
28
29
channel float  	chan_LS2Conf_LS6_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS7_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS8_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2Conf_LS9_genotype      __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
30

31
// Send ligand-atom positions from Conform to InterE & IntraE
Leonardo Solis's avatar
Leonardo Solis committed
32
channel float8  chan_Conf2Intere_xyz           __attribute__((depth(CHAN_DEPTH_ATOMXYZ)));
33
34

#if 0
Leonardo Solis's avatar
Leonardo Solis committed
35
channel char2  	chan_Conf2Intere_actmode;
36
37
38
#endif
channel char  	chan_Conf2Intere_actmode;

Leonardo Solis's avatar
Leonardo Solis committed
39
channel float8	chan_Conf2Intrae_xyz           __attribute__((depth(CHAN_DEPTH_ATOMXYZ)));
40
41

#if 0
Leonardo Solis's avatar
Leonardo Solis committed
42
channel char2  	chan_Conf2Intrae_actmode;	
43
44
#endif
channel char  	chan_Conf2Intrae_actmode;
45

46
// Send energy values from InterE & IntraE to genotype-senders (IC, GG, LSs)
Leonardo Solis's avatar
Leonardo Solis committed
47
#if 1
48
49
channel float 	chan_Intere2StoreIC_intere     __attribute__((depth(2)));
channel float 	chan_Intere2StoreGG_intere     __attribute__((depth(2)));
50
51
#endif
#if 1
52
53
54
channel float 	chan_Intere2StoreLS_LS1_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS2_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS3_intere __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
55
56
channel float 	chan_Intere2StoreLS_LS4_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS5_intere __attribute__((depth(2)));
57
58
59
60
channel float 	chan_Intere2StoreLS_LS6_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS7_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS8_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS9_intere __attribute__((depth(2)));
61
#endif
Leonardo Solis's avatar
Leonardo Solis committed
62
#if 0
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
channel float 	chan_Intere2StoreIC_intere     __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreGG_intere     __attribute__((depth(MAX_POPSIZE)));
#endif
#if 0
channel float 	chan_Intere2StoreLS_LS1_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS2_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS3_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS4_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS5_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS6_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS7_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS8_intere __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intere2StoreLS_LS9_intere __attribute__((depth(MAX_POPSIZE)));
#endif

Leonardo Solis's avatar
Leonardo Solis committed
78
#if 1
79
80
channel float 	chan_Intrae2StoreIC_intrae     __attribute__((depth(2)));
channel float 	chan_Intrae2StoreGG_intrae     __attribute__((depth(2)));
81
82
#endif
#if 1
83
84
85
channel float 	chan_Intrae2StoreLS_LS1_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS2_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS3_intrae __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
86
87
channel float 	chan_Intrae2StoreLS_LS4_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS5_intrae __attribute__((depth(2)));
88
89
90
91
channel float 	chan_Intrae2StoreLS_LS6_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS7_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS8_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS9_intrae __attribute__((depth(2)));
92
#endif
Leonardo Solis's avatar
Leonardo Solis committed
93
#if 0
94
95
96
97
98
99
100
101
102
103
104
105
106
107
channel float 	chan_Intrae2StoreIC_intrae     __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreGG_intrae     __attribute__((depth(MAX_POPSIZE)));
#endif
#if 0
channel float 	chan_Intrae2StoreLS_LS1_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS2_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS3_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS4_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS5_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS6_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS7_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS8_intrae __attribute__((depth(MAX_POPSIZE)));
channel float 	chan_Intrae2StoreLS_LS9_intrae __attribute__((depth(MAX_POPSIZE)));
#endif
Leonardo Solis's avatar
Leonardo Solis committed
108

109
110
111
112
// Send PRNG outputs from generators to consumers
channel float8   chan_PRNG2GA_BT_ushort_float_prng;
channel uchar2   chan_PRNG2GA_GG_uchar_prng;
channel float    chan_PRNG2GA_GG_float_prng     __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
113
channel ushort16 chan_PRNG2GA_LS123_ushort_prng;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
channel float    chan_PRNG2GA_LS_float_prng     __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS2_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS3_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS4_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS5_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS6_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS7_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS8_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS9_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));

// Turn-off signals to PRNG generators

// FIXME: these channels don't go anymore through IGL_Arbiter.
// That was initially the case, but was fixed.
// Name should be changed accordingly (GA instead of Arbiter)
// to avoid misleading data-flow information
130
131
132
133
134
135
136
channel bool 	chan_Arbiter_BT_ushort_float_off;
channel bool    chan_Arbiter_GG_uchar_off;
channel bool    chan_Arbiter_GG_float_off;
channel bool    chan_Arbiter_LS123_ushort_off;
channel bool    chan_Arbiter_LS_float_off;
channel bool    chan_Arbiter_LS2_float_off;
channel bool    chan_Arbiter_LS3_float_off;
Leonardo Solis's avatar
Leonardo Solis committed
137
138
channel bool    chan_Arbiter_LS4_float_off;
channel bool    chan_Arbiter_LS5_float_off;
139
140
141
142
channel bool    chan_Arbiter_LS6_float_off;
channel bool    chan_Arbiter_LS7_float_off;
channel bool    chan_Arbiter_LS8_float_off;
channel bool    chan_Arbiter_LS9_float_off;
143
144

// Send energy values and genotypes to LSs
Leonardo Solis's avatar
Leonardo Solis committed
145
146
147
channel float   chan_GA2LS_LS1_energy;
channel float   chan_GA2LS_LS2_energy;
channel float   chan_GA2LS_LS3_energy;
Leonardo Solis's avatar
Leonardo Solis committed
148
149
channel float   chan_GA2LS_LS4_energy;
channel float   chan_GA2LS_LS5_energy;
150
151
152
153
channel float   chan_GA2LS_LS6_energy;
channel float   chan_GA2LS_LS7_energy;
channel float   chan_GA2LS_LS8_energy;
channel float   chan_GA2LS_LS9_energy;
Leonardo Solis's avatar
Leonardo Solis committed
154
155
156
channel float  	chan_GA2LS_LS1_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS2_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS3_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
157
158
channel float  	chan_GA2LS_LS4_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS5_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
159
160
161
162
channel float  	chan_GA2LS_LS6_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS7_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS8_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS9_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
163

164
// Send LS status from LSs to IGL_Arbiter
Leonardo Solis's avatar
Leonardo Solis committed
165
166
167
channel bool    chan_LS2Arbiter_LS1_end;
channel bool    chan_LS2Arbiter_LS2_end;
channel bool    chan_LS2Arbiter_LS3_end;
Leonardo Solis's avatar
Leonardo Solis committed
168
169
channel bool    chan_LS2Arbiter_LS4_end;
channel bool    chan_LS2Arbiter_LS5_end;
170
171
172
173
channel bool    chan_LS2Arbiter_LS6_end;
channel bool    chan_LS2Arbiter_LS7_end;
channel bool    chan_LS2Arbiter_LS8_end;
channel bool    chan_LS2Arbiter_LS9_end;
174
175

// Get LS-eval-count, new energy, new genotype from LSs
176
177
channel float2  chan_LS2GA_LS1_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS2_evalenergy      __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
178
179
180
channel float2  chan_LS2GA_LS3_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS4_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS5_evalenergy      __attribute__((depth(2)));
181
182
183
184
channel float2  chan_LS2GA_LS6_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS7_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS8_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS9_evalenergy      __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
185
186
187
channel float  	chan_LS2GA_LS1_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS2_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS3_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
188
189
channel float  	chan_LS2GA_LS4_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS5_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
190
191
192
193
channel float  	chan_LS2GA_LS6_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS7_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS8_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS9_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
194

195
// Turn-off signals to LSs
196
channel bool    chan_GA2LS_Off1_active;
Leonardo Solis's avatar
Leonardo Solis committed
197
198
channel bool    chan_GA2LS_Off2_active;
channel bool    chan_GA2LS_Off3_active;
Leonardo Solis's avatar
Leonardo Solis committed
199
200
channel bool    chan_GA2LS_Off4_active;
channel bool    chan_GA2LS_Off5_active;
201
202
203
204
channel bool    chan_GA2LS_Off6_active;
channel bool    chan_GA2LS_Off7_active;
channel bool    chan_GA2LS_Off8_active;
channel bool    chan_GA2LS_Off9_active;
Leonardo Solis's avatar
Leonardo Solis committed
205

206
207
// Send genotype-producer-channel selector and genotype 
// from IGL_Arbiter to Conform
208
#if 0
209
channel char2  chan_IGL2Conform_actmode	       __attribute__((depth(9))); // active, mode
210
211
212
#endif
channel char  chan_IGL2Conform_actmode	       __attribute__((depth(9))); // active, mode

213
channel float  chan_IGL2Conform_genotype       __attribute__((depth(9*CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
214

215
// Turn-off signal to IGL_Arbiter, Conform, InterE, IntraE
Leonardo Solis's avatar
Leonardo Solis committed
216
channel bool   chan_IGLArbiter_Off;
Leonardo Solis's avatar
Leonardo Solis committed
217

218
219
220
221
222
223
224
225
226
227
#if defined (FIXED_POINT_CONFORM) || \
    defined (FIXED_POINT_LS1)     || \
    defined (FIXED_POINT_LS2)     || \
    defined (FIXED_POINT_LS3)     || \
    defined (FIXED_POINT_LS4)     || \
    defined (FIXED_POINT_LS5)     || \
    defined (FIXED_POINT_LS6)     || \
    defined (FIXED_POINT_LS7)     || \
    defined (FIXED_POINT_LS8)     || \
    defined (FIXED_POINT_LS9)
228
229
230
#include "../defines_fixedpt.h"

typedef int3          fixedpt3;
231
typedef int4	      fixedpt4;
232
233
#endif

Leonardo Solis's avatar
Leonardo Solis committed
234
#if defined (FIXED_POINT_INTERE) || defined (FIXED_POINT_INTRAE)
235
236
237
#include "../defines_fixedpt_64.h"
#endif

Leonardo Solis's avatar
Leonardo Solis committed
238
// --------------------------------------------------------------------------
239
// Map the argument into the interval 0 - 180, or 0 - 360
Leonardo Solis's avatar
Leonardo Solis committed
240
241
242
243
244
245
246
// by adding/subtracting n*ang_max to/from it.
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------

float map_angle_180(float angle)
{
	float x = angle;
247
248
249
250
251
252
	//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
253
254
255
256
257
258
	return x;
}

float map_angle_360(float angle)
{
	float x = angle;
259
260
261
262
263
264
	//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
265
266
267
	return x;
}

268
269
270
271
272
273
274
275
276
#if defined (FIXED_POINT_LS1) || \
    defined (FIXED_POINT_LS2) || \
    defined (FIXED_POINT_LS3) || \
    defined (FIXED_POINT_LS4) || \
    defined (FIXED_POINT_LS5) || \
    defined (FIXED_POINT_LS6) || \
    defined (FIXED_POINT_LS7) || \
    defined (FIXED_POINT_LS8) || \
    defined (FIXED_POINT_LS9)
277
278
279
280
281
282
#define FIXEDPT_180	0xB40000
#define FIXEDPT_360	0x1680000

fixedpt fixedpt_map_angle_180(fixedpt angle)
{
	fixedpt x = angle;
283
284
285
286
287
288
	//while (x < 0.0f) 
	if (x < 0)
	{ x += FIXEDPT_180; }
	//while (x > 180.0f)
	if (x > FIXEDPT_180)
	{ x -= FIXEDPT_180; }
289
290
291
292
293
294
	return x;
}

fixedpt fixedpt_map_angle_360(fixedpt angle)
{
	fixedpt x = angle;
295
296
297
298
299
300
	//while (x < 0.0f) 
	if (x < 0)
	{ x += FIXEDPT_360; }
	//while (x > 360.0f)
	if (x > FIXEDPT_360)
	{ x -= FIXEDPT_360;}
301
302
303
304
	return x;
}
#endif

305
// --------------------------------------------------------------------------
306
// Lamarckian Genetic-Algorithm (GA): GA + LS (Local Search) 
307
308
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------
309
__kernel __attribute__ ((max_global_work_dim(0)))
310
311
void Krnl_GA(__global       float*           restrict GlobPopulationCurrent,
	     __global       float*           restrict GlobEnergyCurrent,
312
313
314
315
	     #if defined(SINGLE_COPY_POP_ENE)
   	     __global       unsigned int*    restrict GlobEvals_performed,
             __global       unsigned int*    restrict GlobGens_performed,
	     #else
Leonardo Solis's avatar
Leonardo Solis committed
316
	     __global       unsigned int*    restrict GlobEvalsGenerations_performed,
317
	     #endif
Leonardo Solis's avatar
Leonardo Solis committed
318
319
320
321
322
323
324
			    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
325
326
		    	    float                     Host_two_absmaxdmov,
			    float                     Host_two_absmaxdang,
Leonardo Solis's avatar
Leonardo Solis committed
327
328
			    float                     DockConst_crossover_rate,
			    unsigned int              DockConst_num_of_lsentities,
329
330
331
332
333
334
335
336
			    unsigned char             DockConst_num_of_genes
	     #if defined(SINGLE_COPY_POP_ENE)
	     					      ,
	                    unsigned short            Host_RunId,
			    unsigned int 	      Host_Offset_Pop,
			    unsigned int	      Host_Offset_Ene
	     #endif
	     )
337
{
338
	#if defined (DEBUG_KRNL_GA)
339
	printf("\n");
Leonardo Solis's avatar
Leonardo Solis committed
340
	printf("%-40s %u\n", "DockConst_pop_size: ",        		DockConst_pop_size);
Leonardo Solis's avatar
Leonardo Solis committed
341
342
343
344
345
346
	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
347
348
	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
349
	printf("%-40s %f\n", "DockConst_crossover_rate: ", 		DockConst_crossover_rate);
Leonardo Solis's avatar
Leonardo Solis committed
350
	printf("%-40s %u\n", "DockConst_num_of_lsentities: ",   	DockConst_num_of_lsentities);
Leonardo Solis's avatar
Leonardo Solis committed
351
	printf("%-40s %u\n", "DockConst_num_of_genes: ",        	DockConst_num_of_genes);
352
353
	#endif

354
	// Other banking configuration (see PopNext, eneNext) might reduce logic
355
	// but makes PopCurr stallable
Leonardo Solis's avatar
Leonardo Solis committed
356
357
358
	__local float LocalPopCurr[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
	__local float LocalEneCurr[MAX_POPSIZE];

Leonardo Solis's avatar
Leonardo Solis committed
359
360
361
362
363
	#if defined(SINGLE_COPY_POP_ENE)
	__global float* GlobPopCurr = & GlobPopulationCurrent [Host_Offset_Pop];
	__global float* GlobEneCurr = & GlobEnergyCurrent     [Host_Offset_Ene];
	#endif

364
	// ------------------------------------------------------------------
365
	// Initial Calculation (IC) of scores
366
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
367
	for (ushort pop_cnt = 0; pop_cnt < DockConst_pop_size; pop_cnt++) {
368
		// Calculate energy
369
		write_channel_altera(chan_GA2IGL_IC_active, true);
370
371
		mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
372
		for (uchar pipe_cnt=0; pipe_cnt<DockConst_num_of_genes; pipe_cnt++) {
373
#if 0
374
			#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
375
			LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE] = GlobPopCurr[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
376
			#else
Leonardo Solis's avatar
Leonardo Solis committed
377
			LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE] = GlobPopulationCurrent[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
378
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
379
			write_channel_altera(chan_IC2Conf_genotype, LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE]);	
380
381
#endif

Leonardo Solis's avatar
Leonardo Solis committed
382
#if 1
383
384
385
386
387
388
389
390
391
			float tmp_ic;
			#if defined(SINGLE_COPY_POP_ENE)
			tmp_ic = GlobPopCurr[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
			#else
			tmp_ic = GlobPopulationCurrent[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
			#endif

			LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE] = tmp_ic;
			write_channel_altera(chan_IC2Conf_genotype, tmp_ic);
Leonardo Solis's avatar
Leonardo Solis committed
392
#endif
393
		}
394

Leonardo Solis's avatar
Leonardo Solis committed
395
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
396
		printf("\nIC - tx pop: %u", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
397
		#endif
398

399
		// Read energy
Leonardo Solis's avatar
Leonardo Solis committed
400
#if 1
401
402
403
404
405
406
407
408
		float energyIA_IC_rx;
		float energyIE_IC_rx;
		bool intra_valid = false;
		bool inter_valid = false;
		while( (intra_valid == false) || (inter_valid == false)) {
			if (intra_valid == false) {
				energyIA_IC_rx = read_channel_nb_altera(chan_Intrae2StoreIC_intrae, &intra_valid);
			}
Leonardo Solis's avatar
Leonardo Solis committed
409
			else if (inter_valid == false) {
410
411
412
				energyIE_IC_rx = read_channel_nb_altera(chan_Intere2StoreIC_intere, &inter_valid);
			}
		}
413
#endif
414

Leonardo Solis's avatar
Leonardo Solis committed
415
#if 0
416
417
418
419
420
		mem_fence(CLK_CHANNEL_MEM_FENCE);
		float energyIA_IC_rx = read_channel_altera(chan_Intrae2StoreIC_intrae);
		mem_fence(CLK_CHANNEL_MEM_FENCE);
		float energyIE_IC_rx = read_channel_altera(chan_Intere2StoreIC_intere);		
#endif
Leonardo Solis's avatar
Leonardo Solis committed
421
		LocalEneCurr[pop_cnt] = energyIA_IC_rx + energyIE_IC_rx;
422

Leonardo Solis's avatar
Leonardo Solis committed
423
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
424
		printf(", IC - rx pop: %u\n", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
425
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
426
	}
427
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
428

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

Leonardo Solis's avatar
Leonardo Solis committed
431
	uint generation_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
432

Leonardo Solis's avatar
Leonardo Solis committed
433
	while ((eval_cnt < DockConst_num_of_energy_evals) && (generation_cnt < DockConst_num_of_generations)) {
Leonardo Solis's avatar
Leonardo Solis committed
434

435
436
437
		//float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
		//float LocalEneNext[MAX_POPSIZE];

438
		// This configuration reduces logic and does not increase block RAM usage
439
440
441
442
443
444
445
446
447
448
449
450
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(32),
			              )) LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];

		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(4),
			              )) LocalEneNext[MAX_POPSIZE];

451
		// ------------------------------------------------------------------
452
		// Genetic Generation (GG)
453
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
454
455
456
457
458
		float __attribute__ ((
				       memory,
		   		       numbanks(1),
			               bankwidth(64),
			               singlepump,
459
 			               numreadports(6),
Leonardo Solis's avatar
Leonardo Solis committed
460
461
462
463
464
			               numwriteports(1)
			              )) loc_energies[MAX_POPSIZE];

		ushort best_entity = 0;

Leonardo Solis's avatar
Leonardo Solis committed
465
466
467
468
469
//		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];

470
471
472
473
474
475
476
477
			#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
478
		}
479

480
481
482
483
		#if defined (DEBUG_KRNL_GA)
		printf("best_entity: %3u, energy: %20.6f\n", best_entity, loc_energies[best_entity]);
		#endif

484
485
		#pragma ivdep array (LocalPopNext)
		#pragma ivdep array (LocalEneNext)
Leonardo Solis's avatar
Leonardo Solis committed
486
		for (ushort new_pop_cnt = 1; new_pop_cnt < DockConst_pop_size; new_pop_cnt++) {
487
488

			// ---------------------------------------------------
489
			// Elitism: copying the best entity to new population
490
491
492
493
			// ---------------------------------------------------
			if (new_pop_cnt == 1) {
				#pragma unroll
				for (uchar gene_cnt=0; gene_cnt<ACTUAL_GENOTYPE_LENGTH; gene_cnt++) { 		
Leonardo Solis's avatar
Leonardo Solis committed
494
					LocalPopNext[0][gene_cnt & MASK_GENOTYPE] = LocalPopCurr[best_entity][gene_cnt & MASK_GENOTYPE]; 	
495
496
497
498
				} 		
				LocalEneNext[0] = loc_energies[best_entity];
			}

499
500
501
			#if defined (DEBUG_KRNL_GA)
			printf("Krnl_GA: %u\n", new_pop_cnt);
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
502

Leonardo Solis's avatar
Leonardo Solis committed
503
504
			float local_entity_1 [ACTUAL_GENOTYPE_LENGTH];
			float local_entity_2 [ACTUAL_GENOTYPE_LENGTH]; 
Leonardo Solis's avatar
Leonardo Solis committed
505
		
506
			// ---------------------------------------------------
507
			// Binary-Tournament (BT) selection
508
			// ---------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
509

510
511
			// Get ushort binary_tournament selection prngs (parent index)
			// Get float binary_tournament selection prngs (tournament rate)
512
			float8 bt_tmp = read_channel_altera(chan_PRNG2GA_BT_ushort_float_prng);
Leonardo Solis's avatar
Leonardo Solis committed
513
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
514

515
			// Convert: float prng that must be still converted to short
516
517
518
519
520
			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
521
			// short prng ready to be used, replace ushort prng_BT_U[4];
522
523
524
525
526
			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;

Leonardo Solis's avatar
Leonardo Solis committed
527
			// float prng ready to used, replace float prng_BT_F[4];
528
529
530
531
532
			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
533
534
			ushort parent1;
			ushort parent2; 
Leonardo Solis's avatar
Leonardo Solis committed
535

536
			// First parent
537
538
539
540
541
542
543
			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;}}

544
			// The better will be the second parent
545
546
547
548
549
550
			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
551

Leonardo Solis's avatar
Leonardo Solis committed
552
			// local_entity_1 and local_entity_2 are population-parent1, population-parent2
Leonardo Solis's avatar
Leonardo Solis committed
553
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
554
555
				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
556
			}
557

558
			// ---------------------------------------------------
559
			// Mating parents
560
			// ---------------------------------------------------	
Leonardo Solis's avatar
Leonardo Solis committed
561

562
563
			// get uchar genetic_generation prngs (gene index)
			// get float genetic_generation prngs (mutation rate)
564
			uchar2 prng_GG_C = read_channel_altera(chan_PRNG2GA_GG_uchar_prng);
Leonardo Solis's avatar
Leonardo Solis committed
565
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
566
567
568
569

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

571
572
573
			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
574
575
576
577
				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; }
578
579
			}
			
580
			// Reuse of bt prng float as crossover-rate
Leonardo Solis's avatar
Leonardo Solis committed
581
582
			bool crossover_yes = (DockConst_crossover_rate > bt_tmp_f0);

Leonardo Solis's avatar
Leonardo Solis committed
583
584
585
			write_channel_altera(chan_GA2IGL_GG_active, true);
			mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
586
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
587
588

				float prngGG = read_channel_altera(chan_PRNG2GA_GG_float_prng);
Leonardo Solis's avatar
Leonardo Solis committed
589
590
				mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
591
				float tmp_offspring;
Leonardo Solis's avatar
Leonardo Solis committed
592

593
				// Performing crossover
Leonardo Solis's avatar
Leonardo Solis committed
594
595
596
597
598
599
600
				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
				   ) {
601
					tmp_offspring = local_entity_1[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
602
603
				}
				else {
604
					tmp_offspring = local_entity_2[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
605
606
				}

607
				// Performing mutation
Leonardo Solis's avatar
Leonardo Solis committed
608
				if (DockConst_mutation_rate > prngGG) {
Leonardo Solis's avatar
Leonardo Solis committed
609
					if(gene_cnt<3) {
Leonardo Solis's avatar
Leonardo Solis committed
610
						tmp_offspring = tmp_offspring + Host_two_absmaxdmov*prngGG-DockConst_abs_max_dmov;
Leonardo Solis's avatar
Leonardo Solis committed
611
612
613
					}
					else {
						float tmp;
Leonardo Solis's avatar
Leonardo Solis committed
614
615
616
						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
617
618
619
					}
				}

620
				// Calculate energy
Leonardo Solis's avatar
Leonardo Solis committed
621
				LocalPopNext [new_pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_offspring;
622
				write_channel_altera(chan_GG2Conf_genotype, tmp_offspring);
Leonardo Solis's avatar
Leonardo Solis committed
623
			}
624

Leonardo Solis's avatar
Leonardo Solis committed
625
626
627
			#if defined (DEBUG_KRNL_GG)
			printf("GG - tx pop: %u", new_pop_cnt); 		
			#endif	
628

629
			// Read energy
Leonardo Solis's avatar
Leonardo Solis committed
630
#if 1
631
632
633
634
635
636
637
638
			float energyIA_GG_rx;
			float energyIE_GG_rx;
			bool intra_valid = false;
			bool inter_valid = false;
			while( (intra_valid == false) || (inter_valid == false)) {
				if (intra_valid == false) {
					energyIA_GG_rx = read_channel_nb_altera(chan_Intrae2StoreGG_intrae, &intra_valid);
				}
Leonardo Solis's avatar
Leonardo Solis committed
639
				else if (inter_valid == false) {
640
641
642
					energyIE_GG_rx = read_channel_nb_altera(chan_Intere2StoreGG_intere, &inter_valid);
				}
			}
643
644
#endif

Leonardo Solis's avatar
Leonardo Solis committed
645
#if 0
646
647
648
649
650
			mem_fence(CLK_CHANNEL_MEM_FENCE);
			float energyIA_GG_rx = read_channel_altera(chan_Intrae2StoreGG_intrae);
			mem_fence(CLK_CHANNEL_MEM_FENCE);
			float energyIE_GG_rx = read_channel_altera(chan_Intere2StoreGG_intere);			
#endif
Leonardo Solis's avatar
Leonardo Solis committed
651
			LocalEneNext[new_pop_cnt] = energyIA_GG_rx + energyIE_GG_rx;
Leonardo Solis's avatar
Leonardo Solis committed
652
653

			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
654
			printf(", GG - rx pop: %u\n", new_pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
655
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
656
		} 
657
658
		// ------------------------------------------------------------------
		// LS: Local Search
659
		// Subject num_of_entity_for_ls pieces of offsprings to LS 
Leonardo Solis's avatar
Leonardo Solis committed
660
		// ------------------------------------------------------------------
661

Leonardo Solis's avatar
Leonardo Solis committed
662
		uint ls_eval_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
663

Leonardo Solis's avatar
Leonardo Solis committed
664
		#pragma ivdep
665
		for (ushort ls_ent_cnt=0; ls_ent_cnt<DockConst_num_of_lsentities; ls_ent_cnt+=9) {
Leonardo Solis's avatar
Leonardo Solis committed
666

667
			// Choose random & different entities on every iteration
668
			ushort16 entity_ls = read_channel_altera(chan_PRNG2GA_LS123_ushort_prng);
Leonardo Solis's avatar
Leonardo Solis committed
669
670
			mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
671
672
673
674
675
			ushort entity_ls1 = entity_ls.s0;
			ushort entity_ls2 = entity_ls.s1;
			ushort entity_ls3 = entity_ls.s2;
			ushort entity_ls4 = entity_ls.s3;
			ushort entity_ls5 = entity_ls.s4;
676
677
678
679
			ushort entity_ls6 = entity_ls.s5;
			ushort entity_ls7 = entity_ls.s6;
			ushort entity_ls8 = entity_ls.s7;
			ushort entity_ls9 = entity_ls.s8;
680

681
682
683
			write_channel_altera(chan_GA2LS_LS1_energy, LocalEneNext[entity_ls1]);
			write_channel_altera(chan_GA2LS_LS2_energy, LocalEneNext[entity_ls2]);
			write_channel_altera(chan_GA2LS_LS3_energy, LocalEneNext[entity_ls3]);
Leonardo Solis's avatar
Leonardo Solis committed
684
685
			write_channel_altera(chan_GA2LS_LS4_energy, LocalEneNext[entity_ls4]);
			write_channel_altera(chan_GA2LS_LS5_energy, LocalEneNext[entity_ls5]);
686
687
688
689
			write_channel_altera(chan_GA2LS_LS6_energy, LocalEneNext[entity_ls6]);
			write_channel_altera(chan_GA2LS_LS7_energy, LocalEneNext[entity_ls7]);
			write_channel_altera(chan_GA2LS_LS8_energy, LocalEneNext[entity_ls8]);
			write_channel_altera(chan_GA2LS_LS9_energy, LocalEneNext[entity_ls9]);
Leonardo Solis's avatar
Leonardo Solis committed
690
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
691

Leonardo Solis's avatar
Leonardo Solis committed
692
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
693
694
695
				write_channel_altera(chan_GA2LS_LS1_genotype, LocalPopNext[entity_ls1][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS2_genotype, LocalPopNext[entity_ls2][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS3_genotype, LocalPopNext[entity_ls3][gene_cnt & MASK_GENOTYPE]);
Leonardo Solis's avatar
Leonardo Solis committed
696
697
				write_channel_altera(chan_GA2LS_LS4_genotype, LocalPopNext[entity_ls4][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS5_genotype, LocalPopNext[entity_ls5][gene_cnt & MASK_GENOTYPE]);
698
699
700
701
				write_channel_altera(chan_GA2LS_LS6_genotype, LocalPopNext[entity_ls6][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS7_genotype, LocalPopNext[entity_ls7][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS8_genotype, LocalPopNext[entity_ls8][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS9_genotype, LocalPopNext[entity_ls9][gene_cnt & MASK_GENOTYPE]);
Leonardo Solis's avatar
Leonardo Solis committed
702
703
			}
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
704

705
706
707
			float2 evalenergy_tmp1;
			float2 evalenergy_tmp2;
			float2 evalenergy_tmp3;
Leonardo Solis's avatar
Leonardo Solis committed
708
709
			float2 evalenergy_tmp4;
			float2 evalenergy_tmp5;
710
711
712
713
			float2 evalenergy_tmp6;
			float2 evalenergy_tmp7;
			float2 evalenergy_tmp8;
			float2 evalenergy_tmp9;
714
715
			bool ls1_done = false;
			bool ls2_done = false;
Leonardo Solis's avatar
Leonardo Solis committed
716
717
			bool ls3_done = false;
			bool ls4_done = false;
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
			bool ls5_done = false;
			bool ls6_done = false;
			bool ls7_done = false;
			bool ls8_done = false;
			bool ls9_done = false;  
  
			while( (ls1_done == false) || 
			       (ls2_done == false) || 
			       (ls3_done == false) || 
			       (ls4_done == false) || 
			       (ls5_done == false) ||
			       (ls6_done == false) || 
			       (ls7_done == false) || 
			       (ls8_done == false) || 
			       (ls9_done == false) 
			)
			{
735
736
737
738
739
740
741
742
743
				if (ls1_done == false) {
					evalenergy_tmp1 = read_channel_nb_altera(chan_LS2GA_LS1_evalenergy, &ls1_done);
				}
				else if (ls2_done == false) {
					evalenergy_tmp2 = read_channel_nb_altera(chan_LS2GA_LS2_evalenergy, &ls2_done);
				}
				else if (ls3_done == false) {
					evalenergy_tmp3 = read_channel_nb_altera(chan_LS2GA_LS3_evalenergy, &ls3_done);
				}
Leonardo Solis's avatar
Leonardo Solis committed
744
745
746
747
748
749
				else if (ls4_done == false) {
					evalenergy_tmp4 = read_channel_nb_altera(chan_LS2GA_LS4_evalenergy, &ls4_done);
				}
				else if (ls5_done == false) {
					evalenergy_tmp5 = read_channel_nb_altera(chan_LS2GA_LS5_evalenergy, &ls5_done);
				}
750
751
752
753
754
755
756
757
758
759
760
761
				else if (ls6_done == false) {
					evalenergy_tmp6 = read_channel_nb_altera(chan_LS2GA_LS6_evalenergy, &ls6_done);
				}
				else if (ls7_done == false) {
					evalenergy_tmp7 = read_channel_nb_altera(chan_LS2GA_LS7_evalenergy, &ls7_done);
				}
				else if (ls8_done == false) {
					evalenergy_tmp8 = read_channel_nb_altera(chan_LS2GA_LS8_evalenergy, &ls8_done);
				}
				else if (ls9_done == false) {
					evalenergy_tmp9 = read_channel_nb_altera(chan_LS2GA_LS9_evalenergy, &ls9_done);
				}
762
			}
763
		
764
765
766
			#if defined (DEBUG_KRNL_LS)
			printf("LS - got all eval & energies back\n");
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
767
768
769
770

			float eetmp1 = evalenergy_tmp1.x;
			float eetmp2 = evalenergy_tmp2.x;
			float eetmp3 = evalenergy_tmp3.x;
Leonardo Solis's avatar
Leonardo Solis committed
771
772
			float eetmp4 = evalenergy_tmp4.x;
			float eetmp5 = evalenergy_tmp5.x;
773
774
775
776
			float eetmp6 = evalenergy_tmp6.x;
			float eetmp7 = evalenergy_tmp7.x;
			float eetmp8 = evalenergy_tmp8.x;
			float eetmp9 = evalenergy_tmp9.x;
Leonardo Solis's avatar
Leonardo Solis committed
777
778
779
780

			uint eval_tmp1 = *(uint*)&eetmp1;
			uint eval_tmp2 = *(uint*)&eetmp2;
			uint eval_tmp3 = *(uint*)&eetmp3;
Leonardo Solis's avatar
Leonardo Solis committed
781
782
			uint eval_tmp4 = *(uint*)&eetmp4;
			uint eval_tmp5 = *(uint*)&eetmp5;
783
784
785
786
			uint eval_tmp6 = *(uint*)&eetmp6;
			uint eval_tmp7 = *(uint*)&eetmp7;
			uint eval_tmp8 = *(uint*)&eetmp8;
			uint eval_tmp9 = *(uint*)&eetmp9;
Leonardo Solis's avatar
Leonardo Solis committed
787
788
789
790

			LocalEneNext[entity_ls1] = evalenergy_tmp1.y;
			LocalEneNext[entity_ls2] = evalenergy_tmp2.y;
			LocalEneNext[entity_ls3] = evalenergy_tmp3.y;
Leonardo Solis's avatar
Leonardo Solis committed
791
792
			LocalEneNext[entity_ls4] = evalenergy_tmp4.y;
			LocalEneNext[entity_ls5] = evalenergy_tmp5.y;
793
794
795
796
			LocalEneNext[entity_ls6] = evalenergy_tmp6.y;
			LocalEneNext[entity_ls7] = evalenergy_tmp7.y;
			LocalEneNext[entity_ls8] = evalenergy_tmp8.y;
			LocalEneNext[entity_ls9] = evalenergy_tmp9.y;
797

798
			#pragma ivdep
Leonardo Solis's avatar
Leonardo Solis committed
799
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
800
801
802
				LocalPopNext[entity_ls1][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS1_genotype);
				LocalPopNext[entity_ls2][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS2_genotype);
				LocalPopNext[entity_ls3][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS3_genotype);
Leonardo Solis's avatar
Leonardo Solis committed
803
804
				LocalPopNext[entity_ls4][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS4_genotype);
				LocalPopNext[entity_ls5][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS5_genotype);
805
806
807
808
				LocalPopNext[entity_ls6][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS6_genotype);
				LocalPopNext[entity_ls7][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS7_genotype);
				LocalPopNext[entity_ls8][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS8_genotype);
				LocalPopNext[entity_ls9][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS9_genotype);
Leonardo Solis's avatar
Leonardo Solis committed
809
			}
Leonardo Solis's avatar
Leonardo Solis committed
810

811
			ls_eval_cnt += eval_tmp1 + eval_tmp2 + eval_tmp3 + eval_tmp4 + eval_tmp5 + eval_tmp6 + eval_tmp7 + eval_tmp8 + eval_tmp9;
812
813

			#if defined (DEBUG_KRNL_LS)
Leonardo Solis's avatar
Leonardo Solis committed
814
			printf("%u, ls_eval_cnt: %u\n", ls_ent_cnt, ls_eval_cnt);
815
816
			printf("LS - got all genotypes back\n");
			#endif
817
818
		} // End of for-loop ls_ent_cnt
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
819

820
		// Update current pops & energies
821
		for (ushort pop_cnt=0; pop_cnt<DockConst_pop_size; pop_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
822
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
823
				LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = LocalPopNext[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
824
825
826
827
828
			}

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

829
		// Update energy evaluations count: count LS and GG evals
Leonardo Solis's avatar
Leonardo Solis committed
830
831
		eval_cnt += ls_eval_cnt + DockConst_pop_size; 

832
		// Update generation count
Leonardo Solis's avatar
Leonardo Solis committed
833
834
		generation_cnt++;

835
836
837
		#if defined (DEBUG_KRNL_GA)
		printf("eval_cnt: %u, generation_cnt: %u\n", eval_cnt, generation_cnt);
		#endif
838
	} // End while eval_cnt & generation_cnt
839

840
	// ------------------------------------------------------------------
841
	// Off: turn off all other kernels
842
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
843

844
	// Turn off PRNG kernels
845
846
847
848
849
850
851
	write_channel_altera(chan_Arbiter_BT_ushort_float_off,  false);
	write_channel_altera(chan_Arbiter_GG_uchar_off, 	false);
	write_channel_altera(chan_Arbiter_GG_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS123_ushort_off,  	false);
	write_channel_altera(chan_Arbiter_LS_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS2_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS3_float_off, 	false);
Leonardo Solis's avatar
Leonardo Solis committed
852
853
	write_channel_altera(chan_Arbiter_LS4_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS5_float_off, 	false);
854
855
856
857
	write_channel_altera(chan_Arbiter_LS6_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS7_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS8_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS9_float_off, 	false);
858
859
	mem_fence(CLK_CHANNEL_MEM_FENCE);

860
	// Turn off LS kernels
861
862
863
	write_channel_altera(chan_GA2LS_Off1_active,  		false);
	write_channel_altera(chan_GA2LS_Off2_active,  		false);
	write_channel_altera(chan_GA2LS_Off3_active,  		false);
Leonardo Solis's avatar
Leonardo Solis committed
864
865
	write_channel_altera(chan_GA2LS_Off4_active,  		false);
	write_channel_altera(chan_GA2LS_Off5_active,  		false);
866
867
868
869
	write_channel_altera(chan_GA2LS_Off6_active,  		false);
	write_channel_altera(chan_GA2LS_Off7_active,  		false);
	write_channel_altera(chan_GA2LS_Off8_active,  		false);
	write_channel_altera(chan_GA2LS_Off9_active,  		false);
870
871
	mem_fence(CLK_CHANNEL_MEM_FENCE);

872
	// Turn off IGL_Arbiter, Conform, InterE, IntraE kernerls
873
874
	write_channel_altera(chan_IGLArbiter_Off,     		false);
	mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
875

876
	// Write final pop & energies back to FPGA-board DDRs
Leonardo Solis's avatar
Leonardo Solis committed
877
	for (ushort pop_cnt=0;pop_cnt<DockConst_pop_size; pop_cnt++) { 	
Leonardo Solis's avatar
Leonardo Solis committed
878

Leonardo Solis's avatar
Leonardo Solis committed
879
		for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
880
			#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
881
			GlobPopCurr[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt] = LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE];
882
			#else
Leonardo Solis's avatar
Leonardo Solis committed
883
			GlobPopulationCurrent[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt] = LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE];
884
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
885
		}
886
887

		#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
888
		GlobEneCurr[pop_cnt] = LocalEneCurr[pop_cnt];
889
		#else
Leonardo Solis's avatar
Leonardo Solis committed
890
		GlobEnergyCurrent[pop_cnt] = LocalEneCurr[pop_cnt];
891
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
892
893
	}

894
	#if defined (DEBUG_KRNL_GA)
Leonardo Solis's avatar
Leonardo Solis committed
895
	printf("GA: %u %u\n", active, DockConst_pop_size -1);
896
897
	#endif

898
899
	#if defined (DEBUG_ACTIVE_KERNEL)
	printf("	%-20s: %s\n", "Krnl_GA", "disabled");
900
	#endif
Leonardo Solis's avatar
Leonardo Solis committed
901

902
903
	// Write final evals & generation counts to FPGA-board DDRs
	#if defined(SINGLE_COPY_POP_ENE)
904
905
	GlobEvals_performed[Host_RunId] = eval_cnt;
	GlobGens_performed [Host_RunId] = generation_cnt;
906
	#else
Leonardo Solis's avatar
Leonardo Solis committed
907
908
	GlobEvalsGenerations_performed[0] = eval_cnt;
	GlobEvalsGenerations_performed[1] = generation_cnt;
909
	#endif
910
911
912
913
914
}

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

Leonardo Solis's avatar
Leonardo Solis committed
915
#include "Krnl_PRNG.cl"
916
917

#include "Krnl_LS.cl"
918
919
#include "Krnl_LS2.cl"
#include "Krnl_LS3.cl"
Leonardo Solis's avatar
Leonardo Solis committed
920
921
#include "Krnl_LS4.cl"
#include "Krnl_LS5.cl"
922
923
924
925
#include "Krnl_LS6.cl"
#include "Krnl_LS7.cl"
#include "Krnl_LS8.cl"
#include "Krnl_LS9.cl"
926
927

#include "Krnl_IGL_Arbiter.cl"
928
929
//#include "Krnl_IGL_SimplifArbiter.cl"

930
931
#include "Krnl_Conform.cl"
#include "Krnl_InterE.cl"
932
#include "Krnl_IntraE.cl"
933