Krnl_Conform.cl 9.64 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
__kernel __attribute__ ((reqd_work_group_size(1,1,1)))
7
void Krnl_Conform(
8
	     __global const int*  restrict KerConstStatic_rotlist_const,
9
10
	     __constant float3*   restrict KerConstStatic_ref_coords_const,
	     __constant float3*   restrict KerConstStatic_rotbonds_moving_vectors_const,
11
	     __constant float3*   restrict KerConstStatic_rotbonds_unit_vectors_const,  
12
13
14
			      unsigned int          DockConst_rotbondlist_length,
			      unsigned char         DockConst_num_of_atoms,
			      unsigned char         DockConst_num_of_genes,
15
	     __constant float4*   restrict KerConstStatic_ref_orientation_quats_const,
16
			      unsigned short        Host_RunId
17
)
18
{
Leonardo Solis's avatar
Leonardo Solis committed
19
20
21
22
23
24
25
	#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

26
	// Check best practices guide
Leonardo Solis's avatar
Leonardo Solis committed
27
	// Table 11. Effects of numbanks and bankwidth on the Bank Geometry ...
28
29
30
31
32
	// 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
33
	char active = 0x01;	
34

Leonardo Solis's avatar
Leonardo Solis committed
35
	__local int rotlist_localcache [MAX_NUM_OF_ROTATIONS];
Leonardo Solis's avatar
Leonardo Solis committed
36

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

43
__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
44
LOOP_WHILE_CONFORM_MAIN:
45
while(active) {
Leonardo Solis's avatar
Leonardo Solis committed
46
47
	char mode;

48

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

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

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

75
	float   genotype [ACTUAL_GENOTYPE_LENGTH];
Leonardo Solis's avatar
Leonardo Solis committed
76

77
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
78
	LOOP_FOR_CONFORM_READ_GENOTYPE:
79
	for (uchar i=0; i<DockConst_num_of_genes; i++) {
80
81
		float fl_tmp;
		switch (mode) {
Leonardo Solis's avatar
Leonardo Solis committed
82
83
			case 'I':  read_pipe_block(chan_IC2Conf_genotype,     &fl_tmp); break;
			case 'G':  read_pipe_block(chan_GG2Conf_genotype,     &fl_tmp); break;
84
85
86
87
88
89
90
91
		}
		
		if (i > 2) {
			fl_tmp = fl_tmp * DEG_TO_RAD;
		}

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

Leonardo Solis's avatar
Leonardo Solis committed
92
93
94
95
96
97
98
99
100
		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
101
102
	}

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

107
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
108
	LOOP_FOR_CONFORM_MAIN:
Leonardo Solis's avatar
Leonardo Solis committed
109
	for (ushort rotation_counter = 0; rotation_counter < DockConst_rotbondlist_length; rotation_counter++)
110
	{
Leonardo Solis's avatar
Leonardo Solis committed
111
		int rotation_list_element = rotlist_localcache [rotation_counter];
112

113
		if ((rotation_list_element & RLIST_DUMMY_MASK) == 0)	// If not dummy rotation
114
		{
Leonardo Solis's avatar
Leonardo Solis committed
115
			uint atom_id = rotation_list_element & RLIST_ATOMID_MASK;
116

117
			// Capturing atom coordinates
Leonardo Solis's avatar
Leonardo Solis committed
118
			float3 atom_to_rotate;
Leonardo Solis's avatar
Leonardo Solis committed
119

120
			if ((rotation_list_element & RLIST_FIRSTROT_MASK) != 0)	// If first rotation of this atom
121
			{	
122
				atom_to_rotate = KerConstStatic_ref_coords_const [atom_id];
123
124
			}
			else
Leonardo Solis's avatar
Leonardo Solis committed
125
			{	
Leonardo Solis's avatar
Leonardo Solis committed
126
				atom_to_rotate = loc_coords[atom_id];
127
128
			}

129
			// Capturing rotation vectors and angle
130
131
132
			float3   rotation_unitvec;
			float3   rotation_movingvec;
			float    rotation_angle;
Leonardo Solis's avatar
Leonardo Solis committed
133

134
			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// If general rotation
135
			{
136
137
138
139
140
141
142
143
				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
144
				rotation_unitvec = genrot_unitvec;
145
				rotation_angle = genrotangle;
Leonardo Solis's avatar
Leonardo Solis committed
146
				rotation_movingvec = genotype_xyz;
147
			}
148
			else	// If rotating around rotatable bond
149
			{
Leonardo Solis's avatar
Leonardo Solis committed
150
				uint rotbond_id = (rotation_list_element & RLIST_RBONDID_MASK) >> RLIST_RBONDID_SHIFT;
151

152
				rotation_unitvec = KerConstStatic_rotbonds_unit_vectors_const [rotbond_id];
Leonardo Solis's avatar
Leonardo Solis committed
153
				
Leonardo Solis's avatar
Leonardo Solis committed
154
				rotation_angle = genotype [6+rotbond_id];
Leonardo Solis's avatar
Leonardo Solis committed
155

156
				rotation_movingvec = KerConstStatic_rotbonds_moving_vectors_const [rotbond_id];
157

158
159
				// In addition performing the first movement 
				// which is needed only if rotating around rotatable bond
Leonardo Solis's avatar
Leonardo Solis committed
160
				atom_to_rotate -= rotation_movingvec;
161
162
			}

163
			// Performing rotation
164
165
			float4 quatrot_left;
			float4 quatrot_temp;
Leonardo Solis's avatar
Leonardo Solis committed
166

167
			rotation_angle = rotation_angle*0.5f;
168

Leonardo Solis's avatar
Leonardo Solis committed
169
			float sin_angle, cos_angle;
170
171
			sin_angle      = native_sin(rotation_angle);
			cos_angle      = native_cos(rotation_angle);
172
173
174
175
			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;
176

177
178
179
			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// If general rotation, 
										// two rotations should be performed 
										// (multiplying the quaternions)
180
			{
181
				const float4 ref_orientation_quats_const = KerConstStatic_ref_orientation_quats_const[Host_RunId];
182
183
184
185
				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;
186

187
188
				// Calculating quatrot_left*ref_orientation_quats_const, 
				// which means that reference orientation rotation is the first
189
				quatrot_temp = quatrot_left;
190

191
				// Taking the first element of ref_orientation_quats_const member
192
193
194
195
196
197
198
199
200
				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);
201
			}
Leonardo Solis's avatar
Leonardo Solis committed
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
			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);
220

221
			// Performing final movement and storing values
Leonardo Solis's avatar
Leonardo Solis committed
222
			loc_coords[atom_id] = atom_to_rotate + rotation_movingvec;
Leonardo Solis's avatar
Leonardo Solis committed
223

224
		} // End if-statement not dummy rotation
Leonardo Solis's avatar
Leonardo Solis committed
225
226
227

		mem_fence(CLK_LOCAL_MEM_FENCE);

228
229
	} // End rotation_counter for-loop

230
	#if defined (DEBUG_KRNL_CONFORM)
231
	printf("BEFORE Out CONFORM CHANNEL\n");
232
	#endif
233

234
	// --------------------------------------------------------------
235
	// Send ligand atomic coordinates to channel 
236
	// --------------------------------------------------------------
237
	__attribute__((xcl_pipeline_loop))
Leonardo Solis's avatar
Leonardo Solis committed
238
	LOOP_FOR_CONFORM_WRITE_XYZ:
Leonardo Solis's avatar
Leonardo Solis committed
239
240
	for (uchar pipe_cnt=0; pipe_cnt<DockConst_num_of_atoms; pipe_cnt+=2) {
		if (pipe_cnt == 0) {
241
242
			write_pipe_block(chan_Conf2Intere_actmode, &mode);
			write_pipe_block(chan_Conf2Intrae_actmode, &mode);
Leonardo Solis's avatar
Leonardo Solis committed
243
		}
lvs's avatar
lvs committed
244
/*
Leonardo Solis's avatar
Leonardo Solis committed
245
		mem_fence(CLK_CHANNEL_MEM_FENCE);
lvs's avatar
lvs committed
246
*/
Leonardo Solis's avatar
Leonardo Solis committed
247
248
249

		float3 tmp_coords[2];

Leonardo Solis's avatar
Leonardo Solis committed
250
251
		__attribute__((opencl_unroll_hint))
		LOOP_CONFORM_OUT:
Leonardo Solis's avatar
Leonardo Solis committed
252
253
254
255
256
257
258
259
		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
260

lvs's avatar
lvs committed
261
262
		write_pipe_block(chan_Conf2Intere_xyz, &tmp);
		write_pipe_block(chan_Conf2Intrae_xyz, &tmp);
Leonardo Solis's avatar
Leonardo Solis committed
263
	}
264

265
	// --------------------------------------------------------------
266
	#if defined (DEBUG_KRNL_CONFORM)
267
	printf("AFTER Out CONFORM CHANNEL\n");
268
	#endif
269

270
} // End of while(active)
271

272
#if defined (DEBUG_ACTIVE_KERNEL)
273
printf("	%-20s: %s\n", "Krnl_Conform", "disabled");
274
#endif
275

276
277
278
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------