Krnl_LS2.cl 7.85 KB
Newer Older
1
2
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
3
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
4
void Krnl_LS2(
5
		unsigned short            DockConst_max_num_of_iters,		
Leonardo Solis's avatar
Leonardo Solis committed
6
		float                     DockConst_rho_lower_bound,
7
		float                     DockConst_base_dmov_mul_sqrt3,
8
		unsigned char             DockConst_num_of_genes,
9
   		float                     DockConst_base_dang_mul_sqrt3,
Leonardo Solis's avatar
Leonardo Solis committed
10
		unsigned char             DockConst_cons_limit
11
12

#if !defined(SW_EMU)
13
14
		// IMPORTANT: enable this dummy global argument only for "hw" build.
		// Check ../common_xilinx/utility/boards.mk
15
16
17
18
		// https://forums.xilinx.com/t5/SDAccel/ERROR-KernelCheck-83-114-in-sdx-2017-4/td-p/818135
		,
		__global int *dummy
#endif
19
)
20
{
21
22
23
24
25
26
27
28
29
	#if 0
	printf("\nLS2: DockConst_max_num_of_iters: %u\n",   DockConst_max_num_of_iters);
	printf("LS2: DockConst_rho_lower_bound: %f\n",      DockConst_rho_lower_bound);
	printf("LS2: DockConst_base_dmov_mul_sqrt3: %f\n",  DockConst_base_dmov_mul_sqrt3);
	printf("LS2: DockConst_num_of_genes: %u\n",  	    DockConst_num_of_genes);
	printf("LS2: DockConst_base_dang_mul_sqrt3: %f\n",  DockConst_base_dang_mul_sqrt3);
	printf("LS2: DockConst_cons_limit: %u\n",           DockConst_cons_limit);
	#endif

30
31
	/*bool valid = true;*/
	char valid = 0x01;
32

33
34
__attribute__((xcl_pipeline_loop))
LOOP_WHILE_LS2_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
35
while(valid) {
36

37
	int active;
38
	nb_pipe_status valid_active = PIPE_STATUS_FAILURE;
Leonardo Solis's avatar
Leonardo Solis committed
39
40

	float current_energy;
41
42
	nb_pipe_status valid_energy = PIPE_STATUS_FAILURE;

43
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
44
	LOOP_WHILE_LS2_ACTIVE:
45
	while( (valid_active != PIPE_STATUS_SUCCESS) && (valid_energy != PIPE_STATUS_SUCCESS)) {
lvs's avatar
lvs committed
46
47
		valid_active = read_pipe(chan_GA2LS_Off2_active, &active);
		valid_energy = read_pipe(chan_GA2LS_LS2_energy,  &current_energy);
Leonardo Solis's avatar
Leonardo Solis committed
48
	}
49

50
	// (active == 1) means stop LS
51
52
53
54
55
	/*valid = (active != 1) || (valid_energy == PIPE_STATUS_SUCCESS);*/

	// Received something on pipe: chan_GA2LS_Off_active?
	// If so, then turn this kernel off
	valid = (valid_active == PIPE_STATUS_SUCCESS)? 0x00:0x01;
Leonardo Solis's avatar
Leonardo Solis committed
56

57
	if (valid) {
Leonardo Solis's avatar
Leonardo Solis committed
58
		float   genotype [ACTUAL_GENOTYPE_LENGTH];
Leonardo Solis's avatar
Leonardo Solis committed
59

60
		__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
61
		LOOP_FOR_LS2_READ_INPUT_GENOTYPE:
62
		for (uchar i=0; i<DockConst_num_of_genes; i++) {
lvs's avatar
lvs committed
63
			read_pipe_block(chan_GA2LS_LS2_genotype, &genotype [i]);
64
		}
65
	
66
		#if defined (DEBUG_KRNL_LS2)
67
		printf("In of while iter LS2\n");
68
		#endif
69

70
71
72
73
74
75
		float rho = 1.0f;
		ushort iteration_cnt = 0;
		uchar  cons_succ     = 0;
		uchar  cons_fail     = 0;
		uint   LS_eval       = 0;
		bool   positive_direction = true;
Leonardo Solis's avatar
Leonardo Solis committed
76

77
		// performing local search
78
		__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
79
		LOOP_WHILE_LS2_ITERATION_RHO:
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		while ((iteration_cnt < DockConst_max_num_of_iters) && (rho > DockConst_rho_lower_bound)) {	
			// -----------------------------------------------
			// Exit condition is groups here. It allows pipelining
			if (positive_direction == true) { 
				if (cons_succ >= DockConst_cons_limit) {
					rho = LS_EXP_FACTOR*rho;
					cons_fail = 0;
					cons_succ = 0;
				}
				else if (cons_fail >= DockConst_cons_limit) {
					rho = LS_CONT_FACTOR*rho;
					cons_fail = 0;
					cons_succ = 0;
				}
				iteration_cnt++;
			}
Leonardo Solis's avatar
Leonardo Solis committed
96

97
98
99
100
			#if defined (DEBUG_KRNL_LS2)
			printf("LS2 positive?: %u, iteration_cnt: %u, rho: %f, limit rho: %f\n", positive_direction, iteration_cnt, rho, DockConst_rho_lower_bound);
			#endif
			// -----------------------------------------------
101

102
103
104
105
			float entity_possible_new_genotype   [ACTUAL_GENOTYPE_LENGTH];
			float genotype_bias                  [ACTUAL_GENOTYPE_LENGTH];
			float deviate_plus_bias              [ACTUAL_GENOTYPE_LENGTH];
			float deviate_minus_bias             [ACTUAL_GENOTYPE_LENGTH];
106

107
108
109
			// Tell Krnl_Conf_Arbiter, LS2 is done
			// Not completely strict as the (iteration_cnt < DockConst_max_num_of_iters) is ignored
			// In practice, rho condition dominates most of the cases
110
111
			int tmp_int = (rho < DockConst_rho_lower_bound)?0:1;
			write_pipe_block(chan_LS2Arbiter_LS2_end, &tmp_int);
lvs's avatar
lvs committed
112
/*
113
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
114
*/
115
116
117
		
			// new random deviate
			// rho is the deviation of the uniform distribution
118
			__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
119
			LOOP_FOR_LS2_WRITE_GENOTYPE:
120
			for (uchar i=0; i<DockConst_num_of_genes; i++) {
lvs's avatar
lvs committed
121
				float tmp_prng;
Leonardo Solis's avatar
Leonardo Solis committed
122
				read_pipe_block(chan_PRNG2LS2_float_prng, &tmp_prng);
lvs's avatar
lvs committed
123
/*
124
				mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
125
*/
126
127
				// tmp1 is genotype_deviate
				float tmp1 = rho * (2.0f*tmp_prng - 1.0f);
Leonardo Solis's avatar
Leonardo Solis committed
128

129
130
				if (i<3) { tmp1 = tmp1 * DockConst_base_dmov_mul_sqrt3; }
				else 	 { tmp1 = tmp1 * DockConst_base_dang_mul_sqrt3; }
Leonardo Solis's avatar
Leonardo Solis committed
131

132
				float deviate = 0.4f*tmp1;
Leonardo Solis's avatar
Leonardo Solis committed
133

134
135
136
				// tmp2 is the addition: genotype_deviate + genotype_bias
				// tmp3 is entity_possible_new_genotype
				float tmp_bias = (iteration_cnt == 1)? 0.0f:genotype_bias [i];
lvs's avatar
lvs committed
137
				float bias = 0.6f * tmp_bias;
Leonardo Solis's avatar
Leonardo Solis committed
138

139
140
				deviate_plus_bias  [i] = deviate + bias;
				deviate_minus_bias [i] = deviate - bias;
141

142
143
				float tmp2 = tmp1 + tmp_bias;
				float tmp3 = (positive_direction == true)? (genotype [i] + tmp2): (genotype [i] - tmp2);
144

145
				if (i>2) {if (i==4) { tmp3 = map_angle_180(tmp3);}
146
					  else      { tmp3 = map_angle_360(tmp3);}}
147

148
				entity_possible_new_genotype [i] = tmp3;
lvs's avatar
lvs committed
149
				write_pipe_block(chan_LS2Conf_LS2_genotype, &tmp3);
Leonardo Solis's avatar
Leonardo Solis committed
150

151
152
153
154
				#if defined (DEBUG_KRNL_LS2)
				printf("LS2_genotype sent: %u\n", i);
				#endif
			}
155

156
157
158
159
			//printf("Energy to calculate sent from LS2 ... ");

			float energyIA_LS_rx;
			float energyIE_LS_rx;
lvs's avatar
lvs committed
160

161
162
163
			nb_pipe_status intra_valid = PIPE_STATUS_FAILURE;
			nb_pipe_status inter_valid = PIPE_STATUS_FAILURE;

164
			__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
165
			LOOP_WHILE_LS2_READ_ENERGIES:
166
167
168
			while( (intra_valid != PIPE_STATUS_SUCCESS) || (inter_valid != PIPE_STATUS_SUCCESS)) {

				if (intra_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
169
					intra_valid = read_pipe(chan_Intrae2StoreLS_LS2_intrae, &energyIA_LS_rx);
170
				}
171
				else if (inter_valid != PIPE_STATUS_SUCCESS) {
lvs's avatar
lvs committed
172
					inter_valid = read_pipe(chan_Intere2StoreLS_LS2_intere, &energyIE_LS_rx);
173
174
				}
			}
175

176
			float candidate_energy = energyIA_LS_rx + energyIE_LS_rx;
177

178
179
			// update LS energy-evaluation count
			LS_eval++;
180

181
182
183
			#if defined (DEBUG_KRNL_LS2)
			printf("INTERE received in LS2: %u\n", LS_eval);
			#endif
Leonardo Solis's avatar
Leonardo Solis committed
184

185
186
187
			if (candidate_energy < current_energy) {
				// updating offspring_genotype
				// updating genotype_bias
188

189
				__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
190
				LOOP_FOR_LS2_FLOATPT_UPDATE_POS_GENOTYPE:
191
192
193
194
195
				for (uchar i=0; i<DockConst_num_of_genes; i++) {
					genotype_bias [i] = (positive_direction == true) ?  deviate_plus_bias  [i] : 
											    deviate_minus_bias [i] ;
					genotype [i] = entity_possible_new_genotype [i];
				}	
Leonardo Solis's avatar
Leonardo Solis committed
196

197
198
199
200
				current_energy = candidate_energy;
				cons_succ++;
				cons_fail = 0;
				positive_direction = true;				
Leonardo Solis's avatar
Leonardo Solis committed
201
			}
202
203
204
			else {
				// updating (halving) genotype_bias

205
				__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
206
				LOOP_FOR_LS2_FLOATPT_UPDATE_NEG_GENOTYPE:
207
208
209
210
211
212
213
214
215
				for (uchar i=0; i<DockConst_num_of_genes; i++) {
					genotype_bias [i] = (iteration_cnt == 1)? 0.0f: (0.5f*genotype_bias [i]);
				}

				if (positive_direction == false) {
					cons_fail++;
					cons_succ = 0;
				}
				positive_direction = !positive_direction;
216
			}
217
		} // end of while (iteration_cnt) && (rho)
218
	
219
220
221
		#if defined (DEBUG_KRNL_LS2)
		printf("Out of while iter LS2\n");
		#endif
222

223
		// write back data to GA
224
		__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
225
		LOOP_FOR_LS2_WRITEBACK2GA:
226
227
228
		for (uchar i=0; i<DockConst_num_of_genes; i++) {
			if (i == 0) {
				float2 evalenergy  = {*(float*)&LS_eval, current_energy};
lvs's avatar
lvs committed
229
				write_pipe_block(chan_LS2GA_LS2_evalenergy, &evalenergy);	
230
			}
lvs's avatar
lvs committed
231
/*
232
			mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
233
*/
234

lvs's avatar
lvs committed
235
			write_pipe_block(chan_LS2GA_LS2_genotype, &genotype [i]);
236
		}
Leonardo Solis's avatar
Leonardo Solis committed
237

238
	} // End of if (valid)
239
	
Leonardo Solis's avatar
Leonardo Solis committed
240
} // End of while (valid)		
241
242

#if defined (DEBUG_ACTIVE_KERNEL)
243
printf("	%-20s: %s\n", "Krnl_LS2", "disabled");		
244
245
246
247
248
249
#endif
	
}

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