Révision 127

Pi/XPU/PiXPU.py (revision 127)
1
#!/usr/bin/env python
1
#!/usr/bin/env python3
2 2

  
3 3
#
4 4
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL
......
39 39
        self.Max=numpy.max(Data)
40 40
        self.Min=numpy.min(Data)
41 41
    def display(self):
42
        print "%s %s %s %s %s" % (self.Avg,self.Med,self.Std,self.Min,self.Max)
42
        print("%s %s %s %s %s" % (self.Avg,self.Med,self.Std,self.Min,self.Max))
43 43

  
44 44
class Experience:
45 45
    """Metrology for experiences"""
......
63 63
    def Metrology(self,Data):
64 64
        Duration=PenStacle(Data)
65 65
        Rate=PenStacle(Iterations/Data)
66
        print "Duration %s" % Duration
67
        print "Rate %s" % Rate
66
        print("Duration %s" % Duration)
67
        print("Rate %s" % Rate)
68 68

  
69 69

  
70 70
        
......
401 401
    
402 402
def MetropolisCuda(InputCU):
403 403

  
404
    print "Inside ",InputCU
404
    print("Inside ",InputCU)
405 405
    
406 406
    iterations=InputCU['Iterations']
407 407
    steps=InputCU['Steps']
......
422 422
        for Id in range(cuda.Device.count()):
423 423
            if Id==Device:
424 424
                XPU=cuda.Device(Id)
425
                print "GPU selected %s" % (XPU.name())
425
                print("GPU selected %s" % XPU.name())
426 426
        print
427 427

  
428 428
#        import pycuda.driver as cuda
......
430 430
#        import pycuda.autoinit
431 431

  
432 432
    except ImportError:
433
        print "Platform does not seem to support CUDA"
433
        print("Platform does not seem to support CUDA")
434 434

  
435 435
    Context=XPU.make_context()
436 436
    circle=numpy.zeros(blocks*threads).astype(numpy.uint64)  
......
441 441
    try:
442 442
        mod = SourceModule(KernelCodeCuda(),options=['--compiler-options','-DTRNG=%i -DTYPE=%s' % (Marsaglia[RNG],Computing[ValueType])])
443 443
    except:
444
        print "Compilation seems to broke"
444
        print("Compilation seems to broke")
445 445
  
446 446
    MetropolisBlocksCU=mod.get_function("MainLoopBlocks")
447 447
    MetropolisThreadsCU=mod.get_function("MainLoopThreads")
......
465 465
                               numpy.uint32(nprnd(2**32)),
466 466
                               grid=(blocks,1),block=(threads,1,1))
467 467
        except:
468
            print "Crash during CUDA call"
468
            print("Crash during CUDA call")
469 469

  
470 470
        elapsed = time.time()-start_time
471
        print "(Blocks/Threads)=(%i,%i) method done in %.2f s..." % (blocks,threads,elapsed)
471
        print("(Blocks/Threads)=(%i,%i) method done in %.2f s..." % (blocks,threads,elapsed))
472 472

  
473 473
        MyDuration[i]=elapsed
474 474

  
475 475
    OutputCU={'Inside':sum(circle),'NewIterations':numpy.uint64(iterationsCU*jobs),'Duration':MyDuration}
476
    print OutputCU
476
    print(OutputCU)
477 477
    
478 478
    Context.detach()
479 479
    return(OutputCU)
......
482 482

  
483 483
    import pyopencl as cl
484 484

  
485
    print "Inside ",InputCL
485
    print("Inside ",InputCL)
486 486
    
487 487
    iterations=InputCL['Iterations']
488 488
    steps=InputCL['Steps']
......
501 501
        for device in platform.get_devices():
502 502
            if Id==Device:
503 503
                XPU=device
504
                print "CPU/GPU selected: ",device.name.lstrip()
504
                print("CPU/GPU selected: ",device.name.lstrip())
505 505
                HasXPU=True
506 506
            Id+=1
507 507

  
508 508
    if HasXPU==False:
509
        print "No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1)
509
        print("No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1))
510 510
        sys.exit()              
511 511

  
512 512
    # Je cree le contexte et la queue pour son execution
......
514 514
        ctx = cl.Context([XPU])
515 515
        queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE)
516 516
    except:
517
        print "Crash during context creation"
517
        print("Crash during context creation")
518 518

  
519 519
    # Je recupere les flag possibles pour les buffers
520 520
    mf = cl.mem_flags
......
551 551
        cl.enqueue_copy(queue, circle, circleCL).wait()
552 552

  
553 553
        elapsed = time.time()-start_time
554
        print "(Blocks/Threads)=(%i,%i) method done in %.2f s..." % (blocks,threads,elapsed)
554
        print("(Blocks/Threads)=(%i,%i) method done in %.2f s..." % (blocks,threads,elapsed))
555 555
            
556 556
        # Elapsed method based on CLLaunch doesn't work for Beignet OpenCL
557 557
        # elapsed = 1e-9*(CLLaunch.profile.end - CLLaunch.profile.start)
......
565 565
    circleCL.release()
566 566

  
567 567
    OutputCL={'Inside':sum(circle),'NewIterations':numpy.uint64(iterationsCL*jobs),'Duration':MyDuration}
568
    print OutputCL
568
    print(OutputCL)
569 569
    return(OutputCL)
570 570

  
571 571

  
......
581 581
        coeffs_Amdahl[1]=coeffs_Amdahl[1]*coeffs_Amdahl[0]/D[0]
582 582
        coeffs_Amdahl[2]=coeffs_Amdahl[2]*coeffs_Amdahl[0]/D[0]
583 583
        coeffs_Amdahl[0]=D[0]
584
        print "Amdahl Normalized: T=%.2f(%.6f+%.6f/N)" % \
585
            (coeffs_Amdahl[0],coeffs_Amdahl[1],coeffs_Amdahl[2])
584
        print("Amdahl Normalized: T=%.2f(%.6f+%.6f/N)" % (coeffs_Amdahl[0],coeffs_Amdahl[1],coeffs_Amdahl[2]))
586 585
    except:
587
        print "Impossible to fit for Amdahl law : only %i elements" % len(D) 
586
        print("Impossible to fit for Amdahl law : only %i elements" % len(D))
588 587

  
589 588
    try:
590 589
        coeffs_AmdahlR, matcov_AmdahlR = curve_fit(AmdahlR, N, D)
......
592 591
        D_AmdahlR=AmdahlR(N,coeffs_AmdahlR[0],coeffs_AmdahlR[1])
593 592
        coeffs_AmdahlR[1]=coeffs_AmdahlR[1]*coeffs_AmdahlR[0]/D[0]
594 593
        coeffs_AmdahlR[0]=D[0]
595
        print "Amdahl Reduced Normalized: T=%.2f(%.6f+%.6f/N)" % \
596
            (coeffs_AmdahlR[0],1-coeffs_AmdahlR[1],coeffs_AmdahlR[1])
594
        print("Amdahl Reduced Normalized: T=%.2f(%.6f+%.6f/N)" % (coeffs_AmdahlR[0],1-coeffs_AmdahlR[1],coeffs_AmdahlR[1]))
597 595

  
598 596
    except:
599
        print "Impossible to fit for Reduced Amdahl law : only %i elements" \
600
            % len(D) 
597
        print("Impossible to fit for Reduced Amdahl law : only %i elements" % len(D))
601 598

  
602 599
    try:
603 600
        coeffs_Mylq, matcov_Mylq = curve_fit(Mylq, N, D)
......
606 603
        # coeffs_Mylq[2]=coeffs_Mylq[2]*coeffs_Mylq[0]/D[0]
607 604
        coeffs_Mylq[3]=coeffs_Mylq[3]*coeffs_Mylq[0]/D[0]
608 605
        coeffs_Mylq[0]=D[0]
609
        print "Mylq Normalized : T=%.2f(%.6f+%.6f/N)+%.6f*N" % (coeffs_Mylq[0],
606
        print("Mylq Normalized : T=%.2f(%.6f+%.6f/N)+%.6f*N" % (coeffs_Mylq[0],
610 607
                                                                coeffs_Mylq[1],
611 608
                                                                coeffs_Mylq[3],
612
                                                                coeffs_Mylq[2])
609
                                                                coeffs_Mylq[2]))
613 610
        D_Mylq=Mylq(N,coeffs_Mylq[0],coeffs_Mylq[1],coeffs_Mylq[2],
614 611
                    coeffs_Mylq[3])
615 612
    except:
616
        print "Impossible to fit for Mylq law : only %i elements" % len(D) 
613
        print("Impossible to fit for Mylq law : only %i elements" % len(D))
617 614

  
618 615
    try:
619 616
        coeffs_Mylq2, matcov_Mylq2 = curve_fit(Mylq2, N, D)
......
623 620
        # coeffs_Mylq2[3]=coeffs_Mylq2[3]*coeffs_Mylq2[0]/D[0]
624 621
        coeffs_Mylq2[4]=coeffs_Mylq2[4]*coeffs_Mylq2[0]/D[0]
625 622
        coeffs_Mylq2[0]=D[0]
626
        print "Mylq 2nd order Normalized: T=%.2f(%.6f+%.6f/N)+%.6f*N+%.6f*N^2" % \
627
            (coeffs_Mylq2[0],coeffs_Mylq2[1],
628
             coeffs_Mylq2[4],coeffs_Mylq2[2],coeffs_Mylq2[3])
623
        print("Mylq 2nd order Normalized: T=%.2f(%.6f+%.6f/N)+%.6f*N+%.6f*N^2" % (coeffs_Mylq2[0],coeffs_Mylq2[1],coeffs_Mylq2[4],coeffs_Mylq2[2],coeffs_Mylq2[3]))
629 624

  
630 625
    except:
631
        print "Impossible to fit for 2nd order Mylq law : only %i elements" % len(D) 
626
        print("Impossible to fit for 2nd order Mylq law : only %i elements" % len(D))
632 627

  
633 628
    if Curves:
634 629
        plt.xlabel("Number of Threads/work Items")
......
639 634
        pAmdahl,=plt.plot(N,D_Amdahl,label="Loi de Amdahl")    
640 635
        pMylq,=plt.plot(N,D_Mylq,label="Loi de Mylq")
641 636
    except:
642
        print "Fit curves seem not to be available"
637
        print("Fit curves seem not to be available")
643 638

  
644 639
    plt.legend()
645 640
    plt.show()
......
685 680
    try:
686 681
        opts, args = getopt.getopt(sys.argv[1:],"hocg:i:b:e:s:f:l:t:r:d:m:v:",["gpustyle=","iterations=","blocksBegin=","blocksEnd=","blocksStep=","threadsFirst=","threadsLast=","threadssTep=","redo=","device=","marsaglia=","valuetype="])
687 682
    except getopt.GetoptError:
688
        print HowToUse % sys.argv[0]
683
        print(HowToUse % sys.argv[0])
689 684
        sys.exit(2)
690 685

  
691 686
    # List of Devices
......
694 689
        
695 690
    for opt, arg in opts:
696 691
        if opt == '-h':
697
            print HowToUse % sys.argv[0]
692
            print(HowToUse % sys.argv[0])
698 693

  
699
            print "\nInformations about devices detected under OpenCL API:"
694
            print("\nInformations about devices detected under OpenCL API:")
700 695
            # For PyOpenCL import
701 696
            try:
702 697
                import pyopencl as cl
......
704 699
                for platform in cl.get_platforms():
705 700
                    for device in platform.get_devices():
706 701
                        deviceType=cl.device_type.to_string(device.type)
707
                        print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
702
                        print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
708 703
                        Id=Id+1
709 704

  
710 705
            except:
711
                print "Your platform does not seem to support OpenCL"
706
                print("Your platform does not seem to support OpenCL")
712 707

  
713
            print "\nInformations about devices detected under CUDA API:"
708
            print("\nInformations about devices detected under CUDA API:")
714 709
            # For PyCUDA import
715 710
            try:
716 711
                import pycuda.driver as cuda
717 712
                cuda.init()
718 713
                for Id in range(cuda.Device.count()):
719 714
                    device=cuda.Device(Id)
720
                    print "Device #%i of type GPU : %s" % (Id,device.name())
715
                    print("Device #%i of type GPU : %s" % (Id,device.name()))
721 716
                print
722 717
            except:
723
                print "Your platform does not seem to support CUDA"
718
                print("Your platform does not seem to support CUDA")
724 719
        
725 720
            sys.exit()
726 721
        
......
755 750
        elif opt in ("-r", "--redo"):
756 751
            Redo = int(arg)
757 752

  
758
    print "Devices Identification : %s" % Devices
759
    print "GpuStyle used : %s" % GpuStyle
760
    print "Iterations : %s" % Iterations
761
    print "Number of Blocks on begin : %s" % BlocksBegin
762
    print "Number of Blocks on end : %s" % BlocksEnd
763
    print "Step on Blocks : %s" % BlocksStep
764
    print "Number of Threads on begin : %s" % ThreadsBegin
765
    print "Number of Threads on end : %s" % ThreadsEnd
766
    print "Step on Threads : %s" % ThreadsStep
767
    print "Number of redo : %s" % Redo
768
    print "Metrology done out of XPU : %r" % OutMetrology
769
    print "Type of Marsaglia RNG used : %s" % RNG
770
    print "Type of variable : %s" % ValueType
753
    print("Devices Identification : %s" % Devices)
754
    print("GpuStyle used : %s" % GpuStyle)
755
    print("Iterations : %s" % Iterations)
756
    print("Number of Blocks on begin : %s" % BlocksBegin)
757
    print("Number of Blocks on end : %s" % BlocksEnd)
758
    print("Step on Blocks : %s" % BlocksStep)
759
    print("Number of Threads on begin : %s" % ThreadsBegin)
760
    print("Number of Threads on end : %s" % ThreadsEnd)
761
    print("Step on Threads : %s" % ThreadsStep)
762
    print("Number of redo : %s" % Redo)
763
    print("Metrology done out of XPU : %r" % OutMetrology)
764
    print("Type of Marsaglia RNG used : %s" % RNG)
765
    print("Type of variable : %s" % ValueType)
771 766

  
772 767
    if GpuStyle=='CUDA':
773 768
        try:
......
777 772
            cuda.init()
778 773
            for Id in range(cuda.Device.count()):
779 774
                device=cuda.Device(Id)
780
                print "Device #%i of type GPU : %s" % (Id,device.name())
775
                print("Device #%i of type GPU : %s" % (Id,device.name()))
781 776
                if Id in Devices:
782 777
                    Alu[Id]='GPU'
783 778
            
784 779
        except ImportError:
785
            print "Platform does not seem to support CUDA"
780
            print("Platform does not seem to support CUDA")
786 781

  
787 782
    if GpuStyle=='OpenCL':
788 783
        try:
......
792 787
            for platform in cl.get_platforms():
793 788
                for device in platform.get_devices():
794 789
                    deviceType=cl.device_type.to_string(device.type)
795
                    print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip())
790
                    print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()))
796 791

  
797 792
                    if Id in Devices:
798 793
                    # Set the Alu as detected Device Type
799 794
                        Alu[Id]=deviceType
800 795
                    Id=Id+1
801 796
        except ImportError:
802
            print "Platform does not seem to support OpenCL"
797
            print("Platform does not seem to support OpenCL")
803 798

  
804
    print Devices,Alu
799
    print(Devices,Alu)
805 800
            
806
    BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
807
    ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
801
    BlocksList=range(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
802
    ThreadsList=range(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
808 803
    
809 804
    ExploredJobs=numpy.array([]).astype(numpy.uint32)
810 805
    ExploredBlocks=numpy.array([]).astype(numpy.uint32)
......
849 844
                        NewIterations=OutputCU['NewIterations']
850 845
                        Duration=OutputCU['Duration']
851 846
                    except:
852
                        print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
847
                        print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
853 848
                elif GpuStyle=='OpenCL':
854 849
                    try:
855 850
                        InputCL={}
......
865 860
                        NewIterations=OutputCL['NewIterations']
866 861
                        Duration=OutputCL['Duration']
867 862
                    except:
868
                        print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
863
                        print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
869 864
                Duration=numpy.append(Duration,time.time()-start)
870 865
                Rate=numpy.append(Rate,NewIterations/Duration[-1])
871 866
        else:
......
884 879
                    NewIterations=OutputCU['NewIterations']
885 880
                    Duration=OutputCU['Duration']
886 881
                except:
887
                    print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
882
                    print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
888 883
            elif GpuStyle=='OpenCL':
889 884
                try:
890 885
                    InputCL={}
......
900 895
                    NewIterations=OutputCL['NewIterations']
901 896
                    Duration=OutputCL['Duration']
902 897
                except:
903
                    print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
898
                    print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
904 899
            Rate=NewIterations/Duration
905 900
            
906 901
        avgD=numpy.append(avgD,numpy.average(Duration))
......
914 909
        minR=numpy.append(minR,numpy.min(Rate))
915 910
        maxR=numpy.append(maxR,numpy.max(Rate))
916 911

  
917
        print "%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1])
912
        print("%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1]))
918 913
        
919 914
        numpy.savez("Pi_%s_%s_%s_%s_%s_%s_%s_%s_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu[Devices[0]],GpuStyle,BlocksBegin,BlocksEnd,ThreadsBegin,ThreadsEnd,Iterations,Devices[0],Metrology,gethostname()),(ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR))
920 915
        ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ]
Pi/XPU/PiXpuMPI.py (revision 127)
1
#!/usr/bin/env python
1
#!/usr/bin/env python3
2 2

  
3 3
#
4 4
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL
......
81 81
        try:
82 82
            opts, args = getopt.getopt(sys.argv[1:],"hocg:i:b:e:s:f:l:t:r:d:m:v:",["gpustyle=","iterations=","blocksBegin=","blocksEnd=","blocksStep=","threadsFirst=","threadsLast=","threadssTep=","redo=","device=","marsaglia=","valuetype="])
83 83
        except getopt.GetoptError:
84
            print HowToUse % sys.argv[0]
84
            print(HowToUse % sys.argv[0])
85 85
            sys.exit(2)
86 86

  
87 87
        # List of Devices
......
90 90
        
91 91
        for opt, arg in opts:
92 92
            if opt == '-h':
93
                print HowToUse % sys.argv[0]
93
                print(HowToUse % sys.argv[0])
94 94

  
95
                print "\nInformations about devices detected under OpenCL:"
95
                print("\nInformations about devices detected under OpenCL:")
96 96
                # For PyOpenCL import
97 97
                try:
98 98
                    import pyopencl as cl
......
100 100
                    for platform in cl.get_platforms():
101 101
                        for device in platform.get_devices():
102 102
                            deviceType=cl.device_type.to_string(device.type)
103
                            print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
103
                            print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
104 104
                            Id=Id+1
105 105

  
106 106
                    print
107 107
                except:
108
                    print "Your platform does not seem to support OpenCL"
108
                    print("Your platform does not seem to support OpenCL")
109 109

  
110 110
                # For PyCUDA import
111 111
                try:
......
113 113
                    cuda.init()
114 114
                    for Id in range(cuda.Device.count()):
115 115
                        device=cuda.Device(Id)
116
                        print "Device #%i of type GPU : %s" % (Id,device.name())
116
                        print("Device #%i of type GPU : %s" % (Id,device.name()))
117 117
                    print
118 118
                except:
119
                    print "Your platform does not seem to support CUDA"
119
                    print("Your platform does not seem to support CUDA")
120 120
        
121 121
                sys.exit()
122 122
                    
......
150 150
            elif opt in ("-r", "--redo"):
151 151
                Redo = int(arg)
152 152

  
153
        print "Devices Identification : %s" % Devices
154
        print "GpuStyle used : %s" % GpuStyle
155
        print "Iterations : %s" % Iterations
156
        print "Number of Blocks on begin : %s" % BlocksBegin
157
        print "Number of Blocks on end : %s" % BlocksEnd
158
        print "Step on Blocks : %s" % BlocksStep
159
        print "Number of Threads on begin : %s" % ThreadsBegin
160
        print "Number of Threads on end : %s" % ThreadsEnd
161
        print "Step on Threads : %s" % ThreadsStep
162
        print "Number of redo : %s" % Redo
163
        print "Metrology done out of XPU : %r" % OutMetrology
164
        print "Type of Marsaglia RNG used : %s" % RNG
165
        print "Type of variable : %s" % ValueType
153
        print("Devices Identification : %s" % Devices)
154
        print("GpuStyle used : %s" % GpuStyle)
155
        print("Iterations : %s" % Iterations)
156
        print("Number of Blocks on begin : %s" % BlocksBegin)
157
        print("Number of Blocks on end : %s" % BlocksEnd)
158
        print("Step on Blocks : %s" % BlocksStep)
159
        print("Number of Threads on begin : %s" % ThreadsBegin)
160
        print("Number of Threads on end : %s" % ThreadsEnd)
161
        print("Step on Threads : %s" % ThreadsStep)
162
        print("Number of redo : %s" % Redo)
163
        print("Metrology done out of XPU : %r" % OutMetrology)
164
        print("Type of Marsaglia RNG used : %s" % RNG)
165
        print("Type of variable : %s" % ValueType)
166 166

  
167 167
        if GpuStyle=='CUDA':
168 168
            try:
......
172 172
                import pycuda.autoinit
173 173
                from pycuda.compiler import SourceModule
174 174
            except ImportError:
175
                print "Platform does not seem to support CUDA"
175
                print("Platform does not seem to support CUDA")
176 176

  
177 177
        if GpuStyle=='OpenCL':
178 178
            try:
......
182 182
                for platform in cl.get_platforms():
183 183
                    for device in platform.get_devices():
184 184
                        deviceType=cl.device_type.to_string(device.type)
185
                        print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip())
185
                        print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()))
186 186

  
187 187
                        if Id in Devices:
188 188
                            # Set the Alu as detected Device Type
189 189
                            Alu[Id]=deviceType
190 190
                        Id=Id+1
191 191
            except ImportError:
192
                print "Platform does not seem to support OpenCL"
192
                print("Platform does not seem to support OpenCL")
193 193

  
194
        print Devices,Alu
194
        print(Devices,Alu)
195 195
            
196
        BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
197
        ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
196
        BlocksList=range(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
197
        ThreadsList=range(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
198 198
    
199 199
        ExploredJobs=numpy.array([]).astype(numpy.uint32)
200 200
        ExploredBlocks=numpy.array([]).astype(numpy.uint32)
......
228 228
                        try:
229 229
                            Inside,NewIterations,DurationItem=MetropolisCuda(circle,Iterations,1,Blocks,Threads,RNG,ValueType)
230 230
                        except:
231
                            print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
231
                            print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
232 232
                    elif GpuStyle=='OpenCL':
233 233
                        try:
234 234
                            MetroParamCL={}
......
242 242
                            
243 243
                            OutputCL=MetropolisOpenCL(MetroParamCL)
244 244
                        except:
245
                            print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
245
                            print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
246 246
                    Duration=numpy.append(Duration,time.time()-start)
247 247
                    Rate=numpy.append(Rate,NewIterations/Duration[-1])
248 248
            else:
......
250 250
                    try:
251 251
                        Inside,NewIterations,Duration=MetropolisCuda(circle,Iterations,Redo,Blocks,Threads,RNG,ValueType)
252 252
                    except:
253
                        print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
253
                        print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
254 254
                elif GpuStyle=='OpenCL':
255 255
                    try:
256 256
                        IterationsMPI=numpy.uint64(Iterations/len(Devices))
......
268 268
                        InputCL['RNG']=RNG
269 269
                        InputCL['ValueType']=ValueType
270 270
                        for Device in Devices[1:]:
271
                            print "Send to device %i on rank %i" % (Device,r)
271
                            print("Send to device %i on rank %i" % (Device,r))
272 272
                            InputCL['Device']=Device
273 273
                            comm.send('CONTINUE',dest=r,tag=11)
274 274
                            comm.send(InputCL,dest=r,tag=11)
275 275
                            r+=1
276 276

  
277 277
                        # Compute on rank 0
278
                        print "Compute on rank 0"
278
                        print("Compute on rank 0")
279 279
                        InputCL['Device']=Devices[0]
280 280
                        OutputCL=MetropolisOpenCL(InputCL)
281 281
                        Inside=OutputCL['Inside']
282 282
                        NewIterations=OutputCL['NewIterations']
283 283
                            
284
                        for slave in xrange(1,len(Devices)):
285
                            print "Get OutputCL from %i" % slave
284
                        for slave in range(1,len(Devices)):
285
                            print("Get OutputCL from %i" % slave)
286 286
                            OutputCL=comm.recv(source=slave,tag=11)
287
                            print OutputCL
287
                            print(OutputCL)
288 288
                            NewIterations+=OutputCL['NewIterations']
289 289
                            Inside+=OutputCL['Inside']
290 290

  
291 291
                        Duration=time.time()-time_start
292
                        print "Pi estimation %.8f" % (4./NewIterations*Inside)
292
                        print("Pi estimation %.8f" % (4./NewIterations*Inside))
293 293
                    except:
294
                        print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)
294
                        print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
295 295
            #Duration=OutputCL['Duration']
296 296
            #NewIterations=OutputCL['NewIterations']
297 297
            Rate=NewIterations/Duration
......
307 307
            minR=numpy.append(minR,numpy.min(Rate))
308 308
            maxR=numpy.append(maxR,numpy.max(Rate))
309 309

  
310
            print "%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1])
310
            print("%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1]))
311 311
        
312 312
            numpy.savez("Pi_%s_%s_%s_%s_%s_%s_%s_%s_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu[Devices[0]],GpuStyle,BlocksBegin,BlocksEnd,ThreadsBegin,ThreadsEnd,Iterations,Devices[0],Metrology,gethostname()),(ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR))
313 313
            ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ]
......
316 316
            if Fit:
317 317
                FitAndPrint(ExploredJobs,median,Curves)
318 318
        # Send MPI exit tag
319
        for slave in xrange(1,RankSize):
319
        for slave in range(1,RankSize):
320 320
            comm.send('BREAK',dest=slave,tag=11)
321 321
            
322 322
    else:
......
325 325
            if Signal=='CONTINUE':
326 326
                # Receive information from Master
327 327
                InputCL=comm.recv(source=0,tag=11)
328
                print "Parameters retreive for rank %s of %s on %s from master:" % (rank,RankSize,gethostname())
329
                print "Input CL:" % InputCL
328
                print("Parameters retreive for rank %s of %s on %s from master:" % (rank,RankSize,gethostname()))
329
                print("Input CL:" % InputCL)
330 330
                # Execute on slave
331 331
                OutputCL=MetropolisOpenCL(InputCL)
332
                print "Output CL:" % OutputCL
332
                print("Output CL:" % OutputCL)
333 333
                # Send information to Master
334 334
                comm.send(OutputCL,dest=0,tag=11)
335
                print "Data sent to master"
335
                print("Data sent to master")
336 336
            else:
337
                print 'Exit signal from Master'
337
                print('Exit signal from Master')
338 338
                break
Pi/XPU/PiXpuThreads.py (revision 127)
1
#!/usr/bin/env python
1
#!/usr/bin/env python3
2 2

  
3 3
#
4 4
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL
......
33 33
class threadWithReturn(Thread):
34 34
    def __init__(self, *args, **kwargs):
35 35
        super(threadWithReturn, self).__init__(*args, **kwargs)
36

  
37 36
        self._return = None
38 37

  
39 38
    def run(self):
40
        if self._Thread__target is not None:
41
            self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
39
        if self._target is not None:
40
            self._return = self._target(*self._args, **self._kwargs)
42 41

  
43 42
    def join(self, *args, **kwargs):
44 43
        super(threadWithReturn, self).join(*args, **kwargs)
45

  
46 44
        return self._return
47 45

  
48 46
if __name__=='__main__':
......
86 84
    try:
87 85
        opts, args = getopt.getopt(sys.argv[1:],"hocg:i:b:e:s:f:l:t:r:d:m:v:",["gpustyle=","iterations=","blocksBegin=","blocksEnd=","blocksStep=","threadsFirst=","threadsLast=","threadssTep=","redo=","device=","marsaglia=","valuetype="])
88 86
    except getopt.GetoptError:
89
        print HowToUse % sys.argv[0]
87
        print(HowToUse % sys.argv[0])
90 88
        sys.exit(2)
91 89

  
92 90
    # List of Devices
......
95 93
        
96 94
    for opt, arg in opts:
97 95
        if opt == '-h':
98
            print HowToUse % sys.argv[0]
96
            print(HowToUse % sys.argv[0])
99 97

  
100
            print "\nInformations about devices detected under OpenCL:"
98
            print("\nInformations about devices detected under OpenCL:")
101 99
            # For PyOpenCL import
102 100
            try:
103 101
                import pyopencl as cl
......
105 103
                for platform in cl.get_platforms():
106 104
                    for device in platform.get_devices():
107 105
                        deviceType=cl.device_type.to_string(device.type)
108
                        print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
106
                        print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
109 107
                        Id=Id+1
110 108

  
111 109
                print
112 110
                sys.exit()
113 111
            except ImportError:
114
                print "Your platform does not seem to support OpenCL"
112
                print("Your platform does not seem to support OpenCL")
115 113
        
116 114
        elif opt == '-o':
117 115
            OutMetrology=True
......
143 141
        elif opt in ("-r", "--redo"):
144 142
            Redo = int(arg)
145 143

  
146
    print "Devices Identification : %s" % Devices
147
    print "GpuStyle used : %s" % GpuStyle
148
    print "Iterations : %s" % Iterations
149
    print "Number of Blocks on begin : %s" % BlocksBegin
150
    print "Number of Blocks on end : %s" % BlocksEnd
151
    print "Step on Blocks : %s" % BlocksStep
152
    print "Number of Threads on begin : %s" % ThreadsBegin
153
    print "Number of Threads on end : %s" % ThreadsEnd
154
    print "Step on Threads : %s" % ThreadsStep
155
    print "Number of redo : %s" % Redo
156
    print "Metrology done out of XPU : %r" % OutMetrology
157
    print "Type of Marsaglia RNG used : %s" % RNG
158
    print "Type of variable : %s" % ValueType
144
    print("Devices Identification : %s" % Devices)
145
    print("GpuStyle used : %s" % GpuStyle)
146
    print("Iterations : %s" % Iterations)
147
    print("Number of Blocks on begin : %s" % BlocksBegin)
148
    print("Number of Blocks on end : %s" % BlocksEnd)
149
    print("Step on Blocks : %s" % BlocksStep)
150
    print("Number of Threads on begin : %s" % ThreadsBegin)
151
    print("Number of Threads on end : %s" % ThreadsEnd)
152
    print("Step on Threads : %s" % ThreadsStep)
153
    print("Number of redo : %s" % Redo)
154
    print("Metrology done out of XPU : %r" % OutMetrology)
155
    print("Type of Marsaglia RNG used : %s" % RNG)
156
    print("Type of variable : %s" % ValueType)
159 157

  
160 158
    if GpuStyle=='CUDA':
161 159
        try:
......
165 163
            import pycuda.autoinit
166 164
            from pycuda.compiler import SourceModule
167 165
        except ImportError:
168
            print "Platform does not seem to support CUDA"
166
            print("Platform does not seem to support CUDA")
169 167

  
170 168
    if GpuStyle=='OpenCL':
171 169
        try:
......
175 173
            for platform in cl.get_platforms():
176 174
                for device in platform.get_devices():
177 175
                    deviceType=cl.device_type.to_string(device.type)
178
                    print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip())
176
                    print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()))
179 177

  
180 178
                    if Id in Devices:
181 179
                    # Set the Alu as detected Device Type
182 180
                        Alu[Id]=deviceType
183 181
                    Id=Id+1
184 182
        except ImportError:
185
            print "Platform does not seem to support OpenCL"
183
            print("Platform does not seem to support OpenCL")
186 184

  
187
    print Devices,Alu
185
    print(Devices,Alu)
188 186
            
189
    BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
190
    ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
187
    BlocksList=range(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
188
    ThreadsList=range(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
191 189
    
192 190
    ExploredJobs=numpy.array([]).astype(numpy.uint32)
193 191
    ExploredBlocks=numpy.array([]).astype(numpy.uint32)
......
234 232
                        try:
235 233
                            MyThread=threadWithReturn(target=MetropolisCUDA, args=(InputCL,))
236 234
                        except:
237
                            print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
235
                            print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
238 236
                    elif GpuStyle=='OpenCL':
239 237
                        try:
240 238
                            MyThread=threadWithReturn(target=MetropolisOpenCL, args=(InputCL,))
241 239
                        except:
242
                            print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
240
                            print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            )
243 241
                    
244
                    print "Start on #%i device..." % Device
242
                    print("Start on #%i device..." % Device)
245 243
                    MyThread.start()
246 244
                    MyThreads.append(MyThread)
247 245

  
......
251 249
                    OutputCL=MyThread.join()
252 250
                    NewIterations+=OutputCL['NewIterations']
253 251
                    Inside+=OutputCL['Inside']
254
                print "Pi equals:",4./NewIterations*Inside
252
                print("Pi equals:",4./NewIterations*Inside)
255 253

  
256 254
                Duration=numpy.append(Duration,time.time()-time_start)
257 255
                Rate=numpy.append(Rate,NewIterations/Duration[-1])
......
271 269
                    try:
272 270
                        MyThread=threadWithReturn(target=MetropolisCUDA, args=(InputCL,))
273 271
                    except:
274
                        print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
272
                        print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
275 273
                elif GpuStyle=='OpenCL':
276 274
                    try:
277 275
                        MyThread=threadWithReturn(target=MetropolisOpenCL, args=(InputCL,))
278 276
                    except:
279
                        print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
277
                        print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
280 278
                    
281
                print "Start on #%i device..." % Device
279
                print("Start on #%i device..." % Device)
282 280
                MyThread.start()
283 281
                MyThreads.append(MyThread)
284 282

  
......
289 287
                NewIterations+=OutputCL['NewIterations']
290 288
                Inside+=OutputCL['Inside']
291 289
            Duration=time.time()-time_start
292
            print "Pi equals:",4./NewIterations*Inside
290
            print("Pi equals:",4./NewIterations*Inside)
293 291
            Rate=1./Duration*NewIterations
294 292
            
295 293
        avgD=numpy.append(avgD,numpy.average(Duration))
......
303 301
        minR=numpy.append(minR,numpy.min(Rate))
304 302
        maxR=numpy.append(maxR,numpy.max(Rate))
305 303

  
306
        print "%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1])
304
        print("%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1]))
307 305
        
308 306
        numpy.savez("Pi_%s_%s_%s_%s_%s_%s_%s_%s_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu[Devices[0]],GpuStyle,BlocksBegin,BlocksEnd,ThreadsBegin,ThreadsEnd,Iterations,Devices[0],Metrology,gethostname()),(ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR))
309 307
        ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ]

Formats disponibles : Unified diff