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

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

268
269
270
	__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
271

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

Leonardo Solis's avatar
Leonardo Solis committed
290
			LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_ic;
Leonardo Solis's avatar
Leonardo Solis committed
291
			write_pipe_block(chan_IC2Conf_genotype, &tmp_ic);	
292
		}
293

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

298
		// Read energy
299
300
		float energyIA_IC_rx;
		float energyIE_IC_rx;
301
302
303
304

		nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
		nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;	

305
		__attribute__((xcl_pipeline_loop))
306
		LOOP_WHILE_GA_IC_INNER_READ_ENERGY:
307
		while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {
lvs's avatar
lvs committed
308

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

Leonardo Solis's avatar
Leonardo Solis committed
317
		LocalEneCurr[pop_cnt] = energyIA_IC_rx + energyIE_IC_rx;
318

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

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

Leonardo Solis's avatar
Leonardo Solis committed
327
	uint generation_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
328

329
330
	__attribute__((xcl_pipeline_loop))
	LOOP_WHILE_GA_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
331
	while ((eval_cnt < DockConst_num_of_energy_evals) && (generation_cnt < DockConst_num_of_generations)) {
Leonardo Solis's avatar
Leonardo Solis committed
332

333
334
335
		//float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
		//float LocalEneNext[MAX_POPSIZE];

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

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

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

		ushort best_entity = 0;

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

379
380
381
382
383
384
385
386
			#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
387
		}
388

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

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

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

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

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

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

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

461
			// First parent
462
463
464
465
466
467
468
			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;}}

469
			// The better will be the second parent
470
471
472
473
474
475
			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
476

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

485
			// ---------------------------------------------------
486
			// Mating parents
487
			// ---------------------------------------------------	
Leonardo Solis's avatar
Leonardo Solis committed
488

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

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

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

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

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

Leonardo Solis's avatar
Leonardo Solis committed
531
				float tmp_offspring;
Leonardo Solis's avatar
Leonardo Solis committed
532

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

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

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

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

570
			// Read energy
571
572
			float energyIA_GG_rx;
			float energyIE_GG_rx;
lvs's avatar
lvs committed
573

574
575
576
			nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
			nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;

577
578
			__attribute__((xcl_pipeline_loop))
			LOOP_WHILE_GA_INNER_READ_ENERGIES:
579
580
581
			while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {

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

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

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

Leonardo Solis's avatar
Leonardo Solis committed
602
		uint ls_eval_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
603

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

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

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

lvs's avatar
lvs committed
629
630
631
632
633
634
635
636
637
			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]);
638
639

//printf("test point LS 2\n");
lvs's avatar
lvs committed
640
/*
Leonardo Solis's avatar
Leonardo Solis committed
641
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
642
*/
643
644
			__attribute__((xcl_pipeline_loop))
			LOOP_GA_LS_INNER_WRITE_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
645
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
lvs's avatar
lvs committed
646
647
648
649
650
651
652
653
654
				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
655
			}
lvs's avatar
lvs committed
656
/*
Leonardo Solis's avatar
Leonardo Solis committed
657
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
658
*/
659
//printf("test point LS 3\n");
Leonardo Solis's avatar
Leonardo Solis committed
660

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

671
672
673
674
675
676
677
678
679
680
			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;  

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

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

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

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

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

lvs's avatar
lvs committed
764
765
766
767
768
769
770
771
772
				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
773
			}
Leonardo Solis's avatar
Leonardo Solis committed
774

775
			ls_eval_cnt += eval_tmp1 + eval_tmp2 + eval_tmp3 + eval_tmp4 + eval_tmp5 + eval_tmp6 + eval_tmp7 + eval_tmp8 + eval_tmp9;
776
777

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

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

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

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

798
		// Update energy evaluations count: count LS and GG evals
Leonardo Solis's avatar
Leonardo Solis committed
799
800
		eval_cnt += ls_eval_cnt + DockConst_pop_size; 

801
		// Update generation count
Leonardo Solis's avatar
Leonardo Solis committed
802
803
		generation_cnt++;

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

809
	// ------------------------------------------------------------------
810
	// Off: turn off all other kernels
811
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
812

813
	// Turn off PRNG kernels
814
	const int tmp_int_one = 1;
Leonardo Solis's avatar
Leonardo Solis committed
815
816
817
818
819
	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
820
821
822
823
824
825
826
827
828
	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
829
/*
830
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
831
*/
832

833
	// Turn off LS kernels
834
835
836
837
838
839
840
841
842
	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
843
/*
844
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
845
*/
846

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

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

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