Révision 168 NBody/NBodyGL.py
| NBodyGL.py (revision 168) | ||
|---|---|---|
| 317 | 317 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,0.e0f,vf.s0,vf.s1,vf.s2,0.e0f)); |
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 |
__kernel void SplutterPoints(__global MYFLOAT4* clDataX, MYFLOAT box,
|
|
| 320 |
__kernel void InBallSplutterPoints(__global MYFLOAT4* clDataX, MYFLOAT radius,
|
|
| 321 | 321 |
uint seed_z,uint seed_w) |
| 322 | 322 |
{
|
| 323 |
int gid=get_global_id(0); |
|
| 324 |
uint zmwc=seed_z+gid; |
|
| 325 |
uint wmwc=seed_w-gid;
|
|
| 326 |
private MYFLOAT Heat; |
|
| 323 |
private int gid=get_global_id(0);
|
|
| 324 |
private uint zmwc=seed_z+gid;
|
|
| 325 |
private uint wmwc=seed_w+(gid+1)%2;
|
|
| 326 |
private MYFLOAT Heat,Radius,Theta,Phi,PosX,PosY,PosZ,SinTheta;
|
|
| 327 | 327 |
|
| 328 | 328 |
for (int i=0;i<gid;i++) |
| 329 | 329 |
{
|
| ... | ... | |
| 331 | 331 |
} |
| 332 | 332 |
|
| 333 | 333 |
// More accurate distribution based on spherical coordonates |
| 334 |
// Disactivated because of AMD Oland GPU problems
|
|
| 335 |
// MYFLOAT Radius=Heat*box;
|
|
| 336 |
// MYFLOAT Theta=acos((float)(-2.e0f*MWCfp+1.0e0f));
|
|
| 337 |
// MYFLOAT Phi=(MYFLOAT)2.e0f*PI*MWCfp;
|
|
| 338 |
// MYFLOAT sinTheta=sin((float)Theta);
|
|
| 339 |
// MYFLOAT PosX=cos((float)Phi)*Radius*sinTheta;
|
|
| 340 |
// MYFLOAT PosY=sin((float)Phi)*Radius*sinTheta;
|
|
| 341 |
// MYFLOAT PosZ=cos((float)Theta)*Radius;
|
|
| 334 |
// Disactivated because of AMD Oland GPU crash on launch
|
|
| 335 |
// Radius=MWCfp*radius;
|
|
| 336 |
// Theta=(MYFLOAT)acos((float)(-2.e0f*MWCfp+1.0e0f));
|
|
| 337 |
// Phi=(MYFLOAT)(2.e0f*PI*MWCfp);
|
|
| 338 |
// SinTheta=sin((float)Theta);
|
|
| 339 |
// PosX=cos((float)Phi)*Radius*SinTheta;
|
|
| 340 |
// PosY=sin((float)Phi)*Radius*SinTheta;
|
|
| 341 |
// PosZ=cos((float)Theta)*Radius; |
|
| 342 | 342 |
// clDataX[gid]=(MYFLOAT4)(PosX,PosY,PosZ,0.e0f); |
| 343 | 343 |
|
| 344 |
MYFLOAT4 Position=(MYFLOAT4)((Heat-0.5e0f)*box,(MWCfp-0.5e0f)*box,(MWCfp-0.5e0f)*box,0.e0f);
|
|
| 344 |
MYFLOAT4 Position=(MYFLOAT4)((MWCfp-0.5e0f)*radius,(MWCfp-0.5e0f)*radius,(MWCfp-0.5e0f)*radius,0.e0f);
|
|
| 345 | 345 |
MYFLOAT Length=(MYFLOAT)length((MYFLOAT4)Position); |
| 346 |
clDataX[gid]=Position/Length*box*MWCfp;
|
|
| 346 |
clDataX[gid]=Position/Length*fmod(radius,Length);
|
|
| 347 | 347 |
|
| 348 | 348 |
barrier(CLK_GLOBAL_MEM_FENCE); |
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 |
__kernel void InBoxSplutterPoints(__global MYFLOAT4* clDataX, MYFLOAT box, |
|
| 352 |
uint seed_z,uint seed_w) |
|
| 353 |
{
|
|
| 354 |
int gid=get_global_id(0); |
|
| 355 |
uint zmwc=seed_z+gid; |
|
| 356 |
uint wmwc=seed_w-gid; |
|
| 357 |
private MYFLOAT Heat; |
|
| 358 |
|
|
| 359 |
for (int i=0;i<gid;i++) |
|
| 360 |
{
|
|
| 361 |
Heat=MWCfp; |
|
| 362 |
} |
|
| 363 |
|
|
| 364 |
clDataX[gid]=(MYFLOAT4)((MWCfp-0.5e0f)*box,(MWCfp-0.5e0f)*box,(MWCfp-0.5e0f)*box,0.e0f); |
|
| 365 |
|
|
| 366 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 367 |
} |
|
| 368 |
|
|
| 351 | 369 |
__kernel void SplutterStress(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,__global MYFLOAT4* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w) |
| 352 | 370 |
{
|
| 353 | 371 |
int gid = get_global_id(0); |
| ... | ... | |
| 566 | 584 |
|
| 567 | 585 |
if __name__=='__main__': |
| 568 | 586 |
|
| 569 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox,Iterations,Verbose |
|
| 587 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox,Iterations,Verbose,Durations
|
|
| 570 | 588 |
|
| 571 | 589 |
# ValueType |
| 572 | 590 |
ValueType='FP32' |
| ... | ... | |
| 578 | 596 |
Device=0 |
| 579 | 597 |
# Number of bodies is integer |
| 580 | 598 |
Number=2 |
| 581 |
# Size of box |
|
| 582 |
SizeOfBall=MyFloat(1.) |
|
| 599 |
# Number of iterations (for standalone execution) |
|
| 600 |
Iterations=100 |
|
| 601 |
# Size of shape |
|
| 602 |
SizeOfShape=MyFloat(1.) |
|
| 583 | 603 |
# Initial velocity of particules |
| 584 | 604 |
Velocity=MyFloat(1.) |
| 585 | 605 |
# Step |
| 586 |
Step=MyFloat(1./256)
|
|
| 606 |
Step=MyFloat(1./32)
|
|
| 587 | 607 |
# Method of integration |
| 588 | 608 |
Method='ImplicitEuler' |
| 589 | 609 |
# InitialRandom |
| ... | ... | |
| 596 | 616 |
Verbose=False |
| 597 | 617 |
# OpenGL demo |
| 598 | 618 |
OpenGL=False |
| 619 |
# Shape to distribute |
|
| 620 |
Shape='Box' |
|
| 599 | 621 |
|
| 600 |
HowToUse='%s -h [Help] -r [InitialRandom] -g [OpenGL] -e [VirielStress] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBall> -v <Velocity> -s <Step> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>'
|
|
| 622 |
HowToUse='%s -h [Help] -r [InitialRandom] -g [OpenGL] -e [VirielStress] -d <DeviceId> -n <NumberOfParticules> -i <Iterations> -z <SizeOfBoxOrBall> -v <Velocity> -s <Step> -b <Ball|Box> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>'
|
|
| 601 | 623 |
|
| 602 | 624 |
try: |
| 603 |
opts, args = getopt.getopt(sys.argv[1:],"rgehd:n:z:v:s:m:t:",["random","viriel","device=","number=","size=","velocity=","step=","method=","valuetype="])
|
|
| 625 |
opts, args = getopt.getopt(sys.argv[1:],"rgehd:n:i:z:v:s:m:t:b:",["random","opengl","viriel","device=","number=","iterations=","size=","velocity=","step=","method=","valuetype=","shape="])
|
|
| 604 | 626 |
except getopt.GetoptError: |
| 605 | 627 |
print(HowToUse % sys.argv[0]) |
| 606 | 628 |
sys.exit(2) |
| ... | ... | |
| 614 | 636 |
Id=0 |
| 615 | 637 |
for platform in cl.get_platforms(): |
| 616 | 638 |
for device in platform.get_devices(): |
| 639 |
# Failed now because of POCL implementation |
|
| 617 | 640 |
#deviceType=cl.device_type.to_string(device.type) |
| 618 | 641 |
deviceType="xPU" |
| 619 | 642 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
|
| ... | ... | |
| 626 | 649 |
elif opt in ("-t", "--valuetype"):
|
| 627 | 650 |
if arg=='FP64': |
| 628 | 651 |
class MyFloat(np.float64): pass |
| 629 |
clType4=cl_array.vec.double4 |
|
| 630 | 652 |
else: |
| 631 | 653 |
class MyFloat(np.float32):pass |
| 632 |
clType4=cl_array.vec.float4 |
|
| 633 | 654 |
ValueType = arg |
| 634 | 655 |
elif opt in ("-d", "--device"):
|
| 635 | 656 |
Device=int(arg) |
| 636 | 657 |
elif opt in ("-m", "--method"):
|
| 637 | 658 |
Method=arg |
| 659 |
elif opt in ("-b", "--shape"):
|
|
| 660 |
Shape=arg |
|
| 638 | 661 |
elif opt in ("-n", "--number"):
|
| 639 | 662 |
Number=int(arg) |
| 663 |
elif opt in ("-i", "--iterations"):
|
|
| 664 |
Iterations=int(arg) |
|
| 640 | 665 |
elif opt in ("-z", "--size"):
|
| 641 |
SizeOfBall=MyFloat(arg)
|
|
| 666 |
SizeOfShape=MyFloat(arg)
|
|
| 642 | 667 |
elif opt in ("-v", "--velocity"):
|
| 643 | 668 |
Velocity=MyFloat(arg) |
| 644 | 669 |
VirielStress=False |
| ... | ... | |
| 653 | 678 |
elif opt in ("-g", "--opengl"):
|
| 654 | 679 |
OpenGL=True |
| 655 | 680 |
|
| 656 |
SizeOfBall=MyFloat(SizeOfBall*Number/2.)
|
|
| 681 |
SizeOfShape=MyFloat(SizeOfShape*Number)
|
|
| 657 | 682 |
Velocity=MyFloat(Velocity) |
| 658 | 683 |
Step=MyFloat(Step) |
| 659 | 684 |
|
| 660 | 685 |
print("Device choosed : %s" % Device)
|
| 661 | 686 |
print("Number of particules : %s" % Number)
|
| 662 |
print("Size of Ball : %s" % SizeOfBall)
|
|
| 687 |
print("Size of Shape : %s" % SizeOfShape)
|
|
| 663 | 688 |
print("Initial velocity : %s" % Velocity)
|
| 664 | 689 |
print("Step of iteration : %s" % Step)
|
| 690 |
print("Number of iterations : %s" % Iterations)
|
|
| 665 | 691 |
print("Method of resolution : %s" % Method)
|
| 666 | 692 |
print("Initial Random for RNG Seed : %s" % InitialRandom)
|
| 667 | 693 |
print("ValueType is : %s" % ValueType)
|
| 668 |
print("Viriel distribution of stress %s" % VirielStress)
|
|
| 694 |
print("Viriel distribution of stress : %s" % VirielStress)
|
|
| 669 | 695 |
|
| 670 | 696 |
# Create Numpy array of CL vector with 8 FP32 |
| 671 | 697 |
MyCoM = np.zeros(4,dtype=MyFloat) |
| ... | ... | |
| 712 | 738 |
|
| 713 | 739 |
mf = cl.mem_flags |
| 714 | 740 |
# Read/Write approach for buffering |
| 715 |
# clDataX = cl.Buffer(ctx, mf.READ_WRITE, MyDataX.nbytes)
|
|
| 716 |
# clDataV = cl.Buffer(ctx, mf.READ_WRITE, MyDataV.nbytes)
|
|
| 717 |
# clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes)
|
|
| 718 |
# clKinetic = cl.Buffer(ctx, mf.READ_WRITE, MyKinetic.nbytes)
|
|
| 719 |
# clCoM = cl.Buffer(ctx, mf.READ_WRITE, MyCoM.nbytes)
|
|
| 741 |
clDataX = cl.Buffer(ctx, mf.READ_WRITE, MyDataX.nbytes) |
|
| 742 |
clDataV = cl.Buffer(ctx, mf.READ_WRITE, MyDataV.nbytes) |
|
| 743 |
clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes) |
|
| 744 |
clKinetic = cl.Buffer(ctx, mf.READ_WRITE, MyKinetic.nbytes) |
|
| 745 |
clCoM = cl.Buffer(ctx, mf.READ_WRITE, MyCoM.nbytes) |
|
| 720 | 746 |
|
| 721 | 747 |
# Write/HostPointer approach for buffering |
| 722 |
clDataX = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataX) |
|
| 723 |
clDataV = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataV) |
|
| 724 |
clPotential = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyPotential) |
|
| 725 |
clKinetic = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyKinetic) |
|
| 726 |
clCoM = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyCoM) |
|
| 748 |
# clDataX = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataX)
|
|
| 749 |
# clDataV = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataV)
|
|
| 750 |
# clPotential = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyPotential)
|
|
| 751 |
# clKinetic = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyKinetic)
|
|
| 752 |
# clCoM = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyCoM)
|
|
| 727 | 753 |
|
| 728 | 754 |
print('All particles superimposed.')
|
| 729 | 755 |
|
| 730 | 756 |
# Set particles to RNG points |
| 731 | 757 |
if InitialRandom: |
| 732 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
|
| 758 |
seed_w=np.uint32(nprnd(2**32)) |
|
| 759 |
seed_z=np.uint32(nprnd(2**32)) |
|
| 733 | 760 |
else: |
| 734 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(110271),np.uint32(250173)) |
|
| 761 |
seed_w=np.uint32(19710211) |
|
| 762 |
seed_z=np.uint32(20081010) |
|
| 763 |
|
|
| 764 |
if Shape=='Ball': |
|
| 765 |
MyRoutines.InBallSplutterPoints(queue,(Number,1),None,clDataX,SizeOfShape,seed_w,seed_z) |
|
| 766 |
else: |
|
| 767 |
MyRoutines.InBoxSplutterPoints(queue,(Number,1),None,clDataX,SizeOfShape,seed_w,seed_z) |
|
| 735 | 768 |
|
| 736 | 769 |
print('All particules distributed')
|
| 737 | 770 |
|
| 738 | 771 |
CLLaunch=MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 739 | 772 |
CLLaunch.wait() |
| 740 | 773 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 741 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 774 |
print('Center Of Mass estimated: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 742 | 775 |
|
| 743 | 776 |
if VirielStress: |
| 744 | 777 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,MyFloat(0.),np.uint32(110271),np.uint32(250173)) |
| ... | ... | |
| 746 | 779 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,Velocity,np.uint32(110271),np.uint32(250173)) |
| 747 | 780 |
CLLaunch.wait() |
| 748 | 781 |
|
| 782 |
print('All particules stressed')
|
|
| 783 |
|
|
| 749 | 784 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
| 750 | 785 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
| 751 | 786 |
CLLaunch.wait() |
| 752 | 787 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 753 | 788 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 754 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 789 |
print('Energy estimated: Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 755 | 790 |
|
| 756 | 791 |
wall_time_start=time.time() |
| 757 | 792 |
|
| 758 | 793 |
# Launch OpenGL Loop |
| 759 |
global SizeOfBox,ViewRX,ViewRY,ViewRZ,Iterations,Durations
|
|
| 794 |
global SizeOfBox,ViewRX,ViewRY,ViewRZ |
|
| 760 | 795 |
ViewRX,ViewRY,ViewRZ = 0.,0.,0. |
| 761 |
SizeOfBox=SizeOfBall
|
|
| 796 |
SizeOfBox=SizeOfShape
|
|
| 762 | 797 |
Iterations=0 |
| 763 | 798 |
Durations=np.array([],dtype=MyFloat) |
| 764 | 799 |
glutInit(sys.argv) |
| ... | ... | |
| 783 | 818 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 784 | 819 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 785 | 820 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 786 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 787 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 821 |
print('\nCenter Of Mass estimated: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 822 |
print('Energy estimated: Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 788 | 823 |
|
| 789 |
print("\nDuration stats on device %s with %s iterations :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n\n\tVariability:\t%s\n" % (Device,Iterations,np.mean(Durations),np.median(Durations),np.std(Durations),np.min(Durations),np.max(Durations),np.std(Durations)/np.median(Durations)))
|
|
| 824 |
print("Duration stats on device %s with %s iterations :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n\n\tVariability:\t%s\n" % (Device,Iterations,np.mean(Durations),np.median(Durations),np.std(Durations),np.min(Durations),np.max(Durations),np.std(Durations)/np.median(Durations)))
|
|
| 790 | 825 |
|
| 791 | 826 |
# Contraction of Square*Size*Hertz: Size*Size/Elapsed |
| 792 | 827 |
Squertz=np.ones(len(Durations)) |
Formats disponibles : Unified diff