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

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

Leonardo Solis's avatar
Leonardo Solis committed
267
268
269
	__global float* GlobPopCurr = & GlobPopulationCurrent [Host_Offset_Pop];
	__global float* GlobEneCurr = & GlobEnergyCurrent     [Host_Offset_Ene];

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

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

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

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

		nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
		nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;	

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

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

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

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

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

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

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

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

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

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

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

		ushort best_entity = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

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

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

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

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

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

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

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

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

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

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