Révision 99

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

  
3
#
4
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL
5
#
6
# CC BY-NC-SA 2011 : Emmanuel QUEMENER <emmanuel.quemener@gmail.com> 
7
# Cecill v2 : Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
8
#
9
# Thanks to Andreas Klockner for PyCUDA:
10
# http://mathema.tician.de/software/pycuda
11
# Thanks to Andreas Klockner for PyOpenCL:
12
# http://mathema.tician.de/software/pyopencl
13
# 
14

  
15
# 2013-01-01 : problems with launch timeout
16
# http://stackoverflow.com/questions/497685/how-do-you-get-around-the-maximum-cuda-run-time
17
# Option "Interactive" "0" in /etc/X11/xorg.conf
18

  
19
# Common tools
20
import numpy
21
from numpy.random import randint as nprnd
22
import sys
23
import getopt
24
import time
25
import math
26
from socket import gethostname
27

  
28
Marsaglia={'CONG':0,'SHR3':1,'MWC':2,'KISS':3}
29
Computing={'INT32':0,'INT64':1,'FP32':2,'FP64':3}
30

  
31
# find prime factors of a number
32
# Get for WWW :
33
# http://pythonism.wordpress.com/2008/05/17/looking-at-factorisation-in-python/
34
def PrimeFactors(x):
35
    factorlist=numpy.array([]).astype('uint32')
36
    loop=2
37
    while loop<=x:
38
        if x%loop==0:
39
            x/=loop
40
            factorlist=numpy.append(factorlist,[loop])
41
        else:
42
            loop+=1
43
    return factorlist
44

  
45
# Try to find the best thread number in Hybrid approach (Blocks&Threads)
46
# output is thread number
47
def BestThreadsNumber(jobs):
48
    factors=PrimeFactors(jobs)
49
    matrix=numpy.append([factors],[factors[::-1]],axis=0)
50
    threads=1
51
    for factor in matrix.transpose().ravel():
52
        threads=threads*factor
53
        if threads*threads>jobs or threads>512:
54
            break
55
    return(long(threads))
56

  
57
# Predicted Amdahl Law (Reduced with s=1-p)  
58
def AmdahlR(N, T1, p):
59
    return (T1*(1-p+p/N))
60

  
61
# Predicted Amdahl Law
62
def Amdahl(N, T1, s, p):
63
    return (T1*(s+p/N))
64

  
65
# Predicted Mylq Law with first order
66
def Mylq(N, T1,s,c,p):
67
    return (T1*(s+p/N)+c*N)
68

  
69
# Predicted Mylq Law with second order
70
def Mylq2(N, T1,s,c1,c2,p):
71
    return (T1*(s+p/N)+c1*N+c2*N*N)
72

  
73
KERNEL_CODE_CUDA="""
74
#define TCONG 0
75
#define TSHR3 1
76
#define TMWC 2
77
#define TKISS 3
78

  
79
#define TINT32 0
80
#define TINT64 1
81
#define TFP32 2
82
#define TFP64 3
83

  
84
// Marsaglia RNG very simple implementation
85

  
86
#define znew  ((z=36969*(z&65535)+(z>>16))<<16)
87
#define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
88
#define MWC   (znew+wnew)
89
#define SHR3  (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
90
#define CONG  (jcong=69069*jcong+1234567)
91
#define KISS  ((MWC^CONG)+SHR3)
92

  
93
#define MWCfp MWC * 2.328306435454494e-10f
94
#define KISSfp KISS * 2.328306435454494e-10f
95
#define SHR3fp SHR3 * 2.328306435454494e-10f
96
#define CONGfp CONG * 2.328306435454494e-10f
97

  
98
__device__ ulong MainLoop(ulong iterations,uint seed_w,uint seed_z,size_t work)
99
{
100

  
101
#if TRNG == TCONG
102
   uint jcong=seed_z+work;
103
#elif TRNG == TSHR3
104
   uint jsr=seed_w+work;
105
#elif TRNG == TMWC
106
   uint z=seed_z+work;
107
   uint w=seed_w+work;
108
#elif TRNG == TKISS
109
   uint jcong=seed_z+work;
110
   uint jsr=seed_w+work;
111
   uint z=seed_z-work;
112
   uint w=seed_w-work;
113
#endif
114

  
115
   ulong total=0;
116

  
117
   for (ulong i=0;i<iterations;i++) {
118

  
119
#if TYPE == TINT32
120
    #define THEONE 1073741824
121
    #if TRNG == TCONG
122
        uint x=CONG>>17 ;
123
        uint y=CONG>>17 ;
124
    #elif TRNG == TSHR3
125
        uint x=SHR3>>17 ;
126
        uint y=SHR3>>17 ;
127
    #elif TRNG == TMWC
128
        uint x=MWC>>17 ;
129
        uint y=MWC>>17 ;
130
    #elif TRNG == TKISS
131
        uint x=KISS>>17 ;
132
        uint y=KISS>>17 ;
133
    #endif
134
#elif TYPE == TINT64
135
    #define THEONE 4611686018427387904
136
    #if TRNG == TCONG
137
        ulong x=(ulong)(CONG>>1) ;
138
        ulong y=(ulong)(CONG>>1) ;
139
    #elif TRNG == TSHR3
140
        ulong x=(ulong)(SHR3>>1) ;
141
        ulong y=(ulong)(SHR3>>1) ;
142
    #elif TRNG == TMWC
143
        ulong x=(ulong)(MWC>>1) ;
144
        ulong y=(ulong)(MWC>>1) ;
145
    #elif TRNG == TKISS
146
        ulong x=(ulong)(KISS>>1) ;
147
        ulong y=(ulong)(KISS>>1) ;
148
    #endif
149
#elif TYPE == TFP32
150
    #define THEONE 1.0f
151
    #if TRNG == TCONG
152
        float x=CONGfp ;
153
        float y=CONGfp ;
154
    #elif TRNG == TSHR3
155
        float x=SHR3fp ;
156
        float y=SHR3fp ;
157
    #elif TRNG == TMWC
158
        float x=MWCfp ;
159
        float y=MWCfp ;
160
    #elif TRNG == TKISS
161
      float x=KISSfp ;
162
      float y=KISSfp ;
163
    #endif
164
#elif TYPE == TFP64
165
    #define THEONE 1.0f
166
    #if TRNG == TCONG
167
        double x=(double)CONGfp ;
168
        double y=(double)CONGfp ;
169
    #elif TRNG == TSHR3
170
        double x=(double)SHR3fp ;
171
        double y=(double)SHR3fp ;
172
    #elif TRNG == TMWC
173
        double x=(double)MWCfp ;
174
        double y=(double)MWCfp ;
175
    #elif TRNG == TKISS
176
        double x=(double)KISSfp ;
177
        double y=(double)KISSfp ;
178
    #endif
179
#endif
180

  
181
      ulong inside=((x*x+y*y) <= THEONE) ? 1:0;
182
      total+=inside;
183
   }
184

  
185
   return(total);
186
}
187

  
188
__global__ void MainLoopBlocks(ulong *s,ulong iterations,uint seed_w,uint seed_z)
189
{
190
   ulong total=MainLoop(iterations,seed_z,seed_w,blockIdx.x);
191
   s[blockIdx.x]=total;
192
   __syncthreads();
193

  
194
}
195

  
196
__global__ void MainLoopThreads(ulong *s,ulong iterations,uint seed_w,uint seed_z)
197
{
198
   ulong total=MainLoop(iterations,seed_z,seed_w,threadIdx.x);
199
   s[threadIdx.x]=total;
200
   __syncthreads();
201

  
202
}
203

  
204
__global__ void MainLoopHybrid(ulong *s,ulong iterations,uint seed_w,uint seed_z)
205
{
206
   ulong total=MainLoop(iterations,seed_z,seed_w,blockDim.x*blockIdx.x+threadIdx.x);
207
   s[blockDim.x*blockIdx.x+threadIdx.x]=total;
208
   __syncthreads();
209
}
210

  
211
"""
212

  
213
KERNEL_CODE_OPENCL="""
214
#define TCONG 0
215
#define TSHR3 1
216
#define TMWC 2
217
#define TKISS 3
218

  
219
#define TINT32 0
220
#define TINT64 1
221
#define TFP32 2
222
#define TFP64 3
223

  
224
// Marsaglia RNG very simple implementation
225
#define znew  ((z=36969*(z&65535)+(z>>16))<<16)
226
#define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
227

  
228
#define MWC   (znew+wnew)
229
#define SHR3  (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
230
#define CONG  (jcong=69069*jcong+1234567)
231
#define KISS  ((MWC^CONG)+SHR3)
232

  
233
#define MWCfp MWC * 2.328306435454494e-10f
234
#define KISSfp KISS * 2.328306435454494e-10f
235
#define CONGfp CONG * 2.328306435454494e-10f
236
#define SHR3fp SHR3 * 2.328306435454494e-10f
237

  
238
ulong MainLoop(ulong iterations,uint seed_z,uint seed_w,size_t work)
239
{
240

  
241
#if TRNG == TCONG
242
   uint jcong=seed_z+work;
243
#elif TRNG == TSHR3
244
   uint jsr=seed_w+work;
245
#elif TRNG == TMWC
246
   uint z=seed_z+work;
247
   uint w=seed_w+work;
248
#elif TRNG == TKISS
249
   uint jcong=seed_z+work;
250
   uint jsr=seed_w+work;
251
   uint z=seed_z-work;
252
   uint w=seed_w-work;
253
#endif
254

  
255
   ulong total=0;
256

  
257
   for (ulong i=0;i<iterations;i++) {
258

  
259
#if TYPE == TINT32
260
    #define THEONE 1073741824
261
    #if TRNG == TCONG
262
        uint x=CONG>>17 ;
263
        uint y=CONG>>17 ;
264
    #elif TRNG == TSHR3
265
        uint x=SHR3>>17 ;
266
        uint y=SHR3>>17 ;
267
    #elif TRNG == TMWC
268
        uint x=MWC>>17 ;
269
        uint y=MWC>>17 ;
270
    #elif TRNG == TKISS
271
        uint x=KISS>>17 ;
272
        uint y=KISS>>17 ;
273
    #endif
274
#elif TYPE == TINT64
275
    #define THEONE 4611686018427387904
276
    #if TRNG == TCONG
277
        ulong x=(ulong)(CONG>>1) ;
278
        ulong y=(ulong)(CONG>>1) ;
279
    #elif TRNG == TSHR3
280
        ulong x=(ulong)(SHR3>>1) ;
281
        ulong y=(ulong)(SHR3>>1) ;
282
    #elif TRNG == TMWC
283
        ulong x=(ulong)(MWC>>1) ;
284
        ulong y=(ulong)(MWC>>1) ;
285
    #elif TRNG == TKISS
286
        ulong x=(ulong)(KISS>>1) ;
287
        ulong y=(ulong)(KISS>>1) ;
288
    #endif
289
#elif TYPE == TFP32
290
    #define THEONE 1.0f
291
    #if TRNG == TCONG
292
        float x=CONGfp ;
293
        float y=CONGfp ;
294
    #elif TRNG == TSHR3
295
        float x=SHR3fp ;
296
        float y=SHR3fp ;
297
    #elif TRNG == TMWC
298
        float x=MWCfp ;
299
        float y=MWCfp ;
300
    #elif TRNG == TKISS
301
      float x=KISSfp ;
302
      float y=KISSfp ;
303
    #endif
304
#elif TYPE == TFP64
305
#pragma OPENCL EXTENSION cl_khr_fp64: enable
306
    #define THEONE 1.0f
307
    #if TRNG == TCONG
308
        double x=(double)CONGfp ;
309
        double y=(double)CONGfp ;
310
    #elif TRNG == TSHR3
311
        double x=(double)SHR3fp ;
312
        double y=(double)SHR3fp ;
313
    #elif TRNG == TMWC
314
        double x=(double)MWCfp ;
315
        double y=(double)MWCfp ;
316
    #elif TRNG == TKISS
317
        double x=(double)KISSfp ;
318
        double y=(double)KISSfp ;
319
    #endif
320
#endif
321

  
322
      ulong inside=((x*x+y*y) <= THEONE) ? 1:0;
323
      total+=inside;
324
   }
325
   
326
   return(total);
327
}
328

  
329
__kernel void MainLoopGlobal(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)
330
{
331
   ulong total=MainLoop(iterations,seed_z,seed_w,get_global_id(0));
332
   barrier(CLK_GLOBAL_MEM_FENCE);
333
   s[get_global_id(0)]=total;     
334
}
335

  
336

  
337
__kernel void MainLoopLocal(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)
338
{
339
   ulong total=MainLoop(iterations,seed_z,seed_w,get_local_id(0));
340
   barrier(CLK_LOCAL_MEM_FENCE);
341
   s[get_local_id(0)]=total;
342
}
343

  
344
__kernel void MainLoopHybrid(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)
345
{
346
   ulong total=MainLoop(iterations,seed_z,seed_w,get_global_id(0));
347
   barrier(CLK_GLOBAL_MEM_FENCE || CLK_LOCAL_MEM_FENCE);
348
   s[get_global_id(0)]=total;
349
}
350

  
351
"""
352

  
353
def MetropolisCuda(circle,iterations,steps,jobs,ParaStyle,RNG,ValueType):
354

  
355
  # Avec PyCUDA autoinit, rien a faire !
356
  
357
    circleCU = cuda.InOut(circle)
358

  
359
    try:
360
        mod = SourceModule(KERNEL_CODE_CUDA,options=['--compiler-options','-Wall -DTRNG=%i -DTYPE=%s' % (Marsaglia[RNG],Computing[ValueType])])
361
    except:
362
        print "Compilation seems to brake"
363
  
364
    MetropolisBlocksCU=mod.get_function("MainLoopBlocks")
365
    MetropolisJobsCU=mod.get_function("MainLoopThreads")
366
    MetropolisHybridCU=mod.get_function("MainLoopHybrid")
367
  
368
    start = pycuda.driver.Event()
369
    stop = pycuda.driver.Event()
370
  
371
    MyPi=numpy.zeros(steps)
372
    MyDuration=numpy.zeros(steps)
373

  
374
    if iterations%jobs==0:
375
        iterationsCL=numpy.uint64(iterations/jobs)
376
        iterationsNew=iterationsCL*jobs
377
    else:
378
        iterationsCL=numpy.uint64(iterations/jobs+1)
379
        iterationsNew=iterations
380

  
381
    for i in range(steps):
382
        start.record()
383
        start.synchronize()
384
        if ParaStyle=='Blocks':
385
            MetropolisBlocksCU(circleCU,
386
                               numpy.uint64(iterationsCL),
387
                               numpy.uint32(nprnd(2**30/jobs)),
388
                               numpy.uint32(nprnd(2**30/jobs)),
389
                               grid=(jobs,1),
390
                               block=(1,1,1))
391
            print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \
392
                (Alu,jobs,1,ParaStyle)      
393
        elif ParaStyle=='Hybrid':
394
            threads=BestThreadsNumber(jobs)
395
            MetropolisHybridCU(circleCU,
396
                               numpy.uint64(iterationsCL),
397
                               numpy.uint32(nprnd(2**30/jobs)),
398
                               numpy.uint32(nprnd(2**30/jobs)),
399
                               grid=(jobs,1),
400
                               block=(threads,1,1))
401
            print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \
402
                (Alu,jobs/threads,threads,ParaStyle)
403
        else:
404
            MetropolisJobsCU(circleCU,
405
                             numpy.uint64(iterationsCL),
406
                             numpy.uint32(nprnd(2**30/jobs)),
407
                             numpy.uint32(nprnd(2**30/jobs)),
408
                             grid=(1,1),
409
                             block=(jobs,1,1))
410
            print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \
411
                (Alu,jobs,1,ParaStyle)
412
        stop.record()
413
        stop.synchronize()
414
                
415
        elapsed = start.time_till(stop)*1e-3
416

  
417
        MyDuration[i]=elapsed
418
        AllPi=4./numpy.float32(iterationsCL)*circle.astype(numpy.float32)
419
        MyPi[i]=numpy.median(AllPi)
420
        print MyPi[i],numpy.std(AllPi),MyDuration[i]
421

  
422

  
423
    print jobs,numpy.mean(MyDuration),numpy.median(MyDuration),numpy.std(MyDuration),numpy.mean(Iterations/MyDuration),numpy.median(Iterations/MyDuration),numpy.std(Iterations/MyDuration)
424

  
425
    return(numpy.mean(MyDuration),numpy.median(MyDuration),numpy.std(MyDuration),numpy.mean(Iterations/MyDuration),numpy.median(Iterations/MyDuration),numpy.std(Iterations/MyDuration))
426

  
427

  
428
def MetropolisOpenCL(circle,iterations,steps,jobs,ParaStyle,Device,
429
                     RNG,ValueType):
430
	
431
    # Initialisation des variables en les CASTant correctement
432
     
433
    Id=1
434
    HasXPU=False
435
    for platform in cl.get_platforms():
436
        for device in platform.get_devices():
437
            if Id==Device:
438
                XPU=device
439
                print "CPU/GPU selected: ",device.name.lstrip()
440
                HasXPU=True
441
            Id+=1
442

  
443
    if HasXPU==False:
444
        print "No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1)
445
        sys.exit()              
446
				
447
    # Je cree le contexte et la queue pour son execution
448
    ctx = cl.Context([XPU])
449
    queue = cl.CommandQueue(ctx,
450
                            properties=\
451
                            cl.command_queue_properties.PROFILING_ENABLE)
452

  
453
    # Je recupere les flag possibles pour les buffers
454
    mf = cl.mem_flags
455
	
456
    circleCL = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=circle)
457

  
458
  
459
    MetropolisCL = cl.Program(ctx,KERNEL_CODE_OPENCL).build( \
460
        options = "-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%s" % (Marsaglia[RNG],Computing[ValueType]))
461

  
462
    i=0
463

  
464
    MyPi=numpy.zeros(steps)
465
    MyDuration=numpy.zeros(steps)
466
  
467
    if iterations%jobs==0:
468
        iterationsCL=numpy.uint64(iterations/jobs)
469
        iterationsNew=numpy.uint64(iterationsCL*jobs)
470
    else:
471
        iterationsCL=numpy.uint64(iterations/jobs+1)
472
        iterationsNew=numpy.uint64(iterations)
473

  
474
    for i in range(steps):
475
		
476
        if ParaStyle=='Blocks':
477
            # Call OpenCL kernel
478
            # (jobs,) is Global work size (only 1 work size)
479
            # None is local work size
480
            # circleCL is lattice translated in CL format
481
            # SeedZCL is lattice translated in CL format
482
            # SeedWCL is lattice translated in CL format
483
            # step is number of iterations
484
            CLLaunch=MetropolisCL.MainLoopGlobal(queue,(jobs,),None,circleCL,
485
                                                numpy.uint64(iterationsCL),
486
                                                numpy.uint32(nprnd(2**30/jobs)),
487
                                                numpy.uint32(nprnd(2**30/jobs)))
488
            print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \
489
                (Alu,jobs,1,ParaStyle)
490
        elif ParaStyle=='Hybrid':
491
            threads=BestThreadsNumber(jobs)
492
            # en OpenCL, necessaire de mettre un Global_id identique au local_id
493
            CLLaunch=MetropolisCL.MainLoopHybrid(queue,(jobs,),(threads,),
494
                                                 circleCL,
495
                                                 numpy.uint64(iterationsCL),
496
                                                 numpy.uint32(nprnd(2**30/jobs)),
497
                                                 numpy.uint32(nprnd(2**30/jobs)))
498
        
499
            print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \
500
                (Alu,jobs/threads,threads,ParaStyle)
501
        else:
502
            # en OpenCL, necessaire de mettre un Global_id identique au local_id
503
            CLLaunch=MetropolisCL.MainLoopLocal(queue,(jobs,),(jobs,),circleCL,
504
                                                numpy.uint64(iterationsCL),
505
                                                numpy.uint32(nprnd(2**30/jobs)),
506
                                                numpy.uint32(nprnd(2**30/jobs)))
507
            print "%s with %i %s done" % (Alu,jobs,ParaStyle)
508

  
509
        CLLaunch.wait()
510
        cl.enqueue_copy(queue, circle, circleCL).wait()
511

  
512
        elapsed = 1e-9*(CLLaunch.profile.end - CLLaunch.profile.start)
513

  
514
        print circle,numpy.mean(circle),numpy.median(circle),numpy.std(circle)
515
        MyDuration[i]=elapsed
516
        AllPi=4./numpy.float32(iterationsCL)*circle.astype(numpy.float32)
517
        MyPi[i]=numpy.median(AllPi)
518
        print MyPi[i],numpy.std(AllPi),MyDuration[i]
519

  
520
    circleCL.release()
521

  
522
    print jobs,numpy.mean(MyDuration),numpy.median(MyDuration),numpy.std(MyDuration),numpy.mean(Iterations/MyDuration),numpy.median(Iterations/MyDuration),numpy.std(Iterations/MyDuration)
523
	
524
    return(numpy.mean(MyDuration),numpy.median(MyDuration),numpy.std(MyDuration),numpy.mean(Iterations/MyDuration),numpy.median(Iterations/MyDuration),numpy.std(Iterations/MyDuration))
525

  
526

  
527
def FitAndPrint(N,D,Curves):
528

  
529
    from scipy.optimize import curve_fit
530
    import matplotlib.pyplot as plt
531

  
532
    try:
533
        coeffs_Amdahl, matcov_Amdahl = curve_fit(Amdahl, N, D)
534

  
535
        D_Amdahl=Amdahl(N,coeffs_Amdahl[0],coeffs_Amdahl[1],coeffs_Amdahl[2])
536
        coeffs_Amdahl[1]=coeffs_Amdahl[1]*coeffs_Amdahl[0]/D[0]
537
        coeffs_Amdahl[2]=coeffs_Amdahl[2]*coeffs_Amdahl[0]/D[0]
538
        coeffs_Amdahl[0]=D[0]
539
        print "Amdahl Normalized: T=%.2f(%.6f+%.6f/N)" % \
540
            (coeffs_Amdahl[0],coeffs_Amdahl[1],coeffs_Amdahl[2])
541
    except:
542
        print "Impossible to fit for Amdahl law : only %i elements" % len(D) 
543

  
544
    try:
545
        coeffs_AmdahlR, matcov_AmdahlR = curve_fit(AmdahlR, N, D)
546

  
547
        D_AmdahlR=AmdahlR(N,coeffs_AmdahlR[0],coeffs_AmdahlR[1])
548
        coeffs_AmdahlR[1]=coeffs_AmdahlR[1]*coeffs_AmdahlR[0]/D[0]
549
        coeffs_AmdahlR[0]=D[0]
550
        print "Amdahl Reduced Normalized: T=%.2f(%.6f+%.6f/N)" % \
551
            (coeffs_AmdahlR[0],1-coeffs_AmdahlR[1],coeffs_AmdahlR[1])
552

  
553
    except:
554
        print "Impossible to fit for Reduced Amdahl law : only %i elements" \
555
            % len(D) 
556

  
557
    try:
558
        coeffs_Mylq, matcov_Mylq = curve_fit(Mylq, N, D)
559

  
560
        coeffs_Mylq[1]=coeffs_Mylq[1]*coeffs_Mylq[0]/D[0]
561
        # coeffs_Mylq[2]=coeffs_Mylq[2]*coeffs_Mylq[0]/D[0]
562
        coeffs_Mylq[3]=coeffs_Mylq[3]*coeffs_Mylq[0]/D[0]
563
        coeffs_Mylq[0]=D[0]
564
        print "Mylq Normalized : T=%.2f(%.6f+%.6f/N)+%.6f*N" % (coeffs_Mylq[0],
565
                                                                coeffs_Mylq[1],
566
                                                                coeffs_Mylq[3],
567
                                                                coeffs_Mylq[2])
568
        D_Mylq=Mylq(N,coeffs_Mylq[0],coeffs_Mylq[1],coeffs_Mylq[2],
569
                    coeffs_Mylq[3])
570
    except:
571
        print "Impossible to fit for Mylq law : only %i elements" % len(D) 
572

  
573
    try:
574
        coeffs_Mylq2, matcov_Mylq2 = curve_fit(Mylq2, N, D)
575

  
576
        coeffs_Mylq2[1]=coeffs_Mylq2[1]*coeffs_Mylq2[0]/D[0]
577
        # coeffs_Mylq2[2]=coeffs_Mylq2[2]*coeffs_Mylq2[0]/D[0]
578
        # coeffs_Mylq2[3]=coeffs_Mylq2[3]*coeffs_Mylq2[0]/D[0]
579
        coeffs_Mylq2[4]=coeffs_Mylq2[4]*coeffs_Mylq2[0]/D[0]
580
        coeffs_Mylq2[0]=D[0]
581
        print "Mylq 2nd order Normalized: T=%.2f(%.6f+%.6f/N)+%.6f*N+%.6f*N^2" % \
582
            (coeffs_Mylq2[0],coeffs_Mylq2[1],
583
             coeffs_Mylq2[4],coeffs_Mylq2[2],coeffs_Mylq2[3])
584

  
585
    except:
586
        print "Impossible to fit for 2nd order Mylq law : only %i elements" % len(D) 
587

  
588
    if Curves:
589
        plt.xlabel("Number of Threads/work Items")
590
        plt.ylabel("Total Elapsed Time")
591

  
592
        Experience,=plt.plot(N,D,'ro') 
593
    try:
594
        pAmdahl,=plt.plot(N,D_Amdahl,label="Loi de Amdahl")    
595
        pMylq,=plt.plot(N,D_Mylq,label="Loi de Mylq")
596
    except:
597
        print "Fit curves seem not to be available"
598

  
599
    plt.legend()
600
    plt.show()
601

  
602
if __name__=='__main__':
603

  
604
    # Set defaults values
605
  
606
    # Alu can be CPU, GPU or ACCELERATOR
607
    Alu='CPU'
608
    # Id of GPU : 1 is for first find !
609
    Device=0
610
    # GPU style can be Cuda (Nvidia implementation) or OpenCL
611
    GpuStyle='OpenCL'
612
    # Parallel distribution can be on Threads or Blocks
613
    ParaStyle='Blocks'
614
    # Iterations is integer
615
    Iterations=100000000
616
    # JobStart in first number of Jobs to explore
617
    JobStart=1
618
    # JobEnd is last number of Jobs to explore
619
    JobEnd=16
620
    # JobStep is the step of Jobs to explore
621
    JobStep=1
622
    # Redo is the times to redo the test to improve metrology
623
    Redo=1
624
    # OutMetrology is method for duration estimation : False is GPU inside
625
    OutMetrology=False
626
    Metrology='InMetro'
627
    # Curves is True to print the curves
628
    Curves=False
629
    # Fit is True to print the curves
630
    Fit=False
631
    # Marsaglia RNG
632
    RNG='MWC'
633
    # Value type : INT32, INT64, FP32, FP64
634
    ValueType='FP32'
635
  
636
    try:
637
        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="])
638
    except getopt.GetoptError:
639
        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]
640
        sys.exit(2)
641
    
642
    for opt, arg in opts:
643
        if opt == '-h':
644
            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]
645

  
646
            print "\nInformations about devices detected under OpenCL:"
647
            # For PyOpenCL import
648
            try:
649
                import pyopencl as cl
650
                Id=1
651
                for platform in cl.get_platforms():
652
                    for device in platform.get_devices():
653
                        deviceType=cl.device_type.to_string(device.type)
654
                        print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
655
                        Id=Id+1
656

  
657
                print
658
                sys.exit()
659
            except ImportError:
660
                print "Your platform does not seem to support OpenCL"
661
        
662
        elif opt == '-o':
663
            OutMetrology=True
664
            Metrology='OutMetro'
665
        elif opt == '-c':
666
            Curves=True
667
        elif opt == '-f':
668
            Fit=True
669
        elif opt in ("-a", "--alu"):
670
            Alu = arg
671
        elif opt in ("-d", "--device"):
672
            Device = int(arg)
673
        elif opt in ("-g", "--gpustyle"):
674
            GpuStyle = arg
675
        elif opt in ("-p", "--parastyle"):
676
            ParaStyle = arg
677
        elif opt in ("-m", "--marsaglia"):
678
            RNG = arg
679
        elif opt in ("-v", "--valuetype"):
680
            ValueType = arg
681
        elif opt in ("-i", "--iterations"):
682
            Iterations = numpy.uint64(arg)
683
        elif opt in ("-s", "--jobstart"):
684
            JobStart = int(arg)
685
        elif opt in ("-e", "--jobend"):
686
            JobEnd = int(arg)
687
        elif opt in ("-t", "--jobstep"):
688
            JobStep = int(arg)
689
        elif opt in ("-r", "--redo"):
690
            Redo = int(arg)
691

  
692
    if Alu=='CPU' and GpuStyle=='CUDA':
693
        print "Alu can't be CPU for CUDA, set Alu to GPU"
694
        Alu='GPU'
695

  
696
    if ParaStyle not in ('Blocks','Threads','Hybrid'):
697
        print "%s not exists, ParaStyle set as Threads !" % ParaStyle
698
        ParaStyle='Threads'
699

  
700
    print "Compute unit : %s" % Alu
701
    print "Device Identification : %s" % Device
702
    print "GpuStyle used : %s" % GpuStyle
703
    print "Parallel Style used : %s" % ParaStyle
704
    print "Iterations : %s" % Iterations
705
    print "Number of threads on start : %s" % JobStart
706
    print "Number of threads on end : %s" % JobEnd
707
    print "Number of redo : %s" % Redo
708
    print "Metrology done out of CPU/GPU : %r" % OutMetrology
709
    print "Type of Marsaglia RNG used : %s" % RNG
710
    print "Type of variable : %s" % ValueType
711

  
712
    if GpuStyle=='CUDA':
713
        try:
714
            # For PyCUDA import
715
            import pycuda.driver as cuda
716
            import pycuda.gpuarray as gpuarray
717
            import pycuda.autoinit
718
            from pycuda.compiler import SourceModule
719
        except ImportError:
720
            print "Platform does not seem to support CUDA"
721

  
722
    if GpuStyle=='OpenCL':
723
        try:
724
            # For PyOpenCL import
725
            import pyopencl as cl
726
            Id=1
727
            for platform in cl.get_platforms():
728
                for device in platform.get_devices():
729
                    deviceType=cl.device_type.to_string(device.type)
730
                    print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
731

  
732
                    if Id == Device:
733
                    # Set the Alu as detected Device Type
734
                    Alu=deviceType
735
                    Id=Id+1
736
        except ImportError:
737
            print "Platform does not seem to support CUDA"
738
      
739
    average=numpy.array([]).astype(numpy.float32)
740
    median=numpy.array([]).astype(numpy.float32)
741
    stddev=numpy.array([]).astype(numpy.float32)
742
    averageRate=numpy.array([]).astype(numpy.float32)
743
    medianRate=numpy.array([]).astype(numpy.float32)
744
    stddevRate=numpy.array([]).astype(numpy.float32)
745

  
746
    ExploredJobs=numpy.array([]).astype(numpy.uint32)
747

  
748
    Jobs=JobStart
749

  
750
    while Jobs <= JobEnd:
751
        avg,med,std=0,0,0
752
        ExploredJobs=numpy.append(ExploredJobs,Jobs)
753
        circle=numpy.zeros(Jobs).astype(numpy.uint64)
754

  
755
        if OutMetrology: 
756
            duration=numpy.array([]).astype(numpy.float32)
757
            rate=numpy.array([]).astype(numpy.float32)
758
            for i in range(Redo):
759
                start=time.time()
760
                if GpuStyle=='CUDA':
761
                    try:
762
                        a,m,s,aR,mR,sR=MetropolisCuda(circle,Iterations,1,Jobs,ParaStyle,RNG,ValueType)
763
                    except:
764
                        print "Problem with %i // computations on Cuda" % Jobs
765
                elif GpuStyle=='OpenCL':
766
                    try:
767
                        a,m,s,aR,mR,sR=MetropolisOpenCL(circle,Iterations,1,Jobs,ParaStyle,Alu,Device,RNG,ValueType)
768
                    except:
769
                        print "Problem with %i // computations on OpenCL" % Jobs            
770
                duration=numpy.append(duration,time.time()-start)
771
                rate=numpy.append(rate,Iterations/(time.time()-start))
772
                if (a,m,s) != (0,0,0): 
773
                    avg=numpy.mean(duration)
774
                    med=numpy.median(duration)
775
                    std=numpy.std(duration)
776
                    avgR=numpy.mean(Iterations/duration)
777
                    medR=numpy.median(Iterations/duration)
778
                    stdR=numpy.std(Iterations/duration)
779
                else:
780
                    print "Values seem to be wrong..."
781
        else:
782
            if GpuStyle=='CUDA':
783
                try:
784
                    avg,med,std,avgR,medR,stdR=MetropolisCuda(circle,Iterations,Redo,Jobs,ParaStyle,RNG,ValueType)
785
                except:
786
                    print "Problem with %i // computations on Cuda" % Jobs
787
            elif GpuStyle=='OpenCL':
788
                try:
789
                    avg,med,std,avgR,medR,stdR=MetropolisOpenCL(circle,Iterations,Redo,Jobs,ParaStyle,Alu,Device,RNG,ValueType)
790
                except:
791
                    print "Problem with %i // computations on OpenCL" % Jobs            
792

  
793
            if (avg,med,std) != (0,0,0):
794
                print "jobs,avg,med,std",Jobs,avg,med,std
795
                average=numpy.append(average,avg)
796
                median=numpy.append(median,med)
797
                stddev=numpy.append(stddev,std)
798
                averageRate=numpy.append(averageRate,avgR)
799
                medianRate=numpy.append(medianRate,medR)
800
                stddevRate=numpy.append(stddevRate,stdR)
801
            else:
802
                print "Values seem to be wrong..."
803
                #THREADS*=2
804
            if len(average)!=0:
805
                averageRate=averageRate.astype(int)
806
                medianRate=medianRate.astype(int)
807
                stddevRate=stddevRate.astype(int)
808
      numpy.savez("Pi_%s_%s_%s_%s_%s_%s_%i_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu,GpuStyle,ParaStyle,JobStart,JobEnd,Iterations,Device,Metrology,gethostname()),(ExploredJobs,average,median,stddev,averageRate,medianRate,stddevRate))
809
      ToSave=[ ExploredJobs,average,median,stddev,averageRate,medianRate,stddevRate ]
810
      numpy.savetxt("Pi_%s_%s_%s_%s_%s_%s_%i_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu,GpuStyle,ParaStyle,JobStart,JobEnd,Iterations,Device,Metrology,gethostname()),numpy.transpose(ToSave),fmt='%i %e %e %e %i %i %i')
811
    Jobs+=JobStep
812

  
813
  if Fit:
814
    FitAndPrint(ExploredJobs,median,Curves)

Formats disponibles : Unified diff