Révision 106 Pi/XPU/PiXPU.py
| PiXPU.py (revision 106) | ||
|---|---|---|
| 456 | 456 |
|
| 457 | 457 |
#def MetropolisOpenCL(circle,iterations,steps,blocks,threads,Device, |
| 458 | 458 |
# RNG,ValueType): |
| 459 |
def MetropolisOpenCL(MetroParamCL):
|
|
| 459 |
def MetropolisOpenCL(InputCL):
|
|
| 460 | 460 |
|
| 461 | 461 |
import pyopencl as cl |
| 462 | 462 |
|
| 463 |
iterations=MetroParamCL['Iterations']
|
|
| 464 |
steps=MetroParamCL['Steps']
|
|
| 465 |
blocks=MetroParamCL['Blocks']
|
|
| 466 |
threads=MetroParamCL['Threads']
|
|
| 467 |
Device=MetroParamCL['Device']
|
|
| 468 |
RNG=MetroParamCL['RNG']
|
|
| 469 |
ValueType=MetroParamCL['ValueType']
|
|
| 463 |
iterations=InputCL['Iterations']
|
|
| 464 |
steps=InputCL['Steps']
|
|
| 465 |
blocks=InputCL['Blocks']
|
|
| 466 |
threads=InputCL['Threads']
|
|
| 467 |
Device=InputCL['Device']
|
|
| 468 |
RNG=InputCL['RNG']
|
|
| 469 |
ValueType=InputCL['ValueType']
|
|
| 470 | 470 |
|
| 471 |
print iterations,steps,blocks,threads,Device,RNG,ValueType |
|
| 471 |
# print iterations,steps,blocks,threads,Device,RNG,ValueType
|
|
| 472 | 472 |
|
| 473 | 473 |
Marsaglia,Computing=DictionariesAPI() |
| 474 | 474 |
|
| ... | ... | |
| 479 | 479 |
for platform in cl.get_platforms(): |
| 480 | 480 |
for device in platform.get_devices(): |
| 481 | 481 |
if Id==Device: |
| 482 |
PlatForm=platform |
|
| 482 | 483 |
XPU=device |
| 483 | 484 |
print "CPU/GPU selected: ",device.name.lstrip() |
| 484 | 485 |
HasXPU=True |
| ... | ... | |
| 489 | 490 |
sys.exit() |
| 490 | 491 |
|
| 491 | 492 |
# Je cree le contexte et la queue pour son execution |
| 492 |
ctx = cl.Context([XPU]) |
|
| 493 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
|
| 493 |
try: |
|
| 494 |
ctx = cl.Context([XPU]) |
|
| 495 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
|
| 496 |
except: |
|
| 497 |
print "Crash during context creation" |
|
| 494 | 498 |
|
| 495 | 499 |
# Je recupere les flag possibles pour les buffers |
| 496 | 500 |
mf = cl.mem_flags |
| 497 | 501 |
|
| 502 |
circle=numpy.zeros(blocks*threads).astype(numpy.uint64) |
|
| 498 | 503 |
circleCL = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=circle) |
| 499 |
|
|
| 504 |
|
|
| 500 | 505 |
MetropolisCL = cl.Program(ctx,KernelCodeOpenCL()).build( options = "-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%s" % (Marsaglia[RNG],Computing[ValueType])) |
| 501 | 506 |
|
| 502 | 507 |
MyPi=numpy.zeros(steps) |
| ... | ... | |
| 504 | 509 |
|
| 505 | 510 |
jobs=blocks*threads; |
| 506 | 511 |
|
| 507 |
if iterations%jobs==0: |
|
| 508 |
iterationsCL=numpy.uint64(iterations/jobs) |
|
| 509 |
else: |
|
| 510 |
iterationsCL=numpy.uint64(iterations/jobs+1) |
|
| 512 |
iterationsCL=numpy.uint64(iterations/jobs) |
|
| 513 |
if iterations%jobs!=0: |
|
| 514 |
iterationsCL+=1 |
|
| 511 | 515 |
|
| 512 | 516 |
for i in range(steps): |
| 513 | 517 |
start_time=time.time() |
| ... | ... | |
| 541 | 545 |
|
| 542 | 546 |
circleCL.release() |
| 543 | 547 |
|
| 544 |
return(sum(circle),iterationsCL*jobs,MyDuration) |
|
| 548 |
OutputCL={'Inside':sum(circle),'NewIterations':numpy.uint64(iterationsCL*jobs),'Duration':MyDuration}
|
|
| 549 |
print OutputCL |
|
| 550 |
return(OutputCL) |
|
| 545 | 551 |
|
| 546 | 552 |
|
| 547 | 553 |
def FitAndPrint(N,D,Curves): |
| ... | ... | |
| 798 | 804 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads) |
| 799 | 805 |
elif GpuStyle=='OpenCL': |
| 800 | 806 |
try: |
| 801 |
MetroParamCL={}
|
|
| 802 |
MetroParamCL['Iterations']=Iterations |
|
| 803 |
MetroParamCL['Steps']=1 |
|
| 804 |
MetroParamCL['Blocks']=Blocks |
|
| 805 |
MetroParamCL['Threads']=Threads |
|
| 806 |
MetroParamCL['Device']=Devices[0] |
|
| 807 |
MetroParamCL['RNG']=RNG |
|
| 808 |
MetroParamCL['ValueType']=ValueType |
|
| 809 |
|
|
| 810 |
Inside,NewIterations,DurationItem=MetropolisOpenCL(MetroParamCL) |
|
| 807 |
InputCL={}
|
|
| 808 |
InputCL['Iterations']=Iterations |
|
| 809 |
InputCL['Steps']=1 |
|
| 810 |
InputCL['Blocks']=Blocks |
|
| 811 |
InputCL['Threads']=Threads |
|
| 812 |
InputCL['Device']=Devices[0] |
|
| 813 |
InputCL['RNG']=RNG |
|
| 814 |
InputCL['ValueType']=ValueType |
|
| 815 |
OutputCL=MetropolisOpenCL(InputCL) |
|
| 816 |
Inside=OutputCL['Circle'] |
|
| 817 |
NewIterations=OutputCL['NewIterations'] |
|
| 818 |
Duration=OutputCL['Duration'] |
|
| 811 | 819 |
except: |
| 812 | 820 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads) |
| 813 | 821 |
Duration=numpy.append(Duration,time.time()-start) |
| ... | ... | |
| 820 | 828 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads) |
| 821 | 829 |
elif GpuStyle=='OpenCL': |
| 822 | 830 |
try: |
| 823 |
MetroParamCL={}
|
|
| 824 |
MetroParamCL['Iterations']=Iterations |
|
| 825 |
MetroParamCL['Steps']=Redo |
|
| 826 |
MetroParamCL['Blocks']=Blocks |
|
| 827 |
MetroParamCL['Threads']=Threads |
|
| 828 |
MetroParamCL['Device']=Devices[0] |
|
| 829 |
MetroParamCL['RNG']=RNG |
|
| 830 |
MetroParamCL['ValueType']=ValueType |
|
| 831 |
Inside,NewIterations,Duration=MetropolisOpenCL(MetroParamCL) |
|
| 831 |
InputCL={}
|
|
| 832 |
InputCL['Iterations']=Iterations |
|
| 833 |
InputCL['Steps']=Redo |
|
| 834 |
InputCL['Blocks']=Blocks |
|
| 835 |
InputCL['Threads']=Threads |
|
| 836 |
InputCL['Device']=Devices[0] |
|
| 837 |
InputCL['RNG']=RNG |
|
| 838 |
InputCL['ValueType']=ValueType |
|
| 839 |
OutputCL=MetropolisOpenCL(InputCL) |
|
| 840 |
Inside=OutputCL['Inside'] |
|
| 841 |
NewIterations=OutputCL['NewIterations'] |
|
| 842 |
Duration=OutputCL['Duration'] |
|
| 832 | 843 |
except: |
| 833 | 844 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads) |
| 834 | 845 |
Rate=NewIterations/Duration |
Formats disponibles : Unified diff