Révision 107
| Pi/XPU/PiHybrid.py (revision 107) | ||
|---|---|---|
| 26 | 26 |
import itertools |
| 27 | 27 |
from socket import gethostname |
| 28 | 28 |
|
| 29 |
import mpi4py |
|
| 30 |
from mpi4py import MPI |
|
| 29 |
from threading import Thread |
|
| 31 | 30 |
|
| 31 |
class threadWithReturn(Thread): |
|
| 32 |
def __init__(self, *args, **kwargs): |
|
| 33 |
super(threadWithReturn, self).__init__(*args, **kwargs) |
|
| 34 |
|
|
| 35 |
self._return = None |
|
| 36 |
|
|
| 37 |
def run(self): |
|
| 38 |
if self._Thread__target is not None: |
|
| 39 |
self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs) |
|
| 40 |
|
|
| 41 |
def join(self, *args, **kwargs): |
|
| 42 |
super(threadWithReturn, self).join(*args, **kwargs) |
|
| 43 |
|
|
| 44 |
return self._return |
|
| 45 |
|
|
| 32 | 46 |
from PiXPU import * |
| 33 | 47 |
|
| 34 | 48 |
if __name__=='__main__': |
| Pi/XPU/PiXpuMPI.py (revision 107) | ||
|---|---|---|
| 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 |
import itertools |
|
| 27 |
from socket import gethostname |
|
| 28 |
|
|
| 29 |
import mpi4py |
|
| 30 |
from mpi4py import MPI |
|
| 31 |
|
|
| 32 |
from PiXPU import * |
|
| 33 |
|
|
| 34 |
if __name__=='__main__': |
|
| 35 |
|
|
| 36 |
# MPI Init |
|
| 37 |
comm = MPI.COMM_WORLD |
|
| 38 |
rank = comm.Get_rank() |
|
| 39 |
|
|
| 40 |
# Define number of Nodes on with computing is performed (exclude 0) |
|
| 41 |
RankSize=comm.Get_size() |
|
| 42 |
|
|
| 43 |
if rank == 0: |
|
| 44 |
|
|
| 45 |
# Set defaults values |
|
| 46 |
|
|
| 47 |
# Id of Device : 1 is for first find ! |
|
| 48 |
Device=1 |
|
| 49 |
# GPU style can be Cuda (Nvidia implementation) or OpenCL |
|
| 50 |
GpuStyle='OpenCL' |
|
| 51 |
# Iterations is integer |
|
| 52 |
Iterations=10000000 |
|
| 53 |
# BlocksBlocks in first number of Blocks to explore |
|
| 54 |
BlocksBegin=1 |
|
| 55 |
# BlocksEnd is last number of Blocks to explore |
|
| 56 |
BlocksEnd=16 |
|
| 57 |
# BlocksStep is the step of Blocks to explore |
|
| 58 |
BlocksStep=1 |
|
| 59 |
# ThreadsBlocks in first number of Blocks to explore |
|
| 60 |
ThreadsBegin=1 |
|
| 61 |
# ThreadsEnd is last number of Blocks to explore |
|
| 62 |
ThreadsEnd=1 |
|
| 63 |
# ThreadsStep is the step of Blocks to explore |
|
| 64 |
ThreadsStep=1 |
|
| 65 |
# Redo is the times to redo the test to improve metrology |
|
| 66 |
Redo=1 |
|
| 67 |
# OutMetrology is method for duration estimation : False is GPU inside |
|
| 68 |
OutMetrology=False |
|
| 69 |
Metrology='InMetro' |
|
| 70 |
# Curves is True to print the curves |
|
| 71 |
Curves=False |
|
| 72 |
# Fit is True to print the curves |
|
| 73 |
Fit=False |
|
| 74 |
# Marsaglia RNG |
|
| 75 |
RNG='MWC' |
|
| 76 |
# Value type : INT32, INT64, FP32, FP64 |
|
| 77 |
ValueType='FP32' |
|
| 78 |
|
|
| 79 |
HowToUse='%s -o (Out of Core Metrology) -c (Print Curves) -d <DeviceId> -g <CUDA/OpenCL> -i <Iterations> -b <BlocksBegin> -e <BlocksEnd> -s <BlocksStep> -f <ThreadsFirst> -l <ThreadsLast> -t <ThreadssTep> -r <RedoToImproveStats> -m <SHR3/CONG/MWC/KISS> -v <INT32/INT64/FP32/FP64>' |
|
| 80 |
|
|
| 81 |
try: |
|
| 82 |
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="]) |
|
| 83 |
except getopt.GetoptError: |
|
| 84 |
print HowToUse % sys.argv[0] |
|
| 85 |
sys.exit(2) |
|
| 86 |
|
|
| 87 |
# List of Devices |
|
| 88 |
Devices=[] |
|
| 89 |
Alu={}
|
|
| 90 |
|
|
| 91 |
for opt, arg in opts: |
|
| 92 |
if opt == '-h': |
|
| 93 |
print HowToUse % sys.argv[0] |
|
| 94 |
|
|
| 95 |
print "\nInformations about devices detected under OpenCL:" |
|
| 96 |
# For PyOpenCL import |
|
| 97 |
try: |
|
| 98 |
import pyopencl as cl |
|
| 99 |
Id=1 |
|
| 100 |
for platform in cl.get_platforms(): |
|
| 101 |
for device in platform.get_devices(): |
|
| 102 |
deviceType=cl.device_type.to_string(device.type) |
|
| 103 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()) |
|
| 104 |
Id=Id+1 |
|
| 105 |
|
|
| 106 |
|
|
| 107 |
sys.exit() |
|
| 108 |
except ImportError: |
|
| 109 |
print "Your platform does not seem to support OpenCL" |
|
| 110 |
|
|
| 111 |
elif opt == '-o': |
|
| 112 |
OutMetrology=True |
|
| 113 |
Metrology='OutMetro' |
|
| 114 |
elif opt == '-c': |
|
| 115 |
Curves=True |
|
| 116 |
elif opt in ("-d", "--device"):
|
|
| 117 |
Devices.append(int(arg)) |
|
| 118 |
elif opt in ("-g", "--gpustyle"):
|
|
| 119 |
GpuStyle = arg |
|
| 120 |
elif opt in ("-m", "--marsaglia"):
|
|
| 121 |
RNG = arg |
|
| 122 |
elif opt in ("-v", "--valuetype"):
|
|
| 123 |
ValueType = arg |
|
| 124 |
elif opt in ("-i", "--iterations"):
|
|
| 125 |
Iterations = numpy.uint64(arg) |
|
| 126 |
elif opt in ("-b", "--blocksbegin"):
|
|
| 127 |
BlocksBegin = int(arg) |
|
| 128 |
elif opt in ("-e", "--blocksend"):
|
|
| 129 |
BlocksEnd = int(arg) |
|
| 130 |
elif opt in ("-s", "--blocksstep"):
|
|
| 131 |
BlocksStep = int(arg) |
|
| 132 |
elif opt in ("-f", "--threadsfirst"):
|
|
| 133 |
ThreadsBegin = int(arg) |
|
| 134 |
elif opt in ("-l", "--threadslast"):
|
|
| 135 |
ThreadsEnd = int(arg) |
|
| 136 |
elif opt in ("-t", "--threadsstep"):
|
|
| 137 |
ThreadsStep = int(arg) |
|
| 138 |
elif opt in ("-r", "--redo"):
|
|
| 139 |
Redo = int(arg) |
|
| 140 |
|
|
| 141 |
print "Devices Identification : %s" % Devices |
|
| 142 |
print "GpuStyle used : %s" % GpuStyle |
|
| 143 |
print "Iterations : %s" % Iterations |
|
| 144 |
print "Number of Blocks on begin : %s" % BlocksBegin |
|
| 145 |
print "Number of Blocks on end : %s" % BlocksEnd |
|
| 146 |
print "Step on Blocks : %s" % BlocksStep |
|
| 147 |
print "Number of Threads on begin : %s" % ThreadsBegin |
|
| 148 |
print "Number of Threads on end : %s" % ThreadsEnd |
|
| 149 |
print "Step on Threads : %s" % ThreadsStep |
|
| 150 |
print "Number of redo : %s" % Redo |
|
| 151 |
print "Metrology done out of XPU : %r" % OutMetrology |
|
| 152 |
print "Type of Marsaglia RNG used : %s" % RNG |
|
| 153 |
print "Type of variable : %s" % ValueType |
|
| 154 |
|
|
| 155 |
if GpuStyle=='CUDA': |
|
| 156 |
try: |
|
| 157 |
# For PyCUDA import |
|
| 158 |
import pycuda.driver as cuda |
|
| 159 |
import pycuda.gpuarray as gpuarray |
|
| 160 |
import pycuda.autoinit |
|
| 161 |
from pycuda.compiler import SourceModule |
|
| 162 |
except ImportError: |
|
| 163 |
print "Platform does not seem to support CUDA" |
|
| 164 |
|
|
| 165 |
if GpuStyle=='OpenCL': |
|
| 166 |
try: |
|
| 167 |
# For PyOpenCL import |
|
| 168 |
import pyopencl as cl |
|
| 169 |
Id=1 |
|
| 170 |
for platform in cl.get_platforms(): |
|
| 171 |
for device in platform.get_devices(): |
|
| 172 |
deviceType=cl.device_type.to_string(device.type) |
|
| 173 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()) |
|
| 174 |
|
|
| 175 |
if Id in Devices: |
|
| 176 |
# Set the Alu as detected Device Type |
|
| 177 |
Alu[Id]=deviceType |
|
| 178 |
Id=Id+1 |
|
| 179 |
except ImportError: |
|
| 180 |
print "Platform does not seem to support OpenCL" |
|
| 181 |
|
|
| 182 |
print Devices,Alu |
|
| 183 |
|
|
| 184 |
BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep) |
|
| 185 |
ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep) |
|
| 186 |
|
|
| 187 |
ExploredJobs=numpy.array([]).astype(numpy.uint32) |
|
| 188 |
ExploredBlocks=numpy.array([]).astype(numpy.uint32) |
|
| 189 |
ExploredThreads=numpy.array([]).astype(numpy.uint32) |
|
| 190 |
avgD=numpy.array([]).astype(numpy.float32) |
|
| 191 |
medD=numpy.array([]).astype(numpy.float32) |
|
| 192 |
stdD=numpy.array([]).astype(numpy.float32) |
|
| 193 |
minD=numpy.array([]).astype(numpy.float32) |
|
| 194 |
maxD=numpy.array([]).astype(numpy.float32) |
|
| 195 |
avgR=numpy.array([]).astype(numpy.float32) |
|
| 196 |
medR=numpy.array([]).astype(numpy.float32) |
|
| 197 |
stdR=numpy.array([]).astype(numpy.float32) |
|
| 198 |
minR=numpy.array([]).astype(numpy.float32) |
|
| 199 |
maxR=numpy.array([]).astype(numpy.float32) |
|
| 200 |
|
|
| 201 |
for Blocks,Threads in itertools.product(BlocksList,ThreadsList): |
|
| 202 |
|
|
| 203 |
# print Blocks,Threads |
|
| 204 |
circle=numpy.zeros(Blocks*Threads).astype(numpy.uint64) |
|
| 205 |
ExploredJobs=numpy.append(ExploredJobs,Blocks*Threads) |
|
| 206 |
ExploredBlocks=numpy.append(ExploredBlocks,Blocks) |
|
| 207 |
ExploredThreads=numpy.append(ExploredThreads,Threads) |
|
| 208 |
|
|
| 209 |
if OutMetrology: |
|
| 210 |
DurationItem=numpy.array([]).astype(numpy.float32) |
|
| 211 |
Duration=numpy.array([]).astype(numpy.float32) |
|
| 212 |
Rate=numpy.array([]).astype(numpy.float32) |
|
| 213 |
for i in range(Redo): |
|
| 214 |
start=time.time() |
|
| 215 |
if GpuStyle=='CUDA': |
|
| 216 |
try: |
|
| 217 |
Inside,NewIterations,DurationItem=MetropolisCuda(circle,Iterations,1,Blocks,Threads,RNG,ValueType) |
|
| 218 |
except: |
|
| 219 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads) |
|
| 220 |
elif GpuStyle=='OpenCL': |
|
| 221 |
try: |
|
| 222 |
MetroParamCL={}
|
|
| 223 |
MetroParamCL['Iterations']=Iterations |
|
| 224 |
MetroParamCL['Steps']=1 |
|
| 225 |
MetroParamCL['Blocks']=Blocks |
|
| 226 |
MetroParamCL['Threads']=Threads |
|
| 227 |
MetroParamCL['Device']=Devices[0] |
|
| 228 |
MetroParamCL['RNG']=RNG |
|
| 229 |
MetroParamCL['ValueType']=ValueType |
|
| 230 |
|
|
| 231 |
OutputCL=MetropolisOpenCL(MetroParamCL) |
|
| 232 |
except: |
|
| 233 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads) |
|
| 234 |
Duration=numpy.append(Duration,time.time()-start) |
|
| 235 |
Rate=numpy.append(Rate,NewIterations/Duration[-1]) |
|
| 236 |
else: |
|
| 237 |
if GpuStyle=='CUDA': |
|
| 238 |
try: |
|
| 239 |
Inside,NewIterations,Duration=MetropolisCuda(circle,Iterations,Redo,Blocks,Threads,RNG,ValueType) |
|
| 240 |
except: |
|
| 241 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads) |
|
| 242 |
elif GpuStyle=='OpenCL': |
|
| 243 |
try: |
|
| 244 |
IterationsMPI=numpy.uint64(Iterations/len(Devices)) |
|
| 245 |
if Iterations%len(Devices)!=0: |
|
| 246 |
IterationsMPI+=1 |
|
| 247 |
|
|
| 248 |
r=1 |
|
| 249 |
time_start=time.time() |
|
| 250 |
for Device in Devices: |
|
| 251 |
InputCL={}
|
|
| 252 |
InputCL['Iterations']=IterationsMPI |
|
| 253 |
InputCL['Steps']=Redo |
|
| 254 |
InputCL['Blocks']=Blocks |
|
| 255 |
InputCL['Threads']=Threads |
|
| 256 |
InputCL['Device']=Device |
|
| 257 |
InputCL['RNG']=RNG |
|
| 258 |
InputCL['ValueType']=ValueType |
|
| 259 |
print "Send to device %i on rank %i" % (Device,r) |
|
| 260 |
comm.send('CONTINUE',dest=r,tag=11)
|
|
| 261 |
comm.send(InputCL,dest=r,tag=11) |
|
| 262 |
r+=1 |
|
| 263 |
|
|
| 264 |
Inside=0 |
|
| 265 |
NewIterations=0 |
|
| 266 |
for slave in xrange(1,len(Devices)+1): |
|
| 267 |
print "slave %i" % slave |
|
| 268 |
OutputCL=comm.recv(source=slave,tag=11) |
|
| 269 |
print "OutputCL from rank %s %s" % (slave,OutputCL) |
|
| 270 |
NewIterations+=OutputCL['NewIterations'] |
|
| 271 |
Inside+=OutputCL['Inside'] |
|
| 272 |
|
|
| 273 |
Duration=time.time()-time_start |
|
| 274 |
print "Pi estimation %.8f" % (4./NewIterations*Inside) |
|
| 275 |
except: |
|
| 276 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads) |
|
| 277 |
Duration=OutputCL['Duration'] |
|
| 278 |
NewIterations=OutputCL['NewIterations'] |
|
| 279 |
Rate=NewIterations/Duration |
|
| 280 |
|
|
| 281 |
avgD=numpy.append(avgD,numpy.average(Duration)) |
|
| 282 |
medD=numpy.append(medD,numpy.median(Duration)) |
|
| 283 |
stdD=numpy.append(stdD,numpy.std(Duration)) |
|
| 284 |
minD=numpy.append(minD,numpy.min(Duration)) |
|
| 285 |
maxD=numpy.append(maxD,numpy.max(Duration)) |
|
| 286 |
avgR=numpy.append(avgR,numpy.average(Rate)) |
|
| 287 |
medR=numpy.append(medR,numpy.median(Rate)) |
|
| 288 |
stdR=numpy.append(stdR,numpy.std(Rate)) |
|
| 289 |
minR=numpy.append(minR,numpy.min(Rate)) |
|
| 290 |
maxR=numpy.append(maxR,numpy.max(Rate)) |
|
| 291 |
|
|
| 292 |
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]) |
|
| 293 |
|
|
| 294 |
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))
|
|
| 295 |
ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ] |
|
| 296 |
numpy.savetxt("Pi_%s_%s_%s_%s_%s_%s_%s_%i_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu[Devices[0]],GpuStyle,BlocksBegin,BlocksEnd,ThreadsBegin,ThreadsEnd,Iterations,Devices[0],Metrology,gethostname()),numpy.transpose(ToSave),fmt='%i %i %e %e %e %e %e %i %i %i %i %i')
|
|
| 297 |
|
|
| 298 |
if Fit: |
|
| 299 |
FitAndPrint(ExploredJobs,median,Curves) |
|
| 300 |
# Send MPI exit tag |
|
| 301 |
for slave in xrange(1,RankSize): |
|
| 302 |
comm.send('BREAK',dest=slave,tag=11)
|
|
| 303 |
|
|
| 304 |
else: |
|
| 305 |
while True: |
|
| 306 |
Signal=comm.recv(source=0,tag=11) |
|
| 307 |
if Signal=='CONTINUE': |
|
| 308 |
# Receive information from Master |
|
| 309 |
InputCL=comm.recv(source=0,tag=11) |
|
| 310 |
print "Parameters retreive from master %s " % InputCL |
|
| 311 |
# Execute on slave |
|
| 312 |
OutputCL=MetropolisOpenCL(InputCL) |
|
| 313 |
print OutputCL |
|
| 314 |
# Send information to Master |
|
| 315 |
comm.send(OutputCL,dest=0,tag=11) |
|
| 316 |
print "Data sent to master" |
|
| 317 |
else: |
|
| 318 |
print 'Exit signal from Master' |
|
| 319 |
break |
|
| 0 | 320 | |
| Pi/XPU/PiXpuThreads.py (revision 107) | ||
|---|---|---|
| 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 |
import itertools |
|
| 27 |
from socket import gethostname |
|
| 28 |
|
|
| 29 |
from threading import Thread |
|
| 30 |
|
|
| 31 |
from PiXPU import * |
|
| 32 |
|
|
| 33 |
class threadWithReturn(Thread): |
|
| 34 |
def __init__(self, *args, **kwargs): |
|
| 35 |
super(threadWithReturn, self).__init__(*args, **kwargs) |
|
| 36 |
|
|
| 37 |
self._return = None |
|
| 38 |
|
|
| 39 |
def run(self): |
|
| 40 |
if self._Thread__target is not None: |
|
| 41 |
self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs) |
|
| 42 |
|
|
| 43 |
def join(self, *args, **kwargs): |
|
| 44 |
super(threadWithReturn, self).join(*args, **kwargs) |
|
| 45 |
|
|
| 46 |
return self._return |
|
| 47 |
|
|
| 48 |
if __name__=='__main__': |
|
| 49 |
|
|
| 50 |
# Set defaults values |
|
| 51 |
|
|
| 52 |
# Id of Device : 1 is for first find ! |
|
| 53 |
Device=1 |
|
| 54 |
# GPU style can be Cuda (Nvidia implementation) or OpenCL |
|
| 55 |
GpuStyle='OpenCL' |
|
| 56 |
# Iterations is integer |
|
| 57 |
Iterations=10000000 |
|
| 58 |
# BlocksBlocks in first number of Blocks to explore |
|
| 59 |
BlocksBegin=1 |
|
| 60 |
# BlocksEnd is last number of Blocks to explore |
|
| 61 |
BlocksEnd=16 |
|
| 62 |
# BlocksStep is the step of Blocks to explore |
|
| 63 |
BlocksStep=1 |
|
| 64 |
# ThreadsBlocks in first number of Blocks to explore |
|
| 65 |
ThreadsBegin=1 |
|
| 66 |
# ThreadsEnd is last number of Blocks to explore |
|
| 67 |
ThreadsEnd=1 |
|
| 68 |
# ThreadsStep is the step of Blocks to explore |
|
| 69 |
ThreadsStep=1 |
|
| 70 |
# Redo is the times to redo the test to improve metrology |
|
| 71 |
Redo=1 |
|
| 72 |
# OutMetrology is method for duration estimation : False is GPU inside |
|
| 73 |
OutMetrology=False |
|
| 74 |
Metrology='InMetro' |
|
| 75 |
# Curves is True to print the curves |
|
| 76 |
Curves=False |
|
| 77 |
# Fit is True to print the curves |
|
| 78 |
Fit=False |
|
| 79 |
# Marsaglia RNG |
|
| 80 |
RNG='MWC' |
|
| 81 |
# Value type : INT32, INT64, FP32, FP64 |
|
| 82 |
ValueType='FP32' |
|
| 83 |
|
|
| 84 |
HowToUse='%s -o (Out of Core Metrology) -c (Print Curves) -d <DeviceId> -g <CUDA/OpenCL> -i <Iterations> -b <BlocksBegin> -e <BlocksEnd> -s <BlocksStep> -f <ThreadsFirst> -l <ThreadsLast> -t <ThreadssTep> -r <RedoToImproveStats> -m <SHR3/CONG/MWC/KISS> -v <INT32/INT64/FP32/FP64>' |
|
| 85 |
|
|
| 86 |
try: |
|
| 87 |
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="]) |
|
| 88 |
except getopt.GetoptError: |
|
| 89 |
print HowToUse % sys.argv[0] |
|
| 90 |
sys.exit(2) |
|
| 91 |
|
|
| 92 |
# List of Devices |
|
| 93 |
Devices=[] |
|
| 94 |
Alu={}
|
|
| 95 |
|
|
| 96 |
for opt, arg in opts: |
|
| 97 |
if opt == '-h': |
|
| 98 |
print HowToUse % sys.argv[0] |
|
| 99 |
|
|
| 100 |
print "\nInformations about devices detected under OpenCL:" |
|
| 101 |
# For PyOpenCL import |
|
| 102 |
try: |
|
| 103 |
import pyopencl as cl |
|
| 104 |
Id=1 |
|
| 105 |
for platform in cl.get_platforms(): |
|
| 106 |
for device in platform.get_devices(): |
|
| 107 |
deviceType=cl.device_type.to_string(device.type) |
|
| 108 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()) |
|
| 109 |
Id=Id+1 |
|
| 110 |
|
|
| 111 |
|
|
| 112 |
sys.exit() |
|
| 113 |
except ImportError: |
|
| 114 |
print "Your platform does not seem to support OpenCL" |
|
| 115 |
|
|
| 116 |
elif opt == '-o': |
|
| 117 |
OutMetrology=True |
|
| 118 |
Metrology='OutMetro' |
|
| 119 |
elif opt == '-c': |
|
| 120 |
Curves=True |
|
| 121 |
elif opt in ("-d", "--device"):
|
|
| 122 |
Devices.append(int(arg)) |
|
| 123 |
elif opt in ("-g", "--gpustyle"):
|
|
| 124 |
GpuStyle = arg |
|
| 125 |
elif opt in ("-m", "--marsaglia"):
|
|
| 126 |
RNG = arg |
|
| 127 |
elif opt in ("-v", "--valuetype"):
|
|
| 128 |
ValueType = arg |
|
| 129 |
elif opt in ("-i", "--iterations"):
|
|
| 130 |
Iterations = numpy.uint64(arg) |
|
| 131 |
elif opt in ("-b", "--blocksbegin"):
|
|
| 132 |
BlocksBegin = int(arg) |
|
| 133 |
elif opt in ("-e", "--blocksend"):
|
|
| 134 |
BlocksEnd = int(arg) |
|
| 135 |
elif opt in ("-s", "--blocksstep"):
|
|
| 136 |
BlocksStep = int(arg) |
|
| 137 |
elif opt in ("-f", "--threadsfirst"):
|
|
| 138 |
ThreadsBegin = int(arg) |
|
| 139 |
elif opt in ("-l", "--threadslast"):
|
|
| 140 |
ThreadsEnd = int(arg) |
|
| 141 |
elif opt in ("-t", "--threadsstep"):
|
|
| 142 |
ThreadsStep = int(arg) |
|
| 143 |
elif opt in ("-r", "--redo"):
|
|
| 144 |
Redo = int(arg) |
|
| 145 |
|
|
| 146 |
print "Devices Identification : %s" % Devices |
|
| 147 |
print "GpuStyle used : %s" % GpuStyle |
|
| 148 |
print "Iterations : %s" % Iterations |
|
| 149 |
print "Number of Blocks on begin : %s" % BlocksBegin |
|
| 150 |
print "Number of Blocks on end : %s" % BlocksEnd |
|
| 151 |
print "Step on Blocks : %s" % BlocksStep |
|
| 152 |
print "Number of Threads on begin : %s" % ThreadsBegin |
|
| 153 |
print "Number of Threads on end : %s" % ThreadsEnd |
|
| 154 |
print "Step on Threads : %s" % ThreadsStep |
|
| 155 |
print "Number of redo : %s" % Redo |
|
| 156 |
print "Metrology done out of XPU : %r" % OutMetrology |
|
| 157 |
print "Type of Marsaglia RNG used : %s" % RNG |
|
| 158 |
print "Type of variable : %s" % ValueType |
|
| 159 |
|
|
| 160 |
if GpuStyle=='CUDA': |
|
| 161 |
try: |
|
| 162 |
# For PyCUDA import |
|
| 163 |
import pycuda.driver as cuda |
|
| 164 |
import pycuda.gpuarray as gpuarray |
|
| 165 |
import pycuda.autoinit |
|
| 166 |
from pycuda.compiler import SourceModule |
|
| 167 |
except ImportError: |
|
| 168 |
print "Platform does not seem to support CUDA" |
|
| 169 |
|
|
| 170 |
if GpuStyle=='OpenCL': |
|
| 171 |
try: |
|
| 172 |
# For PyOpenCL import |
|
| 173 |
import pyopencl as cl |
|
| 174 |
Id=1 |
|
| 175 |
for platform in cl.get_platforms(): |
|
| 176 |
for device in platform.get_devices(): |
|
| 177 |
deviceType=cl.device_type.to_string(device.type) |
|
| 178 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()) |
|
| 179 |
|
|
| 180 |
if Id in Devices: |
|
| 181 |
# Set the Alu as detected Device Type |
|
| 182 |
Alu[Id]=deviceType |
|
| 183 |
Id=Id+1 |
|
| 184 |
except ImportError: |
|
| 185 |
print "Platform does not seem to support OpenCL" |
|
| 186 |
|
|
| 187 |
print Devices,Alu |
|
| 188 |
|
|
| 189 |
BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep) |
|
| 190 |
ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep) |
|
| 191 |
|
|
| 192 |
ExploredJobs=numpy.array([]).astype(numpy.uint32) |
|
| 193 |
ExploredBlocks=numpy.array([]).astype(numpy.uint32) |
|
| 194 |
ExploredThreads=numpy.array([]).astype(numpy.uint32) |
|
| 195 |
avgD=numpy.array([]).astype(numpy.float32) |
|
| 196 |
medD=numpy.array([]).astype(numpy.float32) |
|
| 197 |
stdD=numpy.array([]).astype(numpy.float32) |
|
| 198 |
minD=numpy.array([]).astype(numpy.float32) |
|
| 199 |
maxD=numpy.array([]).astype(numpy.float32) |
|
| 200 |
avgR=numpy.array([]).astype(numpy.float32) |
|
| 201 |
medR=numpy.array([]).astype(numpy.float32) |
|
| 202 |
stdR=numpy.array([]).astype(numpy.float32) |
|
| 203 |
minR=numpy.array([]).astype(numpy.float32) |
|
| 204 |
maxR=numpy.array([]).astype(numpy.float32) |
|
| 205 |
|
|
| 206 |
for Blocks,Threads in itertools.product(BlocksList,ThreadsList): |
|
| 207 |
|
|
| 208 |
ExploredJobs=numpy.append(ExploredJobs,Blocks*Threads) |
|
| 209 |
ExploredBlocks=numpy.append(ExploredBlocks,Blocks) |
|
| 210 |
ExploredThreads=numpy.append(ExploredThreads,Threads) |
|
| 211 |
|
|
| 212 |
IterationsMP=Iterations/len(Devices) |
|
| 213 |
if Iterations%len(Devices)!=0: |
|
| 214 |
IterationsMP+=1 |
|
| 215 |
MyThreads=[] |
|
| 216 |
|
|
| 217 |
if OutMetrology: |
|
| 218 |
DurationItem=numpy.array([]).astype(numpy.float32) |
|
| 219 |
Duration=numpy.array([]).astype(numpy.float32) |
|
| 220 |
Rate=numpy.array([]).astype(numpy.float32) |
|
| 221 |
for i in xrange(Redo): |
|
| 222 |
time_start=time.time() |
|
| 223 |
|
|
| 224 |
for Device in Devices: |
|
| 225 |
InputCL={}
|
|
| 226 |
InputCL['Iterations']=IterationsMP |
|
| 227 |
InputCL['Steps']=1 |
|
| 228 |
InputCL['Blocks']=Blocks |
|
| 229 |
InputCL['Threads']=Threads |
|
| 230 |
InputCL['Device']=Device |
|
| 231 |
InputCL['RNG']=RNG |
|
| 232 |
InputCL['ValueType']=ValueType |
|
| 233 |
if GpuStyle=='CUDA': |
|
| 234 |
try: |
|
| 235 |
MyThread=threadWithReturn(target=MetropolisCUDA, args=(InputCL,)) |
|
| 236 |
except: |
|
| 237 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads) |
|
| 238 |
elif GpuStyle=='OpenCL': |
|
| 239 |
try: |
|
| 240 |
MyThread=threadWithReturn(target=MetropolisOpenCL, args=(InputCL,)) |
|
| 241 |
except: |
|
| 242 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads) |
|
| 243 |
|
|
| 244 |
print "Start on #%i device..." % Device |
|
| 245 |
MyThread.start() |
|
| 246 |
MyThreads.append(MyThread) |
|
| 247 |
|
|
| 248 |
NewIterations=0 |
|
| 249 |
Inside=0 |
|
| 250 |
for MyThread in MyThreads: |
|
| 251 |
OutputCL=MyThread.join() |
|
| 252 |
NewIterations+=OutputCL['NewIterations'] |
|
| 253 |
Inside+=OutputCL['Inside'] |
|
| 254 |
print "Pi equals:",4./NewIterations*Inside |
|
| 255 |
|
|
| 256 |
Duration=numpy.append(Duration,time.time()-time_start) |
|
| 257 |
Rate=numpy.append(Rate,NewIterations/Duration[-1]) |
|
| 258 |
|
|
| 259 |
else: |
|
| 260 |
time_start=time.time() |
|
| 261 |
for Device in Devices: |
|
| 262 |
InputCL={}
|
|
| 263 |
InputCL['Iterations']=IterationsMP |
|
| 264 |
InputCL['Steps']=Redo |
|
| 265 |
InputCL['Blocks']=Blocks |
|
| 266 |
InputCL['Threads']=Threads |
|
| 267 |
InputCL['Device']=Device |
|
| 268 |
InputCL['RNG']=RNG |
|
| 269 |
InputCL['ValueType']=ValueType |
|
| 270 |
if GpuStyle=='CUDA': |
|
| 271 |
try: |
|
| 272 |
MyThread=threadWithReturn(target=MetropolisCUDA, args=(InputCL,)) |
|
| 273 |
except: |
|
| 274 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads) |
|
| 275 |
elif GpuStyle=='OpenCL': |
|
| 276 |
try: |
|
| 277 |
MyThread=threadWithReturn(target=MetropolisOpenCL, args=(InputCL,)) |
|
| 278 |
except: |
|
| 279 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads) |
|
| 280 |
|
|
| 281 |
print "Start on #%i device..." % Device |
|
| 282 |
MyThread.start() |
|
| 283 |
MyThreads.append(MyThread) |
|
| 284 |
|
|
| 285 |
NewIterations=0 |
|
| 286 |
Inside=0 |
|
| 287 |
for MyThread in MyThreads: |
|
| 288 |
OutputCL=MyThread.join() |
|
| 289 |
NewIterations+=OutputCL['NewIterations'] |
|
| 290 |
Inside+=OutputCL['Inside'] |
|
| 291 |
Duration=time.time()-time_start |
|
| 292 |
print "Pi equals:",4./NewIterations*Inside |
|
| 293 |
Rate=1./Duration*NewIterations |
|
| 294 |
|
|
| 295 |
avgD=numpy.append(avgD,numpy.average(Duration)) |
|
| 296 |
medD=numpy.append(medD,numpy.median(Duration)) |
|
| 297 |
stdD=numpy.append(stdD,numpy.std(Duration)) |
|
| 298 |
minD=numpy.append(minD,numpy.min(Duration)) |
|
| 299 |
maxD=numpy.append(maxD,numpy.max(Duration)) |
|
| 300 |
avgR=numpy.append(avgR,numpy.average(Rate)) |
|
| 301 |
medR=numpy.append(medR,numpy.median(Rate)) |
|
| 302 |
stdR=numpy.append(stdR,numpy.std(Rate)) |
|
| 303 |
minR=numpy.append(minR,numpy.min(Rate)) |
|
| 304 |
maxR=numpy.append(maxR,numpy.max(Rate)) |
|
| 305 |
|
|
| 306 |
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]) |
|
| 307 |
|
|
| 308 |
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))
|
|
| 309 |
ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ] |
|
| 310 |
numpy.savetxt("Pi_%s_%s_%s_%s_%s_%s_%s_%i_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu[Devices[0]],GpuStyle,BlocksBegin,BlocksEnd,ThreadsBegin,ThreadsEnd,Iterations,Devices[0],Metrology,gethostname()),numpy.transpose(ToSave),fmt='%i %i %e %e %e %e %e %i %i %i %i %i')
|
|
| 311 |
|
|
| 312 |
if Fit: |
|
| 313 |
FitAndPrint(ExploredJobs,median,Curves) |
|
| 0 | 314 | |
| Pi/XPU/PiXPU.py (revision 107) | ||
|---|---|---|
| 454 | 454 |
|
| 455 | 455 |
return(numpy.mean(MyDuration),numpy.median(MyDuration),numpy.std(MyDuration),numpy.min(MyDuration),numpy.max(MyDuration),numpy.mean(iterationsNew/MyDuration),numpy.median(iterationsNew/MyDuration),numpy.std(iterationsNew/MyDuration),numpy.max(iterationsNew/MyDuration),numpy.max(iterationsNew/MyDuration)) |
| 456 | 456 |
|
| 457 |
#def MetropolisOpenCL(circle,iterations,steps,blocks,threads,Device, |
|
| 458 |
# RNG,ValueType): |
|
| 459 | 457 |
def MetropolisOpenCL(InputCL): |
| 460 | 458 |
|
| 461 | 459 |
import pyopencl as cl |
| 462 | 460 |
|
| 461 |
print "Inside ",InputCL |
|
| 462 |
|
|
| 463 | 463 |
iterations=InputCL['Iterations'] |
| 464 | 464 |
steps=InputCL['Steps'] |
| 465 | 465 |
blocks=InputCL['Blocks'] |
Formats disponibles : Unified diff