Révision 169 NBody/NBodyGL.py

NBodyGL.py (revision 169)
23 23

  
24 24

  
25 25

  
26
Blob="""
27
__kernel void RungeKutta(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
28
{
29
    private int gid = get_global_id(0);
30
    private MYFLOAT8 clDataGid;
31
    
32
    clDataGid=AtomicRungeKutta(clDataX,clDataV,gid,h);
33
    barrier(CLK_GLOBAL_MEM_FENCE);
34
    clDataX[gid]=clDataGid.s0123;
35
    clDataV[gid]=clDataGid.s4567;
36
}
37 26

  
38
"""
39 27

  
40 28

  
41 29

  
......
96 84

  
97 85

  
98 86

  
99

  
100 87
BlobOpenCL= """
101 88
#define TFP32 0
102 89
#define TFP64 1
......
486 473

  
487 474
"""
488 475

  
489
def display(*args):
490

  
491
    global MyDataX,MyDataV,clDataX,clDataV,Step,Method,Number,Iterations,Durations,Verbose
492
    
493
    glClearColor(0.0, 0.0, 0.0, 0.0)
494
    glClear(GL_COLOR_BUFFER_BIT)
495
    glColor3f(1.0,1.0,1.0)
496
    
476
def MainOpenCL(clDataX,clDataV,Step,Method):
497 477
    time_start=time.time()
498 478
    if Method=="RungeKutta":
499 479
        CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clDataX,clDataV,Step)
......
505 485
        CLLaunch=MyRoutines.ImplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step)
506 486
    CLLaunch.wait()
507 487
    Elapsed=time.time()-time_start
508
    Durations=np.append(Durations,Elapsed)
488
    return(Elapsed)
489
    
490
def display(*args):
491

  
492
    global MyDataX,MyDataV,clDataX,clDataV,Step,Method,Number,Iterations,Durations,Verbose,SpeedRendering
493
    
494
    glClearColor(0.0, 0.0, 0.0, 0.0)
495
    glClear(GL_COLOR_BUFFER_BIT)
496
    glColor3f(1.0,1.0,1.0)
497
    
498
    Elapsed=MainOpenCL(clDataX,clDataV,Step,Method)
499
    if SpeedRendering:
500
        cl.enqueue_copy(queue, MyDataV, clDataV)
501
        MyDataV.reshape(Number,4)[:,3]=1
502
        glVertexPointerf(MyDataV.reshape(Number,4))
503
    else:
504
        cl.enqueue_copy(queue, MyDataX, clDataX)
505
        MyDataX.reshape(Number,4)[:,3]=1
506
        glVertexPointerf(MyDataX.reshape(Number,4))
507

  
509 508
    if Verbose:
510 509
        print("Duration of #%s iteration: %s" % (Iterations,Elapsed))
511 510
    else:
512 511
        sys.stdout.write('.')
513 512
        sys.stdout.flush()
514
    cl.enqueue_copy(queue, MyDataX, clDataX)
515
    # print(MyDataX.reshape(Number,4))
516
    MyDataX.reshape(Number,4)[:,3]=1
517
    glVertexPointerf(MyDataX.reshape(Number,4))
518
    # cl.enqueue_copy(queue, MyDataV, clDataV)
519
    # print(MyDataV.reshape(Number,4))
520
    # MyDataV.reshape(Number,4)[:,3]=1
521
    # glVertexPointerf(MyDataV.reshape(Number,4))
513
    Durations=np.append(Durations,MainOpenCL(clDataX,clDataV,Step,Method))    
522 514
    glEnableClientState(GL_VERTEX_ARRAY)
523 515
    glDrawArrays(GL_POINTS, 0, Number)
524 516
    glDisableClientState(GL_VERTEX_ARRAY)
......
530 522
    pass
531 523

  
532 524
def keyboard(k,x,y):
533
    global ViewRZ,SizeOfBox
525
    global ViewRZ,SizeOfBox,SpeedRendering
534 526
    LC_Z = as_8_bit( 'z' )
535 527
    UC_Z = as_8_bit( 'Z' )
536 528
    Plus = as_8_bit( '+' )
537 529
    Minus = as_8_bit( '-' )
530
    Switch = as_8_bit( 's' )
538 531

  
539 532
    Zoom=1
540 533
    if k == LC_Z:
......
545 538
        Zoom *= 2.0
546 539
    elif k == Minus:
547 540
        Zoom /= 2.0
541
    elif k == Switch:
542
        if SpeedRendering:
543
            SpeedRendering=False
544
        else:
545
            SpeedRendering=True
548 546
    elif ord(k) == 27: # Escape
549 547
        glutLeaveMainLoop()
550 548
        return(False)
......
614 612
    VirielStress=True
615 613
    # Verbose
616 614
    Verbose=False
617
    # OpenGL demo
615
    # OpenGL real time rendering
618 616
    OpenGL=False
617
    # Speed rendering
618
    SpeedRendering=False
619 619
    # Shape to distribute
620 620
    Shape='Box'
621 621
    
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>'
622
    HowToUse='%s -h [Help] -r [InitialRandom] -g [OpenGL] -e [VirielStress] -d <DeviceId> -p [SpeedRendering] -n <NumberOfParticules> -i <Iterations> -z <SizeOfBoxOrBall> -v <Velocity> -s <Step> -b <Ball|Box> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>'
623 623

  
624 624
    try:
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="])
625
        opts, args = getopt.getopt(sys.argv[1:],"rpgehd:n:i:z:v:s:m:t:b:",["random","rendering","opengl","viriel","device=","number=","iterations=","size=","velocity=","step=","method=","valuetype=","shape="])
626 626
    except getopt.GetoptError:
627 627
        print(HowToUse % sys.argv[0])
628 628
        sys.exit(2)
......
677 677
            VirielStress=True
678 678
        elif opt in ("-g", "--opengl"):
679 679
            OpenGL=True
680
        elif opt in ("-p", "--rendering"):
681
            SpeedRendering=True
680 682
                        
681 683
    SizeOfShape=MyFloat(SizeOfShape*Number)
682 684
    Velocity=MyFloat(Velocity)
......
692 694
    print("Initial Random for RNG Seed : %s" % InitialRandom)
693 695
    print("ValueType is : %s" % ValueType)
694 696
    print("Viriel distribution of stress : %s" % VirielStress)
697
    print("OpenGL real time rendering : %s" % OpenGL)
698
    print("Speed rendering : %s" % SpeedRendering)
695 699

  
696 700
    # Create Numpy array of CL vector with 8 FP32    
697 701
    MyCoM = np.zeros(4,dtype=MyFloat)
......
787 791
    cl.enqueue_copy(queue,MyPotential,clPotential)
788 792
    cl.enqueue_copy(queue,MyKinetic,clKinetic)
789 793
    print('Energy estimated: Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
790
 
794

  
795
    if SpeedRendering:
796
        SizeOfBox=max(2*MyKinetic)
797
    else:
798
        SizeOfBox=SizeOfShape        
799
    
791 800
    wall_time_start=time.time()
792 801

  
793
    # Launch OpenGL Loop
794
    global SizeOfBox,ViewRX,ViewRY,ViewRZ
795
    ViewRX,ViewRY,ViewRZ = 0.,0.,0.
796
    SizeOfBox=SizeOfShape
797
    Iterations=0
798 802
    Durations=np.array([],dtype=MyFloat)
799
    glutInit(sys.argv)
800
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
801
    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION)
802
    glutSetOption(GLUT_ACTION_GLUTMAINLOOP_RETURNS,GLUT_ACTION_CONTINUE_EXECUTION) 
803
    glutInitWindowSize(512,512)
804
    glutCreateWindow(b'NBodyGL')
805
    setup_viewport()
806
    glutReshapeFunc(reshape)
807
    glutDisplayFunc(display)
808
    glutIdleFunc(display)
809
#   glutMouseFunc(mouse)
810
    glutSpecialFunc(special)
811
    glutKeyboardFunc(keyboard)
812
    glutMainLoop()
813
    
803
    print('Starting!')
804
    if OpenGL:
805
        global SizeOfBox,ViewRX,ViewRY,ViewRZ
806
        Iterations=0
807
        ViewRX,ViewRY,ViewRZ = 0.,0.,0.
808
        # Launch OpenGL Loop
809
        glutInit(sys.argv)
810
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
811
        glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION)
812
        glutInitWindowSize(512,512)
813
        glutCreateWindow(b'NBodyGL')
814
        setup_viewport()
815
        glutReshapeFunc(reshape)
816
        glutDisplayFunc(display)
817
        glutIdleFunc(display)
818
        #   glutMouseFunc(mouse)
819
        glutSpecialFunc(special)
820
        glutKeyboardFunc(keyboard)
821
        glutMainLoop()
822
    else:
823
        for iteration in range(Iterations):
824
            Elapsed=MainOpenCL(clDataX,clDataV,Step,Method)
825
            if Verbose:
826
                print("Duration of #%s iteration: %s" % (Iterations,Elapsed))
827
            else:
828
                sys.stdout.write('.')
829
                sys.stdout.flush()
830
            Durations=np.append(Durations,Elapsed)
831

  
832
    print('\nEnding!')
833
            
814 834
    MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number))
815 835
    CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential)
816 836
    CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic)

Formats disponibles : Unified diff