Krnl_GA.cl 40.9 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
80
81
82
83
84
85
86
87
88
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)));
pipe float    chan_PRNG2GA_LS_float_prng     	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS2_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS3_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS4_float_prng   	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS5_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS6_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS7_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS8_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
pipe float    chan_PRNG2GA_LS9_float_prng    	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_64)));
89
90
91
92
93
94
95

// Turn-off signals to PRNG generators

// FIXME: these channels don't go anymore through IGL_Arbiter.
// That was initially the case, but was fixed.
// Name should be changed accordingly (GA instead of Arbiter)
// to avoid misleading data-flow information
lvs's avatar
lvs committed
96
97

// Resized to valid SDAccel depths: 16, 32, ...
98
99
100
101
102
103
104
105
106
107
108
109
110
pipe int    chan_Arbiter_BT_ushort_float_off	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_GG_uchar_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_GG_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS123_ushort_off	__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS2_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS3_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS4_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS5_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS6_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS7_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS8_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
pipe int    chan_Arbiter_LS9_float_off		__attribute__((xcl_reqd_pipe_depth(PIPE_DEPTH_16)));
111
112

// Send energy values and genotypes to LSs
lvs's avatar
lvs committed
113
// Resized to valid SDAccel depths: 16, 32, ...
114
115
116
117
118
119
120
121
122
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
123
124

// Resized to valid SDAccel depths: 16, 32, ...
125
126
127
128
129
130
131
132
133
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
134

135
// Send LS status from LSs to IGL_Arbiter
136
137
138
139
140
141
142
143
144
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)));
145
// Get LS-eval-count, new energy, new genotype from LSs
lvs's avatar
lvs committed
146
// Resized to valid SDAccel depths: 16, 32, ...
147
148
149
150
151
152
153
154
155
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
156
157

// Resized to valid SDAccel depths: 16, 32, ...
158
159
160
161
162
163
164
165
166
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
167

168
// Turn-off signals to LSs
lvs's avatar
lvs committed
169
// Resized to valid SDAccel depths: 16, 32, ...
170
171
172
173
174
175
176
177
178
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
179
180

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

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

190
191
192
193
194
195
196
197
198
199
#if defined (FIXED_POINT_CONFORM) || \
    defined (FIXED_POINT_LS1)     || \
    defined (FIXED_POINT_LS2)     || \
    defined (FIXED_POINT_LS3)     || \
    defined (FIXED_POINT_LS4)     || \
    defined (FIXED_POINT_LS5)     || \
    defined (FIXED_POINT_LS6)     || \
    defined (FIXED_POINT_LS7)     || \
    defined (FIXED_POINT_LS8)     || \
    defined (FIXED_POINT_LS9)
200
201
202
#include "../defines_fixedpt.h"

typedef int3          fixedpt3;
203
typedef int4	      fixedpt4;
204
205
#endif

Leonardo Solis's avatar
Leonardo Solis committed
206
#if defined (FIXED_POINT_INTERE) || defined (FIXED_POINT_INTRAE)
207
208
209
#include "../defines_fixedpt_64.h"
#endif

Leonardo Solis's avatar
Leonardo Solis committed
210
// --------------------------------------------------------------------------
211
// Map the argument into the interval 0 - 180, or 0 - 360
Leonardo Solis's avatar
Leonardo Solis committed
212
213
214
215
216
217
218
// by adding/subtracting n*ang_max to/from it.
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------

float map_angle_180(float angle)
{
	float x = angle;
219
220
221
222
223
224
	//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
225
226
227
228
229
230
	return x;
}

float map_angle_360(float angle)
{
	float x = angle;
231
232
233
234
235
236
	//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
237
238
239
	return x;
}

240
241
242
243
244
245
246
247
248
#if defined (FIXED_POINT_LS1) || \
    defined (FIXED_POINT_LS2) || \
    defined (FIXED_POINT_LS3) || \
    defined (FIXED_POINT_LS4) || \
    defined (FIXED_POINT_LS5) || \
    defined (FIXED_POINT_LS6) || \
    defined (FIXED_POINT_LS7) || \
    defined (FIXED_POINT_LS8) || \
    defined (FIXED_POINT_LS9)
249
250
251
252
253
254
#define FIXEDPT_180	0xB40000
#define FIXEDPT_360	0x1680000

fixedpt fixedpt_map_angle_180(fixedpt angle)
{
	fixedpt x = angle;
255
256
257
258
259
260
	//while (x < 0.0f) 
	if (x < 0)
	{ x += FIXEDPT_180; }
	//while (x > 180.0f)
	if (x > FIXEDPT_180)
	{ x -= FIXEDPT_180; }
261
262
263
264
265
266
	return x;
}

fixedpt fixedpt_map_angle_360(fixedpt angle)
{
	fixedpt x = angle;
267
268
269
270
271
272
	//while (x < 0.0f) 
	if (x < 0)
	{ x += FIXEDPT_360; }
	//while (x > 360.0f)
	if (x > FIXEDPT_360)
	{ x -= FIXEDPT_360;}
273
274
275
276
	return x;
}
#endif

277
// --------------------------------------------------------------------------
278
// Lamarckian Genetic-Algorithm (GA): GA + LS (Local Search) 
279
280
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
281
/*
282
__kernel __attribute__ ((max_global_work_dim(0)))
lvs's avatar
lvs committed
283
284
*/
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
lvs's avatar
lvs committed
285
286
void Krnl_GA(
	     __global       float*           restrict GlobPopulationCurrent,
287
	     __global       float*           restrict GlobEnergyCurrent,
288
289
290
291
	     #if defined(SINGLE_COPY_POP_ENE)
   	     __global       unsigned int*    restrict GlobEvals_performed,
             __global       unsigned int*    restrict GlobGens_performed,
	     #else
Leonardo Solis's avatar
Leonardo Solis committed
292
	     __global       unsigned int*    restrict GlobEvalsGenerations_performed,
293
	     #endif
Leonardo Solis's avatar
Leonardo Solis committed
294
295
296
297
298
299
300
			    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
301
302
		    	    float                     Host_two_absmaxdmov,
			    float                     Host_two_absmaxdang,
Leonardo Solis's avatar
Leonardo Solis committed
303
304
			    float                     DockConst_crossover_rate,
			    unsigned int              DockConst_num_of_lsentities,
305
306
307
308
309
310
311
312
			    unsigned char             DockConst_num_of_genes
	     #if defined(SINGLE_COPY_POP_ENE)
	     					      ,
	                    unsigned short            Host_RunId,
			    unsigned int 	      Host_Offset_Pop,
			    unsigned int	      Host_Offset_Ene
	     #endif
	     )
313
{
314
	#if defined (DEBUG_KRNL_GA)
315
	printf("\n");
Leonardo Solis's avatar
Leonardo Solis committed
316
	printf("%-40s %u\n", "DockConst_pop_size: ",        		DockConst_pop_size);
Leonardo Solis's avatar
Leonardo Solis committed
317
318
319
320
321
322
	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
323
324
	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
325
	printf("%-40s %f\n", "DockConst_crossover_rate: ", 		DockConst_crossover_rate);
Leonardo Solis's avatar
Leonardo Solis committed
326
	printf("%-40s %u\n", "DockConst_num_of_lsentities: ",   	DockConst_num_of_lsentities);
Leonardo Solis's avatar
Leonardo Solis committed
327
	printf("%-40s %u\n", "DockConst_num_of_genes: ",        	DockConst_num_of_genes);
328
329
	#endif

330
	// Other banking configuration (see PopNext, eneNext) might reduce logic
331
	// but makes PopCurr stallable
Leonardo Solis's avatar
Leonardo Solis committed
332
333
334
	__local float LocalPopCurr[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
	__local float LocalEneCurr[MAX_POPSIZE];

Leonardo Solis's avatar
Leonardo Solis committed
335
336
337
338
339
	#if defined(SINGLE_COPY_POP_ENE)
	__global float* GlobPopCurr = & GlobPopulationCurrent [Host_Offset_Pop];
	__global float* GlobEneCurr = & GlobEnergyCurrent     [Host_Offset_Ene];
	#endif

340
	// ------------------------------------------------------------------
341
	// Initial Calculation (IC) of scores
342
	// ------------------------------------------------------------------
343
	__attribute__((xcl_pipeline_loop))
344
	LOOP_FOR_GA_IC_OUTER:
Leonardo Solis's avatar
Leonardo Solis committed
345
	for (ushort pop_cnt = 0; pop_cnt < DockConst_pop_size; pop_cnt++) {
346
		// Calculate energy
347
348
		const int tmp_int_zero = 0;
		write_pipe_block(chan_GA2IGL_IC_active, &tmp_int_zero);
lvs's avatar
lvs committed
349
350
351
/*
		mem_fence(CLK_CHANNEL_MEM_FENCE);
*/
352
		__attribute__((xcl_pipeline_loop))
353
		LOOP_FOR_GA_IC_INNER_WRITE_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
354
		for (uchar pipe_cnt=0; pipe_cnt<DockConst_num_of_genes; pipe_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
355
			float tmp_ic;
356
			#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
357
			tmp_ic = GlobPopCurr[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
358
			#else
Leonardo Solis's avatar
Leonardo Solis committed
359
			tmp_ic = GlobPopulationCurrent[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
360
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
361

Leonardo Solis's avatar
Leonardo Solis committed
362
363
			LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE] = tmp_ic;
			write_pipe_block(chan_IC2Conf_genotype, &tmp_ic);	
364
		}
365

Leonardo Solis's avatar
Leonardo Solis committed
366
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
367
		printf("\nIC - tx pop: %u", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
368
		#endif
369

370
		// Read energy
371
372
		float energyIA_IC_rx;
		float energyIE_IC_rx;
373
374
375
376

		nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
		nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;	

377
		__attribute__((xcl_pipeline_loop))
378
		LOOP_WHILE_GA_IC_INNER_READ_ENERGY:
379
		while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {
lvs's avatar
lvs committed
380

381
			if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
382
				intra_valid = read_pipe(chan_Intrae2StoreIC_intrae, &energyIA_IC_rx);
383
			}
384
			else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
385
				inter_valid = read_pipe(chan_Intere2StoreIC_intere, &energyIE_IC_rx);
386
387
388
			}
		}

Leonardo Solis's avatar
Leonardo Solis committed
389
		LocalEneCurr[pop_cnt] = energyIA_IC_rx + energyIE_IC_rx;
390

391
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
392
		printf(", IC - rx pop: %u\n", pop_cnt); 		
393
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
394
	}
395
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
396

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

Leonardo Solis's avatar
Leonardo Solis committed
399
	uint generation_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
400

401
402
	__attribute__((xcl_pipeline_loop))
	LOOP_WHILE_GA_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
403
	while ((eval_cnt < DockConst_num_of_energy_evals) && (generation_cnt < DockConst_num_of_generations)) {
Leonardo Solis's avatar
Leonardo Solis committed
404

405
406
407
		//float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
		//float LocalEneNext[MAX_POPSIZE];

408
		// This configuration reduces logic and does not increase block RAM usage
lvs's avatar
lvs committed
409
/*
410
411
412
413
414
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(32),
			              )) LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
lvs's avatar
lvs committed
415
416
*/
		float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
417

lvs's avatar
lvs committed
418
/*
419
420
421
422
423
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(4),
			              )) LocalEneNext[MAX_POPSIZE];
lvs's avatar
lvs committed
424
425
*/
		float LocalEneNext[MAX_POPSIZE];
426

427
		// ------------------------------------------------------------------
428
		// Genetic Generation (GG)
429
		// ------------------------------------------------------------------
lvs's avatar
lvs committed
430
/*
Leonardo Solis's avatar
Leonardo Solis committed
431
432
433
434
435
		float __attribute__ ((
				       memory,
		   		       numbanks(1),
			               bankwidth(64),
			               singlepump,
436
 			               numreadports(6),
Leonardo Solis's avatar
Leonardo Solis committed
437
438
			               numwriteports(1)
			              )) loc_energies[MAX_POPSIZE];
lvs's avatar
lvs committed
439
440
*/
		float loc_energies[MAX_POPSIZE];
Leonardo Solis's avatar
Leonardo Solis committed
441
442
443

		ushort best_entity = 0;

444
445
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_SHIFT: 
Leonardo Solis's avatar
Leonardo Solis committed
446
447
448
449
450
//		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];

451
452
453
454
455
456
457
458
			#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
459
		}
460

461
462
463
464
		#if defined (DEBUG_KRNL_GA)
		printf("best_entity: %3u, energy: %20.6f\n", best_entity, loc_energies[best_entity]);
		#endif

465
		/*
466
467
		#pragma ivdep array (LocalPopNext)
		#pragma ivdep array (LocalEneNext)
468
		*/
469
470
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_OUTER_GLOBAL: 
Leonardo Solis's avatar
Leonardo Solis committed
471
		for (ushort new_pop_cnt = 1; new_pop_cnt < DockConst_pop_size; new_pop_cnt++) {
472
473

			// ---------------------------------------------------
474
			// Elitism: copying the best entity to new population
475
476
			// ---------------------------------------------------
			if (new_pop_cnt == 1) {
477
				__attribute__((xcl_pipeline_loop))
478
				LOOP_FOR_GA_INNER_ELITISM:
479
				for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
480
					LocalPopNext[0][gene_cnt & MASK_GENOTYPE] = LocalPopCurr[best_entity][gene_cnt & MASK_GENOTYPE]; 	
481
482
483
484
				} 		
				LocalEneNext[0] = loc_energies[best_entity];
			}

485
			#if defined (DEBUG_KRNL_GA)
486
			printf("Krnl_GA: %u\n", new_pop_cnt);
487
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
488

Leonardo Solis's avatar
Leonardo Solis committed
489
490
			float local_entity_1 [ACTUAL_GENOTYPE_LENGTH];
			float local_entity_2 [ACTUAL_GENOTYPE_LENGTH]; 
Leonardo Solis's avatar
Leonardo Solis committed
491
		
492
			// ---------------------------------------------------
493
			// Binary-Tournament (BT) selection
494
			// ---------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
495

496
497
			// Get ushort binary_tournament selection prngs (parent index)
			// Get float binary_tournament selection prngs (tournament rate)
lvs's avatar
lvs committed
498
499
500
			float8 bt_tmp;
			read_pipe_block(chan_PRNG2GA_BT_ushort_float_prng, &bt_tmp);
/*
Leonardo Solis's avatar
Leonardo Solis committed
501
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
502
*/
Leonardo Solis's avatar
Leonardo Solis committed
503
//printf("test point 1\n");
504
			// Convert: float prng that must be still converted to short
505
506
507
508
509
			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
510
			// short prng ready to be used, replace ushort prng_BT_U[4];
511
/*
512
513
514
515
			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;
516
517
518
519
520
521
522
*/
			// 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;
523

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

533
			// First parent
534
535
536
537
538
539
540
			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;}}

541
			// The better will be the second parent
542
543
544
545
546
547
			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
548

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

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

561
562
			// get uchar genetic_generation prngs (gene index)
			// get float genetic_generation prngs (mutation rate)
lvs's avatar
lvs committed
563
564
565
			uchar2 prng_GG_C;
			read_pipe_block(chan_PRNG2GA_GG_uchar_prng, &prng_GG_C);
/*
Leonardo Solis's avatar
Leonardo Solis committed
566
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
567
*/
Leonardo Solis's avatar
Leonardo Solis committed
568
//printf("test point 2\n");
Leonardo Solis's avatar
Leonardo Solis committed
569
570
571
572

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

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

586
587
			const int tmp_int_zero = 0;
			write_pipe_block(chan_GA2IGL_GG_active, &tmp_int_zero);
lvs's avatar
lvs committed
588
/*
Leonardo Solis's avatar
Leonardo Solis committed
589
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
590
*/
Leonardo Solis's avatar
Leonardo Solis committed
591
//printf("test point 3\n");
Leonardo Solis's avatar
Leonardo Solis committed
592

593
594
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_INNER_CROSS_MUT:
Leonardo Solis's avatar
Leonardo Solis committed
595
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
lvs's avatar
lvs committed
596
597
598
				float prngGG;
				read_pipe_block(chan_PRNG2GA_GG_float_prng, &prngGG);
/*
Leonardo Solis's avatar
Leonardo Solis committed
599
				mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
600
*/
Leonardo Solis's avatar
Leonardo Solis committed
601
//printf("test point 4\n");
Leonardo Solis's avatar
Leonardo Solis committed
602

Leonardo Solis's avatar
Leonardo Solis committed
603
				float tmp_offspring;
Leonardo Solis's avatar
Leonardo Solis committed
604

605
				// Performing crossover
Leonardo Solis's avatar
Leonardo Solis committed
606
607
608
609
610
611
612
				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
				   ) {
613
					tmp_offspring = local_entity_1[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
614
615
				}
				else {
616
					tmp_offspring = local_entity_2[gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
617
618
				}

619
				// Performing mutation
Leonardo Solis's avatar
Leonardo Solis committed
620
				if (DockConst_mutation_rate > prngGG) {
Leonardo Solis's avatar
Leonardo Solis committed
621
					if(gene_cnt<3) {
Leonardo Solis's avatar
Leonardo Solis committed
622
						tmp_offspring = tmp_offspring + Host_two_absmaxdmov*prngGG-DockConst_abs_max_dmov;
Leonardo Solis's avatar
Leonardo Solis committed
623
624
625
					}
					else {
						float tmp;
Leonardo Solis's avatar
Leonardo Solis committed
626
627
628
						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
629
630
631
					}
				}

632
				// Calculate energy
Leonardo Solis's avatar
Leonardo Solis committed
633
				LocalPopNext [new_pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_offspring;
lvs's avatar
lvs committed
634
				write_pipe_block(chan_GG2Conf_genotype, &tmp_offspring);
Leonardo Solis's avatar
Leonardo Solis committed
635
//printf("test point 5\n");
Leonardo Solis's avatar
Leonardo Solis committed
636
			}
637

638
			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
639
			printf("GG - tx pop: %u", new_pop_cnt); 		
640
			#endif	
641

642
			// Read energy
643
644
			float energyIA_GG_rx;
			float energyIE_GG_rx;
lvs's avatar
lvs committed
645

646
647
648
			nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
			nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;

649
650
			__attribute__((xcl_pipeline_loop))
			LOOP_WHILE_GA_INNER_READ_ENERGIES:
651
652
653
			while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {

				if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
654
					intra_valid = read_pipe(chan_Intrae2StoreGG_intrae, &energyIA_GG_rx);
655
				}
656
				else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
657
					inter_valid = read_pipe(chan_Intere2StoreGG_intere, &energyIE_GG_rx);
658
				}
659

Leonardo Solis's avatar
Leonardo Solis committed
660
//printf("intra_valid: %i, inter_valid: %i\n", intra_valid, inter_valid);
661
			}
Leonardo Solis's avatar
Leonardo Solis committed
662
//printf("test point 5\n");			
Leonardo Solis's avatar
Leonardo Solis committed
663
			LocalEneNext[new_pop_cnt] = energyIA_GG_rx + energyIE_GG_rx;
Leonardo Solis's avatar
Leonardo Solis committed
664

665
			#if defined (DEBUG_KRNL_GG)
Leonardo Solis's avatar
Leonardo Solis committed
666
			printf(", GG - rx pop: %u\n", new_pop_cnt); 		
667
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
668
		} 
669
670
		// ------------------------------------------------------------------
		// LS: Local Search
671
		// Subject num_of_entity_for_ls pieces of offsprings to LS 
Leonardo Solis's avatar
Leonardo Solis committed
672
		// ------------------------------------------------------------------
673

Leonardo Solis's avatar
Leonardo Solis committed
674
		uint ls_eval_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
675

676
		/*
Leonardo Solis's avatar
Leonardo Solis committed
677
		#pragma ivdep
678
		*/
679
680
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_LS_OUTER:
681
		for (ushort ls_ent_cnt=0; ls_ent_cnt<DockConst_num_of_lsentities; ls_ent_cnt+=9) {
Leonardo Solis's avatar
Leonardo Solis committed
682

683
			// Choose random & different entities on every iteration
lvs's avatar
lvs committed
684
685
686
			ushort16 entity_ls;
			read_pipe_block(chan_PRNG2GA_LS123_ushort_prng, &entity_ls);
/*
Leonardo Solis's avatar
Leonardo Solis committed
687
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
688
*/
689
//printf("test point LS 1\n");
Leonardo Solis's avatar
Leonardo Solis committed
690

Leonardo Solis's avatar
Leonardo Solis committed
691
692
693
694
695
			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;
696
697
698
699
			ushort entity_ls6 = entity_ls.s5;
			ushort entity_ls7 = entity_ls.s6;
			ushort entity_ls8 = entity_ls.s7;
			ushort entity_ls9 = entity_ls.s8;
700

lvs's avatar
lvs committed
701
702
703
704
705
706
707
708
709
			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]);
710
711

//printf("test point LS 2\n");
lvs's avatar
lvs committed
712
/*
Leonardo Solis's avatar
Leonardo Solis committed
713
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
714
*/
715
716
			__attribute__((xcl_pipeline_loop))
			LOOP_GA_LS_INNER_WRITE_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
717
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
lvs's avatar
lvs committed
718
719
720
721
722
723
724
725
726
				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
727
			}
lvs's avatar
lvs committed
728
/*
Leonardo Solis's avatar
Leonardo Solis committed
729
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
730
*/
731
//printf("test point LS 3\n");
Leonardo Solis's avatar
Leonardo Solis committed
732

733
734
735
			float2 evalenergy_tmp1;
			float2 evalenergy_tmp2;
			float2 evalenergy_tmp3;
Leonardo Solis's avatar
Leonardo Solis committed
736
737
			float2 evalenergy_tmp4;
			float2 evalenergy_tmp5;
738
739
740
741
			float2 evalenergy_tmp6;
			float2 evalenergy_tmp7;
			float2 evalenergy_tmp8;
			float2 evalenergy_tmp9;
lvs's avatar
lvs committed
742

743
744
745
746
747
748
749
750
751
752
			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;  

753
754
			__attribute__((xcl_pipeline_loop))
			LOOP_WHILE_GA_LS_INNER_READ_ENERGIES:
755
756
757
758
759
760
761
762
763
			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) 
764
765
			)
			{
766
				if (ls1_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
767
					ls1_done = read_pipe(chan_LS2GA_LS1_evalenergy, &evalenergy_tmp1);
768
				}
769
				else if (ls2_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
770
					ls2_done = read_pipe(chan_LS2GA_LS2_evalenergy, &evalenergy_tmp2);
771
				}
772
				else if (ls3_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
773
					ls3_done = read_pipe(chan_LS2GA_LS3_evalenergy, &evalenergy_tmp3);
774
				}
775
				else if (ls4_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
776
					ls4_done = read_pipe(chan_LS2GA_LS4_evalenergy, &evalenergy_tmp4);
Leonardo Solis's avatar
Leonardo Solis committed
777
				}
778
				else if (ls5_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
779
					ls5_done = read_pipe(chan_LS2GA_LS5_evalenergy, &evalenergy_tmp5);
Leonardo Solis's avatar
Leonardo Solis committed
780
				}
781
				else if (ls6_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
782
					ls6_done = read_pipe(chan_LS2GA_LS6_evalenergy, &evalenergy_tmp6);
783
				}
784
				else if (ls7_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
785
					ls7_done = read_pipe(chan_LS2GA_LS7_evalenergy, &evalenergy_tmp7);
786
				}
787
				else if (ls8_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
788
					ls8_done = read_pipe(chan_LS2GA_LS8_evalenergy, &evalenergy_tmp8);
789
				}
790
				else if (ls9_done != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
791
					ls9_done = read_pipe(chan_LS2GA_LS9_evalenergy, &evalenergy_tmp9);
792
				}
793
			}
794
		
795
796
797
			#if defined (DEBUG_KRNL_LS)
			printf("LS - got all eval & energies back\n");
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
798
799
800
801

			float eetmp1 = evalenergy_tmp1.x;
			float eetmp2 = evalenergy_tmp2.x;
			float eetmp3 = evalenergy_tmp3.x;
Leonardo Solis's avatar
Leonardo Solis committed
802
803
			float eetmp4 = evalenergy_tmp4.x;
			float eetmp5 = evalenergy_tmp5.x;
804
805
806
807
			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
808
809
810
811

			uint eval_tmp1 = *(uint*)&eetmp1;
			uint eval_tmp2 = *(uint*)&eetmp2;
			uint eval_tmp3 = *(uint*)&eetmp3;
Leonardo Solis's avatar
Leonardo Solis committed
812
813
			uint eval_tmp4 = *(uint*)&eetmp4;
			uint eval_tmp5 = *(uint*)&eetmp5;
814
815
816
817
			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
818
819
820
821

			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
822
823
			LocalEneNext[entity_ls4] = evalenergy_tmp4.y;
			LocalEneNext[entity_ls5] = evalenergy_tmp5.y;
824
825
826
827
			LocalEneNext[entity_ls6] = evalenergy_tmp6.y;
			LocalEneNext[entity_ls7] = evalenergy_tmp7.y;
			LocalEneNext[entity_ls8] = evalenergy_tmp8.y;
			LocalEneNext[entity_ls9] = evalenergy_tmp9.y;
828

829
			/*
830
			#pragma ivdep
831
			*/
832
833
			__attribute__((xcl_pipeline_loop))
			LOOP_FOR_GA_LS_INNER_READ_GENOTYPE:
Leonardo Solis's avatar
Leonardo Solis committed
834
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
835

lvs's avatar
lvs committed
836
837
838
839
840
841
842
843
844
				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
845
			}
Leonardo Solis's avatar
Leonardo Solis committed
846

847
			ls_eval_cnt += eval_tmp1 + eval_tmp2 + eval_tmp3 + eval_tmp4 + eval_tmp5 + eval_tmp6 + eval_tmp7 + eval_tmp8 + eval_tmp9;
848
849

			#if defined (DEBUG_KRNL_LS)
Leonardo Solis's avatar
Leonardo Solis committed
850
			printf("%u, ls_eval_cnt: %u\n", ls_ent_cnt, ls_eval_cnt);
851
852
			printf("LS - got all genotypes back\n");
			#endif
853
854
		} // End of for-loop ls_ent_cnt
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
855

856
		// Update current pops & energies
857
858
		__attribute__((xcl_pipeline_loop))
		LOOP_FOR_GA_UPDATEPOP_OUTER:
859
		for (ushort pop_cnt=0; pop_cnt<DockConst_pop_size; pop_cnt++) {
860
861
862

			__attribute__((xcl_pipeline_loop))
			LOOP_GA_UPDATEPOP_INNER:
Leonardo Solis's avatar
Leonardo Solis committed
863
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
864
				LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = LocalPopNext[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
865
866
867
868
869
			}

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

870
		// Update energy evaluations count: count LS and GG evals
Leonardo Solis's avatar
Leonardo Solis committed
871
872
		eval_cnt += ls_eval_cnt + DockConst_pop_size; 

873
		// Update generation count
Leonardo Solis's avatar
Leonardo Solis committed
874
875
		generation_cnt++;

876
877
878
		#if defined (DEBUG_KRNL_GA)
		printf("eval_cnt: %u, generation_cnt: %u\n", eval_cnt, generation_cnt);
		#endif
879
	} // End while eval_cnt & generation_cnt
880

881
	// ------------------------------------------------------------------
882
	// Off: turn off all other kernels
883
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
884

885
	// Turn off PRNG kernels
886
887
888
889
890
891
892
893
894
895
896
897
898
899
	const int tmp_int_one = 1;
	write_pipe_block(chan_Arbiter_BT_ushort_float_off,  	&tmp_int_one);
	write_pipe_block(chan_Arbiter_GG_uchar_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_GG_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS123_ushort_off,  	&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS2_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS3_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS4_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS5_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS6_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS7_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS8_float_off, 		&tmp_int_one);
	write_pipe_block(chan_Arbiter_LS9_float_off, 		&tmp_int_one);
lvs's avatar
lvs committed
900
/*
901
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
902
*/
903

904
	// Turn off LS kernels
905
906
907
908
909
910
911
912
913
	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
914
/*
915
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
916
*/
917

918
	// Turn off IGL_Arbiter, Conform, InterE, IntraE kernerls