kupdatesd.br 15.1 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
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

/****************************************************************
* This file is part of the gpu acceleration library for gromacs.
* Author: V. Vishal
* Copyright (C) Pande Group, Stanford, 2006
*****************************************************************/


/* Stochastic Integrator
 *
 * Since we don't have random numbers on the GPU, we 
 * precalculate random numbers from a normal distribution
 * and store them in several large textures. Periodically
 * these will have to be refreshed on the CPU.
 *
 * Doing gathers instead of streams for the random vectors, 
 * but the gathers should be fully coherent anyway. 
 * This is just to avoid some
 * painful manipulations with multiple streams and domains.  
 * */


/* First part before first constraint call 
 *
 *
 * Many things can be precalculated here
 * If we become compute bound (wonder how), we can precalc stuff
 *
 * Assuming constant temperature. If you want to change the 
 * temperature, you have to change all the precalculated 
 * constants that use it.
 * */

/*
kernel void kupdate_sd1(
		float xstrwidth,   //atom stream width
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers

		//sd constants, prefixed with c 
		//to avoid 1 letter names, cxx = sdc[0].xx
		float cem,

		//sd precalculated constants
		float pc1,  //tau_t[0] * ( 1 - sdc[0].em )
		float pc2,  //tau_t[gt] * (sdc[gt].eph - sdc.emh)
		float pc3,  //sdc[0].d/(tau_t * sdc[0].c);
		
		//sdpc = sd precalculated per atom values
		//sdpc.x = 1/sqrt(m)*sig[0].Yv
		//sdpc.y = 1/sqrt(m)*sig[0].V
		float2 sdpc<>,   
		
		//Normally distributed random vectors
		float3 fgauss[][],
	
		float3 sd2X<>,  //This is calculated in sd2 of the previous step	
		float4 posq<>,
		float3 f<>,
		float3 v<>,

		float invmass<>,
		out float3 sd1V<>, //This is V in gromacs, reused in sd2  
		out float3 vnew<>,
		out float4 posqp<>
		){
	float3 Vmh;
	float3 fg1, fg2;
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index

	igauss = indexof( posq );
	linind_gauss = igauss.y * xstrwidth + igauss.x;
	//2 random vectors used in each fragment
	linind_gauss = 2 * linind_gauss + goffset; 	
	
	igauss.y = floor( linind_gauss / gstrwidth );
	igauss.x = linind_gauss - igauss.y * gstrwidth;

	fg1 = fgauss[ igauss ];

	linind_gauss += 1.0f;
	igauss.y = floor( linind_gauss / gstrwidth );
	igauss.x = linind_gauss - igauss.y * gstrwidth;

	fg2 = fgauss[ igauss ];
	
	Vmh  = sd2X * pc3 + sdpc.x * fg1;
	sd1V = sdpc.y * fg2;
	
	vnew = v * cem + invmass * f * pc1 + sd1V - Vmh * cem;
	
	posqp.w = posq.w;
	posqp.xyz = posq.xyz + vnew * pc2;
	
} */

/*Second part of sd update, after first constraints call*/
/*
kernel void kupdate_sd2(
		float xstrwidth,   //stream width of atoms
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers
		
		float pc1,          //this is 1/pc2 for sd1 
		                    //= 1/(tau_t*(sdc[0].eph-sdc[0].emh))
		float pc2,          //= tau_t * sdc[0].d/(sdc[0].em - 1)
		

		//per atom precalcs
		//sdpc.x = 1/sqrt(m)*sig[0].Yx
		//sdpc.y = 1/sqrt(m)*sig[0].X
		float2 sdpc<>,

		//Normally distributed random vectors
		float3 fgauss[][],
		
		float3 sd1V<>,      //calculated in sd1
		float4 posq<>,      //positions pre-update
		float4 posqp<>,     //positions post-constraint
		float3 vnew<>,      //velocities after sd1
		out float3 sd2X<>,  //Used in sd1
		out float3 v<>,     //velocities corrected for constraints
		out float4 posqp2<> //Will need to be constrained again
		){
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index
	float3 fg1, fg2;
	float3 Xmh;

	igauss = indexof( posq );
	linind_gauss = igauss.y * xstrwidth + igauss.x;
	linind_gauss = 2 * linind_gauss + goffset; 	
	
	igauss.y = floor( linind_gauss / gstrwidth );
	igauss.x = linind_gauss - igauss.y * gstrwidth;
	fg1 = fgauss[ igauss ];

	linind_gauss += 1.0f;
	igauss.y = floor( linind_gauss / gstrwidth );
	igauss.x = linind_gauss - igauss.y * gstrwidth;
	fg2 = fgauss[ igauss ];
			
	v = pc1 * ( posqp.xyz - posq.xyz );
	Xmh = sd1V * pc2 + sdpc.x * fg1;
	
	sd2X = sdpc.y * fg2;

	posqp2 = posqp;
	posqp2.xyz += sd2X - Xmh;
} */

//Applies a permutation to the given random vector stream
//Totally bandwidth bound and streams are large, should be done infrequently.
//Probably don't want gvin and gvout to be the same stream!
kernel void kpermute_vectors( float gstrwidth, float perm<>, 
		float3 gvin[][], out float3 gvout<> )
{
	float2 ind;
	//ind.y = floor( perm / gstrwidth );
   ind.y = round( (perm - fmod( perm, gstrwidth ))/gstrwidth );
	ind.x = perm - ind.y * gstrwidth;

	gvout = gvin[ ind ];
}

/*
 * Alternative formulation to handle precision better
 * In updatesd1, we output only deltaV's. The shake
 * kernel uses the deltaV's and the X's to output 
 * a bunch of constrained deltax's
 * */


kernel void kupdate_sd1_fix1(
		float xstrwidth,   //atom stream width
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers

		//sd constants, prefixed with c 
		//to avoid 1 letter names, cxx = sdc[0].xx
		float cem,

		//sd precalculated constants
		float pc1,  //tau_t[0] * ( 1 - sdc[0].em )
		float pc2,  //tau_t[gt] * (sdc[gt].eph - sdc.emh)
		float pc3,  //sdc[0].d/(tau_t * sdc[0].c);
		
		//sdpc = sd precalculated per atom values
		//sdpc.x = 1/sqrt(m)*sig[0].Yv
		//sdpc.y = 1/sqrt(m)*sig[0].V
		float2 sdpc<>,   
		
		//Normally distributed random vectors
		float3 fgauss[][],
	
		float3 sd2X<>,  //This is calculated in sd2 of the previous step	
		float4 posq<>,
		float3 f<>,
		float3 v<>,

		float invmass<>,
		out float3 sd1V<>, //This is V in gromacs, reused in sd2  
		out float3 vnew<>,
		out float4 posqp<>
		){
	float3 Vmh;
	float3 fg1, fg2;
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index

	igauss = indexof( posq );
	linind_gauss = igauss.y * xstrwidth + igauss.x;
	//2 random vectors used in each fragment
	linind_gauss = 2 * linind_gauss + goffset; 	
	
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth);
	igauss.x = linind_gauss - igauss.y * gstrwidth;

	fg1 = fgauss[ igauss ];

	linind_gauss += 1.0f;
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth);
	igauss.x = linind_gauss - igauss.y * gstrwidth;

	fg2 = fgauss[ igauss ];
	
   /* 
      Gromacs code
    n               = 1 -> ngtc

    sdc[n].gdt      = dt/tau_t[n];
    sdc[n].eph      = exp(sdc[n].gdt/2);
    sdc[n].emh      = exp(-sdc[n].gdt/2);
    sdc[n].em       = exp(-sdc[n].gdt);

    sdc[n].b        = sdc[n].gdt*(sqr(sdc[n].eph)-1) - 4*sqr(sdc[n].eph-1);

    // 2.15 in paper (C)
    sdc[n].c        = sdc[n].gdt - 3 + 4*sdc[n].emh - sdc[n].em;

    sdc[n].d        = 2 - sdc[n].eph - sdc[n].emh;

      Vmh           = X[n-start][d]*sdc[gt].d/(tau_t[gt]*sdc[gt].c) + ism*sig[gt].Yv*fgauss(&jran);

      V[n-start][d] = ism*sig[gt].V*fgauss(&jran);

      v[n][d]       = vn*sdc[gt].em + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em) +
                      V[n-start][d] - sdc[gt].em*Vmh;
           
      xprime[n][d]  = x[n][d] + v[n][d]*tau_t[gt]*(sdc[gt].eph - sdc[gt].emh); 
  
   */

// ---------------------------------------------------------------------------------------------
	// float pc3 = sdc[0].d/(tau_t * sdc[0].c);
   //sdpc.x = 1/sqrt(m)*sig[0].Yv
	Vmh  = sd2X * pc3 + sdpc.x * fg1;
// ---------------------------------------------------------------------------------------------

// ---------------------------------------------------------------------------------------------
	//sdpc.y = 1/sqrt(m)*sig[0].V
	sd1V = sdpc.y * fg2;
// ---------------------------------------------------------------------------------------------
	

// ---------------------------------------------------------------------------------------------
   // 
   // float pc1 = tau_t[0] * ( 1 - sdc[0].em )
	vnew = v * cem + invmass * f * pc1 + sd1V - Vmh * cem;
	
   // float pc2 = tau_t[gt] * (sdc[gt].eph - sdc.emh)
	posqp       = posq;
	posqp.xyz  += vnew * pc2;
}

kernel void kupdate_sd2_fix1(
		float xstrwidth,   //stream width of atoms
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers
		
		float pc1,          //this is 1/pc2 for sd1 
		                    //= 1/(tau_t*(sdc[0].eph-sdc[0].emh))
		float pc2,          //= tau_t * sdc[0].d/(sdc[0].em - 1)
		

		//per atom precalcs
		//sdpc.x = 1/sqrt(m)*sig[0].Yx
		//sdpc.y = 1/sqrt(m)*sig[0].X
		float2 sdpc<>,

		//Normally distributed random vectors
		float3 fgauss[][],
		
		float3 sd1V<>,      //calculated in sd1
		float4 posq<>,      //positions pre-update
		float4 posqp<>,     //deltas post constraint
		float3 vnew<>,      //velocities after sd1
		out float3 sd2X<>,  //Used in sd1
		out float3 v<>,     //velocities corrected for constraints
		out float4 posqp2<> //new deltas, need to be constrained again
		){
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index
	float3 fg1, fg2;
	float3 Xmh;

	igauss = indexof( posq );
	linind_gauss = igauss.y * xstrwidth + igauss.x;
	linind_gauss = 2 * linind_gauss + goffset; 	
	
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth); 
	igauss.x = linind_gauss - igauss.y * gstrwidth;
	fg1 = fgauss[ igauss ];

	linind_gauss += 1.0f;
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth); 
	igauss.x = linind_gauss - igauss.y * gstrwidth;
	fg2 = fgauss[ igauss ];
			
	//v   = pc1 * posqp.xyz (!!!!!!!! ?????? !!!!!!!)
	v   = pc1 * (posqp.xyz - posq.xyz);
	Xmh = sd1V * pc2 + sdpc.x * fg1;
	
	sd2X = sdpc.y * fg2;

	posqp2 = posqp;
	posqp2.xyz += sd2X - Xmh;
}


kernel void kupdate_sd1_fix1_FixedRV(
		float xstrwidth,   //atom stream width
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers

		//sd constants, prefixed with c 
		//to avoid 1 letter names, cxx = sdc[0].xx
		float cem,

		//sd precalculated constants
		float pc1,  //tau_t[0] * ( 1 - sdc[0].em )
		float pc2,  //tau_t[gt] * (sdc[gt].eph - sdc.emh)
		float pc3,  //sdc[0].d/(tau_t * sdc[0].c);
		
		//sdpc = sd precalculated per atom values
		//sdpc.x = 1/sqrt(m)*sig[0].Yv
		//sdpc.y = 1/sqrt(m)*sig[0].V
		float2 sdpc<>,   
		
		//Normally distributed random vectors
		float3 fgauss[][],
	
		float3 sd2X<>,  //This is calculated in sd2 of the previous step	
		float4 posq<>,
		float3 f<>,
		float3 v<>,

		float invmass<>,
		out float3 sd1V<>, //This is V in gromacs, reused in sd2  
		out float3 vnew<>,
		out float4 posqp<>
		){
	float3 Vmh;
	float3 fg1, fg2;
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index
   float RandomValueVsp = 0.1f;

	igauss = indexof( posq );
	linind_gauss = igauss.y * xstrwidth + igauss.x;
	//2 random vectors used in each fragment
	linind_gauss = 2 * linind_gauss + goffset; 	
	
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth);
	igauss.x = linind_gauss - igauss.y * gstrwidth;

	//fg1 = fgauss[ igauss ];
   fg1 = float3( RandomValueVsp, RandomValueVsp, RandomValueVsp );

	linind_gauss += 1.0f;
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth);
	igauss.x = linind_gauss - igauss.y * gstrwidth;

	//fg2 = fgauss[ igauss ];
   fg2 = float3( RandomValueVsp, RandomValueVsp, RandomValueVsp );
	
   /* 
      Gromacs code
    n               = 1 -> ngtc

    sdc[n].gdt      = dt/tau_t[n];
    sdc[n].eph      = exp(sdc[n].gdt/2);
    sdc[n].emh      = exp(-sdc[n].gdt/2);
    sdc[n].em       = exp(-sdc[n].gdt);

    sdc[n].b        = sdc[n].gdt*(sqr(sdc[n].eph)-1) - 4*sqr(sdc[n].eph-1);

    // 2.15 in paper (C)
    sdc[n].c        = sdc[n].gdt - 3 + 4*sdc[n].emh - sdc[n].em;

    sdc[n].d        = 2 - sdc[n].eph - sdc[n].emh;

      Vmh           = X[n-start][d]*sdc[gt].d/(tau_t[gt]*sdc[gt].c) + ism*sig[gt].Yv*fgauss(&jran);

      V[n-start][d] = ism*sig[gt].V*fgauss(&jran);

      v[n][d]       = vn*sdc[gt].em + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em) +
                      V[n-start][d] - sdc[gt].em*Vmh;
           
      xprime[n][d]  = x[n][d] + v[n][d]*tau_t[gt]*(sdc[gt].eph - sdc[gt].emh); 
  
   */

// ---------------------------------------------------------------------------------------------
	// float pc3 = sdc[0].d/(tau_t * sdc[0].c);
   //sdpc.x = 1/sqrt(m)*sig[0].Yv
	Vmh  = sd2X * pc3 + sdpc.x * fg1;
// ---------------------------------------------------------------------------------------------

// ---------------------------------------------------------------------------------------------
	//sdpc.y = 1/sqrt(m)*sig[0].V
	sd1V = sdpc.y * fg2;
// ---------------------------------------------------------------------------------------------
	

// ---------------------------------------------------------------------------------------------
   // 
   // float pc1 = tau_t[0] * ( 1 - sdc[0].em )
	vnew = v * cem + invmass * f * pc1 + sd1V - Vmh * cem;
	
   // float pc2 = tau_t[gt] * (sdc[gt].eph - sdc.emh)
	posqp       = posq;
	posqp.xyz  += vnew * pc2;
}

kernel void kupdate_sd2_fix1_FixedRV(
		float xstrwidth,   //stream width of atoms
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers
		
		float pc1,          //this is 1/pc2 for sd1 
		                    //= 1/(tau_t*(sdc[0].eph-sdc[0].emh))
		float pc2,          //= tau_t * sdc[0].d/(sdc[0].em - 1)
		

		//per atom precalcs
		//sdpc.x = 1/sqrt(m)*sig[0].Yx
		//sdpc.y = 1/sqrt(m)*sig[0].X
		float2 sdpc<>,

		//Normally distributed random vectors
		float3 fgauss[][],
		
		float3 sd1V<>,      //calculated in sd1
		float4 posq<>,      //positions pre-update
		float4 posqp<>,     //deltas post constraint
		float3 vnew<>,      //velocities after sd1
		out float3 sd2X<>,  //Used in sd1
		out float3 v<>,     //velocities corrected for constraints
		out float4 posqp2<> //new deltas, need to be constrained again
		){
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index
	float3 fg1, fg2;
	float3 Xmh;
   float RandomValueVsp = 0.1f;

	igauss = indexof( posq );
	linind_gauss = igauss.y * xstrwidth + igauss.x;
	linind_gauss = 2 * linind_gauss + goffset; 	
	
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth); 
	igauss.x = linind_gauss - igauss.y * gstrwidth;
	//fg1 = fgauss[ igauss ];
   fg1 = float3( RandomValueVsp, RandomValueVsp, RandomValueVsp );

	linind_gauss += 1.0f;
	//igauss.y = floor( linind_gauss / gstrwidth );
   igauss.y = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth); 
	igauss.x = linind_gauss - igauss.y * gstrwidth;
	// fg2 = fgauss[ igauss ];
   fg2 = float3( RandomValueVsp, RandomValueVsp, RandomValueVsp );
			
	//v   = pc1 * posqp.xyz (!!!!!!!! ?????? !!!!!!!)
	v   = pc1 * (posqp.xyz - posq.xyz);
	Xmh = sd1V * pc2 + sdpc.x * fg1;
	
	sd2X = sdpc.y * fg2;

	posqp2 = posqp;
	posqp2.xyz += sd2X - Xmh;
}