Krnl_GA.cl 38.8 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
31
32
33
34
35
36
37
38
39
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)));
pipe float  chan_LS2Conf_LS1_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS2_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS3_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS4_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS5_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS6_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS7_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS8_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float  chan_LS2Conf_LS9_genotype      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
Leonardo Solis's avatar
Leonardo Solis committed
40

41
// Send ligand-atom positions from Conform to InterE & IntraE
lvs's avatar
lvs committed
42
// Resized to valid SDAccel depths: 16, 32, ...
43
44
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)));
45

46
47
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)));
48

49
// Send energy values from InterE & IntraE to genotype-senders (IC, GG, LSs)
lvs's avatar
lvs committed
50
// Resized to valid SDAccel depths: 16, 32, ...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)));
pipe float  chan_Intere2StoreLS_LS1_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS2_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS3_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS4_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS5_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS6_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS7_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS8_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intere2StoreLS_LS9_intere __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
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)));
pipe float  chan_Intrae2StoreLS_LS1_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS2_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS3_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS4_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS5_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS6_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS7_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS8_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float  chan_Intrae2StoreLS_LS9_intrae __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
Leonardo Solis's avatar
Leonardo Solis committed
73

74
// Send PRNG outputs from generators to consumers
lvs's avatar
lvs committed
75
// Resized to valid SDAccel depths: 16, 32, ...
76
77
78
79
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)));
pipe ushort16 chan_PRNG2GA_LS123_ushort_prng	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
Leonardo Solis's avatar
Leonardo Solis committed
80

Leonardo Solis's avatar
Leonardo Solis committed
81
82
83
84
85
86
87
88
89
pipe float    chan_PRNG2LS_float_prng     	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS2_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS3_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS4_float_prng   	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS5_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS6_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS7_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS8_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2LS9_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
90
91

// Turn-off signals to PRNG generators
lvs's avatar
lvs committed
92
// Resized to valid SDAccel depths: 16, 32, ...
Leonardo Solis's avatar
Leonardo Solis committed
93
94
95
96
97
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)));
pipe int    chan_GA2PRNG_LS123_ushort_off	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));

Leonardo Solis's avatar
Leonardo Solis committed
98
99
100
101
102
103
104
105
106
pipe int    chan_GA2PRNG_LS_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS2_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS3_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS4_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS5_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS6_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS7_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS8_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2PRNG_LS9_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
107
108

// Send energy values and genotypes to LSs
lvs's avatar
lvs committed
109
// Resized to valid SDAccel depths: 16, 32, ...
110
111
112
113
114
115
116
117
118
pipe float   chan_GA2LS_LS1_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS2_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS3_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS4_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS5_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS6_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS7_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS8_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float   chan_GA2LS_LS9_energy		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
lvs's avatar
lvs committed
119
120

// Resized to valid SDAccel depths: 16, 32, ...
121
122
123
124
125
126
127
128
129
pipe float   chan_GA2LS_LS1_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS2_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS3_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS4_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS5_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS6_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS7_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS8_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_GA2LS_LS9_genotype        	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
Leonardo Solis's avatar
Leonardo Solis committed
130

131
// Send LS status from LSs to IGL_Arbiter
132
133
134
135
136
137
138
139
140
pipe int    chan_LS2Arbiter_LS1_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS2_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS3_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS4_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS5_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS6_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS7_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS8_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_LS2Arbiter_LS9_end		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
141
// Get LS-eval-count, new energy, new genotype from LSs
lvs's avatar
lvs committed
142
// Resized to valid SDAccel depths: 16, 32, ...
143
144
145
146
147
148
149
150
151
pipe float2  chan_LS2GA_LS1_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS2_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS3_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS4_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS5_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS6_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS7_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS8_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe float2  chan_LS2GA_LS9_evalenergy      __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
lvs's avatar
lvs committed
152
153

// Resized to valid SDAccel depths: 16, 32, ...
154
155
156
157
158
159
160
161
162
pipe float   chan_LS2GA_LS1_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS2_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS3_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS4_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS5_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS6_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS7_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS8_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float   chan_LS2GA_LS9_genotype        __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
Leonardo Solis's avatar
Leonardo Solis committed
163

164
// Turn-off signals to LSs
lvs's avatar
lvs committed
165
// Resized to valid SDAccel depths: 16, 32, ...
166
167
168
169
170
171
172
173
174
pipe int    chan_GA2LS_Off1_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off2_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off3_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off4_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off5_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off6_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off7_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off8_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_GA2LS_Off9_active	    __attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
lvs's avatar
lvs committed
175
176

// Send genotype-producer-pipe selector and genotype 
177
// from IGL_Arbiter to Conform
lvs's avatar
lvs committed
178
// Resized to valid SDAccel depths: 16, 32, ...
179
180
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
181

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

Leonardo Solis's avatar
Leonardo Solis committed
186
// --------------------------------------------------------------------------
187
// Map the argument into the interval 0 - 180, or 0 - 360
Leonardo Solis's avatar
Leonardo Solis committed
188
189
190
191
192
193
194
// by adding/subtracting n*ang_max to/from it.
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------

float map_angle_180(float angle)
{
	float x = angle;
195
196
197
198
199
200
	//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
201
202
203
204
205
206
	return x;
}

float map_angle_360(float angle)
{
	float x = angle;
207
208
209
210
211
212
	//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
213
214
215
	return x;
}

216
// --------------------------------------------------------------------------
217
// Lamarckian Genetic-Algorithm (GA): GA + LS (Local Search) 
218
219
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
220
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
lvs's avatar
lvs committed
221
void Krnl_GA(
222
223
	     __global const float*           restrict GlobPopulationCurrentInitial,
	     __global       float*           restrict GlobPopulationCurrentFinal,
224
	     __global       float*           restrict GlobEnergyCurrent,
225
226
   	     __global       unsigned int*    restrict GlobEvals_performed,
             __global       unsigned int*    restrict GlobGens_performed,
Leonardo Solis's avatar
Leonardo Solis committed
227
228
229
230
231
232
233
			    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
234
235
		    	    float                     Host_two_absmaxdmov,
			    float                     Host_two_absmaxdang,
Leonardo Solis's avatar
Leonardo Solis committed
236
237
			    float                     DockConst_crossover_rate,
			    unsigned int              DockConst_num_of_lsentities,
238
			    unsigned char             DockConst_num_of_genes,
239
240
241
242
	                    unsigned short            Host_RunId,
			    unsigned int 	      Host_Offset_Pop,
			    unsigned int	      Host_Offset_Ene
	     )
243
{
244
	#if defined (DEBUG_KRNL_GA)
245
	printf("\n");
Leonardo Solis's avatar
Leonardo Solis committed
246
	printf("%-40s %u\n", "DockConst_pop_size: ",        		DockConst_pop_size);
Leonardo Solis's avatar
Leonardo Solis committed
247
248
249
250
251
252
	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
253
254
	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
255
	printf("%-40s %f\n", "DockConst_crossover_rate: ", 		DockConst_crossover_rate);
Leonardo Solis's avatar
Leonardo Solis committed
256
	printf("%-40s %u\n", "DockConst_num_of_lsentities: ",   	DockConst_num_of_lsentities);
Leonardo Solis's avatar
Leonardo Solis committed
257
	printf("%-40s %u\n", "DockConst_num_of_genes: ",        	DockConst_num_of_genes);
258
259
	#endif

260
	// Other banking configuration (see PopNext, eneNext) might reduce logic
261
	// but makes PopCurr stallable
Leonardo Solis's avatar
Leonardo Solis committed
262
263
264
	__local float LocalPopCurr[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
	__local float LocalEneCurr[MAX_POPSIZE];

265
266
267
	__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
268

269
	// ------------------------------------------------------------------
270
	// Initial Calculation (IC) of scores
271
	// ------------------------------------------------------------------
272
	__attribute__((xcl_pipeline_loop))
273
	LOOP_FOR_GA_IC_OUTER:
Leonardo Solis's avatar
Leonardo Solis committed
274
	for (ushort pop_cnt = 0; pop_cnt < DockConst_pop_size; pop_cnt++) {
275
		// Calculate energy
276
277
		const int tmp_int_zero = 0;
		write_pipe_block(chan_GA2IGL_IC_active, &tmp_int_zero);
lvs's avatar
lvs committed
278
279
280
/*
		mem_fence(CLK_CHANNEL_MEM_FENCE);
*/
281
		__attribute__((xcl_pipeline_loop))
282
		LOOP_FOR_GA_IC_INNER_WRITE_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
283
		for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
284
			float tmp_ic;
285
			tmp_ic = GlobPopCurrInitial[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt];
Leonardo Solis's avatar
Leonardo Solis committed
286

Leonardo Solis's avatar
Leonardo Solis committed
287
			LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_ic;
Leonardo Solis's avatar
Leonardo Solis committed
288
			write_pipe_block(chan_IC2Conf_genotype, &tmp_ic);	
289
		}
290

Leonardo Solis's avatar
Leonardo Solis committed
291
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
292
		printf("\nIC - tx pop: %u", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
293
		#endif
294

295
		// Read energy
296
297
		float energyIA_IC_rx;
		float energyIE_IC_rx;
298
299
300
301

		nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
		nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;	

302
		__attribute__((xcl_pipeline_loop))
303
		LOOP_WHILE_GA_IC_INNER_READ_ENERGY:
304
		while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {
lvs's avatar
lvs committed
305

306
			if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
307
				intra_valid = read_pipe(chan_Intrae2StoreIC_intrae, &energyIA_IC_rx);
308
			}
309
			else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
310
				inter_valid = read_pipe(chan_Intere2StoreIC_intere, &energyIE_IC_rx);
311
312
313
			}
		}

Leonardo Solis's avatar
Leonardo Solis committed
314
		LocalEneCurr[pop_cnt] = energyIA_IC_rx + energyIE_IC_rx;
315

316
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
317
		printf(", IC - rx pop: %u\n", pop_cnt); 		
318
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
319
	}
320
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
321

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

Leonardo Solis's avatar
Leonardo Solis committed
324
	uint generation_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
325

326
327
	__attribute__((xcl_pipeline_loop))
	LOOP_WHILE_GA_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
328
	while ((eval_cnt < DockConst_num_of_energy_evals) && (generation_cnt < DockConst_num_of_generations)) {
Leonardo Solis's avatar
Leonardo Solis committed
329

330
331
332
		//float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
		//float LocalEneNext[MAX_POPSIZE];

333
		// This configuration reduces logic and does not increase block RAM usage
lvs's avatar
lvs committed
334
/*
335
336
337
338
339
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(32),
			              )) LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
lvs's avatar
lvs committed
340
341
*/
		float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
342

lvs's avatar
lvs committed
343
/*
344
345
346
347
348
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(4),
			              )) LocalEneNext[MAX_POPSIZE];
lvs's avatar
lvs committed
349
350
*/
		float LocalEneNext[MAX_POPSIZE];
351

352
		// ------------------------------------------------------------------
353
		// Genetic Generation (GG)
354
		// ------------------------------------------------------------------
lvs's avatar
lvs committed
355
/*
Leonardo Solis's avatar
Leonardo Solis committed
356
357
358
359
360
		float __attribute__ ((
				       memory,
		   		       numbanks(1),
			               bankwidth(64),
			               singlepump,
361
 			               numreadports(6),
Leonardo Solis's avatar
Leonardo Solis committed
362
363
			               numwriteports(1)
			              )) loc_energies[MAX_POPSIZE];
lvs's avatar
lvs committed
364
365
*/
		float loc_energies[MAX_POPSIZE];
Leonardo Solis's avatar
Leonardo Solis committed
366
367
368

		ushort best_entity = 0;

369
370
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_SHIFT: 
Leonardo Solis's avatar
Leonardo Solis committed
371
372
373
374
375
//		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];

376
377
378
379
380
381
382
383
			#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
384
		}
385

386
387
388
389
		#if defined (DEBUG_KRNL_GA)
		printf("best_entity: %3u, energy: %20.6f\n", best_entity, loc_energies[best_entity]);
		#endif

390
		/*
391
392
		#pragma ivdep array (LocalPopNext)
		#pragma ivdep array (LocalEneNext)
393
		*/
394
395
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_OUTER_GLOBAL: 
Leonardo Solis's avatar
Leonardo Solis committed
396
		for (ushort new_pop_cnt = 1; new_pop_cnt < DockConst_pop_size; new_pop_cnt++) {
397
398

			// ---------------------------------------------------
399
			// Elitism: copying the best entity to new population
400
401
			// ---------------------------------------------------
			if (new_pop_cnt == 1) {
402
				__attribute__((xcl_pipeline_loop))
403
				LOOP_FOR_GA_INNER_ELITISM:
404
				for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
405
					LocalPopNext[0][gene_cnt & MASK_GENOTYPE] = LocalPopCurr[best_entity][gene_cnt & MASK_GENOTYPE]; 	
406
407
408
409
				} 		
				LocalEneNext[0] = loc_energies[best_entity];
			}

410
			#if defined (DEBUG_KRNL_GA)
411
			printf("Krnl_GA: %u\n", new_pop_cnt);
412
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
413

Leonardo Solis's avatar
Leonardo Solis committed
414
415
			float local_entity_1 [ACTUAL_GENOTYPE_LENGTH];
			float local_entity_2 [ACTUAL_GENOTYPE_LENGTH]; 
Leonardo Solis's avatar
Leonardo Solis committed
416
		
417
			// ---------------------------------------------------
418
			// Binary-Tournament (BT) selection
419
			// ---------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
420

421
422
			// Get ushort binary_tournament selection prngs (parent index)
			// Get float binary_tournament selection prngs (tournament rate)
lvs's avatar
lvs committed
423
424
425
			float8 bt_tmp;
			read_pipe_block(chan_PRNG2GA_BT_ushort_float_prng, &bt_tmp);
/*
Leonardo Solis's avatar
Leonardo Solis committed
426
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
427
*/
Leonardo Solis's avatar
Leonardo Solis committed
428
//printf("test point 1\n");
429
			// Convert: float prng that must be still converted to short
430
431
432
433
434
			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
435
			// short prng ready to be used, replace ushort prng_BT_U[4];
436
/*
437
438
439
440
			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;
441
442
443
444
445
446
447
*/
			// 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;
448

Leonardo Solis's avatar
Leonardo Solis committed
449
			// float prng ready to used, replace float prng_BT_F[4];
450
451
452
453
454
			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
455
456
			ushort parent1;
			ushort parent2; 
Leonardo Solis's avatar
Leonardo Solis committed
457

458
			// First parent
459
460
461
462
463
464
465
			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;}}

466
			// The better will be the second parent
467
468
469
470
471
472
			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
473

474
475
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_INNER_BT:
Leonardo Solis's avatar
Leonardo Solis committed
476
			// local_entity_1 and local_entity_2 are population-parent1, population-parent2
Leonardo Solis's avatar
Leonardo Solis committed
477
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
478
479
				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
480
			}
481

482
			// ---------------------------------------------------
483
			// Mating parents
484
			// ---------------------------------------------------	
Leonardo Solis's avatar
Leonardo Solis committed
485

486
487
			// get uchar genetic_generation prngs (gene index)
			// get float genetic_generation prngs (mutation rate)
lvs's avatar
lvs committed
488
489
490
			uchar2 prng_GG_C;
			read_pipe_block(chan_PRNG2GA_GG_uchar_prng, &prng_GG_C);
/*
Leonardo Solis's avatar
Leonardo Solis committed
491
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
492
*/
Leonardo Solis's avatar
Leonardo Solis committed
493
//printf("test point 2\n");
Leonardo Solis's avatar
Leonardo Solis committed
494
495
496
497

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

499
500
501
			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
502
503
504
505
				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; }
506
507
			}
			
508
			// Reuse of bt prng float as crossover-rate
Leonardo Solis's avatar
Leonardo Solis committed
509
			bool crossover_yes = (DockConst_crossover_rate > bt_tmp_f0);
Leonardo Solis's avatar
Leonardo Solis committed
510

511
512
			const int tmp_int_zero = 0;
			write_pipe_block(chan_GA2IGL_GG_active, &tmp_int_zero);
lvs's avatar
lvs committed
513
/*
Leonardo Solis's avatar
Leonardo Solis committed
514
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
515
*/
Leonardo Solis's avatar
Leonardo Solis committed
516
//printf("test point 3\n");
Leonardo Solis's avatar
Leonardo Solis committed
517

518
519
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_INNER_CROSS_MUT:
Leonardo Solis's avatar
Leonardo Solis committed
520
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
lvs's avatar
lvs committed
521
522
523
				float prngGG;
				read_pipe_block(chan_PRNG2GA_GG_float_prng, &prngGG);
/*
Leonardo Solis's avatar
Leonardo Solis committed
524
				mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
525
*/
Leonardo Solis's avatar
Leonardo Solis committed
526
//printf("test point 4\n");
Leonardo Solis's avatar
Leonardo Solis committed
527

Leonardo Solis's avatar
Leonardo Solis committed
528
				float tmp_offspring;
Leonardo Solis's avatar
Leonardo Solis committed
529

530
				// Performing crossover
Leonardo Solis's avatar
Leonardo Solis committed
531
532
533
534
535
536
537
				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
				   ) {
538
					tmp_offspring = local_entity_1[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
539
540
				}
				else {
541
					tmp_offspring = local_entity_2[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
542
543
				}

544
				// Performing mutation
Leonardo Solis's avatar
Leonardo Solis committed
545
				if (DockConst_mutation_rate > prngGG) {
Leonardo Solis's avatar
Leonardo Solis committed
546
					if(gene_cnt<3) {
Leonardo Solis's avatar
Leonardo Solis committed
547
						tmp_offspring = tmp_offspring + Host_two_absmaxdmov*prngGG-DockConst_abs_max_dmov;
Leonardo Solis's avatar
Leonardo Solis committed
548
549
550
					}
					else {
						float tmp;
Leonardo Solis's avatar
Leonardo Solis committed
551
552
553
						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
554
555
556
					}
				}

557
				// Calculate energy
Leonardo Solis's avatar
Leonardo Solis committed
558
				LocalPopNext [new_pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_offspring;
lvs's avatar
lvs committed
559
				write_pipe_block(chan_GG2Conf_genotype, &tmp_offspring);
Leonardo Solis's avatar
Leonardo Solis committed
560
//printf("test point 5\n");
Leonardo Solis's avatar
Leonardo Solis committed
561
			}
562

563
			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
564
			printf("GG - tx pop: %u", new_pop_cnt); 		
565
			#endif	
566

567
			// Read energy
568
569
			float energyIA_GG_rx;
			float energyIE_GG_rx;
lvs's avatar
lvs committed
570

571
572
573
			nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
			nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;

574
575
			__attribute__((xcl_pipeline_loop))
			LOOP_WHILE_GA_INNER_READ_ENERGIES:
576
577
578
			while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {

				if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
579
					intra_valid = read_pipe(chan_Intrae2StoreGG_intrae, &energyIA_GG_rx);
580
				}
581
				else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
582
					inter_valid = read_pipe(chan_Intere2StoreGG_intere, &energyIE_GG_rx);
583
				}
584

Leonardo Solis's avatar
Leonardo Solis committed
585
//printf("intra_valid: %i, inter_valid: %i\n", intra_valid, inter_valid);
586
			}
Leonardo Solis's avatar
Leonardo Solis committed
587
//printf("test point 5\n");			
Leonardo Solis's avatar
Leonardo Solis committed
588
			LocalEneNext[new_pop_cnt] = energyIA_GG_rx + energyIE_GG_rx;
Leonardo Solis's avatar
Leonardo Solis committed
589

590
			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
591
			printf(", GG - rx pop: %u\n", new_pop_cnt); 		
592
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
593
		} 
594
595
		// ------------------------------------------------------------------
		// LS: Local Search
596
		// Subject num_of_entity_for_ls pieces of offsprings to LS 
Leonardo Solis's avatar
Leonardo Solis committed
597
		// ------------------------------------------------------------------
598

Leonardo Solis's avatar
Leonardo Solis committed
599
		uint ls_eval_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
600

601
		/*
Leonardo Solis's avatar
Leonardo Solis committed
602
		#pragma ivdep
603
		*/
604
605
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_LS_OUTER:
606
		for (ushort ls_ent_cnt=0; ls_ent_cnt<DockConst_num_of_lsentities; ls_ent_cnt+=9) {
Leonardo Solis's avatar
Leonardo Solis committed
607

608
			// Choose random & different entities on every iteration
lvs's avatar
lvs committed
609
610
611
			ushort16 entity_ls;
			read_pipe_block(chan_PRNG2GA_LS123_ushort_prng, &entity_ls);
/*
Leonardo Solis's avatar
Leonardo Solis committed
612
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
613
*/
614
//printf("test point LS 1\n");
Leonardo Solis's avatar
Leonardo Solis committed
615

Leonardo Solis's avatar
Leonardo Solis committed
616
617
618
619
620
			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;
621
622
623
624
			ushort entity_ls6 = entity_ls.s5;
			ushort entity_ls7 = entity_ls.s6;
			ushort entity_ls8 = entity_ls.s7;
			ushort entity_ls9 = entity_ls.s8;
625

lvs's avatar
lvs committed
626
627
628
629
630
631
632
633
634
			write_pipe_block(chan_GA2LS_LS1_energy, &LocalEneNext[entity_ls1]);
			write_pipe_block(chan_GA2LS_LS2_energy, &LocalEneNext[entity_ls2]);
			write_pipe_block(chan_GA2LS_LS3_energy, &LocalEneNext[entity_ls3]);
			write_pipe_block(chan_GA2LS_LS4_energy, &LocalEneNext[entity_ls4]);
			write_pipe_block(chan_GA2LS_LS5_energy, &LocalEneNext[entity_ls5]);
			write_pipe_block(chan_GA2LS_LS6_energy, &LocalEneNext[entity_ls6]);
			write_pipe_block(chan_GA2LS_LS7_energy, &LocalEneNext[entity_ls7]);
			write_pipe_block(chan_GA2LS_LS8_energy, &LocalEneNext[entity_ls8]);
			write_pipe_block(chan_GA2LS_LS9_energy, &LocalEneNext[entity_ls9]);
635
636

//printf("test point LS 2\n");
lvs's avatar
lvs committed
637
/*
Leonardo Solis's avatar
Leonardo Solis committed
638
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
639
*/
640
641
			__attribute__((xcl_pipeline_loop))
			LOOP_GA_LS_INNER_WRITE_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
642
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
lvs's avatar
lvs committed
643
644
645
646
647
648
649
650
651
				write_pipe_block(chan_GA2LS_LS1_genotype, &LocalPopNext[entity_ls1][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS2_genotype, &LocalPopNext[entity_ls2][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS3_genotype, &LocalPopNext[entity_ls3][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS4_genotype, &LocalPopNext[entity_ls4][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS5_genotype, &LocalPopNext[entity_ls5][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS6_genotype, &LocalPopNext[entity_ls6][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS7_genotype, &LocalPopNext[entity_ls7][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS8_genotype, &LocalPopNext[entity_ls8][gene_cnt & MASK_GENOTYPE]);
				write_pipe_block(chan_GA2LS_LS9_genotype, &LocalPopNext[entity_ls9][gene_cnt & MASK_GENOTYPE]);
Leonardo Solis's avatar
Leonardo Solis committed
652
			}
lvs's avatar
lvs committed
653
/*
Leonardo Solis's avatar
Leonardo Solis committed
654
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
655
*/
656
//printf("test point LS 3\n");
Leonardo Solis's avatar
Leonardo Solis committed
657

658
659
660
			float2 evalenergy_tmp1;
			float2 evalenergy_tmp2;
			float2 evalenergy_tmp3;
Leonardo Solis's avatar
Leonardo Solis committed
661
662
			float2 evalenergy_tmp4;
			float2 evalenergy_tmp5;
663
664
665
666
			float2 evalenergy_tmp6;
			float2 evalenergy_tmp7;
			float2 evalenergy_tmp8;
			float2 evalenergy_tmp9;
lvs's avatar
lvs committed
667

668
669
670
671
672
673
674
675
676
677
			nb_pipe_status ls1_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls2_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls3_done = PIPE_STATUS_FAILURE;
		 	nb_pipe_status ls4_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls5_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls6_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls7_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls8_done = PIPE_STATUS_FAILURE;
			nb_pipe_status ls9_done = PIPE_STATUS_FAILURE;  

678
679
			__attribute__((xcl_pipeline_loop))
			LOOP_WHILE_GA_LS_INNER_READ_ENERGIES:
680
681
682
683
684
685
686
687
688
			while( (ls1_done != PIPE_STATUS_SUCCESS) || 
			       (ls2_done != PIPE_STATUS_SUCCESS) || 
			       (ls3_done != PIPE_STATUS_SUCCESS) || 
			       (ls4_done != PIPE_STATUS_SUCCESS) || 
			       (ls5_done != PIPE_STATUS_SUCCESS) ||
			       (ls6_done != PIPE_STATUS_SUCCESS) || 
			       (ls7_done != PIPE_STATUS_SUCCESS) || 
			       (ls8_done != PIPE_STATUS_SUCCESS) || 
			       (ls9_done != PIPE_STATUS_SUCCESS) 
689
690
			)
			{
691
				if (ls1_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
692
					ls1_done = read_pipe(chan_LS2GA_LS1_evalenergy, &evalenergy_tmp1);
693
				}
694
				else if (ls2_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
695
					ls2_done = read_pipe(chan_LS2GA_LS2_evalenergy, &evalenergy_tmp2);
696
				}
697
				else if (ls3_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
698
					ls3_done = read_pipe(chan_LS2GA_LS3_evalenergy, &evalenergy_tmp3);
699
				}
700
				else if (ls4_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
701
					ls4_done = read_pipe(chan_LS2GA_LS4_evalenergy, &evalenergy_tmp4);
Leonardo Solis's avatar
Leonardo Solis committed
702
				}
703
				else if (ls5_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
704
					ls5_done = read_pipe(chan_LS2GA_LS5_evalenergy, &evalenergy_tmp5);
Leonardo Solis's avatar
Leonardo Solis committed
705
				}
706
				else if (ls6_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
707
					ls6_done = read_pipe(chan_LS2GA_LS6_evalenergy, &evalenergy_tmp6);
708
				}
709
				else if (ls7_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
710
					ls7_done = read_pipe(chan_LS2GA_LS7_evalenergy, &evalenergy_tmp7);
711
				}
712
				else if (ls8_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
713
					ls8_done = read_pipe(chan_LS2GA_LS8_evalenergy, &evalenergy_tmp8);
714
				}
715
				else if (ls9_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
716
					ls9_done = read_pipe(chan_LS2GA_LS9_evalenergy, &evalenergy_tmp9);
717
				}
718
			}
719
		
720
721
722
			#if defined (DEBUG_KRNL_LS)
			printf("LS - got all eval & energies back\n");
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
723
724
725
726

			float eetmp1 = evalenergy_tmp1.x;
			float eetmp2 = evalenergy_tmp2.x;
			float eetmp3 = evalenergy_tmp3.x;
Leonardo Solis's avatar
Leonardo Solis committed
727
728
			float eetmp4 = evalenergy_tmp4.x;
			float eetmp5 = evalenergy_tmp5.x;
729
730
731
732
			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
733
734
735
736

			uint eval_tmp1 = *(uint*)&eetmp1;
			uint eval_tmp2 = *(uint*)&eetmp2;
			uint eval_tmp3 = *(uint*)&eetmp3;
Leonardo Solis's avatar
Leonardo Solis committed
737
738
			uint eval_tmp4 = *(uint*)&eetmp4;
			uint eval_tmp5 = *(uint*)&eetmp5;
739
740
741
742
			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
743
744
745
746

			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
747
748
			LocalEneNext[entity_ls4] = evalenergy_tmp4.y;
			LocalEneNext[entity_ls5] = evalenergy_tmp5.y;
749
750
751
752
			LocalEneNext[entity_ls6] = evalenergy_tmp6.y;
			LocalEneNext[entity_ls7] = evalenergy_tmp7.y;
			LocalEneNext[entity_ls8] = evalenergy_tmp8.y;
			LocalEneNext[entity_ls9] = evalenergy_tmp9.y;
753

754
			/*
755
			#pragma ivdep
756
			*/
757
758
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_LS_INNER_READ_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
759
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
760

lvs's avatar
lvs committed
761
762
763
764
765
766
767
768
769
				read_pipe_block(chan_LS2GA_LS1_genotype, &LocalPopNext[entity_ls1][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS2_genotype, &LocalPopNext[entity_ls2][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS3_genotype, &LocalPopNext[entity_ls3][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS4_genotype, &LocalPopNext[entity_ls4][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS5_genotype, &LocalPopNext[entity_ls5][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS6_genotype, &LocalPopNext[entity_ls6][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS7_genotype, &LocalPopNext[entity_ls7][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS8_genotype, &LocalPopNext[entity_ls8][gene_cnt & MASK_GENOTYPE]);
				read_pipe_block(chan_LS2GA_LS9_genotype, &LocalPopNext[entity_ls9][gene_cnt & MASK_GENOTYPE]);
Leonardo Solis's avatar
Leonardo Solis committed
770
			}
Leonardo Solis's avatar
Leonardo Solis committed
771

772
			ls_eval_cnt += eval_tmp1 + eval_tmp2 + eval_tmp3 + eval_tmp4 + eval_tmp5 + eval_tmp6 + eval_tmp7 + eval_tmp8 + eval_tmp9;
773
774

			#if defined (DEBUG_KRNL_LS)
Leonardo Solis's avatar
Leonardo Solis committed
775
			printf("%u, ls_eval_cnt: %u\n", ls_ent_cnt, ls_eval_cnt);
776
777
			printf("LS - got all genotypes back\n");
			#endif
778
779
		} // End of for-loop ls_ent_cnt
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
780

781
		// Update current pops & energies
782
783
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_UPDATEPOP_OUTER:
784
		for (ushort pop_cnt=0; pop_cnt<DockConst_pop_size; pop_cnt++) {
785
786
787

			__attribute__((xcl_pipeline_loop))
			LOOP_GA_UPDATEPOP_INNER:
Leonardo Solis's avatar
Leonardo Solis committed
788
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
789
				LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = LocalPopNext[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
790
791
792
793
794
			}

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

795
		// Update energy evaluations count: count LS and GG evals
Leonardo Solis's avatar
Leonardo Solis committed
796
797
		eval_cnt += ls_eval_cnt + DockConst_pop_size; 

798
		// Update generation count
Leonardo Solis's avatar
Leonardo Solis committed
799
800
		generation_cnt++;

801
802
803
		#if defined (DEBUG_KRNL_GA)
		printf("eval_cnt: %u, generation_cnt: %u\n", eval_cnt, generation_cnt);
		#endif
804
	} // End while eval_cnt & generation_cnt
805

806
	// ------------------------------------------------------------------
807
	// Off: turn off all other kernels
808
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
809

810
	// Turn off PRNG kernels
811
	const int tmp_int_one = 1;
Leonardo Solis's avatar
Leonardo Solis committed
812
813
814
815
816
	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);
	write_pipe_block(chan_GA2PRNG_LS123_ushort_off,  	&tmp_int_one);

Leonardo Solis's avatar
Leonardo Solis committed
817
818
819
820
821
822
823
824
825
	write_pipe_block(chan_GA2PRNG_LS_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS2_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS3_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS4_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS5_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS6_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS7_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS8_float_off, 		&tmp_int_one);
	write_pipe_block(chan_GA2PRNG_LS9_float_off, 		&tmp_int_one);
lvs's avatar
lvs committed
826
/*
827
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
828
*/
829

830
	// Turn off LS kernels
831
832
833
834
835
836
837
838
839
	write_pipe_block(chan_GA2LS_Off1_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off2_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off3_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off4_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off5_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off6_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off7_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off8_active,  		&tmp_int_one);
	write_pipe_block(chan_GA2LS_Off9_active,  		&tmp_int_one);
lvs's avatar
lvs committed
840
/*
841
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
842
*/
843

844
	// Turn off IGL_Arbiter, Conform, InterE, IntraE kernerls
845
	write_pipe_block(chan_IGLArbiter_Off,     		&tmp_int_one);
lvs's avatar
lvs committed
846
/*
847
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
848
*/
Leonardo Solis's avatar
Leonardo Solis committed
849

850
	// Write final pop & energies back to FPGA-board DDRs
851
852
	__attribute__((xcl_pipeline_loop))
	LOOP_GA_WRITEPOP2DDR_OUTER:
Leonardo Solis's avatar
Leonardo Solis committed
853
	for (ushort pop_cnt=0;pop_cnt<DockConst_pop_size; pop_cnt++) { 	
Leonardo Solis's avatar
Leonardo Solis committed
854

855
856
		__attribute__((xcl_pipeline_loop))
		LOOP_GA_WRITEPOP2DDR_INNER:
Leonardo Solis's avatar
Leonardo Solis committed
857
		for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
858
			GlobPopCurrFinal[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt] = LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
859
		}
860

Leonardo Solis's avatar
Leonardo Solis committed
861
		GlobEneCurr[pop_cnt] = LocalEneCurr[pop_cnt];
Leonardo Solis's avatar