Révision 72 Pi/GPU/Pi-GPU.py

Pi-GPU.py (revision 72)
22 22
import math
23 23
from socket import gethostname
24 24

  
25
Marsaglia={'CONG':0,'SHR3':1,'MWC':2,'KISS':3}
26
Computing={'INT32':0,'INT64':1,'FP32':2,'FP64':3}
27

  
25 28
# find prime factors of a number
26 29
# Get for WWW :
27 30
# http://pythonism.wordpress.com/2008/05/17/looking-at-factorisation-in-python/
......
77 80

  
78 81
#define MWCfp MWC * 2.328306435454494e-10f
79 82
#define KISSfp KISS * 2.328306435454494e-10f
83
#define SHR3fp SHR3 * 2.328306435454494e-10f
84
#define CONGfp CONG * 2.328306435454494e-10f
80 85

  
81 86
__global__ void MainLoopBlocks(ulong *s,ulong iterations,uint seed_w,uint seed_z)
82 87
{
......
103 108

  
104 109
__global__ void MainLoopThreads(ulong *s,ulong iterations,uint seed_w,uint seed_z)
105 110
{
106
   uint z=seed_z/(threadIdx.x+1);
107
   uint w=seed_w/(threadIdx.x+1);
111
   uint z=seed_z+threadIdx.x;
112
   uint w=seed_w+threadIdx.x;
108 113

  
109 114
   ulong total=0;
110 115

  
......
126 131

  
127 132
__global__ void MainLoopHybrid(ulong *s,ulong iterations,uint seed_w,uint seed_z)
128 133
{
129
   uint z=seed_z/(blockDim.x*blockIdx.x+threadIdx.x+1);
130
   uint w=seed_w/(blockDim.x*blockIdx.x+threadIdx.x+1);
134
   uint z=seed_z+(blockDim.x*blockIdx.x+threadIdx.x+1);
135
   uint w=seed_w+(blockDim.x*blockIdx.x+threadIdx.x+1);
131 136

  
132 137
   ulong total=0;
133 138

  
......
149 154

  
150 155
__global__ void MainLoopBlocks64(ulong *s,ulong iterations,uint seed_w,uint seed_z)
151 156
{
152
   uint z=seed_z/(blockIdx.x+1);
153
   uint w=seed_w/(blockIdx.x+1);
157
   uint z=seed_z+blockIdx.x;
158
   uint w=seed_w+blockIdx.x;
154 159

  
155 160
   ulong total=0;
156 161

  
......
172 177

  
173 178
__global__ void MainLoopThreads64(ulong *s,ulong iterations,uint seed_w,uint seed_z)
174 179
{
175
   uint z=seed_z/(threadIdx.x+1);
176
   uint w=seed_w/(threadIdx.x+1);
180
   uint z=seed_z+threadIdx.x;
181
   uint w=seed_w+threadIdx.x;
177 182

  
178 183
   ulong total=0;
179 184

  
......
195 200

  
196 201
__global__ void MainLoopHybrid64(ulong *s,ulong iterations,uint seed_w,uint seed_z)
197 202
{
198
   uint z=seed_z/(blockDim.x*blockIdx.x+threadIdx.x+1);
199
   uint w=seed_w/(blockDim.x*blockIdx.x+threadIdx.x+1);
203
   uint z=seed_z+blockDim.x*blockIdx.x+threadIdx.x;
204
   uint w=seed_w+blockDim.x*blockIdx.x+threadIdx.x;
200 205

  
201 206
   ulong total=0;
202 207

  
......
218 223
"""
219 224

  
220 225
KERNEL_CODE_OPENCL="""
226
#define TCONG 0
227
#define TSHR3 1
228
#define TMWC 2
229
#define TKISS 3
230

  
231
#define TINT32 0
232
#define TINT64 1
233
#define TFP32 2
234
#define TFP64 3
235

  
221 236
#pragma OPENCL EXTENSION cl_khr_fp64: enable
222 237

  
223 238
// Marsaglia RNG very simple implementation
224 239
#define znew  ((z=36969*(z&65535)+(z>>16))<<16)
225 240
#define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
241

  
226 242
#define MWC   (znew+wnew)
227 243
#define SHR3  (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
228 244
#define CONG  (jcong=69069*jcong+1234567)
......
230 246

  
231 247
#define MWCfp MWC * 2.328306435454494e-10f
232 248
#define KISSfp KISS * 2.328306435454494e-10f
249
#define CONGfp CONG * 2.328306435454494e-10f
250
#define SHR3fp SHR3 * 2.328306435454494e-10f
233 251

  
234 252
__kernel void MainLoopGlobal(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)
235 253
{
254

  
255
// Initialization of RNG numbers
256
#if TRNG == TCONG
257
   uint jcong=seed_z+get_global_id(0);
258
#elif TRNG == TSHR3
259
   uint jsr=seed_w+get_global_id(0);
260
#elif TRNG == TMWC
236 261
   uint z=seed_z+get_global_id(0);
237 262
   uint w=seed_w+get_global_id(0);
263
#elif TRNG == TKISS
264
   uint jcong=seed_z+get_global_id(0);
265
   uint jsr=seed_w+get_global_id(0);
266
   uint z=seed_z-get_global_id(0);
267
   uint w=seed_w-get_global_id(0);
268
#endif
238 269

  
239 270
   ulong total=0;
240 271

  
272
   // Main Loop
241 273
   for (ulong i=0;i<iterations;i++) {
242 274

  
275
//
276

  
277
#if TYPE == TINT32
278
#define THEONE 1073741824
279
#if TRNG == TCONG
280
      uint x=CONG>>17 ;
281
      uint y=CONG>>17 ;
282
#elif TRNG == TSHR3
283
      uint x=SHR3>>17 ;
284
      uint y=SHR3>>17 ;
285
#elif TRNG == TMWC
286
      uint x=MWC>>17 ;
287
      uint y=MWC>>17 ;
288
#elif TRNG == TKISS
289
      uint x=KISS>>17 ;
290
      uint y=KISS>>17 ;
291
#endif
292
#elif TYPE == TINT64
293
#define THEONE 4611686018427387904
294
#if TRNG == TCONG
295
      ulong x=(ulong)(CONG>>1) ;
296
      ulong y=(ulong)(CONG>>1) ;
297
#elif TRNG == TSHR3
298
      ulong x=(ulong)(SHR3>>1) ;
299
      ulong y=(ulong)(SHR3>>1) ;
300
#elif TRNG == TMWC
301
      ulong x=(ulong)(MWC>>1) ;
302
      ulong y=(ulong)(MWC>>1) ;
303
#elif TRNG == TKISS
304
      ulong x=(ulong)(KISS>>1) ;
305
      ulong y=(ulong)(KISS>>1) ;
306
#endif
307
#elif TYPE == TFP32
308
#define THEONE 1.0f
309
#if TRNG == TCONG
310
      float x=CONGfp ;
311
      float y=CONGfp ;
312
#elif TRNG == TSHR3
313
      float x=SHR3fp ;
314
      float y=SHR3fp ;
315
#elif TRNG == TMWC
243 316
      float x=MWCfp ;
244 317
      float y=MWCfp ;
318
#elif TRNG == TKISS
319
      float x=KISSfp ;
320
      float y=KISSfp ;
321
#endif
322
#elif TYPE == TFP64
323
#define THEONE 1.0f
324
#if TRNG == TCONG
325
      double x=(double)CONGfp ;
326
      double y=(double)CONGfp ;
327
#elif TRNG == TSHR3
328
      double x=(double)SHR3fp ;
329
      double y=(double)SHR3fp ;
330
#elif TRNG == TMWC
331
      double x=(double)MWCfp ;
332
      double y=(double)MWCfp ;
333
#elif TRNG == TKISS
334
      double x=(double)KISSfp ;
335
      double y=(double)KISSfp ;
336
#endif
337
#endif
245 338

  
246 339
      // Matching test
247
      ulong inside=((x*x+y*y) <= 1.0f) ? 1:0;
340
      ulong inside=((x*x+y*y) <= THEONE) ? 1:0;
248 341
      total+=inside;
249 342
   }
343

  
250 344
   barrier(CLK_GLOBAL_MEM_FENCE);
251 345
   s[get_global_id(0)]=total;
252 346
      
......
460 554

  
461 555

  
462 556
def MetropolisOpenCL(circle,iterations,steps,jobs,ParaStyle,Alu,Device,
463
                     DoublePrecision):
557
                     DoublePrecision,RNG,ValueType):
464 558
	
465 559
  # Initialisation des variables en les CASTant correctement
466 560
    
......
507 601
	
508 602
  circleCL = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=circle)
509 603

  
604
  
510 605
  MetropolisCL = cl.Program(ctx,KERNEL_CODE_OPENCL).build( \
511
    options = "-cl-mad-enable -cl-fast-relaxed-math")
606
    options = "-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%s" % (Marsaglia[RNG],Computing[ValueType]))
512 607

  
513 608
  i=0
514 609

  
......
703 798
  Fit=False
704 799
  # DoublePrecision on FP calculus
705 800
  DoublePrecision=False
706

  
801
  # Marsaglia RNG
802
  RNG='KISS'
803
  # Value type : INT32, INT64, FP32, FP64
804
  ValueType='INT32'
805
  
707 806
  try:
708
    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="])
807
    opts, args = getopt.getopt(sys.argv[1:],"hocfa:g:p:i:s:e:t:r:d:m:v:",["alu=","gpustyle=","parastyle=","iterations=","jobstart=","jobend=","jobstep=","redo=","device=","marsaglia=","valuetype="])
709 808
  except getopt.GetoptError:
710
    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]
809
    print '%s -o (Out of Core Metrology) -c (Print Curves) -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> -m <SHR3/CONG/MWC/KISS> -v <INT32/INT64/FP32/FP64> ' % sys.argv[0]
711 810
    sys.exit(2)
712 811
    
713 812
  for opt, arg in opts:
714 813
    if opt == '-h':
715
      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]
814
      print '%s -o (Out of Core Metrology) -c (Print Curves) -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> -m <SHR3/CONG/MWC/KISS> -v <INT32/INT64/FP32/FP64>' % sys.argv[0]
716 815

  
717 816
      print "\nInformations about devices detected under OpenCL:"
718 817
      # For PyOpenCL import
......
747 846
      GpuStyle = arg
748 847
    elif opt in ("-p", "--parastyle"):
749 848
      ParaStyle = arg
849
    elif opt in ("-m", "--marsaglia"):
850
      RNG = arg
851
    elif opt in ("-v", "--valuetype"):
852
      ValueType = arg
750 853
    elif opt in ("-i", "--iterations"):
751 854
      Iterations = numpy.uint64(arg)
752 855
    elif opt in ("-s", "--jobstart"):
......
776 879
  print "Number of redo : %s" % Redo
777 880
  print "Metrology done out of CPU/GPU : %r" % OutMetrology
778 881
  print "Double Precision in Kernels : %r" % DoublePrecision
882
  print "Type of Marsaglia RNG used : %s" % RNG
883
  print "Type of variable : %s" % ValueType
779 884

  
780 885
  if GpuStyle=='CUDA':
781 886
    try:
......
829 934
        elif GpuStyle=='OpenCL':
830 935
          try:
831 936
            a,m,s=MetropolisOpenCL(circle,Iterations,1,Jobs,ParaStyle,
832
                                   Alu,Device,DoublePrecision)
937
                                   Alu,Device,DoublePrecision,RNG,ValueType)
833 938
          except:
834 939
            print "Problem with %i // computations on OpenCL" % Jobs            
835 940
        duration=numpy.append(duration,time.time()-start)
......
848 953
          print "Problem with %i // computations on Cuda" % Jobs
849 954
      elif GpuStyle=='OpenCL':
850 955
        try:
851
          avg,med,std=MetropolisOpenCL(circle,Iterations,Redo,Jobs,ParaStyle,Alu,Device,DoublePrecision)
956
          avg,med,std=MetropolisOpenCL(circle,Iterations,Redo,Jobs,ParaStyle,
957
                                       Alu,Device,DoublePrecision,RNG,ValueType)
852 958
        except:
853 959
          print "Problem with %i // computations on OpenCL" % Jobs            
854 960

  

Formats disponibles : Unified diff