Krnl_IntraE.cl 12.4 KB
Newer Older
1
2
3
4
5
6
// sqrt7 ////https://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi
float sqrt_custom(const float x) 
{ 	//uint i = as_uint(x);	
	uint i = *(uint*) &x;    	
	i  += 127 << 23;	// adjust bias   	
	i >>= 1; 		// approximation of square root 	
7
	return as_float(i);	// return *(float*) &i; 
8
9
}  

10
// --------------------------------------------------------------------------
11
12
// IntraE calculates the intramolecular energy of a set of atomic ligand 
// contributor-pairs.
13
14
// Originally from: processligand.c
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
15
/*
16
__kernel __attribute__ ((max_global_work_dim(0)))
lvs's avatar
lvs committed
17
18
*/
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
19
void Krnl_IntraE(
20
21
 	     __constant     float* restrict KerConstStatic_atom_charges_const,
 	     __constant     char*  restrict KerConstStatic_atom_types_const,
22

23
	     __global const char3* restrict KerConstStatic_intraE_contributors_const,
Leonardo Solis's avatar
Leonardo Solis committed
24
25
26
27
28
29
30

			float 				 DockConst_smooth,
	     __constant     float* restrict KerConstStatic_reqm,
	     __constant     float* restrict KerConstStatic_reqm_hbond,    
	     __constant     uint*  restrict KerConstStatic_atom1_types_reqm,
	     __constant     uint*  restrict KerConstStatic_atom2_types_reqm,  

31
32
33
34
	     __constant     float* restrict KerConstStatic_VWpars_AC_const,
	     __constant     float* restrict KerConstStatic_VWpars_BD_const,
	     __constant     float* restrict KerConstStatic_dspars_S_const,
 	     __constant     float* restrict KerConstStatic_dspars_V_const,
Leonardo Solis's avatar
Leonardo Solis committed
35

36
37
38
39
40
41
			unsigned char                    DockConst_num_of_atoms,
		   	unsigned int                     DockConst_num_of_intraE_contributors,
		  	float                            DockConst_grid_spacing,
			unsigned char                    DockConst_num_of_atypes,
			float                            DockConst_coeff_elec,
			float                            DockConst_qasp,
42
			float                            DockConst_coeff_desolv
43
)
44
{
Leonardo Solis's avatar
Leonardo Solis committed
45
	char active = 0x01;
Leonardo Solis's avatar
Leonardo Solis committed
46

47
	__local char3  intraE_contributors_localcache   [MAX_INTRAE_CONTRIBUTORS];
Leonardo Solis's avatar
Leonardo Solis committed
48
	LOOP_FOR_INTRAE_CONTRIBUTORS:
49
50
51
52
	for (ushort i=0; i<MAX_INTRAE_CONTRIBUTORS; i++) {
		intraE_contributors_localcache [i] = KerConstStatic_intraE_contributors_const [i];	
	}

53
#pragma max_concurrency 32
Leonardo Solis's avatar
Leonardo Solis committed
54
LOOP_WHILE_INTRAE_MAIN:
55
while(active) {
Leonardo Solis's avatar
Leonardo Solis committed
56
57
	char mode;

lvs's avatar
lvs committed
58
/*
59
60
61
62
63
64
65
66
	float3 __attribute__ ((
			      memory,
			      numbanks(2),
			      bankwidth(16),
			      singlepump,
			      numreadports(2),
			      numwriteports(1)
			    )) loc_coords[MAX_NUM_OF_ATOMS];
lvs's avatar
lvs committed
67
68
*/
	float3 loc_coords[MAX_NUM_OF_ATOMS];
69

70
71
72
73
	//printf("BEFORE In INTRA CHANNEL\n");
	// --------------------------------------------------------------
	// Wait for ligand atomic coordinates in channel
	// --------------------------------------------------------------
lvs's avatar
lvs committed
74
/*
Leonardo Solis's avatar
Leonardo Solis committed
75
	char2 actmode = read_channel_altera(chan_Conf2Intrae_actmode);
lvs's avatar
lvs committed
76
*/
77
#if 0
lvs's avatar
lvs committed
78
	char2 actmode;
79
80
#endif
	char actmode;
lvs's avatar
lvs committed
81
82
	read_pipe_block(chan_Conf2Intrae_actmode, &actmode);
/*
83
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
84
*/
85
#if 0
Leonardo Solis's avatar
Leonardo Solis committed
86
87
	active = actmode.x;
	mode   = actmode.y;
88
89
90
#endif
	active = actmode;
	mode   = actmode;
91

Leonardo Solis's avatar
Leonardo Solis committed
92
	LOOP_FOR_INTRAE_READ_XYZ:
Leonardo Solis's avatar
Leonardo Solis committed
93
	for (uchar pipe_cnt=0; pipe_cnt<DockConst_num_of_atoms; pipe_cnt+=2) {
lvs's avatar
lvs committed
94
/*
Leonardo Solis's avatar
Leonardo Solis committed
95
		float8 tmp = read_channel_altera(chan_Conf2Intrae_xyz);
lvs's avatar
lvs committed
96
97
98
99
*/
		float8 tmp;
		read_pipe_block(chan_Conf2Intrae_xyz, &tmp);

Leonardo Solis's avatar
Leonardo Solis committed
100
101
102
103
104
105
		float3 tmp1 = {tmp.s0, tmp.s1, tmp.s2};
		float3 tmp2 = {tmp.s4, tmp.s5, tmp.s6};
		loc_coords[pipe_cnt] = tmp1;
		loc_coords[pipe_cnt+1] = tmp2;
	}

106

107
108
	// --------------------------------------------------------------
	//printf("AFTER In INTRA CHANNEL\n");
109

110
111
112
	#if defined (DEBUG_ACTIVE_KERNEL)
	if (active == 0) {printf("	%-20s: %s\n", "Krnl_IntraE", "must be disabled");}
	#endif
Leonardo Solis's avatar
Leonardo Solis committed
113

Leonardo Solis's avatar
Leonardo Solis committed
114
	float intraE = 0.0f;
115

116
117
118
	#if defined (FIXED_POINT_INTRAE)
	// Create shift register to reduce II (initially II=32, unroll-factor=8) 
	// Use fixedpt64 to reduce II=4 (after shift-register) downto II=1
119
120
121
	//float shift_intraE[33];
	fixedpt64 shift_intraE[33];

Leonardo Solis's avatar
Leonardo Solis committed
122
/*
123
	#pragma unroll
Leonardo Solis's avatar
Leonardo Solis committed
124
125
126
*/
	__attribute__((opencl_unroll_hint))
	LOOP_INTRAE_SHIFT_INIT:
127
128
129
130
131
	for (uchar i=0; i<33; i++) {
		//shift_intraE[i] = 0.0f;
		shift_intraE[i] = 0;
	}

132
	#endif
133

134
	// For each intramolecular atom contributor pair
135

136
	//#pragma unroll 10
Leonardo Solis's avatar
Leonardo Solis committed
137
	LOOP_FOR_INTRAE_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
138
	for (ushort contributor_counter=0; contributor_counter<DockConst_num_of_intraE_contributors; contributor_counter++) {
Leonardo Solis's avatar
Leonardo Solis committed
139

140
141
		char3 ref_intraE_contributors_const;
		ref_intraE_contributors_const = intraE_contributors_localcache[contributor_counter];
Leonardo Solis's avatar
Leonardo Solis committed
142

143
144
		char atom1_id = ref_intraE_contributors_const.x;
		char atom2_id = ref_intraE_contributors_const.y;
Leonardo Solis's avatar
Leonardo Solis committed
145

Leonardo Solis's avatar
Leonardo Solis committed
146
147
148
149
150
		float3 loc_coords_atid1 = loc_coords[atom1_id];
		float3 loc_coords_atid2 = loc_coords[atom2_id];
		float subx = loc_coords_atid1.x - loc_coords_atid2.x;
		float suby = loc_coords_atid1.y - loc_coords_atid2.y;
		float subz = loc_coords_atid1.z - loc_coords_atid2.z;
151

Leonardo Solis's avatar
Leonardo Solis committed
152
153
		//atomic_distance = sqrt(subx*subx + suby*suby + subz*subz)*DockConst_grid_spacing;
		float atomic_distance = sqrt_custom(subx*subx + suby*suby + subz*subz)*DockConst_grid_spacing;
154

Leonardo Solis's avatar
Leonardo Solis committed
155
156
/*
		if (atomic_distance < 1.0f) {
157
			#if defined (DEBUG_KRNL_INTRAE)
Leonardo Solis's avatar
Leonardo Solis committed
158
			printf("\n\nToo low distance (%f) between atoms %u and %u\n", atomic_distance, atom1_id, atom2_id);
159
			#endif
160
			//return HIGHEST_ENERGY;	// Returning maximal value
Leonardo Solis's avatar
Leonardo Solis committed
161
			atomic_distance = 1.0f;
162
		}
Leonardo Solis's avatar
Leonardo Solis committed
163
*/
164

165
		#if defined (DEBUG_KRNL_INTRAE)
166
		printf("\n\nCalculating energy contribution of atoms %u and %u\n", atom1_id+1, atom2_id+1);
Leonardo Solis's avatar
Leonardo Solis committed
167
		printf("Distance: %f\n", atomic_distance);
168
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
169

Leonardo Solis's avatar
Leonardo Solis committed
170
/*
Leonardo Solis's avatar
Leonardo Solis committed
171
172
173
174
		float partialE1;
		float partialE2;
		float partialE3;
		float partialE4;
Leonardo Solis's avatar
Leonardo Solis committed
175
*/
176
		// But only if the distance is less than distance cutoff value and 20.48A (because of the tables)
177
		//if ((distance_leo < 8.0f) && (distance_leo < 20.48f))
Leonardo Solis's avatar
Leonardo Solis committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		float partialE1 = 0.0f;
		float partialE2 = 0.0f;
		float partialE3 = 0.0f;
		float partialE4 = 0.0f;

 		// Getting types ids
		char atom1_typeid = KerConstStatic_atom_types_const [atom1_id];
		char atom2_typeid = KerConstStatic_atom_types_const [atom2_id];

		// Getting optimum pair distance (opt_distance) from reqm and reqm_hbond
		// reqm: equilibrium internuclear separation 
		//       (sum of the vdW radii of two like atoms (A)) in the case of vdW
		// reqm_hbond: equilibrium internuclear separation
		// 	 (sum of the vdW radii of two like atoms (A)) in the case of hbond 
		float opt_distance;

		uint atom1_type_vdw_hb = KerConstStatic_atom1_types_reqm [atom1_typeid];
     	        uint atom2_type_vdw_hb = KerConstStatic_atom2_types_reqm [atom2_typeid];

		if (ref_intraE_contributors_const.z == 1)	// H-bond
198
		{
Leonardo Solis's avatar
Leonardo Solis committed
199
200
201
202
203
204
			opt_distance = KerConstStatic_reqm_hbond [atom1_type_vdw_hb] + KerConstStatic_reqm_hbond [atom2_type_vdw_hb];
		}
		else	// Van der Waals
		{
			opt_distance = 0.5f*(KerConstStatic_reqm [atom1_type_vdw_hb] + KerConstStatic_reqm [atom2_type_vdw_hb]);
		}
205

Leonardo Solis's avatar
Leonardo Solis committed
206
207
208
209
		// Getting smoothed distance
		// smoothed_distance = function(atomic_distance, opt_distance)
		float smoothed_distance;
		float delta_distance = 0.5f*DockConst_smooth;
210

Leonardo Solis's avatar
Leonardo Solis committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
		/*printf("delta_distance: %f\n", delta_distance);*/

		if (atomic_distance <= (opt_distance - delta_distance)) {
			smoothed_distance = atomic_distance + delta_distance;
		}
		else if (atomic_distance < (opt_distance + delta_distance)) {
			smoothed_distance = opt_distance;
		}
		else { // else if (atomic_distance >= (opt_distance + delta_distance))
			smoothed_distance = atomic_distance - delta_distance;
		}

		float distance_pow_2  = atomic_distance*atomic_distance; 

		float smoothed_distance_pow_2 = smoothed_distance*smoothed_distance; 
		float inverse_smoothed_distance_pow_2  = native_divide(1.0f, smoothed_distance_pow_2);
		float inverse_smoothed_distance_pow_4  = inverse_smoothed_distance_pow_2 * inverse_smoothed_distance_pow_2;
		float inverse_smoothed_distance_pow_6  = inverse_smoothed_distance_pow_4 * inverse_smoothed_distance_pow_2;
		float inverse_smoothed_distance_pow_10 = inverse_smoothed_distance_pow_6 * inverse_smoothed_distance_pow_4;
		float inverse_smoothed_distance_pow_12 = inverse_smoothed_distance_pow_6 * inverse_smoothed_distance_pow_6;

		if (atomic_distance < 8.0f) 
		{
234
			// Calculating van der Waals / hydrogen bond term
Leonardo Solis's avatar
Leonardo Solis committed
235
			partialE1 = KerConstStatic_VWpars_AC_const [atom1_typeid*DockConst_num_of_atypes+atom2_typeid]*inverse_smoothed_distance_pow_12;
236

237
238
			float tmp_pE2 = KerConstStatic_VWpars_BD_const [atom1_typeid*DockConst_num_of_atypes+atom2_typeid];

239
			if (ref_intraE_contributors_const.z == 1)	// H-bond
Leonardo Solis's avatar
Leonardo Solis committed
240
				partialE2 = tmp_pE2 * inverse_smoothed_distance_pow_10;
241
			else	// Van der Waals
Leonardo Solis's avatar
Leonardo Solis committed
242
243
				partialE2 = tmp_pE2 * inverse_smoothed_distance_pow_6;
		} // End of if: if (dist < dcutoff)	
244

Leonardo Solis's avatar
Leonardo Solis committed
245
246
		// Calculating electrostatic term
		partialE3 = native_divide(  (DockConst_coeff_elec*KerConstStatic_atom_charges_const[atom1_id]*KerConstStatic_atom_charges_const[atom2_id]) , (atomic_distance*(-8.5525f + native_divide(86.9525f, (1.0f + 7.7839f*native_exp(-0.3154f*atomic_distance)))))       );
247

Leonardo Solis's avatar
Leonardo Solis committed
248
249
250
251
252
		// Calculating desolvation term
		partialE4 = (
			  (KerConstStatic_dspars_S_const [atom1_typeid] + DockConst_qasp*fabs(KerConstStatic_atom_charges_const[atom1_id])) * KerConstStatic_dspars_V_const [atom2_typeid] + 
			  (KerConstStatic_dspars_S_const [atom2_typeid] + DockConst_qasp*fabs(KerConstStatic_atom_charges_const[atom2_id])) * KerConstStatic_dspars_V_const [atom1_typeid]) * 
			 DockConst_coeff_desolv*native_exp(-0.0386f*distance_pow_2);
Leonardo Solis's avatar
Leonardo Solis committed
253
	
254
		#if defined (FIXED_POINT_INTRAE)
255
256
257
258
259
260
		//shift_intraE[32] = shift_intraE[0] + partialE1 + partialE2 + partialE3 + partialE4;
		shift_intraE[32] = shift_intraE[0] + fixedpt64_fromfloat(partialE1) + 
						     fixedpt64_fromfloat(partialE2) + 
						     fixedpt64_fromfloat(partialE3) + 
						     fixedpt64_fromfloat(partialE4);

Leonardo Solis's avatar
Leonardo Solis committed
261
/*
262
		#pragma unroll
Leonardo Solis's avatar
Leonardo Solis committed
263
264
265
*/
		__attribute__((opencl_unroll_hint))
		LOOP_INTRAE_SHIFT:
266
267
268
		for (uchar j=0; j<32; j++) {
			shift_intraE[j] = shift_intraE[j+1];
		}
269
		#else
Leonardo Solis's avatar
Leonardo Solis committed
270
		intraE += partialE1 + partialE2 + partialE3 + partialE4;
271
		#endif
Leonardo Solis's avatar
Leonardo Solis committed
272
	
Leonardo Solis's avatar
Leonardo Solis committed
273
	} // End of contributor_counter for-loop
274

275
	#if defined (FIXED_POINT_INTRAE)
276
277
	fixedpt64 fixpt_intraE = 0;

Leonardo Solis's avatar
Leonardo Solis committed
278
/*
279
	#pragma unroll
Leonardo Solis's avatar
Leonardo Solis committed
280
281
282
*/
	__attribute__((opencl_unroll_hint))
	LOOP_INTRAE_SHIFT_RED:
283
284
285
286
287
	for (uchar j=0; j<32; j++) {
		//intraE += shift_intraE[j];
		fixpt_intraE += shift_intraE[j];
	}
	intraE = fixedpt64_tofloat(fixpt_intraE);
288
	#endif
289

290
291
292
	// --------------------------------------------------------------
	// Send intramolecular energy to channel
	// --------------------------------------------------------------
293
	switch (mode) {
lvs's avatar
lvs committed
294
/*
295
296
		case 'I':  write_channel_altera(chan_Intrae2StoreIC_intrae, intraE);     break;
		case 'G':  write_channel_altera(chan_Intrae2StoreGG_intrae, intraE);     break;
297
298
299
		case 0x01: write_channel_altera(chan_Intrae2StoreLS_LS1_intrae, intraE); break;
		case 0x02: write_channel_altera(chan_Intrae2StoreLS_LS2_intrae, intraE); break;
		case 0x03: write_channel_altera(chan_Intrae2StoreLS_LS3_intrae, intraE); break;
Leonardo Solis's avatar
Leonardo Solis committed
300
301
		case 0x04: write_channel_altera(chan_Intrae2StoreLS_LS4_intrae, intraE); break;
		case 0x05: write_channel_altera(chan_Intrae2StoreLS_LS5_intrae, intraE); break;
302
303
304
305
		case 0x06: write_channel_altera(chan_Intrae2StoreLS_LS6_intrae, intraE); break;
		case 0x07: write_channel_altera(chan_Intrae2StoreLS_LS7_intrae, intraE); break;
		case 0x08: write_channel_altera(chan_Intrae2StoreLS_LS8_intrae, intraE); break;
		case 0x09: write_channel_altera(chan_Intrae2StoreLS_LS9_intrae, intraE); break;
lvs's avatar
lvs committed
306
307
308
309
310
311
312
313
314
315
316
317
*/
		case 'I':  write_pipe_block(chan_Intrae2StoreIC_intrae, &intraE);     break;
		case 'G':  write_pipe_block(chan_Intrae2StoreGG_intrae, &intraE);     break;
		case 0x01: write_pipe_block(chan_Intrae2StoreLS_LS1_intrae, &intraE); break;
		case 0x02: write_pipe_block(chan_Intrae2StoreLS_LS2_intrae, &intraE); break;
		case 0x03: write_pipe_block(chan_Intrae2StoreLS_LS3_intrae, &intraE); break;
		case 0x04: write_pipe_block(chan_Intrae2StoreLS_LS4_intrae, &intraE); break;
		case 0x05: write_pipe_block(chan_Intrae2StoreLS_LS5_intrae, &intraE); break;
		case 0x06: write_pipe_block(chan_Intrae2StoreLS_LS6_intrae, &intraE); break;
		case 0x07: write_pipe_block(chan_Intrae2StoreLS_LS7_intrae, &intraE); break;
		case 0x08: write_pipe_block(chan_Intrae2StoreLS_LS8_intrae, &intraE); break;
		case 0x09: write_pipe_block(chan_Intrae2StoreLS_LS9_intrae, &intraE); break;
318
	}
319
	// --------------------------------------------------------------
320

Leonardo Solis's avatar
Leonardo Solis committed
321
322
323
324
	#if defined (DEBUG_KRNL_INTRAE)
	printf("AFTER Out INTRAE CHANNEL\n");
	#endif

325
} // End of while(active)
326
327
328
329

	#if defined (DEBUG_ACTIVE_KERNEL)
	printf("	%-20s: %s\n", "Krnl_IntraE", "disabled");
	#endif
330
331
332
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------