Révision 142 NBody/NBody.py
| NBody.py (revision 142) | ||
|---|---|---|
| 18 | 18 |
Computing={'FP32':0,'FP64':1}
|
| 19 | 19 |
return(Marsaglia,Computing) |
| 20 | 20 |
|
| 21 |
BlobOpenCL= """ |
|
| 22 | 21 |
|
| 23 | 22 |
|
| 24 | 23 |
|
| ... | ... | |
| 28 | 27 |
|
| 29 | 28 |
|
| 30 | 29 |
|
| 30 |
|
|
| 31 |
BlobOpenCL= """ |
|
| 31 | 32 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
| 32 | 33 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535) |
| 33 | 34 |
#define MWC (znew+wnew) |
| ... | ... | |
| 90 | 91 |
} |
| 91 | 92 |
|
| 92 | 93 |
barrier(CLK_GLOBAL_MEM_FENCE); |
| 93 |
// return((MYFLOAT)5e-1f*potential);
|
|
| 94 |
//return(5e-1f*potential);
|
|
| 94 | 95 |
return(potential); |
| 95 | 96 |
} |
| 96 | 97 |
|
| 97 |
MYFLOAT AtomicPotentialCoM(__global MYFLOAT8* clData,__global MYFLOAT8* clCoM,int gid)
|
|
| 98 |
MYFLOAT AtomicPotentialCoM(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM,int gid)
|
|
| 98 | 99 |
{
|
| 99 |
return(PairPotential(clData[gid].lo,clCoM[0].lo));
|
|
| 100 |
return(PairPotential(clData[gid].lo,clCoM[0])); |
|
| 100 | 101 |
} |
| 101 | 102 |
|
| 102 | 103 |
MYFLOAT8 AtomicRungeKutta(__global MYFLOAT8* clDataIn,int gid,MYFLOAT dt) |
| ... | ... | |
| 239 | 240 |
clData[gid].s01234567 = (MYFLOAT8) (x0,y0,z0,0e0f,0e0f,0e0f,0e0f,0e0f); |
| 240 | 241 |
} |
| 241 | 242 |
|
| 242 |
__kernel void SplutterStress(__global MYFLOAT8* clData, __global MYFLOAT8* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w)
|
|
| 243 |
__kernel void SplutterStress(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w)
|
|
| 243 | 244 |
{
|
| 244 | 245 |
int gid = get_global_id(0); |
| 245 |
int N = get_global_size(0);
|
|
| 246 |
MYFLOAT N = (MYFLOAT)get_global_size(0);
|
|
| 246 | 247 |
uint z=seed_z+(uint)gid; |
| 247 | 248 |
uint w=seed_w-(uint)gid; |
| 248 | 249 |
|
| 249 |
MYFLOAT theta=MWCfp*PI; |
|
| 250 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2e0f; |
|
| 251 |
MYFLOAT sinTheta=sin(theta); |
|
| 252 |
|
|
| 253 | 250 |
if (velocity<SMALL_NUM) {
|
| 254 |
MYFLOAT4 axis=(MYFLOAT4) (1e0f,1e0f,1e0f,0e0f); |
|
| 255 |
MYFLOAT4 SpeedVector=sqrt(-AtomicPotentialCoM(clData,clCoM,gid))/(MYFLOAT)N*(MYFLOAT4)normalize(cross(clData[gid].lo,clCoM[0].lo)); |
|
| 256 |
// MYFLOAT4 SpeedVector=(MYFLOAT4)normalize(cross(clData[gid].lo,clCoM[0].lo))*sqrt(-AtomicPotentialCoM(clData,clCoM,gid))/sqrt(32e0f); |
|
| 251 |
MYFLOAT4 SpeedVector=(MYFLOAT4)normalize(cross(clData[gid].lo,clCoM[0]))*sqrt(-AtomicPotential(clData,gid)*5e-1f); |
|
| 257 | 252 |
clData[gid].hi=SpeedVector; |
| 258 | 253 |
} |
| 259 | 254 |
else |
| 260 | 255 |
{
|
| 256 |
MYFLOAT theta=MWCfp*PI; |
|
| 257 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2e0f; |
|
| 258 |
MYFLOAT sinTheta=sin(theta); |
|
| 259 |
|
|
| 261 | 260 |
clData[gid].s4=velocity*sinTheta*cos(phi); |
| 262 | 261 |
clData[gid].s5=velocity*sinTheta*sin(phi); |
| 263 | 262 |
clData[gid].s6=velocity*cos(theta); |
| ... | ... | |
| 300 | 299 |
clData[gid]=clDataGid; |
| 301 | 300 |
} |
| 302 | 301 |
|
| 303 |
__kernel void Potential(__global MYFLOAT8* clData,__global MYFLOAT8* clCoM,__global MYFLOAT* clPotential)
|
|
| 302 |
__kernel void CoMPotential(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM,__global MYFLOAT* clPotential)
|
|
| 304 | 303 |
{
|
| 305 | 304 |
int gid = get_global_id(0); |
| 306 | 305 |
|
| 307 |
clPotential[gid]=PairPotential(clData[gid].lo,clCoM[0].lo);
|
|
| 306 |
clPotential[gid]=PairPotential(clData[gid].lo,clCoM[0]); |
|
| 308 | 307 |
} |
| 309 | 308 |
|
| 310 |
__kernel void PotentialOld(__global MYFLOAT8* clData,__global MYFLOAT* clPotential)
|
|
| 309 |
__kernel void Potential(__global MYFLOAT8* clData,__global MYFLOAT* clPotential) |
|
| 311 | 310 |
{
|
| 312 | 311 |
int gid = get_global_id(0); |
| 313 | 312 |
|
| ... | ... | |
| 324 | 323 |
clPotential[gid]=(MYFLOAT)5e-1f*potential; |
| 325 | 324 |
} |
| 326 | 325 |
|
| 327 |
__kernel void CenterOfMass(__global MYFLOAT8* clData,__global MYFLOAT8* clCoM,int Size)
|
|
| 326 |
__kernel void CenterOfMass(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM,int Size)
|
|
| 328 | 327 |
{
|
| 329 | 328 |
MYFLOAT4 CoM=clData[0].lo; |
| 330 |
|
|
| 329 |
|
|
| 331 | 330 |
for (int i=1;i<Size;i++) |
| 332 | 331 |
{
|
| 333 | 332 |
CoM+=clData[i].lo; |
| 334 | 333 |
} |
| 335 |
|
|
| 334 |
|
|
| 336 | 335 |
barrier(CLK_GLOBAL_MEM_FENCE); |
| 337 |
clCoM[0]=(MYFLOAT8)(CoM.s0,CoM.s1,CoM.s2,0e0f,0e0f,0e0f,0e0f,0e0f)/(MYFLOAT)Size;
|
|
| 336 |
clCoM[0]=(MYFLOAT4)(CoM.s0,CoM.s1,CoM.s2,0e0f)/(MYFLOAT)Size;
|
|
| 338 | 337 |
} |
| 339 | 338 |
|
| 340 | 339 |
__kernel void Kinetic(__global MYFLOAT8* clData,__global MYFLOAT* clKinetic) |
| ... | ... | |
| 342 | 341 |
int gid = get_global_id(0); |
| 343 | 342 |
|
| 344 | 343 |
barrier(CLK_GLOBAL_MEM_FENCE); |
| 345 |
clKinetic[gid]=(MYFLOAT)5e-1f*(MYFLOAT)pow(distance(clData[gid].hi,(MYFLOAT4)(0e0f,0e0f,0e0f,0e0f)),2);
|
|
| 344 |
clKinetic[gid]=(MYFLOAT)5e-1f*(MYFLOAT)pow(length(clData[gid].hi),2);
|
|
| 346 | 345 |
} |
| 347 | 346 |
""" |
| 348 | 347 |
|
| ... | ... | |
| 354 | 353 |
# ValueType |
| 355 | 354 |
ValueType='FP32' |
| 356 | 355 |
class MyFloat(np.float32):pass |
| 357 |
clType=cl_array.vec.float8 |
|
| 356 |
clType8=cl_array.vec.float8 |
|
| 357 |
clType4=cl_array.vec.float4 |
|
| 358 | 358 |
# Set defaults values |
| 359 | 359 |
np.set_printoptions(precision=2) |
| 360 | 360 |
# Id of Device : 1 is for first find ! |
| ... | ... | |
| 382 | 382 |
# Viriel Distribution of stress |
| 383 | 383 |
VirielStress=True |
| 384 | 384 |
|
| 385 |
HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -g [GraphSamples] -c [CheckEnergies] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|ImplicitEuler|ExplicitEuler> -t <FP32|FP64>' |
|
| 385 |
HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -g [GraphSamples] -c [CheckEnergies] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|ImplicitEuler|ExplicitEuler|Heun> -t <FP32|FP64>'
|
|
| 386 | 386 |
|
| 387 | 387 |
try: |
| 388 | 388 |
opts, args = getopt.getopt(sys.argv[1:],"rehgcd:n:z:v:i:s:m:t:",["random","viriel","graph","check","device=","number=","size=","velocity=","iterations=","step=","method=","valuetype="]) |
| ... | ... | |
| 411 | 411 |
elif opt in ("-t", "--valuetype"):
|
| 412 | 412 |
if arg=='FP64': |
| 413 | 413 |
class MyFloat(np.float64): pass |
| 414 |
clType=cl_array.vec.double8 |
|
| 414 |
clType8=cl_array.vec.double8 |
|
| 415 |
clType4=cl_array.vec.double4 |
|
| 415 | 416 |
else: |
| 416 | 417 |
class MyFloat(np.float32):pass |
| 417 |
clType=cl_array.vec.float8 |
|
| 418 |
clType8=cl_array.vec.float8 |
|
| 419 |
clType4=cl_array.vec.float4 |
|
| 418 | 420 |
ValueType = arg |
| 419 | 421 |
elif opt in ("-d", "--device"):
|
| 420 | 422 |
Device=int(arg) |
| ... | ... | |
| 458 | 460 |
print("Viriel distribution of stress %s" % VirielStress)
|
| 459 | 461 |
|
| 460 | 462 |
# Create Numpy array of CL vector with 8 FP32 |
| 461 |
MyCoM = np.zeros(1, dtype=clType)
|
|
| 462 |
MyData = np.zeros(Number, dtype=clType) |
|
| 463 |
MyCoM = np.zeros(1,dtype=clType4)
|
|
| 464 |
MyData = np.zeros(Number, dtype=clType8)
|
|
| 463 | 465 |
MyPotential = np.zeros(Number, dtype=MyFloat) |
| 464 | 466 |
MyKinetic = np.zeros(Number, dtype=MyFloat) |
| 465 | 467 |
|
| ... | ... | |
| 493 | 495 |
# MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = "-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType])) |
| 494 | 496 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = "-DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType])) |
| 495 | 497 |
|
| 496 |
# Initial forced values for exploration |
|
| 497 |
# MyData[0][0]=np.float32(-1.) |
|
| 498 |
# MyData[0][1]=np.float32(0.) |
|
| 499 |
# MyData[0][5]=np.float32(1.) |
|
| 500 |
# MyData[1][0]=np.float32(1.) |
|
| 501 |
# MyData[1][1]=np.float32(0.) |
|
| 502 |
# MyData[1][5]=np.float32(-1.) |
|
| 503 |
|
|
| 504 | 498 |
mf = cl.mem_flags |
| 505 | 499 |
clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes) |
| 506 | 500 |
clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes) |
| ... | ... | |
| 521 | 515 |
print('All particules distributed')
|
| 522 | 516 |
|
| 523 | 517 |
MyRoutines.CenterOfMass(queue,(1,1),None,clData,clCoM,np.int32(Number)) |
| 524 |
cl.enqueue_copy(queue, MyCoM, clCoM)
|
|
| 525 |
print('Center Of Mass:',MyCoM)
|
|
| 518 |
cl.enqueue_copy(queue,MyCoM,clCoM)
|
|
| 519 |
print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0][0],MyCoM[0][1],MyCoM[0][2]))
|
|
| 526 | 520 |
|
| 527 | 521 |
if VirielStress: |
| 528 | 522 |
MyRoutines.SplutterStress(queue,(Number,1),None,clData,clCoM,np.float32(0.),np.uint32(110271),np.uint32(250173)) |
| ... | ... | |
| 541 | 535 |
#print(t0,t1,tL) |
| 542 | 536 |
#print(s0,s1,sL) |
| 543 | 537 |
|
| 544 |
#CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clCoM,clPotential) |
|
| 545 |
CLLaunch=MyRoutines.PotentialOld(queue,(Number,1),None,clData,clPotential) |
|
| 538 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential) |
|
| 546 | 539 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic) |
| 547 | 540 |
CLLaunch.wait() |
| 548 | 541 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 549 | 542 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 550 |
print(np.sum(MyPotential)/2+2*np.sum(MyKinetic),np.sum(MyPotential)/2,np.sum(MyKinetic))
|
|
| 543 |
print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 551 | 544 |
|
| 552 | 545 |
if GraphSamples: |
| 553 | 546 |
cl.enqueue_copy(queue, MyData, clData) |
| ... | ... | |
| 567 | 560 |
CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clData,Step) |
| 568 | 561 |
CLLaunch.wait() |
| 569 | 562 |
if CheckEnergies: |
| 570 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clCoM,clPotential)
|
|
| 563 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential) |
|
| 571 | 564 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic) |
| 572 | 565 |
CLLaunch.wait() |
| 573 | 566 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| ... | ... | |
| 583 | 576 |
tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]]) |
| 584 | 577 |
print("\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Iterations))
|
| 585 | 578 |
|
| 586 |
#CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clCoM,clPotential) |
|
| 587 |
CLLaunch=MyRoutines.PotentialOld(queue,(Number,1),None,clData,clPotential) |
|
| 579 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential) |
|
| 588 | 580 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic) |
| 589 | 581 |
CLLaunch.wait() |
| 590 | 582 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 591 | 583 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 592 |
print(np.sum(MyPotential)/2+2*np.sum(MyKinetic),np.sum(MyPotential)/2,np.sum(MyKinetic)) |
|
| 584 |
print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 585 |
MyRoutines.CenterOfMass(queue,(1,1),None,clData,clCoM,np.int32(Number)) |
|
| 586 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
|
| 587 |
print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0][0],MyCoM[0][1],MyCoM[0][2]))
|
|
| 593 | 588 |
|
| 594 | 589 |
if GraphSamples: |
| 595 | 590 |
t0=np.transpose(np.reshape(t0,(Iterations+1,3))) |
Formats disponibles : Unified diff