stringify.h 68.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// OCLADOCK: AUTOMATICALLY GENERATED FILE, DO NOT EDIT
#ifndef STRINGIFY_H
#define STRINGIFY_H
const char *calcenergy_ocl =
"/*\n"
" * (C) 2013. Evopro Innovation Kft.\n"
" *\n"
" * defines.h\n"
" *\n"
" *  Created on: 2009.05.29.\n"
" *      Author: pechan.imre\n"
" */\n"
"\n"
"#ifndef DEFINES_H_\n"
"#define DEFINES_H_\n"
"\n"
"#if defined (N16WI)\n"
"	#define NUM_OF_THREADS_PER_BLOCK 16\n"
"#elif defined (N32WI)\n"
"	#define NUM_OF_THREADS_PER_BLOCK 32\n"
"#elif defined (N64WI)\n"
"	#define NUM_OF_THREADS_PER_BLOCK 64\n"
"#elif defined (N128WI)\n"
"	#define NUM_OF_THREADS_PER_BLOCK 128\n"
"#else\n"
"	#define NUM_OF_THREADS_PER_BLOCK 64\n"
"#endif\n"
"\n"
"#define MAX_NUM_OF_ATOMS 				90\n"
"#define MAX_NUM_OF_ATYPES 			14\n"
"#define MAX_INTRAE_CONTRIBUTORS 8128\n"
"#define MAX_NUM_OF_ROTATIONS 		4096\n"
"#define MAX_NUM_OF_ROTBONDS 		32\n"
"#define MAX_POPSIZE 						2048\n"
"#define MAX_NUM_OF_RUNS 				1000\n"
"\n"
"// Must be bigger than MAX_NUM_OF_ROTBONDS+6\n"
"#define GENOTYPE_LENGTH_IN_GLOBMEM 64\n"
"#define ACTUAL_GENOTYPE_LENGTH	(MAX_NUM_OF_ROTBONDS+6)\n"
"\n"
"#define LS_EXP_FACTOR 					2.0f\n"
"#define LS_CONT_FACTOR 					0.5f\n"
"\n"
"// Improvements over Pechan's implementation\n"
"#define NATIVE_PRECISION\n"
"#define ASYNC_COPY\n"
"#define IMPROVE_GRID\n"
"#define RESTRICT_ARGS\n"
"#define MAPPED_COPY\n"
"\n"
"#endif /* DEFINES_H_ */\n"
"#ifndef CALCENERGY_BASIC_H_\n"
"#define CALCENERGY_BASIC_H_\n"
"\n"
"\n"
"#define RLIST_ATOMID_MASK 	 0x000000FF\n"
"#define RLIST_RBONDID_MASK 	 0x0000FF00\n"
"#define RLIST_RBONDID_SHIFT  8\n"
"#define RLIST_FIRSTROT_MASK  0x00010000\n"
"#define RLIST_GENROT_MASK 	 0x00020000\n"
"#define RLIST_DUMMY_MASK 	 	 0x00040000\n"
"#define DEG_TO_RAD 					 0.0174533f\n"
"\n"
"// LCG: linear congruential generator constants\n"
"#define RAND_A 							1103515245u\n"
"#define RAND_C 							12345u\n"
"// WARNING: it is supposed that unsigned int is 32 bit long\n"
"#define MAX_UINT 						4294967296.0f\n"
"\n"
"// Macro for capturing grid values\n"
"	// Original\n"
"	#define GETGRIDVALUE(mempoi,gridsize_x,gridsize_y,gridsize_z,t,z,y,x)   *(mempoi + gridsize_x*(y + gridsize_y*(z + gridsize_z*t)) + x)\n"
"\n"
"	// Optimization 1\n"
"	// #define GETGRIDVALUE_OPT(mempoi,gridsize_x,gridsize_y,mul_tmp,z,y,x)   *(mempoi + gridsize_x*(y + gridsize_y*(z + mul_tmp)) + x)\n"
"\n"
"	// Optimization 2\n"
"	// Implemented direclty in the kernel code: calcenergy_fourkernels_intel.cl\n"
"\n"
"// Macro for trilinear interpolation\n"
"#define TRILININTERPOL(cube, weights) (cube[0][0][0]*weights[0][0][0] +cube[1][0][0]*weights[1][0][0] +	\\\n"
"				       cube[0][1][0]*weights[0][1][0] +cube[1][1][0]*weights[1][1][0] + \\\n"
"				       cube[0][0][1]*weights[0][0][1] +cube[1][0][1]*weights[1][0][1] + \\\n"
"				       cube[0][1][1]*weights[0][1][1] +cube[1][1][1]*weights[1][1][1])\n"
"\n"
"#endif /* CALCENERGY_BASIC_H_ */\n"
"\n"
"// All related pragmas are in defines.h (accesible by host and device code)\n"
"\n"
"void gpu_calc_energy(	    int    dockpars_rotbondlist_length,\n"
"			    char   dockpars_num_of_atoms,\n"
"			    char   dockpars_gridsize_x,\n"
"			    char   dockpars_gridsize_y,\n"
"			    char   dockpars_gridsize_z,\n"
"		#if defined (RESTRICT_ARGS)\n"
"			__global const float* restrict dockpars_fgrids, // cannot be allocated in __constant (too large)\n"
"		#else\n"
"			__global const float* dockpars_fgrids, // cannot be allocated in __constant (too large)\n"
"		#endif\n"
"		            char   dockpars_num_of_atypes,\n"
"		            int    dockpars_num_of_intraE_contributors,\n"
"			    float  dockpars_grid_spacing,\n"
"			    float  dockpars_coeff_elec,\n"
"			    float  dockpars_qasp,\n"
"			    float  dockpars_coeff_desolv,\n"
"\n"
"		    __local float* genotype,\n"
"		    __local float* energy,\n"
"		    __local int*   run_id,\n"
"	\n"
"                    // Some OpenCL compilers don't allow local var outside kernels\n"
"		    // so this local vars are passed from a kernel\n"
"		    __local float* calc_coords_x,\n"
"		    __local float* calc_coords_y,\n"
"		    __local float* calc_coords_z,\n"
"		    __local float* partial_energies,\n"
"\n"
"	       __constant float* atom_charges_const,\n"
"               __constant char*  atom_types_const,\n"
"               __constant char*  intraE_contributors_const,\n"
"               __constant float* VWpars_AC_const,\n"
"               __constant float* VWpars_BD_const,\n"
"               __constant float* dspars_S_const,\n"
"               __constant float* dspars_V_const,\n"
"               __constant int*   rotlist_const,\n"
"               __constant float* ref_coords_x_const,\n"
"               __constant float* ref_coords_y_const,\n"
"               __constant float* ref_coords_z_const,\n"
"               __constant float* rotbonds_moving_vectors_const,\n"
"               __constant float* rotbonds_unit_vectors_const,\n"
"               __constant float* ref_orientation_quats_const\n"
")\n"
"\n"
"//The GPU device function calculates the energy of the entity described by genotype, dockpars and the liganddata\n"
"//arrays in constant memory and returns it in the energy parameter. The parameter run_id has to be equal to the ID\n"
"//of the run whose population includes the current entity (which can be determined with blockIdx.x), since this\n"
"//determines which reference orientation should be used.\n"
"{\n"
"	int contributor_counter;\n"
"	char atom1_id, atom2_id, atom1_typeid, atom2_typeid;\n"
"\n"
"	// Name changed to distance_leo to avoid\n"
"	// errors as \"distance\" is the name of OpenCL function\n"
"	//float subx, suby, subz, distance;\n"
"	float subx, suby, subz, distance_leo;\n"
"\n"
"	float x, y, z, dx, dy, dz, q;\n"
"	float cube[2][2][2];\n"
"	float weights[2][2][2];\n"
"	int x_low, x_high, y_low, y_high, z_low, z_high;\n"
"\n"
"	float phi, theta, genrotangle, rotation_angle, sin_angle;\n"
"	float genrot_unitvec[3], rotation_unitvec[3], rotation_movingvec[3];\n"
"	int rotation_counter, rotation_list_element;\n"
"	float atom_to_rotate[3];\n"
"	int atom_id, rotbond_id;\n"
"	float quatrot_left_x, quatrot_left_y, quatrot_left_z, quatrot_left_q;\n"
"	float quatrot_temp_x, quatrot_temp_y, quatrot_temp_z, quatrot_temp_q;\n"
"\n"
"        // Some OpenCL compilers don't allow local var outside kernels\n"
"	// so this local vars are passed from a kernel\n"
"	//__local float calc_coords_x[MAX_NUM_OF_ATOMS];\n"
"	//__local float calc_coords_y[MAX_NUM_OF_ATOMS];\n"
"	//__local float calc_coords_z[MAX_NUM_OF_ATOMS];\n"
"	//__local float partial_energies[NUM_OF_THREADS_PER_BLOCK];\n"
"\n"
"	partial_energies[get_local_id(0)] = 0.0f;\n"
"\n"
"	//CALCULATE CONFORMATION\n"
"\n"
"	//calculate vectors for general rotation\n"
"	phi         = genotype[3]*DEG_TO_RAD;\n"
"	theta       = genotype[4]*DEG_TO_RAD;\n"
"	genrotangle = genotype[5]*DEG_TO_RAD;\n"
"\n"
"#if defined (IMPROVE_GRID)\n"
"\n"
"	#if defined (NATIVE_PRECISION)\n"
"	sin_angle = native_sin(theta);\n"
"	genrot_unitvec [0] = sin_angle*native_cos(phi);\n"
"	genrot_unitvec [1] = sin_angle*native_sin(phi);\n"
"	genrot_unitvec [2] = native_cos(theta);\n"
"	#elif defined (HALF_PRECISION)\n"
"	sin_angle = half_sin(theta);\n"
"	genrot_unitvec [0] = sin_angle*half_cos(phi);\n"
"	genrot_unitvec [1] = sin_angle*half_sin(phi);\n"
"	genrot_unitvec [2] = half_cos(theta);\n"
"	#else	// Full precision\n"
"	sin_angle = sin(theta);\n"
"	genrot_unitvec [0] = sin_angle*cos(phi);\n"
"	genrot_unitvec [1] = sin_angle*sin(phi);\n"
"	genrot_unitvec [2] = cos(theta);\n"
"	#endif\n"
"\n"
"	// INTERMOLECULAR for-loop (intermediate results)\n"
"	// It stores a product of two chars\n"
"	unsigned int mul_tmp;\n"
"\n"
"	unsigned char g1 = dockpars_gridsize_x;\n"
"	unsigned int  g2 = dockpars_gridsize_x * dockpars_gridsize_y;\n"
"  unsigned int  g3 = dockpars_gridsize_x * dockpars_gridsize_y * dockpars_gridsize_z;\n"
"\n"
"	unsigned int ylow_times_g1, yhigh_times_g1;\n"
"	unsigned int zlow_times_g2, zhigh_times_g2;\n"
"\n"
"	unsigned int cube_000;\n"
"	unsigned int cube_100;\n"
"  unsigned int cube_010;\n"
"	unsigned int cube_110;\n"
"	unsigned int cube_001;\n"
"  unsigned int cube_101;\n"
"  unsigned int cube_011;\n"
"  unsigned int cube_111;\n"
"\n"
"#else\n"
"	sin_angle = sin(theta);\n"
"	genrot_unitvec [0] = sin_angle*cos(phi);\n"
"	genrot_unitvec [1] = sin_angle*sin(phi);\n"
"	genrot_unitvec [2] = cos(theta);\n"
"#endif\n"
"\n"
"	// ================================================\n"
"	// Iterating over elements of rotation list\n"
"	// ================================================\n"
"	for (rotation_counter = get_local_id(0);\n"
"	     rotation_counter < dockpars_rotbondlist_length;\n"
"	     rotation_counter+=NUM_OF_THREADS_PER_BLOCK)\n"
"	{\n"
"		rotation_list_element = rotlist_const[rotation_counter];\n"
"\n"
"		if ((rotation_list_element & RLIST_DUMMY_MASK) == 0)	//if not dummy rotation\n"
"		{\n"
"			atom_id = rotation_list_element & RLIST_ATOMID_MASK;\n"
"\n"
"			//capturing atom coordinates\n"
"			if ((rotation_list_element & RLIST_FIRSTROT_MASK) != 0)	//if firts rotation of this atom\n"
"			{\n"
"				atom_to_rotate[0] = ref_coords_x_const[atom_id];\n"
"				atom_to_rotate[1] = ref_coords_y_const[atom_id];\n"
"				atom_to_rotate[2] = ref_coords_z_const[atom_id];\n"
"			}\n"
"			else\n"
"			{\n"
"				atom_to_rotate[0] = calc_coords_x[atom_id];\n"
"				atom_to_rotate[1] = calc_coords_y[atom_id];\n"
"				atom_to_rotate[2] = calc_coords_z[atom_id];\n"
"			}\n"
"\n"
"			//capturing rotation vectors and angle\n"
"			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	//if general rotation\n"
"			{\n"
"				rotation_unitvec[0] = genrot_unitvec[0];\n"
"				rotation_unitvec[1] = genrot_unitvec[1];\n"
"				rotation_unitvec[2] = genrot_unitvec[2];\n"
"\n"
"				rotation_angle = genrotangle;\n"
"\n"
"				rotation_movingvec[0] = genotype[0];\n"
"				rotation_movingvec[1] = genotype[1];\n"
"				rotation_movingvec[2] = genotype[2];\n"
"			}\n"
"			else	//if rotating around rotatable bond\n"
"			{\n"
"				rotbond_id = (rotation_list_element & RLIST_RBONDID_MASK) >> RLIST_RBONDID_SHIFT;\n"
"\n"
"				rotation_unitvec[0] = rotbonds_unit_vectors_const[3*rotbond_id];\n"
"				rotation_unitvec[1] = rotbonds_unit_vectors_const[3*rotbond_id+1];\n"
"				rotation_unitvec[2] = rotbonds_unit_vectors_const[3*rotbond_id+2];\n"
"				rotation_angle = genotype[6+rotbond_id]*DEG_TO_RAD;\n"
"\n"
"				rotation_movingvec[0] = rotbonds_moving_vectors_const[3*rotbond_id];\n"
"				rotation_movingvec[1] = rotbonds_moving_vectors_const[3*rotbond_id+1];\n"
"				rotation_movingvec[2] = rotbonds_moving_vectors_const[3*rotbond_id+2];\n"
"\n"
"				//in addition, performing the first movement which is needed only if rotating around rotatable bond\n"
"				atom_to_rotate[0] -= rotation_movingvec[0];\n"
"				atom_to_rotate[1] -= rotation_movingvec[1];\n"
"				atom_to_rotate[2] -= rotation_movingvec[2];\n"
"			}\n"
"\n"
"			//performing rotation\n"
"\n"
"#if defined (NATIVE_PRECISION)\n"
"			rotation_angle = native_divide(rotation_angle,2);\n"
"			quatrot_left_q = native_cos(rotation_angle);\n"
"			sin_angle = native_sin(rotation_angle);\n"
"#elif defined (HALF_PRECISION)\n"
"			rotation_angle = half_divide(rotation_angle,2);\n"
"			quatrot_left_q = half_cos(rotation_angle);\n"
"			sin_angle = half_sin(rotation_angle);\n"
"#else	// Full precision\n"
"			rotation_angle = rotation_angle/2;\n"
"			quatrot_left_q = cos(rotation_angle);\n"
"			sin_angle = sin(rotation_angle);\n"
"#endif\n"
"			quatrot_left_x = sin_angle*rotation_unitvec[0];\n"
"			quatrot_left_y = sin_angle*rotation_unitvec[1];\n"
"			quatrot_left_z = sin_angle*rotation_unitvec[2];\n"
"\n"
"			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// if general rotation,\n"
"																														// two rotations should be performed\n"
"																														// (multiplying the quaternions)\n"
"			{\n"
"				//calculating quatrot_left*ref_orientation_quats_const,\n"
"				//which means that reference orientation rotation is the first\n"
"				quatrot_temp_q = quatrot_left_q;\n"
"				quatrot_temp_x = quatrot_left_x;\n"
"				quatrot_temp_y = quatrot_left_y;\n"
"				quatrot_temp_z = quatrot_left_z;\n"
"\n"
"				quatrot_left_q = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)]-\n"
"						 						 quatrot_temp_x*ref_orientation_quats_const[4*(*run_id)+1]-\n"
"						 					 	 quatrot_temp_y*ref_orientation_quats_const[4*(*run_id)+2]-\n"
"						   			 	   quatrot_temp_z*ref_orientation_quats_const[4*(*run_id)+3];\n"
"				quatrot_left_x = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)+1]+\n"
"						 						 ref_orientation_quats_const[4*(*run_id)]*quatrot_temp_x+\n"
"						 					 	 quatrot_temp_y*ref_orientation_quats_const[4*(*run_id)+3]-\n"
"						 					 	 ref_orientation_quats_const[4*(*run_id)+2]*quatrot_temp_z;\n"
"				quatrot_left_y = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)+2]+\n"
"						 						 ref_orientation_quats_const[4*(*run_id)]*quatrot_temp_y+\n"
"						      			 ref_orientation_quats_const[4*(*run_id)+1]*quatrot_temp_z-\n"
"						 					 	 quatrot_temp_x*ref_orientation_quats_const[4*(*run_id)+3];\n"
"				quatrot_left_z = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)+3]+\n"
"						 						 ref_orientation_quats_const[4*(*run_id)]*quatrot_temp_z+\n"
"						 					 	 quatrot_temp_x*ref_orientation_quats_const[4*(*run_id)+2]-\n"
"						 					 	 ref_orientation_quats_const[4*(*run_id)+1]*quatrot_temp_y;\n"
"\n"
"			}\n"
"\n"
"			quatrot_temp_q = 0 -\n"
"					 						 quatrot_left_x*atom_to_rotate [0] -\n"
"					 					 	 quatrot_left_y*atom_to_rotate [1] -\n"
"					 					 	 quatrot_left_z*atom_to_rotate [2];\n"
"			quatrot_temp_x = quatrot_left_q*atom_to_rotate [0] +\n"
"					 					   quatrot_left_y*atom_to_rotate [2] -\n"
"					 					 	 quatrot_left_z*atom_to_rotate [1];\n"
"			quatrot_temp_y = quatrot_left_q*atom_to_rotate [1] -\n"
"					 					   quatrot_left_x*atom_to_rotate [2] +\n"
"					 					 	 quatrot_left_z*atom_to_rotate [0];\n"
"			quatrot_temp_z = quatrot_left_q*atom_to_rotate [2] +\n"
"					 						 quatrot_left_x*atom_to_rotate [1] -\n"
"					 					 	 quatrot_left_y*atom_to_rotate [0];\n"
"\n"
"			atom_to_rotate [0] = 0 -\n"
"					     						 quatrot_temp_q*quatrot_left_x +\n"
"					     			 			 quatrot_temp_x*quatrot_left_q -\n"
"					            		 quatrot_temp_y*quatrot_left_z +\n"
"					     			 	     quatrot_temp_z*quatrot_left_y;\n"
"			atom_to_rotate [1] = 0 -\n"
"					     					   quatrot_temp_q*quatrot_left_y +\n"
"					     			 			 quatrot_temp_x*quatrot_left_z +\n"
"					     			 			 quatrot_temp_y*quatrot_left_q -\n"
"					     			 			 quatrot_temp_z*quatrot_left_x;\n"
"			atom_to_rotate [2] = 0 -\n"
"					     					   quatrot_temp_q*quatrot_left_z -\n"
"					     			 			 quatrot_temp_x*quatrot_left_y +\n"
"					     			 			 quatrot_temp_y*quatrot_left_x +\n"
"					     			 			 quatrot_temp_z*quatrot_left_q;\n"
"\n"
"			//performing final movement and storing values\n"
"			calc_coords_x[atom_id] = atom_to_rotate [0] + rotation_movingvec[0];\n"
"			calc_coords_y[atom_id] = atom_to_rotate [1] + rotation_movingvec[1];\n"
"			calc_coords_z[atom_id] = atom_to_rotate [2] + rotation_movingvec[2];\n"
"\n"
"		} // End if-statement not dummy rotation\n"
"\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"\n"
"	} // End rotation_counter for-loop\n"
"\n"
"	// ================================================\n"
"	// CALCULATE INTERMOLECULAR ENERGY\n"
"	// ================================================\n"
"	for (atom1_id = get_local_id(0);\n"
"	     atom1_id < dockpars_num_of_atoms;\n"
"	     atom1_id+= NUM_OF_THREADS_PER_BLOCK)\n"
"	{\n"
"		atom1_typeid = atom_types_const[atom1_id];\n"
"		x = calc_coords_x[atom1_id];\n"
"		y = calc_coords_y[atom1_id];\n"
"		z = calc_coords_z[atom1_id];\n"
"		q = atom_charges_const[atom1_id];\n"
"\n"
"		if ((x < 0) || (y < 0) || (z < 0) || (x >= dockpars_gridsize_x-1)\n"
"				                  						|| (y >= dockpars_gridsize_y-1)\n"
"						  												|| (z >= dockpars_gridsize_z-1)){\n"
"			partial_energies[get_local_id(0)] += 16777216.0f; //100000.0f;\n"
"		}\n"
"		else\n"
"		{\n"
"			//get coordinates\n"
"			x_low = (int)floor(x); y_low = (int)floor(y); z_low = (int)floor(z);\n"
"			x_high = (int)ceil(x); y_high = (int)ceil(y); z_high = (int)ceil(z);\n"
"			dx = x - x_low; dy = y - y_low; dz = z - z_low;\n"
"\n"
"			//calculate interpolation weights\n"
"			weights [0][0][0] = (1-dx)*(1-dy)*(1-dz);\n"
"			weights [1][0][0] = dx*(1-dy)*(1-dz);\n"
"			weights [0][1][0] = (1-dx)*dy*(1-dz);\n"
"			weights [1][1][0] = dx*dy*(1-dz);\n"
"			weights [0][0][1] = (1-dx)*(1-dy)*dz;\n"
"			weights [1][0][1] = dx*(1-dy)*dz;\n"
"			weights [0][1][1] = (1-dx)*dy*dz;\n"
"			weights [1][1][1] = dx*dy*dz;\n"
"\n"
"			//capturing affinity values\n"
"#if defined (IMPROVE_GRID)\n"
"			ylow_times_g1  = y_low*g1;\n"
"			yhigh_times_g1 = y_high*g1;\n"
"		  zlow_times_g2  = z_low*g2;\n"
"			zhigh_times_g2 = z_high*g2;\n"
"\n"
"			cube_000 = x_low  + ylow_times_g1  + zlow_times_g2;\n"
"			cube_100 = x_high + ylow_times_g1  + zlow_times_g2;\n"
"			cube_010 = x_low  + yhigh_times_g1 + zlow_times_g2;\n"
"			cube_110 = x_high + yhigh_times_g1 + zlow_times_g2;\n"
"			cube_001 = x_low  + ylow_times_g1  + zhigh_times_g2;\n"
"			cube_101 = x_high + ylow_times_g1  + zhigh_times_g2;\n"
"			cube_011 = x_low  + yhigh_times_g1 + zhigh_times_g2;\n"
"			cube_111 = x_high + yhigh_times_g1 + zhigh_times_g2;\n"
"			mul_tmp = atom1_typeid*g3;\n"
"\n"
"			cube [0][0][0] = *(dockpars_fgrids + cube_000 + mul_tmp);\n"
"			cube [1][0][0] = *(dockpars_fgrids + cube_100 + mul_tmp);\n"
"      cube [0][1][0] = *(dockpars_fgrids + cube_010 + mul_tmp);\n"
"      cube [1][1][0] = *(dockpars_fgrids + cube_110 + mul_tmp);\n"
"      cube [0][0][1] = *(dockpars_fgrids + cube_001 + mul_tmp);\n"
"      cube [1][0][1] = *(dockpars_fgrids + cube_101 + mul_tmp);\n"
"      cube [0][1][1] = *(dockpars_fgrids + cube_011 + mul_tmp);\n"
"      cube [1][1][1] = *(dockpars_fgrids + cube_111 + mul_tmp);\n"
"\n"
"#else\n"
"			cube [0][0][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_low, x_low);\n"
"			cube [1][0][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_low, x_high);\n"
"			cube [0][1][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      								  dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_high, x_low);\n"
"			cube [1][1][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_high, x_high);\n"
"			cube [0][0][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_low, x_low);\n"
"			cube [1][0][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      								  atom1_typeid, z_high, y_low, x_high);\n"
"			cube [0][1][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_high, x_low);\n"
"			cube [1][1][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_high, x_high);\n"
"#endif\n"
"\n"
"			//calculating affinity energy\n"
"			partial_energies[get_local_id(0)] += TRILININTERPOL(cube, weights);\n"
"\n"
"			//capturing electrostatic values\n"
"			atom1_typeid = dockpars_num_of_atypes;\n"
"\n"
"#if defined (IMPROVE_GRID)\n"
"			mul_tmp = atom1_typeid*g3;\n"
"			cube [0][0][0] = *(dockpars_fgrids + cube_000 + mul_tmp);\n"
"			cube [1][0][0] = *(dockpars_fgrids + cube_100 + mul_tmp);\n"
"      cube [0][1][0] = *(dockpars_fgrids + cube_010 + mul_tmp);\n"
"      cube [1][1][0] = *(dockpars_fgrids + cube_110 + mul_tmp);\n"
"      cube [0][0][1] = *(dockpars_fgrids + cube_001 + mul_tmp);\n"
"      cube [1][0][1] = *(dockpars_fgrids + cube_101 + mul_tmp);\n"
"      cube [0][1][1] = *(dockpars_fgrids + cube_011 + mul_tmp);\n"
"      cube [1][1][1] = *(dockpars_fgrids + cube_111 + mul_tmp);\n"
"\n"
"#else\n"
"			cube [0][0][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      		 						  dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_low, x_low);\n"
"			cube [1][0][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_low, x_high);\n"
"			cube [0][1][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_high, x_low);\n"
"			cube [1][1][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_high, x_high);\n"
"			cube [0][0][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_low, x_low);\n"
"			cube [1][0][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_low, x_high);\n"
"			cube [0][1][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_high, x_low);\n"
"			cube [1][1][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_high, x_high);\n"
"#endif\n"
"\n"
"			//calculating electrosatic energy\n"
"			partial_energies[get_local_id(0)] += q * TRILININTERPOL(cube, weights);\n"
"\n"
"			//capturing desolvation values\n"
"			atom1_typeid = dockpars_num_of_atypes+1;\n"
"\n"
"#if defined (IMPROVE_GRID)\n"
"			mul_tmp = atom1_typeid*g3;\n"
"			cube [0][0][0] = *(dockpars_fgrids + cube_000 + mul_tmp);\n"
"			cube [1][0][0] = *(dockpars_fgrids + cube_100 + mul_tmp);\n"
"      cube [0][1][0] = *(dockpars_fgrids + cube_010 + mul_tmp);\n"
"      cube [1][1][0] = *(dockpars_fgrids + cube_110 + mul_tmp);\n"
"      cube [0][0][1] = *(dockpars_fgrids + cube_001 + mul_tmp);\n"
"      cube [1][0][1] = *(dockpars_fgrids + cube_101 + mul_tmp);\n"
"      cube [0][1][1] = *(dockpars_fgrids + cube_011 + mul_tmp);\n"
"      cube [1][1][1] = *(dockpars_fgrids + cube_111 + mul_tmp);\n"
"\n"
"#else\n"
"			cube [0][0][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_low, x_low);\n"
"			cube [1][0][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_low, x_high);\n"
"			cube [0][1][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_high, x_low);\n"
"			cube [1][1][0] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_low, y_high, x_high);\n"
"			cube [0][0][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_low, x_low);\n"
"			cube [1][0][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_low, x_high);\n"
"			cube [0][1][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_high, x_low);\n"
"			cube [1][1][1] = GETGRIDVALUE(dockpars_fgrids, dockpars_gridsize_x,\n"
"						      									dockpars_gridsize_y, dockpars_gridsize_z,\n"
"						      									atom1_typeid, z_high, y_high, x_high);\n"
"#endif\n"
"\n"
"			//calculating desolvation energy\n"
"			partial_energies[get_local_id(0)] += fabs(q) * TRILININTERPOL(cube, weights);\n"
"		}\n"
"\n"
"	} // End atom1_id for-loop\n"
"\n"
"	// In paper: intermolecular and internal energy calculation\n"
"	// are independent from each other, -> NO BARRIER NEEDED\n"
"  // but require different operations,\n"
"	// thus, they can be executed only sequentially on the GPU.\n"
"\n"
"	// ================================================\n"
"	// CALCULATE INTRAMOLECULAR ENERGY\n"
"	// ================================================\n"
"	for (contributor_counter = get_local_id(0);\n"
"	     contributor_counter < dockpars_num_of_intraE_contributors;\n"
"	     contributor_counter +=NUM_OF_THREADS_PER_BLOCK)\n"
"	{\n"
"		//getting atom IDs\n"
"		atom1_id = intraE_contributors_const[3*contributor_counter];\n"
"		atom2_id = intraE_contributors_const[3*contributor_counter+1];\n"
"\n"
"		//calculating address of first atom's coordinates\n"
"		subx = calc_coords_x[atom1_id];\n"
"		suby = calc_coords_y[atom1_id];\n"
"		subz = calc_coords_z[atom1_id];\n"
"\n"
"		//calculating address of second atom's coordinates\n"
"		subx -= calc_coords_x[atom2_id];\n"
"		suby -= calc_coords_y[atom2_id];\n"
"		subz -= calc_coords_z[atom2_id];\n"
"\n"
"		//calculating distance (distance_leo)\n"
"#if defined (NATIVE_PRECISION)\n"
"		distance_leo = native_sqrt(subx*subx + suby*suby + subz*subz)*dockpars_grid_spacing;\n"
"#elif defined (HALF_PRECISION)\n"
"		distance_leo = half_sqrt(subx*subx + suby*suby + subz*subz)*dockpars_grid_spacing;\n"
"#else	// Full precision\n"
"		distance_leo = sqrt(subx*subx + suby*suby + subz*subz)*dockpars_grid_spacing;\n"
"#endif\n"
"\n"
"		if (distance_leo < 1.0f)\n"
"			distance_leo = 1.0f;\n"
"\n"
"		//calculating energy contributions\n"
"		if ((distance_leo < 8.0f) && (distance_leo < 20.48f))\n"
"		{\n"
"			//getting type IDs\n"
"			atom1_typeid = atom_types_const[atom1_id];\n"
"			atom2_typeid = atom_types_const[atom2_id];\n"
"\n"
"			//calculating van der Waals / hydrogen bond term\n"
"#if defined (NATIVE_PRECISION)\n"
"			partial_energies[get_local_id(0)] += native_divide(VWpars_AC_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],native_powr(distance_leo,12));\n"
"#elif defined (HALF_PRECISION)\n"
"			partial_energies[get_local_id(0)] += half_divide(VWpars_AC_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],half_powr(distance_leo,12));\n"
"#else	// Full precision\n"
"			partial_energies[get_local_id(0)] += VWpars_AC_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid]/powr(distance_leo,12);\n"
"#endif\n"
"\n"
"			if (intraE_contributors_const[3*contributor_counter+2] == 1)	//H-bond\n"
"#if defined (NATIVE_PRECISION)\n"
"				partial_energies[get_local_id(0)] -= native_divide(VWpars_BD_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],native_powr(distance_leo,10));\n"
"#elif defined (HALF_PRECISION)\n"
"				partial_energies[get_local_id(0)] -= half_divide(VWpars_BD_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],half_powr(distance_leo,10));\n"
"#else	// Full precision\n"
"				partial_energies[get_local_id(0)] -= VWpars_BD_const[atom1_typeid*dockpars_num_of_atypes+atom2_typeid]/powr(distance_leo,10);\n"
"#endif\n"
"\n"
"			else	//van der Waals\n"
"#if defined (NATIVE_PRECISION)\n"
"				partial_energies[get_local_id(0)] -= native_divide(VWpars_BD_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],native_powr(distance_leo,6));\n"
"#elif defined (HALF_PRECISION)\n"
"				partial_energies[get_local_id(0)] -= half_divide(VWpars_BD_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],half_powr(distance_leo,6));\n"
"#else	// Full precision\n"
"				partial_energies[get_local_id(0)] -= VWpars_BD_const[atom1_typeid*dockpars_num_of_atypes+atom2_typeid]/powr(distance_leo,6);\n"
"#endif\n"
"\n"
"			//calculating electrostatic term\n"
"#if defined (NATIVE_PRECISION)\n"
"        partial_energies[get_local_id(0)] += native_divide (\n"
"                                                             dockpars_coeff_elec * atom_charges_const[atom1_id] * atom_charges_const[atom2_id],\n"
"                                                             distance_leo * (-8.5525f + native_divide(86.9525f,(1.0f + 7.7839f*native_exp(-0.3154f*distance_leo))))\n"
"                                                             );\n"
"#elif defined (HALF_PRECISION)\n"
"        partial_energies[get_local_id(0)] += half_divide (\n"
"                                                             dockpars_coeff_elec * atom_charges_const[atom1_id] * atom_charges_const[atom2_id],\n"
"                                                             distance_leo * (-8.5525f + half_divide(86.9525f,(1.0f + 7.7839f*half_exp(-0.3154f*distance_leo))))\n"
"                                                             );\n"
"#else	// Full precision\n"
"				partial_energies[get_local_id(0)] += dockpars_coeff_elec*atom_charges_const[atom1_id]*atom_charges_const[atom2_id]/\n"
"			                                       (distance_leo*(-8.5525f + 86.9525f/(1.0f + 7.7839f*exp(-0.3154f*distance_leo))));\n"
"#endif\n"
"\n"
"			//calculating desolvation term\n"
"#if defined (NATIVE_PRECISION)\n"
"			partial_energies[get_local_id(0)] += ((dspars_S_const[atom1_typeid] +\n"
"							       											 dockpars_qasp*fabs(atom_charges_const[atom1_id]))*dspars_V_const[atom2_typeid] +\n"
"					                      					 (dspars_S_const[atom2_typeid] +\n"
"							       								 			 dockpars_qasp*fabs(atom_charges_const[atom2_id]))*dspars_V_const[atom1_typeid]) *\n"
"					                       					 dockpars_coeff_desolv*native_exp(-distance_leo*native_divide(distance_leo,25.92f));\n"
"#elif defined (HALF_PRECISION)\n"
"			partial_energies[get_local_id(0)] += ((dspars_S_const[atom1_typeid] +\n"
"							       											 dockpars_qasp*fabs(atom_charges_const[atom1_id]))*dspars_V_const[atom2_typeid] +\n"
"					                      					 (dspars_S_const[atom2_typeid] +\n"
"							       								 			 dockpars_qasp*fabs(atom_charges_const[atom2_id]))*dspars_V_const[atom1_typeid]) *\n"
"					                       					 dockpars_coeff_desolv*half_exp(-distance_leo*half_divide(distance_leo,25.92f));\n"
"#else	// Full precision\n"
"			partial_energies[get_local_id(0)] += ((dspars_S_const[atom1_typeid] +\n"
"							       									     dockpars_qasp*fabs(atom_charges_const[atom1_id]))*dspars_V_const[atom2_typeid] +\n"
"					                      				   (dspars_S_const[atom2_typeid] +\n"
"							       								 			 dockpars_qasp*fabs(atom_charges_const[atom2_id]))*dspars_V_const[atom1_typeid]) *\n"
"					                       					 dockpars_coeff_desolv*exp(-distance_leo*distance_leo/25.92f);\n"
"#endif\n"
"\n"
"		}\n"
"	} // End contributor_counter for-loop\n"
"\n"
"	barrier(CLK_LOCAL_MEM_FENCE);\n"
"\n"
"	if (get_local_id(0) == 0)\n"
"	{\n"
"		*energy = partial_energies[0];\n"
"\n"
"		for (contributor_counter=1;\n"
"		     contributor_counter<NUM_OF_THREADS_PER_BLOCK;\n"
"		     contributor_counter++)\n"
"		{\n"
"			*energy += partial_energies[contributor_counter];\n"
"		}\n"
"	}\n"
"}\n"
"\n"
"__kernel void __attribute__ ((reqd_work_group_size(NUM_OF_THREADS_PER_BLOCK,1,1)))\n"
"gpu_calc_initpop(	char   dockpars_num_of_atoms,\n"
"			char   dockpars_num_of_atypes,\n"
"			int    dockpars_num_of_intraE_contributors,\n"
"			char   dockpars_gridsize_x,\n"
"			char   dockpars_gridsize_y,\n"
"			char   dockpars_gridsize_z,\n"
"			float  dockpars_grid_spacing,\n"
"		#if defined (RESTRICT_ARGS)\n"
"		  __global const float* restrict dockpars_fgrids, // cannot be allocated in __constant (too large)\n"
"		#else\n"
"		  __global const float* dockpars_fgrids,          // cannot be allocated in __constant (too large)\n"
"		#endif\n"
"			int    dockpars_rotbondlist_length,\n"
"			float  dockpars_coeff_elec,\n"
"			float  dockpars_coeff_desolv,\n"
"\n"
"		#if defined (RESTRICT_ARGS)\n"
"			__global const float* restrict dockpars_conformations_current,\n"
"			__global float* restrict dockpars_energies_current,\n"
"		        __global int*   restrict dockpars_evals_of_new_entities,\n"
"		#else\n"
"			__global const float* dockpars_conformations_current,\n"
"			__global float* dockpars_energies_current,\n"
"		        __global int*   dockpars_evals_of_new_entities,\n"
"		#endif\n"
"\n"
"			int    dockpars_pop_size,\n"
"			float  dockpars_qasp,\n"
"\n"
"	   __constant float* atom_charges_const,\n"
"           __constant char*  atom_types_const,\n"
"	   __constant char*  intraE_contributors_const,\n"
"           __constant float* VWpars_AC_const,\n"
"           __constant float* VWpars_BD_const,\n"
"           __constant float* dspars_S_const,\n"
"           __constant float* dspars_V_const,\n"
"           __constant int*   rotlist_const,\n"
"           __constant float* ref_coords_x_const,\n"
"           __constant float* ref_coords_y_const,\n"
"           __constant float* ref_coords_z_const,\n"
"           __constant float* rotbonds_moving_vectors_const,\n"
"           __constant float* rotbonds_unit_vectors_const,\n"
"           __constant float* ref_orientation_quats_const\n"
"){\n"
"	__local float  genotype[GENOTYPE_LENGTH_IN_GLOBMEM];\n"
"	__local float  energy;\n"
"	__local int    run_id;\n"
"\n"
"        // Some OpenCL compilers don't allow local var outside kernels\n"
"        // so this local vars are passed from a kernel\n"
"	__local float calc_coords_x[MAX_NUM_OF_ATOMS];\n"
"	__local float calc_coords_y[MAX_NUM_OF_ATOMS];\n"
"	__local float calc_coords_z[MAX_NUM_OF_ATOMS];\n"
"	__local float partial_energies[NUM_OF_THREADS_PER_BLOCK];\n"
"\n"
"	event_t ev = async_work_group_copy(genotype,\n"
"			                   dockpars_conformations_current + GENOTYPE_LENGTH_IN_GLOBMEM*get_group_id(0),\n"
"			                   GENOTYPE_LENGTH_IN_GLOBMEM, 0);\n"
"\n"
"	wait_group_events(1,&ev);\n"
"\n"
"	//determining run ID\n"
"	if (get_local_id(0) == 0)\n"
"		run_id = get_group_id(0) / dockpars_pop_size;\n"
"\n"
"	// =============================================================\n"
"	// WARNING: only energy of work-item=0 will be valid\n"
"	gpu_calc_energy(dockpars_rotbondlist_length,\n"
"			dockpars_num_of_atoms,\n"
"			dockpars_gridsize_x,\n"
"			dockpars_gridsize_y,\n"
"			dockpars_gridsize_z,\n"
"			dockpars_fgrids,\n"
"			dockpars_num_of_atypes,\n"
"			dockpars_num_of_intraE_contributors,\n"
"			dockpars_grid_spacing,\n"
"			dockpars_coeff_elec,\n"
"			dockpars_qasp,\n"
"			dockpars_coeff_desolv,\n"
"			genotype,\n"
"			&energy,\n"
"			&run_id,\n"
"			// Some OpenCL compilers don't allow local var outside kernels\n"
"			// so this local vars are passed from a kernel\n"
"			calc_coords_x,\n"
"			calc_coords_y,\n"
"			calc_coords_z,\n"
"			partial_energies,\n"
"\n"
"	                atom_charges_const,\n"
"		        atom_types_const,\n"
"			intraE_contributors_const,\n"
"			VWpars_AC_const,\n"
"			VWpars_BD_const,\n"
"			dspars_S_const,\n"
"			dspars_V_const,\n"
"			rotlist_const,\n"
"			ref_coords_x_const,\n"
"			ref_coords_y_const,\n"
"			ref_coords_z_const,\n"
"			rotbonds_moving_vectors_const,\n"
"			rotbonds_unit_vectors_const,\n"
"			ref_orientation_quats_const);\n"
"	// =============================================================\n"
"\n"
"	if (get_local_id(0) == 0) {\n"
"		dockpars_energies_current[get_group_id(0)] = energy;\n"
"		dockpars_evals_of_new_entities[get_group_id(0)] = 1;\n"
"	}\n"
"}\n"
"__kernel void __attribute__ ((reqd_work_group_size(NUM_OF_THREADS_PER_BLOCK,1,1)))\n"
"gpu_sum_evals(/*unsigned long pop_size,*/\n"
"		unsigned int pop_size,\n"
"	      /*unsigned long num_of_runs,*/\n"
"#if defined (RESTRICT_ARGS)\n"
"	      __global int* restrict dockpars_evals_of_new_entities,\n"
"              __global int* restrict evals_of_runs\n"
"#else\n"
"	      __global int* dockpars_evals_of_new_entities,\n"
"              __global int* evals_of_runs\n"
"#endif\n"
")\n"
"//The GPU global function sums the evaluation counter states\n"
"//which are stored in evals_of_new_entities array foreach entity,\n"
"//calculates the sums for each run and stores it in evals_of_runs array.\n"
"//The number of blocks which should be started equals to num_of_runs,\n"
"//since each block performs the summation for one run.\n"
"{\n"
"  int entity_counter;\n"
"  int sum_evals;\n"
"  __local int partsum_evals[NUM_OF_THREADS_PER_BLOCK];\n"
"\n"
"  partsum_evals[get_local_id(0)] = 0;\n"
"\n"
"#if defined (ASYNC_COPY)\n"
"  __local int local_evals_of_new_entities[MAX_POPSIZE];	// defined in defines.h\n"
"\n"
"  async_work_group_copy(local_evals_of_new_entities,\n"
"                        dockpars_evals_of_new_entities+get_group_id(0)*pop_size,\n"
"                        pop_size,0);\n"
"\n"
"  for (entity_counter=get_local_id(0);\n"
"	     entity_counter<pop_size;\n"
"	     entity_counter+=NUM_OF_THREADS_PER_BLOCK)\n"
"       partsum_evals[get_local_id(0)] += local_evals_of_new_entities[entity_counter];\n"
"\n"
"#else\n"
"  for (entity_counter=get_local_id(0);\n"
"	     entity_counter<pop_size;\n"
"	     entity_counter+=NUM_OF_THREADS_PER_BLOCK)\n"
"       partsum_evals[get_local_id(0)] += dockpars_evals_of_new_entities[get_group_id(0)*pop_size+entity_counter];\n"
"#endif\n"
"\n"
"  barrier(CLK_LOCAL_MEM_FENCE);\n"
"\n"
"  if (get_local_id(0) == 0) {\n"
"    sum_evals = partsum_evals[0];\n"
"    for (entity_counter=1;\n"
"	       entity_counter<NUM_OF_THREADS_PER_BLOCK;\n"
"	       entity_counter++) {\n"
"		     sum_evals += partsum_evals[entity_counter];\n"
"    }\n"
"\n"
"    evals_of_runs[get_group_id(0)] += sum_evals;\n"
"  }\n"
"}\n"
"// -------------------------------------------------------\n"
"//\n"
"// -------------------------------------------------------\n"
"unsigned int gpu_rand(\n"
"			#if defined (RESTRICT_ARGS)\n"
"			__global unsigned int* restrict prng_states\n"
"			#else\n"
"			__global unsigned int* prng_states\n"
"			#endif\n"
")\n"
"//The GPU device function generates a random int\n"
"//with a linear congruential generator.\n"
"//Each thread (supposing num_of_runs*pop_size blocks and NUM_OF_THREADS_PER_BLOCK threads per block)\n"
"//has its own state which is stored in the global memory area pointed by\n"
"//prng_states (thread with ID tx in block with ID bx stores its state in prng_states[bx*NUM_OF_THREADS_PER_BLOCK+$\n"
"//The random number generator uses the gcc linear congruential generator constants.\n"
"{\n"
"  unsigned int state;\n"
"\n"
"#if defined (REPRO)\n"
"	state = 1;\n"
"#else\n"
"  //current state of the threads own PRNG\n"
"  //state = prng_states[get_group_id(0)*NUM_OF_THREADS_PER_BLOCK + get_local_id(0)];\n"
"	state = prng_states[get_global_id(0)];\n"
"\n"
"  //calculating next state\n"
"  state = (RAND_A*state+RAND_C);\n"
"#endif\n"
"  //saving next state to memory\n"
"  //prng_states[get_group_id(0)*NUM_OF_THREADS_PER_BLOCK + get_local_id(0)] = state;\n"
"	prng_states[get_global_id(0)] = state;\n"
"\n"
"  return state;\n"
"}\n"
"\n"
"// -------------------------------------------------------\n"
"//\n"
"// -------------------------------------------------------\n"
"float gpu_randf(\n"
"		#if defined (RESTRICT_ARGS)\n"
"		__global unsigned int* restrict prng_states\n"
"		#else\n"
"		__global unsigned int* prng_states\n"
"		#endif\n"
")\n"
"//The GPU device function generates a\n"
"//random float greater than (or equal to) 0 and less than 1.\n"
"//It uses gpu_rand() function.\n"
"{\n"
"  float state;\n"
"\n"
"	//state will be between 0 and 1\n"
"#if defined (REPRO)\n"
"	state = 0.55f; //0.55f;\n"
"#else\n"
"	#if defined (NATIVE_PRECISION)\n"
"	state =  native_divide(gpu_rand(prng_states),MAX_UINT)*0.999999f;\n"
"	#elif defined (HALF_PRECISION)\n"
"	state =  half_divide(gpu_rand(prng_states),MAX_UINT)*0.999999f;\n"
"	#else	// Full precision\n"
"	state = (((float) gpu_rand(prng_states))/MAX_UINT)*0.999999f;\n"
"	#endif\n"
"#endif\n"
"\n"
"  return state;\n"
"}\n"
"\n"
"// -------------------------------------------------------\n"
"//\n"
"// -------------------------------------------------------\n"
"void map_angle(__local float* angle)\n"
"//The GPU device function maps\n"
"//the input parameter to the interval 0...360\n"
"//(supposing that it is an angle).\n"
"{\n"
"  while (*angle >= 360.0f)\n"
"    *angle -= 360.0f;\n"
"\n"
"  while (*angle < 0.0f)\n"
"    *angle += 360.0f;\n"
"}\n"
"\n"
"// -------------------------------------------------------\n"
"//\n"
"// -------------------------------------------------------\n"
"void gpu_perform_elitist_selection(int    dockpars_pop_size,\n"
"\n"
"	         #if defined (RESTRICT_ARGS)\n"
"				    __global float* restrict dockpars_energies_current,\n"
"				    __global float* restrict dockpars_energies_next,\n"
"				    __global int*   restrict dockpars_evals_of_new_entities,\n"
"					#else\n"
"				    __global float* dockpars_energies_current,\n"
"				    __global float* dockpars_energies_next,\n"
"				    __global int*   dockpars_evals_of_new_entities,\n"
"					#endif\n"
"\n"
"					     int    dockpars_num_of_genes,\n"
"\n"
"					#if defined (RESTRICT_ARGS)\n"
"				    __global float* restrict dockpars_conformations_next,\n"
"		        __global const float* restrict dockpars_conformations_current\n"
"					#else\n"
"				    __global float* dockpars_conformations_next,\n"
"		        __global const float* dockpars_conformations_current\n"
"					#endif\n"
"				\n"
"					,\n"
"                    // Some OpenCL compilers don't allow local var outside kernels\n"
"                    // so this local vars are passed from a kernel\n"
"				    __local float* best_energies,\n"
"				    __local int*   best_IDs,\n"
"				    __local int*   best_ID	\n"
"\n"
"\n"
")\n"
"//The GPU device function performs elitist selection,\n"
"//that is, it looks for the best entity in conformations_current and\n"
"//energies_current of the run that corresponds to the block ID,\n"
"//and copies it to the place of the first entity in\n"
"//conformations_next and energies_next.\n"
"{\n"
"\n"
"	int entity_counter;\n"
"	int gene_counter;\n"
"	float best_energy;\n"
"\n"
"        // Some OpenCL compilers don't allow local var outside kernels\n"
"        // so this local vars are passed from a kernel\n"
"	//__local float best_energies[NUM_OF_THREADS_PER_BLOCK];\n"
"	//__local int best_IDs[NUM_OF_THREADS_PER_BLOCK];\n"
"	//__local int best_ID;\n"
"\n"
"	if (get_local_id(0) < dockpars_pop_size)\n"
"	{\n"
"		best_energies[get_local_id(0)] = dockpars_energies_current[get_group_id(0)+get_local_id(0)];\n"
"		best_IDs[get_local_id(0)] = get_local_id(0);\n"
"	}\n"
"\n"
"	for (entity_counter=NUM_OF_THREADS_PER_BLOCK+get_local_id(0);\n"
"	     entity_counter<dockpars_pop_size;\n"
"	     entity_counter+=NUM_OF_THREADS_PER_BLOCK)\n"
"\n"
"	     if (dockpars_energies_current[get_group_id(0)+entity_counter] < best_energies[get_local_id(0)])\n"
"	     {\n"
"		best_energies[get_local_id(0)] = dockpars_energies_current[get_group_id(0)+entity_counter];\n"
"		best_IDs[get_local_id(0)] = entity_counter;\n"
"	     }\n"
"\n"
"       barrier(CLK_LOCAL_MEM_FENCE);\n"
"\n"
"	//this could be implemented with a tree-like structure\n"
"	//which may be slightly faster\n"