Krnl_Conform.cl 10.3 KB
Newer Older
1
// --------------------------------------------------------------------------
2
3
// Conform changes the conformation of the ligand according to 
// the genotype fed by any producer logic/kernel (IC, GG, LSs).
4
5
// Originally from: processligand.c
// --------------------------------------------------------------------------
lvs's avatar
lvs committed
6
/*
7
__kernel __attribute__ ((max_global_work_dim(0)))
lvs's avatar
lvs committed
8
9
*/
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
10
void Krnl_Conform(
11
	     __global const int*  restrict KerConstStatic_rotlist_const,
12
13
	     __constant float3*   restrict KerConstStatic_ref_coords_const,
	     __constant float3*   restrict KerConstStatic_rotbonds_moving_vectors_const,
14
	     __constant float3*   restrict KerConstStatic_rotbonds_unit_vectors_const,  
15
16
17
			      unsigned int          DockConst_rotbondlist_length,
			      unsigned char         DockConst_num_of_atoms,
			      unsigned char         DockConst_num_of_genes,
18
	     __constant float4*   restrict KerConstStatic_ref_orientation_quats_const,
19
			      unsigned short        Host_RunId
20
)
21
{
Leonardo Solis's avatar
Leonardo Solis committed
22
23
24
25
26
27
28
	#if defined (DEBUG_KRNL_Conform) 
	printf("\n");
	printf("%-40s %u\n", "DockConst->rotbondlist_length: ",	DockConst_rotbondlist_length);
	printf("%-40s %u\n", "DockConst->num_of_atoms: ",  	DockConst_num_of_atoms);
	printf("%-40s %u\n", "DockConst_num_of_genes: ",        DockConst_num_of_genes);	
	#endif

29
	// Check best practices guide
Leonardo Solis's avatar
Leonardo Solis committed
30
	// Table 11. Effects of numbanks and bankwidth on the Bank Geometry ...
31
32
33
34
35
	// Only first three indexes of the lower array are used
	// however size of lower array was declared as 4, 
	// just to keep sizes equal to power of 2
	// __local float  __attribute__((numbanks(8), bankwidth(16))) loc_coords[MAX_NUM_OF_ATOMS][4];

Leonardo Solis's avatar
Leonardo Solis committed
36
	char active = 0x01;	
37

Leonardo Solis's avatar
Leonardo Solis committed
38
	__local int rotlist_localcache [MAX_NUM_OF_ROTATIONS];
Leonardo Solis's avatar
Leonardo Solis committed
39

40
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
41
	LOOP_FOR_CONFORM_ROTBONDLIST:
Leonardo Solis's avatar
Leonardo Solis committed
42
	for (ushort c = 0; c < DockConst_rotbondlist_length; c++) {
Leonardo Solis's avatar
Leonardo Solis committed
43
		rotlist_localcache [c] = KerConstStatic_rotlist_const [c];
Leonardo Solis's avatar
Leonardo Solis committed
44
45
	}

46
__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
47
LOOP_WHILE_CONFORM_MAIN:
48
while(active) {
Leonardo Solis's avatar
Leonardo Solis committed
49
50
	char mode;

51

Leonardo Solis's avatar
Leonardo Solis committed
52
53
54
55
	float  phi;
	float  theta;
	float  genrotangle;
	float3 genotype_xyz;
lvs's avatar
lvs committed
56
/*
Leonardo Solis's avatar
Leonardo Solis committed
57
58
59
60
61
62
63
	float3 __attribute__ ((
			      memory,
			      numbanks(1),
			      bankwidth(16),
			      singlepump,
			      numreadports(3),
			      numwriteports(1)
64
			    )) loc_coords [MAX_NUM_OF_ATOMS];
lvs's avatar
lvs committed
65
66
*/
	float3 loc_coords [MAX_NUM_OF_ATOMS];
Leonardo Solis's avatar
Leonardo Solis committed
67

68
69
	char actmode;
	read_pipe_block(chan_IGL2Conform_actmode, &actmode);
lvs's avatar
lvs committed
70
/*
Leonardo Solis's avatar
Leonardo Solis committed
71
	mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
72
*/
73
74
75
	active = actmode;
	mode   = actmode;

76
77
//printf("Conform: %u\n", mode);

78
	float   genotype [ACTUAL_GENOTYPE_LENGTH];
Leonardo Solis's avatar
Leonardo Solis committed
79

80
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
81
	LOOP_FOR_CONFORM_READ_GENOTYPE:
82
	for (uchar i=0; i<DockConst_num_of_genes; i++) {
83
84
		float fl_tmp;
		switch (mode) {
Leonardo Solis's avatar
Leonardo Solis committed
85
86
			case 'I':  read_pipe_block(chan_IC2Conf_genotype,     &fl_tmp); break;
			case 'G':  read_pipe_block(chan_GG2Conf_genotype,     &fl_tmp); break;
lvs's avatar
lvs committed
87
88
89
90
91
92
93
94
95
			case 0x01: read_pipe_block(chan_LS2Conf_LS1_genotype, &fl_tmp); break;
			case 0x02: read_pipe_block(chan_LS2Conf_LS2_genotype, &fl_tmp); break;
			case 0x03: read_pipe_block(chan_LS2Conf_LS3_genotype, &fl_tmp); break;
			case 0x04: read_pipe_block(chan_LS2Conf_LS4_genotype, &fl_tmp); break;
			case 0x05: read_pipe_block(chan_LS2Conf_LS5_genotype, &fl_tmp); break;
			case 0x06: read_pipe_block(chan_LS2Conf_LS6_genotype, &fl_tmp); break;
			case 0x07: read_pipe_block(chan_LS2Conf_LS7_genotype, &fl_tmp); break;
			case 0x08: read_pipe_block(chan_LS2Conf_LS8_genotype, &fl_tmp); break;
			case 0x09: read_pipe_block(chan_LS2Conf_LS9_genotype, &fl_tmp); break;
96
97
98
99
100
101
102
103
		}
		
		if (i > 2) {
			fl_tmp = fl_tmp * DEG_TO_RAD;
		}

//printf("Conform: %u %u\n", mode, i);

Leonardo Solis's avatar
Leonardo Solis committed
104
105
106
107
108
109
110
111
112
		switch (i) {
			case 0: genotype_xyz.x = fl_tmp; break;
			case 1: genotype_xyz.y = fl_tmp; break;
			case 2: genotype_xyz.z = fl_tmp; break;
			case 3: phi            = fl_tmp; break;
			case 4: theta          = fl_tmp; break;
			case 5: genrotangle    = fl_tmp; break;
		}
		genotype [i] = fl_tmp;
Leonardo Solis's avatar
Leonardo Solis committed
113
114
	}

115
	#if defined (DEBUG_ACTIVE_KERNEL)
Leonardo Solis's avatar
Leonardo Solis committed
116
	if (active == 0x00) {printf("	%-20s: %s\n", "Krnl_Conform", "must be disabled");}
117
	#endif
118

119
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
120
	LOOP_FOR_CONFORM_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
121
	for (ushort rotation_counter = 0; rotation_counter < DockConst_rotbondlist_length; rotation_counter++)
122
	{
Leonardo Solis's avatar
Leonardo Solis committed
123
		int rotation_list_element = rotlist_localcache [rotation_counter];
124

125
		if ((rotation_list_element & RLIST_DUMMY_MASK) == 0)	// If not dummy rotation
126
		{
Leonardo Solis's avatar
Leonardo Solis committed
127
			uint atom_id = rotation_list_element & RLIST_ATOMID_MASK;
128

129
			// Capturing atom coordinates
Leonardo Solis's avatar
Leonardo Solis committed
130
			float3 atom_to_rotate;
Leonardo Solis's avatar
Leonardo Solis committed
131

132
			if ((rotation_list_element & RLIST_FIRSTROT_MASK) != 0)	// If first rotation of this atom
133
			{	
134
				atom_to_rotate = KerConstStatic_ref_coords_const [atom_id];
135
136
			}
			else
Leonardo Solis's avatar
Leonardo Solis committed
137
			{	
Leonardo Solis's avatar
Leonardo Solis committed
138
				atom_to_rotate = loc_coords[atom_id];
139
140
			}

141
			// Capturing rotation vectors and angle
142
143
144
			float3   rotation_unitvec;
			float3   rotation_movingvec;
			float    rotation_angle;
Leonardo Solis's avatar
Leonardo Solis committed
145

146
			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// If general rotation
147
			{
148
149
150
151
152
153
154
155
				float  sin_theta, cos_theta;
				float3 genrot_unitvec;
				sin_theta = native_sin(theta);
				cos_theta = native_cos(theta);
				genrot_unitvec.x = sin_theta*native_cos(phi);
				genrot_unitvec.y = sin_theta*native_sin(phi);
				genrot_unitvec.z = cos_theta;

Leonardo Solis's avatar
Leonardo Solis committed
156
				rotation_unitvec = genrot_unitvec;
157
				rotation_angle = genrotangle;
Leonardo Solis's avatar
Leonardo Solis committed
158
				rotation_movingvec = genotype_xyz;
159
			}
160
			else	// If rotating around rotatable bond
161
			{
Leonardo Solis's avatar
Leonardo Solis committed
162
				uint rotbond_id = (rotation_list_element & RLIST_RBONDID_MASK) >> RLIST_RBONDID_SHIFT;
163

164
				rotation_unitvec = KerConstStatic_rotbonds_unit_vectors_const [rotbond_id];
Leonardo Solis's avatar
Leonardo Solis committed
165
				
Leonardo Solis's avatar
Leonardo Solis committed
166
				rotation_angle = genotype [6+rotbond_id];
Leonardo Solis's avatar
Leonardo Solis committed
167

168
				rotation_movingvec = KerConstStatic_rotbonds_moving_vectors_const [rotbond_id];
169

170
171
				// In addition performing the first movement 
				// which is needed only if rotating around rotatable bond
Leonardo Solis's avatar
Leonardo Solis committed
172
				atom_to_rotate -= rotation_movingvec;
173
174
			}

175
			// Performing rotation
176
177
			float4 quatrot_left;
			float4 quatrot_temp;
Leonardo Solis's avatar
Leonardo Solis committed
178

179
			rotation_angle = rotation_angle*0.5f;
180

Leonardo Solis's avatar
Leonardo Solis committed
181
			float sin_angle, cos_angle;
182
183
			sin_angle      = native_sin(rotation_angle);
			cos_angle      = native_cos(rotation_angle);
184
185
186
187
			quatrot_left.x = sin_angle*rotation_unitvec.x;
			quatrot_left.y = sin_angle*rotation_unitvec.y;
			quatrot_left.z = sin_angle*rotation_unitvec.z;
			quatrot_left.w = cos_angle;
188

189
190
191
			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// If general rotation, 
										// two rotations should be performed 
										// (multiplying the quaternions)
192
			{
193
				const float4 ref_orientation_quats_const = KerConstStatic_ref_orientation_quats_const[Host_RunId];
194
195
196
197
				const float  ref_orientation_quats_const_0 = ref_orientation_quats_const.x;
				const float  ref_orientation_quats_const_1 = ref_orientation_quats_const.y;
				const float  ref_orientation_quats_const_2 = ref_orientation_quats_const.z;
				const float  ref_orientation_quats_const_3 = ref_orientation_quats_const.w;
198

199
200
				// Calculating quatrot_left*ref_orientation_quats_const, 
				// which means that reference orientation rotation is the first
201
				quatrot_temp = quatrot_left;
202

203
				// Taking the first element of ref_orientation_quats_const member
204
205
206
207
208
209
210
211
212
				float4 ref4x = {   ref_orientation_quats_const_0,   ref_orientation_quats_const_3, - ref_orientation_quats_const_2, ref_orientation_quats_const_1};
				float4 ref4y = { - ref_orientation_quats_const_3,   ref_orientation_quats_const_0,   ref_orientation_quats_const_1, ref_orientation_quats_const_2};
				float4 ref4z = {   ref_orientation_quats_const_2, - ref_orientation_quats_const_1,   ref_orientation_quats_const_0, ref_orientation_quats_const_3};
				float4 ref4w = { - ref_orientation_quats_const_1, - ref_orientation_quats_const_2, - ref_orientation_quats_const_3, ref_orientation_quats_const_0};

				quatrot_left.x = dot(quatrot_temp, ref4x);
				quatrot_left.y = dot(quatrot_temp, ref4y);
				quatrot_left.z = dot(quatrot_temp, ref4z);
				quatrot_left.w = dot(quatrot_temp, ref4w);
213
			}
Leonardo Solis's avatar
Leonardo Solis committed
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
			float3 left3x = {  quatrot_left.w, - quatrot_left.z,   quatrot_left.y};
			float3 left3y = {  quatrot_left.z,   quatrot_left.w, - quatrot_left.x};
			float3 left3z = {- quatrot_left.y,   quatrot_left.x,   quatrot_left.w};
			float3 left3w = {- quatrot_left.x, - quatrot_left.y, - quatrot_left.z};

			quatrot_temp.x = dot(left3x, atom_to_rotate);
			quatrot_temp.y = dot(left3y, atom_to_rotate);
			quatrot_temp.z = dot(left3z, atom_to_rotate);
			quatrot_temp.w = dot(left3w, atom_to_rotate);

			float4 left4x = {  quatrot_left.w, - quatrot_left.z,   quatrot_left.y, - quatrot_left.x};
			float4 left4y = {  quatrot_left.z,   quatrot_left.w, - quatrot_left.x, - quatrot_left.y};
			float4 left4z = {- quatrot_left.y,   quatrot_left.x,   quatrot_left.w, - quatrot_left.z};

			atom_to_rotate.x = dot(quatrot_temp, left4x);
			atom_to_rotate.y = dot(quatrot_temp, left4y);
			atom_to_rotate.z = dot(quatrot_temp, left4z);
232

233
			// Performing final movement and storing values
Leonardo Solis's avatar
Leonardo Solis committed
234
			loc_coords[atom_id] = atom_to_rotate + rotation_movingvec;
Leonardo Solis's avatar
Leonardo Solis committed
235

236
		} // End if-statement not dummy rotation
Leonardo Solis's avatar
Leonardo Solis committed
237
238
239

		mem_fence(CLK_LOCAL_MEM_FENCE);

240
241
	} // End rotation_counter for-loop

242
	#if defined (DEBUG_KRNL_CONFORM)
243
	printf("BEFORE Out CONFORM CHANNEL\n");
244
	#endif
245

246
	// --------------------------------------------------------------
247
	// Send ligand atomic coordinates to channel 
248
	// --------------------------------------------------------------
249
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
250
	LOOP_FOR_CONFORM_WRITE_XYZ:
Leonardo Solis's avatar
Leonardo Solis committed
251
252
	for (uchar pipe_cnt=0; pipe_cnt<DockConst_num_of_atoms; pipe_cnt+=2) {
		if (pipe_cnt == 0) {
253
254
			write_pipe_block(chan_Conf2Intere_actmode, &mode);
			write_pipe_block(chan_Conf2Intrae_actmode, &mode);
Leonardo Solis's avatar
Leonardo Solis committed
255
		}
lvs's avatar
lvs committed
256
/*
Leonardo Solis's avatar
Leonardo Solis committed
257
		mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
258
*/
Leonardo Solis's avatar
Leonardo Solis committed
259
260
261

		float3 tmp_coords[2];

Leonardo Solis's avatar
Leonardo Solis committed
262
263
		__attribute__((opencl_unroll_hint))
		LOOP_CONFORM_OUT:
Leonardo Solis's avatar
Leonardo Solis committed
264
265
266
267
268
269
270
271
		for (uchar i=0; i<2; i++) {
			tmp_coords[i] = loc_coords[pipe_cnt+i];
		}

		float8 tmp;

		tmp.s0 = tmp_coords[0].x; tmp.s1 = tmp_coords[0].y; tmp.s2 = tmp_coords[0].z; //tmp.s3
		tmp.s4 = tmp_coords[1].x; tmp.s5 = tmp_coords[1].y; tmp.s6 = tmp_coords[1].z; //tmp.s7
Leonardo Solis's avatar
Leonardo Solis committed
272

lvs's avatar
lvs committed
273
274
		write_pipe_block(chan_Conf2Intere_xyz, &tmp);
		write_pipe_block(chan_Conf2Intrae_xyz, &tmp);
Leonardo Solis's avatar
Leonardo Solis committed
275
	}
276

277
	// --------------------------------------------------------------
278
	#if defined (DEBUG_KRNL_CONFORM)
279
	printf("AFTER Out CONFORM CHANNEL\n");
280
	#endif
281

282
} // End of while(active)
283

284
#if defined (DEBUG_ACTIVE_KERNEL)
285
printf("	%-20s: %s\n", "Krnl_Conform", "disabled");
286
#endif
287

288
289
290
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------