Révision 127 Pi/XPU/PiXpuMPI.py
| PiXpuMPI.py (revision 127) | ||
|---|---|---|
| 1 |
#!/usr/bin/env python |
|
| 1 |
#!/usr/bin/env python3
|
|
| 2 | 2 |
|
| 3 | 3 |
# |
| 4 | 4 |
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL |
| ... | ... | |
| 81 | 81 |
try: |
| 82 | 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 | 83 |
except getopt.GetoptError: |
| 84 |
print HowToUse % sys.argv[0]
|
|
| 84 |
print(HowToUse % sys.argv[0])
|
|
| 85 | 85 |
sys.exit(2) |
| 86 | 86 |
|
| 87 | 87 |
# List of Devices |
| ... | ... | |
| 90 | 90 |
|
| 91 | 91 |
for opt, arg in opts: |
| 92 | 92 |
if opt == '-h': |
| 93 |
print HowToUse % sys.argv[0]
|
|
| 93 |
print(HowToUse % sys.argv[0])
|
|
| 94 | 94 |
|
| 95 |
print "\nInformations about devices detected under OpenCL:"
|
|
| 95 |
print("\nInformations about devices detected under OpenCL:")
|
|
| 96 | 96 |
# For PyOpenCL import |
| 97 | 97 |
try: |
| 98 | 98 |
import pyopencl as cl |
| ... | ... | |
| 100 | 100 |
for platform in cl.get_platforms(): |
| 101 | 101 |
for device in platform.get_devices(): |
| 102 | 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())
|
|
| 103 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
|
|
| 104 | 104 |
Id=Id+1 |
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
except: |
| 108 |
print "Your platform does not seem to support OpenCL"
|
|
| 108 |
print("Your platform does not seem to support OpenCL")
|
|
| 109 | 109 |
|
| 110 | 110 |
# For PyCUDA import |
| 111 | 111 |
try: |
| ... | ... | |
| 113 | 113 |
cuda.init() |
| 114 | 114 |
for Id in range(cuda.Device.count()): |
| 115 | 115 |
device=cuda.Device(Id) |
| 116 |
print "Device #%i of type GPU : %s" % (Id,device.name())
|
|
| 116 |
print("Device #%i of type GPU : %s" % (Id,device.name()))
|
|
| 117 | 117 |
|
| 118 | 118 |
except: |
| 119 |
print "Your platform does not seem to support CUDA"
|
|
| 119 |
print("Your platform does not seem to support CUDA")
|
|
| 120 | 120 |
|
| 121 | 121 |
sys.exit() |
| 122 | 122 |
|
| ... | ... | |
| 150 | 150 |
elif opt in ("-r", "--redo"):
|
| 151 | 151 |
Redo = int(arg) |
| 152 | 152 |
|
| 153 |
print "Devices Identification : %s" % Devices
|
|
| 154 |
print "GpuStyle used : %s" % GpuStyle
|
|
| 155 |
print "Iterations : %s" % Iterations
|
|
| 156 |
print "Number of Blocks on begin : %s" % BlocksBegin
|
|
| 157 |
print "Number of Blocks on end : %s" % BlocksEnd
|
|
| 158 |
print "Step on Blocks : %s" % BlocksStep
|
|
| 159 |
print "Number of Threads on begin : %s" % ThreadsBegin
|
|
| 160 |
print "Number of Threads on end : %s" % ThreadsEnd
|
|
| 161 |
print "Step on Threads : %s" % ThreadsStep
|
|
| 162 |
print "Number of redo : %s" % Redo
|
|
| 163 |
print "Metrology done out of XPU : %r" % OutMetrology
|
|
| 164 |
print "Type of Marsaglia RNG used : %s" % RNG
|
|
| 165 |
print "Type of variable : %s" % ValueType
|
|
| 153 |
print("Devices Identification : %s" % Devices)
|
|
| 154 |
print("GpuStyle used : %s" % GpuStyle)
|
|
| 155 |
print("Iterations : %s" % Iterations)
|
|
| 156 |
print("Number of Blocks on begin : %s" % BlocksBegin)
|
|
| 157 |
print("Number of Blocks on end : %s" % BlocksEnd)
|
|
| 158 |
print("Step on Blocks : %s" % BlocksStep)
|
|
| 159 |
print("Number of Threads on begin : %s" % ThreadsBegin)
|
|
| 160 |
print("Number of Threads on end : %s" % ThreadsEnd)
|
|
| 161 |
print("Step on Threads : %s" % ThreadsStep)
|
|
| 162 |
print("Number of redo : %s" % Redo)
|
|
| 163 |
print("Metrology done out of XPU : %r" % OutMetrology)
|
|
| 164 |
print("Type of Marsaglia RNG used : %s" % RNG)
|
|
| 165 |
print("Type of variable : %s" % ValueType)
|
|
| 166 | 166 |
|
| 167 | 167 |
if GpuStyle=='CUDA': |
| 168 | 168 |
try: |
| ... | ... | |
| 172 | 172 |
import pycuda.autoinit |
| 173 | 173 |
from pycuda.compiler import SourceModule |
| 174 | 174 |
except ImportError: |
| 175 |
print "Platform does not seem to support CUDA"
|
|
| 175 |
print("Platform does not seem to support CUDA")
|
|
| 176 | 176 |
|
| 177 | 177 |
if GpuStyle=='OpenCL': |
| 178 | 178 |
try: |
| ... | ... | |
| 182 | 182 |
for platform in cl.get_platforms(): |
| 183 | 183 |
for device in platform.get_devices(): |
| 184 | 184 |
deviceType=cl.device_type.to_string(device.type) |
| 185 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip())
|
|
| 185 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()))
|
|
| 186 | 186 |
|
| 187 | 187 |
if Id in Devices: |
| 188 | 188 |
# Set the Alu as detected Device Type |
| 189 | 189 |
Alu[Id]=deviceType |
| 190 | 190 |
Id=Id+1 |
| 191 | 191 |
except ImportError: |
| 192 |
print "Platform does not seem to support OpenCL"
|
|
| 192 |
print("Platform does not seem to support OpenCL")
|
|
| 193 | 193 |
|
| 194 |
print Devices,Alu
|
|
| 194 |
print(Devices,Alu)
|
|
| 195 | 195 |
|
| 196 |
BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
|
|
| 197 |
ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
|
|
| 196 |
BlocksList=range(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep) |
|
| 197 |
ThreadsList=range(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep) |
|
| 198 | 198 |
|
| 199 | 199 |
ExploredJobs=numpy.array([]).astype(numpy.uint32) |
| 200 | 200 |
ExploredBlocks=numpy.array([]).astype(numpy.uint32) |
| ... | ... | |
| 228 | 228 |
try: |
| 229 | 229 |
Inside,NewIterations,DurationItem=MetropolisCuda(circle,Iterations,1,Blocks,Threads,RNG,ValueType) |
| 230 | 230 |
except: |
| 231 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
|
|
| 231 |
print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
|
|
| 232 | 232 |
elif GpuStyle=='OpenCL': |
| 233 | 233 |
try: |
| 234 | 234 |
MetroParamCL={}
|
| ... | ... | |
| 242 | 242 |
|
| 243 | 243 |
OutputCL=MetropolisOpenCL(MetroParamCL) |
| 244 | 244 |
except: |
| 245 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)
|
|
| 245 |
print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
|
|
| 246 | 246 |
Duration=numpy.append(Duration,time.time()-start) |
| 247 | 247 |
Rate=numpy.append(Rate,NewIterations/Duration[-1]) |
| 248 | 248 |
else: |
| ... | ... | |
| 250 | 250 |
try: |
| 251 | 251 |
Inside,NewIterations,Duration=MetropolisCuda(circle,Iterations,Redo,Blocks,Threads,RNG,ValueType) |
| 252 | 252 |
except: |
| 253 |
print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
|
|
| 253 |
print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
|
|
| 254 | 254 |
elif GpuStyle=='OpenCL': |
| 255 | 255 |
try: |
| 256 | 256 |
IterationsMPI=numpy.uint64(Iterations/len(Devices)) |
| ... | ... | |
| 268 | 268 |
InputCL['RNG']=RNG |
| 269 | 269 |
InputCL['ValueType']=ValueType |
| 270 | 270 |
for Device in Devices[1:]: |
| 271 |
print "Send to device %i on rank %i" % (Device,r)
|
|
| 271 |
print("Send to device %i on rank %i" % (Device,r))
|
|
| 272 | 272 |
InputCL['Device']=Device |
| 273 | 273 |
comm.send('CONTINUE',dest=r,tag=11)
|
| 274 | 274 |
comm.send(InputCL,dest=r,tag=11) |
| 275 | 275 |
r+=1 |
| 276 | 276 |
|
| 277 | 277 |
# Compute on rank 0 |
| 278 |
print "Compute on rank 0"
|
|
| 278 |
print("Compute on rank 0")
|
|
| 279 | 279 |
InputCL['Device']=Devices[0] |
| 280 | 280 |
OutputCL=MetropolisOpenCL(InputCL) |
| 281 | 281 |
Inside=OutputCL['Inside'] |
| 282 | 282 |
NewIterations=OutputCL['NewIterations'] |
| 283 | 283 |
|
| 284 |
for slave in xrange(1,len(Devices)):
|
|
| 285 |
print "Get OutputCL from %i" % slave
|
|
| 284 |
for slave in range(1,len(Devices)): |
|
| 285 |
print("Get OutputCL from %i" % slave)
|
|
| 286 | 286 |
OutputCL=comm.recv(source=slave,tag=11) |
| 287 |
print OutputCL
|
|
| 287 |
print(OutputCL)
|
|
| 288 | 288 |
NewIterations+=OutputCL['NewIterations'] |
| 289 | 289 |
Inside+=OutputCL['Inside'] |
| 290 | 290 |
|
| 291 | 291 |
Duration=time.time()-time_start |
| 292 |
print "Pi estimation %.8f" % (4./NewIterations*Inside)
|
|
| 292 |
print("Pi estimation %.8f" % (4./NewIterations*Inside))
|
|
| 293 | 293 |
except: |
| 294 |
print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)
|
|
| 294 |
print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
|
|
| 295 | 295 |
#Duration=OutputCL['Duration'] |
| 296 | 296 |
#NewIterations=OutputCL['NewIterations'] |
| 297 | 297 |
Rate=NewIterations/Duration |
| ... | ... | |
| 307 | 307 |
minR=numpy.append(minR,numpy.min(Rate)) |
| 308 | 308 |
maxR=numpy.append(maxR,numpy.max(Rate)) |
| 309 | 309 |
|
| 310 |
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])
|
|
| 310 |
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]))
|
|
| 311 | 311 |
|
| 312 | 312 |
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))
|
| 313 | 313 |
ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ] |
| ... | ... | |
| 316 | 316 |
if Fit: |
| 317 | 317 |
FitAndPrint(ExploredJobs,median,Curves) |
| 318 | 318 |
# Send MPI exit tag |
| 319 |
for slave in xrange(1,RankSize):
|
|
| 319 |
for slave in range(1,RankSize): |
|
| 320 | 320 |
comm.send('BREAK',dest=slave,tag=11)
|
| 321 | 321 |
|
| 322 | 322 |
else: |
| ... | ... | |
| 325 | 325 |
if Signal=='CONTINUE': |
| 326 | 326 |
# Receive information from Master |
| 327 | 327 |
InputCL=comm.recv(source=0,tag=11) |
| 328 |
print "Parameters retreive for rank %s of %s on %s from master:" % (rank,RankSize,gethostname())
|
|
| 329 |
print "Input CL:" % InputCL
|
|
| 328 |
print("Parameters retreive for rank %s of %s on %s from master:" % (rank,RankSize,gethostname()))
|
|
| 329 |
print("Input CL:" % InputCL)
|
|
| 330 | 330 |
# Execute on slave |
| 331 | 331 |
OutputCL=MetropolisOpenCL(InputCL) |
| 332 |
print "Output CL:" % OutputCL
|
|
| 332 |
print("Output CL:" % OutputCL)
|
|
| 333 | 333 |
# Send information to Master |
| 334 | 334 |
comm.send(OutputCL,dest=0,tag=11) |
| 335 |
print "Data sent to master"
|
|
| 335 |
print("Data sent to master")
|
|
| 336 | 336 |
else: |
| 337 |
print 'Exit signal from Master'
|
|
| 337 |
print('Exit signal from Master')
|
|
| 338 | 338 |
break |
Formats disponibles : Unified diff