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

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

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

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

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

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

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

37
// Send energy values from InterE & IntraE to genotype-senders (IC, GG, LSs)
38 39 40 41 42
channel float 	chan_Intere2StoreIC_intere     __attribute__((depth(2)));
channel float 	chan_Intere2StoreGG_intere     __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS1_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS2_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS3_intere __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
43 44
channel float 	chan_Intere2StoreLS_LS4_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS5_intere __attribute__((depth(2)));
45 46 47 48
channel float 	chan_Intere2StoreLS_LS6_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS7_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS8_intere __attribute__((depth(2)));
channel float 	chan_Intere2StoreLS_LS9_intere __attribute__((depth(2)));
49 50 51 52 53
channel float 	chan_Intrae2StoreIC_intrae     __attribute__((depth(2)));
channel float 	chan_Intrae2StoreGG_intrae     __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS1_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS2_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS3_intrae __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
54 55
channel float 	chan_Intrae2StoreLS_LS4_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS5_intrae __attribute__((depth(2)));
56 57 58 59
channel float 	chan_Intrae2StoreLS_LS6_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS7_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS8_intrae __attribute__((depth(2)));
channel float 	chan_Intrae2StoreLS_LS9_intrae __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
60

61 62 63 64
// Send PRNG outputs from generators to consumers
channel float8   chan_PRNG2GA_BT_ushort_float_prng;
channel uchar2   chan_PRNG2GA_GG_uchar_prng;
channel float    chan_PRNG2GA_GG_float_prng     __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
65
channel ushort16 chan_PRNG2GA_LS123_ushort_prng;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
channel float    chan_PRNG2GA_LS_float_prng     __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS2_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS3_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS4_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS5_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS6_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS7_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS8_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float    chan_PRNG2GA_LS9_float_prng    __attribute__((depth(CHAN_DEPTH_GENOTYPE)));

// Turn-off signals to PRNG generators

// FIXME: these channels don't go anymore through IGL_Arbiter.
// That was initially the case, but was fixed.
// Name should be changed accordingly (GA instead of Arbiter)
// to avoid misleading data-flow information
82 83 84 85 86 87 88
channel bool 	chan_Arbiter_BT_ushort_float_off;
channel bool    chan_Arbiter_GG_uchar_off;
channel bool    chan_Arbiter_GG_float_off;
channel bool    chan_Arbiter_LS123_ushort_off;
channel bool    chan_Arbiter_LS_float_off;
channel bool    chan_Arbiter_LS2_float_off;
channel bool    chan_Arbiter_LS3_float_off;
Leonardo Solis's avatar
Leonardo Solis committed
89 90
channel bool    chan_Arbiter_LS4_float_off;
channel bool    chan_Arbiter_LS5_float_off;
91 92 93 94
channel bool    chan_Arbiter_LS6_float_off;
channel bool    chan_Arbiter_LS7_float_off;
channel bool    chan_Arbiter_LS8_float_off;
channel bool    chan_Arbiter_LS9_float_off;
95 96

// Send energy values and genotypes to LSs
Leonardo Solis's avatar
Leonardo Solis committed
97 98 99
channel float   chan_GA2LS_LS1_energy;
channel float   chan_GA2LS_LS2_energy;
channel float   chan_GA2LS_LS3_energy;
Leonardo Solis's avatar
Leonardo Solis committed
100 101
channel float   chan_GA2LS_LS4_energy;
channel float   chan_GA2LS_LS5_energy;
102 103 104 105
channel float   chan_GA2LS_LS6_energy;
channel float   chan_GA2LS_LS7_energy;
channel float   chan_GA2LS_LS8_energy;
channel float   chan_GA2LS_LS9_energy;
Leonardo Solis's avatar
Leonardo Solis committed
106 107 108
channel float  	chan_GA2LS_LS1_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS2_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS3_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
109 110
channel float  	chan_GA2LS_LS4_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS5_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
111 112 113 114
channel float  	chan_GA2LS_LS6_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS7_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS8_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_GA2LS_LS9_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
115

116
// Send LS status from LSs to IGL_Arbiter
Leonardo Solis's avatar
Leonardo Solis committed
117 118 119
channel bool    chan_LS2Arbiter_LS1_end;
channel bool    chan_LS2Arbiter_LS2_end;
channel bool    chan_LS2Arbiter_LS3_end;
Leonardo Solis's avatar
Leonardo Solis committed
120 121
channel bool    chan_LS2Arbiter_LS4_end;
channel bool    chan_LS2Arbiter_LS5_end;
122 123 124 125
channel bool    chan_LS2Arbiter_LS6_end;
channel bool    chan_LS2Arbiter_LS7_end;
channel bool    chan_LS2Arbiter_LS8_end;
channel bool    chan_LS2Arbiter_LS9_end;
126 127

// Get LS-eval-count, new energy, new genotype from LSs
128 129
channel float2  chan_LS2GA_LS1_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS2_evalenergy      __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
130 131 132
channel float2  chan_LS2GA_LS3_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS4_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS5_evalenergy      __attribute__((depth(2)));
133 134 135 136
channel float2  chan_LS2GA_LS6_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS7_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS8_evalenergy      __attribute__((depth(2)));
channel float2  chan_LS2GA_LS9_evalenergy      __attribute__((depth(2)));
Leonardo Solis's avatar
Leonardo Solis committed
137 138 139
channel float  	chan_LS2GA_LS1_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS2_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS3_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
140 141
channel float  	chan_LS2GA_LS4_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS5_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
142 143 144 145
channel float  	chan_LS2GA_LS6_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS7_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS8_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
channel float  	chan_LS2GA_LS9_genotype        __attribute__((depth(CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
146

147
// Turn-off signals to LSs
148
channel bool    chan_GA2LS_Off1_active;
Leonardo Solis's avatar
Leonardo Solis committed
149 150
channel bool    chan_GA2LS_Off2_active;
channel bool    chan_GA2LS_Off3_active;
Leonardo Solis's avatar
Leonardo Solis committed
151 152
channel bool    chan_GA2LS_Off4_active;
channel bool    chan_GA2LS_Off5_active;
153 154 155 156
channel bool    chan_GA2LS_Off6_active;
channel bool    chan_GA2LS_Off7_active;
channel bool    chan_GA2LS_Off8_active;
channel bool    chan_GA2LS_Off9_active;
Leonardo Solis's avatar
Leonardo Solis committed
157

158 159
// Send genotype-producer-channel selector and genotype 
// from IGL_Arbiter to Conform
160 161
channel char2  chan_IGL2Conform_actmode	       __attribute__((depth(9))); // active, mode
channel float  chan_IGL2Conform_genotype       __attribute__((depth(9*CHAN_DEPTH_GENOTYPE)));
Leonardo Solis's avatar
Leonardo Solis committed
162

163
// Turn-off signal to IGL_Arbiter, Conform, InterE, IntraE
Leonardo Solis's avatar
Leonardo Solis committed
164
channel bool   chan_IGLArbiter_Off;
Leonardo Solis's avatar
Leonardo Solis committed
165

166 167 168 169 170 171 172 173 174 175
#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)
176 177 178
#include "../defines_fixedpt.h"

typedef int3          fixedpt3;
179
typedef int4	      fixedpt4;
180 181
#endif

Leonardo Solis's avatar
Leonardo Solis committed
182
#if defined (FIXED_POINT_INTERE) || defined (FIXED_POINT_INTRAE)
183 184 185
#include "../defines_fixedpt_64.h"
#endif

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 218 219 220 221 222 223 224
#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)
225 226 227 228 229 230
#define FIXEDPT_180	0xB40000
#define FIXEDPT_360	0x1680000

fixedpt fixedpt_map_angle_180(fixedpt angle)
{
	fixedpt x = angle;
231 232 233 234 235 236
	//while (x < 0.0f) 
	if (x < 0)
	{ x += FIXEDPT_180; }
	//while (x > 180.0f)
	if (x > FIXEDPT_180)
	{ x -= FIXEDPT_180; }
237 238 239 240 241 242
	return x;
}

fixedpt fixedpt_map_angle_360(fixedpt angle)
{
	fixedpt x = angle;
243 244 245 246 247 248
	//while (x < 0.0f) 
	if (x < 0)
	{ x += FIXEDPT_360; }
	//while (x > 360.0f)
	if (x > FIXEDPT_360)
	{ x -= FIXEDPT_360;}
249 250 251 252
	return x;
}
#endif

253 254
// Shift register sizes
// Such registers are used to reduce Initiation Interval (II)
Leonardo Solis's avatar
Leonardo Solis committed
255 256 257
#define SHIFT_REG_SIZE 10
#define SHIFT_REG_SIZE_MINUS_ONE (SHIFT_REG_SIZE-1)

258
// --------------------------------------------------------------------------
259
// Lamarckian Genetic-Algorithm (GA): GA + LS (Local Search) 
260 261
// Originally from: searchoptimum.c
// --------------------------------------------------------------------------
262
__kernel __attribute__ ((max_global_work_dim(0)))
263 264
void Krnl_GA(__global       float*           restrict GlobPopulationCurrent,
	     __global       float*           restrict GlobEnergyCurrent,
265 266 267 268
	     #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
269
	     __global       unsigned int*    restrict GlobEvalsGenerations_performed,
270
	     #endif
Leonardo Solis's avatar
Leonardo Solis committed
271 272 273 274 275 276 277
			    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
278 279
		    	    float                     Host_two_absmaxdmov,
			    float                     Host_two_absmaxdang,
Leonardo Solis's avatar
Leonardo Solis committed
280 281
			    float                     DockConst_crossover_rate,
			    unsigned int              DockConst_num_of_lsentities,
282 283 284 285 286 287 288 289
			    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
	     )
290
{
291
	#if defined (DEBUG_KRNL_GA)
292
	printf("\n");
Leonardo Solis's avatar
Leonardo Solis committed
293
	printf("%-40s %u\n", "DockConst_pop_size: ",        		DockConst_pop_size);
Leonardo Solis's avatar
Leonardo Solis committed
294 295 296 297 298 299
	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
300 301
	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
302
	printf("%-40s %f\n", "DockConst_crossover_rate: ", 		DockConst_crossover_rate);
Leonardo Solis's avatar
Leonardo Solis committed
303
	printf("%-40s %u\n", "DockConst_num_of_lsentities: ",   	DockConst_num_of_lsentities);
Leonardo Solis's avatar
Leonardo Solis committed
304
	printf("%-40s %u\n", "DockConst_num_of_genes: ",        	DockConst_num_of_genes);
305 306
	#endif

307
	// Other banking configuration (see PopNext, eneNext) might reduce logic
308
	// but makes PopCurr stallable
Leonardo Solis's avatar
Leonardo Solis committed
309 310 311
	__local float LocalPopCurr[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
	__local float LocalEneCurr[MAX_POPSIZE];

Leonardo Solis's avatar
Leonardo Solis committed
312 313 314 315 316
	#if defined(SINGLE_COPY_POP_ENE)
	__global float* GlobPopCurr = & GlobPopulationCurrent [Host_Offset_Pop];
	__global float* GlobEneCurr = & GlobEnergyCurrent     [Host_Offset_Ene];
	#endif

317
	// ------------------------------------------------------------------
318
	// Initial Calculation (IC) of scores
319
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
320
	for (ushort pop_cnt = 0; pop_cnt < DockConst_pop_size; pop_cnt++) {
321
		// Calculate energy
322
		write_channel_altera(chan_GA2IGL_IC_active, true);
323 324
		mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
325
		for (uchar pipe_cnt=0; pipe_cnt<DockConst_num_of_genes; pipe_cnt++) {
326
			#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
327
			LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE] = GlobPopCurr[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
328
			#else
Leonardo Solis's avatar
Leonardo Solis committed
329
			LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE] = GlobPopulationCurrent[pop_cnt*ACTUAL_GENOTYPE_LENGTH + pipe_cnt];
330
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
331
			write_channel_altera(chan_IC2Conf_genotype, LocalPopCurr[pop_cnt][pipe_cnt & MASK_GENOTYPE]);	
332
		}
333

Leonardo Solis's avatar
Leonardo Solis committed
334
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
335
		printf("\nIC - tx pop: %u", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
336
		#endif
337

338
		// Read energy
339 340 341 342 343 344 345 346
		float energyIA_IC_rx;
		float energyIE_IC_rx;
		bool intra_valid = false;
		bool inter_valid = false;
		while( (intra_valid == false) || (inter_valid == false)) {
			if (intra_valid == false) {
				energyIA_IC_rx = read_channel_nb_altera(chan_Intrae2StoreIC_intrae, &intra_valid);
			}
Leonardo Solis's avatar
Leonardo Solis committed
347
			else if (inter_valid == false) {
348 349 350 351
				energyIE_IC_rx = read_channel_nb_altera(chan_Intere2StoreIC_intere, &inter_valid);
			}
		}

Leonardo Solis's avatar
Leonardo Solis committed
352
		LocalEneCurr[pop_cnt] = energyIA_IC_rx + energyIE_IC_rx;
353

Leonardo Solis's avatar
Leonardo Solis committed
354
		#if defined (DEBUG_KRNL_IC)
Leonardo Solis's avatar
Leonardo Solis committed
355
		printf(", IC - rx pop: %u\n", pop_cnt); 		
Leonardo Solis's avatar
Leonardo Solis committed
356
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
357
	}
358
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
359

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

Leonardo Solis's avatar
Leonardo Solis committed
362
	uint generation_cnt = 0;
Leonardo Solis's avatar
Leonardo Solis committed
363

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

366 367 368
		//float LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];
		//float LocalEneNext[MAX_POPSIZE];

369
		// This configuration reduces logic and does not increase block RAM usage
370 371 372 373 374 375 376 377 378 379 380 381
		float __attribute__ ((
				       memory,
		   		       numbanks(4),
			               bankwidth(32),
			              )) LocalPopNext[MAX_POPSIZE][ACTUAL_GENOTYPE_LENGTH];

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

382
		// ------------------------------------------------------------------
383
		// Genetic Generation (GG)
384
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
385 386 387 388 389
		float __attribute__ ((
				       memory,
		   		       numbanks(1),
			               bankwidth(64),
			               singlepump,
390
 			               numreadports(6),
Leonardo Solis's avatar
Leonardo Solis committed
391 392 393
			               numwriteports(1)
			              )) loc_energies[MAX_POPSIZE];

394
		// Shift register to reduce II (initially II=6) of best entity for-loop 
Leonardo Solis's avatar
Leonardo Solis committed
395
		float shift_reg[SHIFT_REG_SIZE];
Leonardo Solis's avatar
Leonardo Solis committed
396

Leonardo Solis's avatar
Leonardo Solis committed
397
		#pragma unroll
Leonardo Solis's avatar
Leonardo Solis committed
398
		for (uchar i=0; i<SHIFT_REG_SIZE; i++) {
Leonardo Solis's avatar
Leonardo Solis committed
399 400 401 402 403
			shift_reg[i] = 0.0f;
		}

		ushort best_entity = 0;

Leonardo Solis's avatar
Leonardo Solis committed
404 405 406 407 408
//		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];

409 410 411 412
			// Identifying best entity
			// The enclosing "if (pop_cnt>0) {}" should not be commented
			// but it is removed in order to improve performance.
			// It does not affect functionality as it performs 
Leonardo Solis's avatar
Leonardo Solis committed
413 414
			// an unnecessary evaluation when pop_cnt=0 
			//if (pop_cnt>0) {
Leonardo Solis's avatar
Leonardo Solis committed
415
			shift_reg[SHIFT_REG_SIZE_MINUS_ONE] = loc_energies[best_entity];
Leonardo Solis's avatar
Leonardo Solis committed
416

Leonardo Solis's avatar
Leonardo Solis committed
417 418 419 420 421 422 423 424
			#pragma unroll
			for (uchar j=0; j<SHIFT_REG_SIZE_MINUS_ONE; j++) {
				shift_reg[j] = shift_reg[j+1];
			}
			
			if (loc_energies[pop_cnt] < shift_reg[0]) {
				best_entity = pop_cnt;
			}
Leonardo Solis's avatar
Leonardo Solis committed
425
			//}
Leonardo Solis's avatar
Leonardo Solis committed
426
		}
427

428 429
		#pragma ivdep array (LocalPopNext)
		#pragma ivdep array (LocalEneNext)
Leonardo Solis's avatar
Leonardo Solis committed
430
		for (ushort new_pop_cnt = 1; new_pop_cnt < DockConst_pop_size; new_pop_cnt++) {
431 432

			// ---------------------------------------------------
433
			// Elitism: copying the best entity to new population
434 435 436 437
			// ---------------------------------------------------
			if (new_pop_cnt == 1) {
				#pragma unroll
				for (uchar gene_cnt=0; gene_cnt<ACTUAL_GENOTYPE_LENGTH; gene_cnt++) { 		
Leonardo Solis's avatar
Leonardo Solis committed
438
					LocalPopNext[0][gene_cnt & MASK_GENOTYPE] = LocalPopCurr[best_entity][gene_cnt & MASK_GENOTYPE]; 	
439 440 441 442
				} 		
				LocalEneNext[0] = loc_energies[best_entity];
			}

443 444 445
			#if defined (DEBUG_KRNL_GA)
			printf("Krnl_GA: %u\n", new_pop_cnt);
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
446

Leonardo Solis's avatar
Leonardo Solis committed
447 448
			float local_entity_1 [ACTUAL_GENOTYPE_LENGTH];
			float local_entity_2 [ACTUAL_GENOTYPE_LENGTH]; 
Leonardo Solis's avatar
Leonardo Solis committed
449
		
450
			// ---------------------------------------------------
451
			// Binary-Tournament (BT) selection
452
			// ---------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
453

454 455
			// Get ushort binary_tournament selection prngs (parent index)
			// Get float binary_tournament selection prngs (tournament rate)
456
			float8 bt_tmp = read_channel_altera(chan_PRNG2GA_BT_ushort_float_prng);
Leonardo Solis's avatar
Leonardo Solis committed
457
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
458

459
			// Convert: float prng that must be still converted to short
460 461 462 463 464
			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
465
			// short prng ready to be used, replace ushort prng_BT_U[4];
466 467 468 469 470
			ushort bt_tmp_u0 = *(uint*)&bt_tmp_uf0;
			ushort bt_tmp_u1 = *(uint*)&bt_tmp_uf1;
			ushort bt_tmp_u2 = *(uint*)&bt_tmp_uf2;
			ushort bt_tmp_u3 = *(uint*)&bt_tmp_uf3;

Leonardo Solis's avatar
Leonardo Solis committed
471
			// float prng ready to used, replace float prng_BT_F[4];
472 473 474 475 476
			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
477 478
			ushort parent1;
			ushort parent2; 
Leonardo Solis's avatar
Leonardo Solis committed
479

480
			// First parent
481 482 483 484 485 486 487
			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;}}

488
			// The better will be the second parent
489 490 491 492 493 494
			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
495

Leonardo Solis's avatar
Leonardo Solis committed
496
			// local_entity_1 and local_entity_2 are population-parent1, population-parent2
Leonardo Solis's avatar
Leonardo Solis committed
497
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
498 499
				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
500
			}
501

502
			// ---------------------------------------------------
503
			// Mating parents
504
			// ---------------------------------------------------	
Leonardo Solis's avatar
Leonardo Solis committed
505

506 507
			// get uchar genetic_generation prngs (gene index)
			// get float genetic_generation prngs (mutation rate)
508
			uchar2 prng_GG_C = read_channel_altera(chan_PRNG2GA_GG_uchar_prng);
Leonardo Solis's avatar
Leonardo Solis committed
509
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
510 511 512 513

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

515 516 517
			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
518 519 520 521
				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; }
522 523
			}
			
524
			// Reuse of bt prng float as crossover-rate
Leonardo Solis's avatar
Leonardo Solis committed
525 526
			bool crossover_yes = (DockConst_crossover_rate > bt_tmp_f0);

Leonardo Solis's avatar
Leonardo Solis committed
527 528 529
			write_channel_altera(chan_GA2IGL_GG_active, true);
			mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
530
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
531 532

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

Leonardo Solis's avatar
Leonardo Solis committed
535
				float tmp_offspring;
Leonardo Solis's avatar
Leonardo Solis committed
536

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

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

564
				// Calculate energy
Leonardo Solis's avatar
Leonardo Solis committed
565
				LocalPopNext [new_pop_cnt][gene_cnt & MASK_GENOTYPE] = tmp_offspring;
566
				write_channel_altera(chan_GG2Conf_genotype, tmp_offspring);
Leonardo Solis's avatar
Leonardo Solis committed
567
			}
568

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

573
			// Read energy
574 575 576 577 578 579 580 581
			float energyIA_GG_rx;
			float energyIE_GG_rx;
			bool intra_valid = false;
			bool inter_valid = false;
			while( (intra_valid == false) || (inter_valid == false)) {
				if (intra_valid == false) {
					energyIA_GG_rx = read_channel_nb_altera(chan_Intrae2StoreGG_intrae, &intra_valid);
				}
Leonardo Solis's avatar
Leonardo Solis committed
582
				else if (inter_valid == false) {
583 584 585 586
					energyIE_GG_rx = read_channel_nb_altera(chan_Intere2StoreGG_intere, &inter_valid);
				}
			}
			
Leonardo Solis's avatar
Leonardo Solis committed
587
			LocalEneNext[new_pop_cnt] = energyIA_GG_rx + energyIE_GG_rx;
Leonardo Solis's avatar
Leonardo Solis committed
588 589

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

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

Leonardo Solis's avatar
Leonardo Solis committed
600
		#pragma ivdep
601
		for (ushort ls_ent_cnt=0; ls_ent_cnt<DockConst_num_of_lsentities; ls_ent_cnt+=9) {
Leonardo Solis's avatar
Leonardo Solis committed
602

603
			// Choose random & different entities on every iteration
604
			ushort16 entity_ls = read_channel_altera(chan_PRNG2GA_LS123_ushort_prng);
Leonardo Solis's avatar
Leonardo Solis committed
605 606
			mem_fence(CLK_CHANNEL_MEM_FENCE);

Leonardo Solis's avatar
Leonardo Solis committed
607 608 609 610 611
			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;
612 613 614 615
			ushort entity_ls6 = entity_ls.s5;
			ushort entity_ls7 = entity_ls.s6;
			ushort entity_ls8 = entity_ls.s7;
			ushort entity_ls9 = entity_ls.s8;
616

617 618 619
			write_channel_altera(chan_GA2LS_LS1_energy, LocalEneNext[entity_ls1]);
			write_channel_altera(chan_GA2LS_LS2_energy, LocalEneNext[entity_ls2]);
			write_channel_altera(chan_GA2LS_LS3_energy, LocalEneNext[entity_ls3]);
Leonardo Solis's avatar
Leonardo Solis committed
620 621
			write_channel_altera(chan_GA2LS_LS4_energy, LocalEneNext[entity_ls4]);
			write_channel_altera(chan_GA2LS_LS5_energy, LocalEneNext[entity_ls5]);
622 623 624 625
			write_channel_altera(chan_GA2LS_LS6_energy, LocalEneNext[entity_ls6]);
			write_channel_altera(chan_GA2LS_LS7_energy, LocalEneNext[entity_ls7]);
			write_channel_altera(chan_GA2LS_LS8_energy, LocalEneNext[entity_ls8]);
			write_channel_altera(chan_GA2LS_LS9_energy, LocalEneNext[entity_ls9]);
Leonardo Solis's avatar
Leonardo Solis committed
626
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
627

Leonardo Solis's avatar
Leonardo Solis committed
628
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
629 630 631
				write_channel_altera(chan_GA2LS_LS1_genotype, LocalPopNext[entity_ls1][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS2_genotype, LocalPopNext[entity_ls2][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS3_genotype, LocalPopNext[entity_ls3][gene_cnt & MASK_GENOTYPE]);
Leonardo Solis's avatar
Leonardo Solis committed
632 633
				write_channel_altera(chan_GA2LS_LS4_genotype, LocalPopNext[entity_ls4][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS5_genotype, LocalPopNext[entity_ls5][gene_cnt & MASK_GENOTYPE]);
634 635 636 637
				write_channel_altera(chan_GA2LS_LS6_genotype, LocalPopNext[entity_ls6][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS7_genotype, LocalPopNext[entity_ls7][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS8_genotype, LocalPopNext[entity_ls8][gene_cnt & MASK_GENOTYPE]);
				write_channel_altera(chan_GA2LS_LS9_genotype, LocalPopNext[entity_ls9][gene_cnt & MASK_GENOTYPE]);
Leonardo Solis's avatar
Leonardo Solis committed
638 639
			}
			mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
640

641 642 643
			float2 evalenergy_tmp1;
			float2 evalenergy_tmp2;
			float2 evalenergy_tmp3;
Leonardo Solis's avatar
Leonardo Solis committed
644 645
			float2 evalenergy_tmp4;
			float2 evalenergy_tmp5;
646 647 648 649
			float2 evalenergy_tmp6;
			float2 evalenergy_tmp7;
			float2 evalenergy_tmp8;
			float2 evalenergy_tmp9;
650 651
			bool ls1_done = false;
			bool ls2_done = false;
Leonardo Solis's avatar
Leonardo Solis committed
652 653
			bool ls3_done = false;
			bool ls4_done = false;
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
			bool ls5_done = false;
			bool ls6_done = false;
			bool ls7_done = false;
			bool ls8_done = false;
			bool ls9_done = false;  
  
			while( (ls1_done == false) || 
			       (ls2_done == false) || 
			       (ls3_done == false) || 
			       (ls4_done == false) || 
			       (ls5_done == false) ||
			       (ls6_done == false) || 
			       (ls7_done == false) || 
			       (ls8_done == false) || 
			       (ls9_done == false) 
			)
			{
671 672 673 674 675 676 677 678 679
				if (ls1_done == false) {
					evalenergy_tmp1 = read_channel_nb_altera(chan_LS2GA_LS1_evalenergy, &ls1_done);
				}
				else if (ls2_done == false) {
					evalenergy_tmp2 = read_channel_nb_altera(chan_LS2GA_LS2_evalenergy, &ls2_done);
				}
				else if (ls3_done == false) {
					evalenergy_tmp3 = read_channel_nb_altera(chan_LS2GA_LS3_evalenergy, &ls3_done);
				}
Leonardo Solis's avatar
Leonardo Solis committed
680 681 682 683 684 685
				else if (ls4_done == false) {
					evalenergy_tmp4 = read_channel_nb_altera(chan_LS2GA_LS4_evalenergy, &ls4_done);
				}
				else if (ls5_done == false) {
					evalenergy_tmp5 = read_channel_nb_altera(chan_LS2GA_LS5_evalenergy, &ls5_done);
				}
686 687 688 689 690 691 692 693 694 695 696 697
				else if (ls6_done == false) {
					evalenergy_tmp6 = read_channel_nb_altera(chan_LS2GA_LS6_evalenergy, &ls6_done);
				}
				else if (ls7_done == false) {
					evalenergy_tmp7 = read_channel_nb_altera(chan_LS2GA_LS7_evalenergy, &ls7_done);
				}
				else if (ls8_done == false) {
					evalenergy_tmp8 = read_channel_nb_altera(chan_LS2GA_LS8_evalenergy, &ls8_done);
				}
				else if (ls9_done == false) {
					evalenergy_tmp9 = read_channel_nb_altera(chan_LS2GA_LS9_evalenergy, &ls9_done);
				}
698
			}
699
		
700 701 702
			#if defined (DEBUG_KRNL_LS)
			printf("LS - got all eval & energies back\n");
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
703 704 705 706

			float eetmp1 = evalenergy_tmp1.x;
			float eetmp2 = evalenergy_tmp2.x;
			float eetmp3 = evalenergy_tmp3.x;
Leonardo Solis's avatar
Leonardo Solis committed
707 708
			float eetmp4 = evalenergy_tmp4.x;
			float eetmp5 = evalenergy_tmp5.x;
709 710 711 712
			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
713 714 715 716

			uint eval_tmp1 = *(uint*)&eetmp1;
			uint eval_tmp2 = *(uint*)&eetmp2;
			uint eval_tmp3 = *(uint*)&eetmp3;
Leonardo Solis's avatar
Leonardo Solis committed
717 718
			uint eval_tmp4 = *(uint*)&eetmp4;
			uint eval_tmp5 = *(uint*)&eetmp5;
719 720 721 722
			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
723 724 725 726

			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
727 728
			LocalEneNext[entity_ls4] = evalenergy_tmp4.y;
			LocalEneNext[entity_ls5] = evalenergy_tmp5.y;
729 730 731 732
			LocalEneNext[entity_ls6] = evalenergy_tmp6.y;
			LocalEneNext[entity_ls7] = evalenergy_tmp7.y;
			LocalEneNext[entity_ls8] = evalenergy_tmp8.y;
			LocalEneNext[entity_ls9] = evalenergy_tmp9.y;
733

734
			#pragma ivdep
Leonardo Solis's avatar
Leonardo Solis committed
735
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
736 737 738
				LocalPopNext[entity_ls1][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS1_genotype);
				LocalPopNext[entity_ls2][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS2_genotype);
				LocalPopNext[entity_ls3][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS3_genotype);
Leonardo Solis's avatar
Leonardo Solis committed
739 740
				LocalPopNext[entity_ls4][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS4_genotype);
				LocalPopNext[entity_ls5][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS5_genotype);
741 742 743 744
				LocalPopNext[entity_ls6][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS6_genotype);
				LocalPopNext[entity_ls7][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS7_genotype);
				LocalPopNext[entity_ls8][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS8_genotype);
				LocalPopNext[entity_ls9][gene_cnt & MASK_GENOTYPE] = read_channel_altera(chan_LS2GA_LS9_genotype);
Leonardo Solis's avatar
Leonardo Solis committed
745
			}
Leonardo Solis's avatar
Leonardo Solis committed
746

747
			ls_eval_cnt += eval_tmp1 + eval_tmp2 + eval_tmp3 + eval_tmp4 + eval_tmp5 + eval_tmp6 + eval_tmp7 + eval_tmp8 + eval_tmp9;
748 749

			#if defined (DEBUG_KRNL_LS)
Leonardo Solis's avatar
Leonardo Solis committed
750
			printf("%u, ls_eval_cnt: %u\n", ls_ent_cnt, ls_eval_cnt);
751 752
			printf("LS - got all genotypes back\n");
			#endif
753 754
		} // End of for-loop ls_ent_cnt
		// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
755

756
		// Update current pops & energies
757
		for (ushort pop_cnt=0; pop_cnt<DockConst_pop_size; pop_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
758
			for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
Leonardo Solis's avatar
Leonardo Solis committed
759
				LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE] = LocalPopNext[pop_cnt][gene_cnt & MASK_GENOTYPE];
Leonardo Solis's avatar
Leonardo Solis committed
760 761 762 763 764
			}

			LocalEneCurr[pop_cnt] = LocalEneNext[pop_cnt];
		}

765
		// Update energy evaluations count: count LS and GG evals
Leonardo Solis's avatar
Leonardo Solis committed
766 767
		eval_cnt += ls_eval_cnt + DockConst_pop_size; 

768
		// Update generation count
Leonardo Solis's avatar
Leonardo Solis committed
769 770
		generation_cnt++;

771 772 773
		#if defined (DEBUG_KRNL_GA)
		printf("eval_cnt: %u, generation_cnt: %u\n", eval_cnt, generation_cnt);
		#endif
774
	} // End while eval_cnt & generation_cnt
775

776
	// ------------------------------------------------------------------
777
	// Off: turn off all other kernels
778
	// ------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
779

780
	// Turn off PRNG kernels
781 782 783 784 785 786 787
	write_channel_altera(chan_Arbiter_BT_ushort_float_off,  false);
	write_channel_altera(chan_Arbiter_GG_uchar_off, 	false);
	write_channel_altera(chan_Arbiter_GG_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS123_ushort_off,  	false);
	write_channel_altera(chan_Arbiter_LS_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS2_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS3_float_off, 	false);
Leonardo Solis's avatar
Leonardo Solis committed
788 789
	write_channel_altera(chan_Arbiter_LS4_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS5_float_off, 	false);
790 791 792 793
	write_channel_altera(chan_Arbiter_LS6_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS7_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS8_float_off, 	false);
	write_channel_altera(chan_Arbiter_LS9_float_off, 	false);
794 795
	mem_fence(CLK_CHANNEL_MEM_FENCE);

796
	// Turn off LS kernels
797 798 799
	write_channel_altera(chan_GA2LS_Off1_active,  		false);
	write_channel_altera(chan_GA2LS_Off2_active,  		false);
	write_channel_altera(chan_GA2LS_Off3_active,  		false);
Leonardo Solis's avatar
Leonardo Solis committed
800 801
	write_channel_altera(chan_GA2LS_Off4_active,  		false);
	write_channel_altera(chan_GA2LS_Off5_active,  		false);
802 803 804 805
	write_channel_altera(chan_GA2LS_Off6_active,  		false);
	write_channel_altera(chan_GA2LS_Off7_active,  		false);
	write_channel_altera(chan_GA2LS_Off8_active,  		false);
	write_channel_altera(chan_GA2LS_Off9_active,  		false);
806 807
	mem_fence(CLK_CHANNEL_MEM_FENCE);

808
	// Turn off IGL_Arbiter, Conform, InterE, IntraE kernerls
809 810
	write_channel_altera(chan_IGLArbiter_Off,     		false);
	mem_fence(CLK_CHANNEL_MEM_FENCE);
Leonardo Solis's avatar
Leonardo Solis committed
811

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

Leonardo Solis's avatar
Leonardo Solis committed
815
		for (uchar gene_cnt=0; gene_cnt<DockConst_num_of_genes; gene_cnt++) {
816
			#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
817
			GlobPopCurr[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt] = LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE];
818
			#else
Leonardo Solis's avatar
Leonardo Solis committed
819
			GlobPopulationCurrent[pop_cnt*ACTUAL_GENOTYPE_LENGTH + gene_cnt] = LocalPopCurr[pop_cnt][gene_cnt & MASK_GENOTYPE];
820
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
821
		}
822 823

		#if defined(SINGLE_COPY_POP_ENE)
Leonardo Solis's avatar
Leonardo Solis committed
824
		GlobEneCurr[pop_cnt] = LocalEneCurr[pop_cnt];
825
		#else
Leonardo Solis's avatar
Leonardo Solis committed
826
		GlobEnergyCurrent[pop_cnt] = LocalEneCurr[pop_cnt];
827
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
828 829
	}

830
	#if defined (DEBUG_KRNL_GA)
Leonardo Solis's avatar
Leonardo Solis committed
831
	printf("GA: %u %u\n", active, DockConst_pop_size -1);
832 833
	#endif

834 835
	#if defined (DEBUG_ACTIVE_KERNEL)
	printf("	%-20s: %s\n", "Krnl_GA", "disabled");
836
	#endif
Leonardo Solis's avatar
Leonardo Solis committed
837

838 839
	// Write final evals & generation counts to FPGA-board DDRs
	#if defined(SINGLE_COPY_POP_ENE)
840 841
	GlobEvals_performed[Host_RunId] = eval_cnt;
	GlobGens_performed [Host_RunId] = generation_cnt;
842
	#else
Leonardo Solis's avatar
Leonardo Solis committed
843 844
	GlobEvalsGenerations_performed[0] = eval_cnt;
	GlobEvalsGenerations_performed[1] = generation_cnt;
845
	#endif
846 847 848 849 850
}

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

Leonardo Solis's avatar
Leonardo Solis committed
851
#include "Krnl_PRNG.cl"
852 853

#include "Krnl_LS.cl"
854 855
#include "Krnl_LS2.cl"
#include "Krnl_LS3.cl"
Leonardo Solis's avatar
Leonardo Solis committed
856 857
#include "Krnl_LS4.cl"
#include "Krnl_LS5.cl"
858 859 860 861
#include "Krnl_LS6.cl"
#include "Krnl_LS7.cl"
#include "Krnl_LS8.cl"
#include "Krnl_LS9.cl"
862 863

#include "Krnl_IGL_Arbiter.cl"
864 865
#include "Krnl_Conform.cl"
#include "Krnl_InterE.cl"
866
#include "Krnl_IntraE.cl"
867