Révision 55

Pi/GPU/Pi-GPU.py (revision 55)
1 1
#!/usr/bin/env python
2 2

  
3 3
#
4
# Pi-by-MC using PyCUDA/PyOpenCL
4
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL
5 5
#
6 6
# CC BY-NC-SA 2011 : <emmanuel.quemener@ens-lyon.fr> 
7 7
#
......
19 19
import sys
20 20
import getopt
21 21
import time
22
import matplotlib.pyplot as plt
23 22
import math
24
from scipy.optimize import curve_fit
25 23
from socket import gethostname
26 24

  
27 25
# find prime factors of a number
......
600 598

  
601 599
def FitAndPrint(N,D,Curves):
602 600

  
601
  from scipy.optimize import curve_fit
602
  import matplotlib.pyplot as plt
603

  
603 604
  try:
604 605
    coeffs_Amdahl, matcov_Amdahl = curve_fit(Amdahl, N, D)
605 606

  
......
672 673
if __name__=='__main__':
673 674

  
674 675
  # Set defaults values
675
  # Alu can be CPU or GPU
676
  
677
  # Alu can be CPU, GPU or ACCELERATOR
676 678
  Alu='CPU'
677 679
  # Id of GPU : 1 is for first find !
678 680
  Device=0
......
695 697
  Metrology='InMetro'
696 698
  # Curves is True to print the curves
697 699
  Curves=False
700
  # Fit is True to print the curves
701
  Fit=False
698 702
  # DoublePrecision on FP calculus
699 703
  DoublePrecision=False
700 704

  
701 705
  try:
702
    opts, args = getopt.getopt(sys.argv[1:],"hocla:g:p:i:s:e:t:r:d:",["alu=","gpustyle=","parastyle=","iterations=","jobstart=","jobend=","jobstep=","redo=","device="])
706
    opts, args = getopt.getopt(sys.argv[1:],"hoclfa:g:p:i:s:e:t:r:d:",["alu=","gpustyle=","parastyle=","iterations=","jobstart=","jobend=","jobstep=","redo=","device="])
703 707
  except getopt.GetoptError:
704
    print '%s -o (Out of Core Metrology) -c (Print Curves) -l (Double Precision) -a <CPU/GPU/ACCELERATOR> -d <DeviceId> -g <CUDA/OpenCL> -p <Threads/Hybrid/Blocks> -i <Iterations> -s <JobStart> -e <JobEnd> -t <JobStep> -r <RedoToImproveStats> ' % sys.argv[0]
708
    print '%s -o (Out of Core Metrology) -c (Print Curves) -l (Double Precision) -f (Fit to Amdahl Law) -a <CPU/GPU/ACCELERATOR> -d <DeviceId> -g <CUDA/OpenCL> -p <Threads/Hybrid/Blocks> -i <Iterations> -s <JobStart> -e <JobEnd> -t <JobStep> -r <RedoToImproveStats> ' % sys.argv[0]
705 709
    sys.exit(2)
706 710
    
707 711
  for opt, arg in opts:
708 712
    if opt == '-h':
709
      print '%s -o (Out of Core Metrology) -c (Print Curves) -l (Double Precision) -a <CPU/GPU/ACCELERATOR> -d <DeviceId> -g <CUDA/OpenCL> -p <Threads/Hybrid/Blocks> -i <Iterations> -s <JobStart> -e <JobEnd> -t <JobStep> -r <RedoToImproveStats>' % sys.argv[0]
713
      print '%s -o (Out of Core Metrology) -c (Print Curves) -l (Double Precision) -f (Fit to Amdahl Law) -a <CPU/GPU/ACCELERATOR> -d <DeviceId> -g <CUDA/OpenCL> -p <Threads/Hybrid/Blocks> -i <Iterations> -s <JobStart> -e <JobEnd> -t <JobStep> -r <RedoToImproveStats>' % sys.argv[0]
710 714

  
711 715
      print "\nInformations about devices detected under OpenCL:"
712 716
      # For PyOpenCL import
713
      import pyopencl as cl
714
      Id=1
715
      for platform in cl.get_platforms():
716
        for device in platform.get_devices():
717
          deviceType=cl.device_type.to_string(device.type)
718
          print "Device #%i of type %s : %s" % (Id,deviceType,device.name)
719
          Id=Id+1
717
      try:
718
        import pyopencl as cl
719
        Id=1
720
        for platform in cl.get_platforms():
721
          for device in platform.get_devices():
722
            deviceType=cl.device_type.to_string(device.type)
723
            print "Device #%i of type %s : %s" % (Id,deviceType,device.name)
724
            Id=Id+1
720 725

  
721
      sys.exit()
726
        print
727
        sys.exit()
728
      except ImportError:
729
        print "Your platform does not seem to support OpenCL"
730
        
722 731
    elif opt == '-o':
723 732
      OutMetrology=True
724 733
      Metrology='OutMetro'
......
726 735
      DoublePrecision=True
727 736
    elif opt == '-c':
728 737
      Curves=True
738
    elif opt == '-f':
739
      Fit=True
729 740
    elif opt in ("-a", "--alu"):
730 741
      Alu = arg
731 742
    elif opt in ("-d", "--device"):
......
765 776
  print "Double Precision in Kernels : %r" % DoublePrecision
766 777

  
767 778
  if GpuStyle=='CUDA':
768
    # For PyCUDA import
769
    import pycuda.driver as cuda
770
    import pycuda.gpuarray as gpuarray
771
    import pycuda.autoinit
772
    from pycuda.compiler import SourceModule
779
    try:
780
      # For PyCUDA import
781
      import pycuda.driver as cuda
782
      import pycuda.gpuarray as gpuarray
783
      import pycuda.autoinit
784
      from pycuda.compiler import SourceModule
785
    except ImportError:
786
      print "Platform does not seem to support CUDA"
773 787

  
774 788
  if GpuStyle=='OpenCL':
775
    # For PyOpenCL import
776
    import pyopencl as cl
777
    Id=1
778
    for platform in cl.get_platforms():
779
      for device in platform.get_devices():
780
        deviceType=cl.device_type.to_string(device.type)
781
        print "Device #%i of type %s : %s" % (Id,deviceType,device.name)
782
        if Id == Device:
783
          # Set the Alu as detected Device Type
784
          Alu=deviceType
785
        Id=Id+1
786

  
789
    try:
790
      # For PyOpenCL import
791
      import pyopencl as cl
792
      Id=1
793
      for platform in cl.get_platforms():
794
        for device in platform.get_devices():
795
          deviceType=cl.device_type.to_string(device.type)
796
          print "Device #%i of type %s : %s" % (Id,deviceType,device.name)
797
          if Id == Device:
798
            # Set the Alu as detected Device Type
799
            Alu=deviceType
800
          Id=Id+1
801
    except ImportError:
802
      print "Platform does not seem to support CUDA"
803
      
787 804
  average=numpy.array([]).astype(numpy.float32)
788 805
  median=numpy.array([]).astype(numpy.float32)
789 806
  stddev=numpy.array([]).astype(numpy.float32)
......
828 845
        except:
829 846
          print "Problem with %i // computations on Cuda" % Jobs
830 847
      elif GpuStyle=='OpenCL':
831
        # try:
832
        #   avg,med,std=MetropolisOpenCL(circle,Iterations,Redo,Jobs,ParaStyle,Alu,Device)
833
        # except:
834
        #   print "Problem with %i // computations on OpenCL" % Jobs            
835
        avg,med,std=MetropolisOpenCL(circle,Iterations,Redo,Jobs,ParaStyle,Alu,Device,DoublePrecision)
848
        try:
849
          avg,med,std=MetropolisOpenCL(circle,Iterations,Redo,Jobs,ParaStyle,Alu,Device,DoublePrecision)
850
        except:
851
          print "Problem with %i // computations on OpenCL" % Jobs            
836 852

  
837 853
    if (avg,med,std) != (0,0,0):
838 854
      print "jobs,avg,med,std",Jobs,avg,med,std
......
852 868
      numpy.savetxt("Pi%s_%s_%s_%s_%s_%i_%.8i_Device%i_%s_%s" % (Precision,Alu,GpuStyle,ParaStyle,JobStart,JobEnd,Iterations,Device,Metrology,gethostname()),numpy.transpose(ToSave))
853 869
    Jobs+=JobStep
854 870

  
855
  FitAndPrint(ExploredJobs,median,Curves)
871
  if Fit:
872
    FitAndPrint(ExploredJobs,median,Curves)
856 873

  

Formats disponibles : Unified diff