Révision 127 Pi/XPU/PiXPU.py

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 ]

Formats disponibles : Unified diff