Révision 128
| NBody/NBody.py (revision 128) | ||
|---|---|---|
| 1 |
#!/usr/bin/env python |
|
| 2 |
# -*- coding: utf-8 -*- |
|
| 1 |
#!/usr/bin/env python3 |
|
| 3 | 2 |
""" |
| 4 | 3 |
Demonstrateur OpenCL d'interaction NCorps |
| 5 | 4 |
|
| ... | ... | |
| 13 | 12 |
import pyopencl.array as cl_array |
| 14 | 13 |
from numpy.random import randint as nprnd |
| 15 | 14 |
|
| 16 |
|
|
| 17 | 15 |
BlobOpenCL= """ |
| 18 | 16 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
| 19 | 17 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535) |
| ... | ... | |
| 209 | 207 |
try: |
| 210 | 208 |
opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:i:s:m:",["device=","number=","size=","velocity=","iterations=","step=","method="]) |
| 211 | 209 |
except getopt.GetoptError: |
| 212 |
print HowToUse % sys.argv[0]
|
|
| 210 |
print(HowToUse % sys.argv[0])
|
|
| 213 | 211 |
sys.exit(2) |
| 214 | 212 |
|
| 215 | 213 |
for opt, arg in opts: |
| 216 | 214 |
if opt == '-h': |
| 217 |
print HowToUse % sys.argv[0]
|
|
| 215 |
print(HowToUse % sys.argv[0])
|
|
| 218 | 216 |
|
| 219 |
print "\nInformations about devices detected under OpenCL:"
|
|
| 217 |
print("\nInformations about devices detected under OpenCL:")
|
|
| 220 | 218 |
try: |
| 221 | 219 |
Id=1 |
| 222 | 220 |
for platform in cl.get_platforms(): |
| 223 | 221 |
for device in platform.get_devices(): |
| 224 | 222 |
deviceType=cl.device_type.to_string(device.type) |
| 225 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
|
|
| 223 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
|
|
| 226 | 224 |
Id=Id+1 |
| 227 | 225 |
sys.exit() |
| 228 | 226 |
except ImportError: |
| 229 |
print "Your platform does not seem to support OpenCL"
|
|
| 227 |
print("Your platform does not seem to support OpenCL")
|
|
| 230 | 228 |
sys.exit() |
| 231 | 229 |
|
| 232 | 230 |
elif opt in ("-d", "--device"):
|
| ... | ... | |
| 244 | 242 |
elif opt in ("-i", "--iterations"):
|
| 245 | 243 |
Iterations=int(arg) |
| 246 | 244 |
|
| 247 |
print "Device choosed : %s" % Device
|
|
| 248 |
print "Number of particules : %s" % Number
|
|
| 249 |
print "Size of Box : %s" % SizeOfBox
|
|
| 250 |
print "Initial velocity % s" % Velocity
|
|
| 251 |
print "Number of iterations % s" % Iterations
|
|
| 252 |
print "Step of iteration % s" % Step
|
|
| 253 |
print "Method of resolution % s" % Method
|
|
| 245 |
print("Device choosed : %s" % Device)
|
|
| 246 |
print("Number of particules : %s" % Number)
|
|
| 247 |
print("Size of Box : %s" % SizeOfBox)
|
|
| 248 |
print("Initial velocity % s" % Velocity)
|
|
| 249 |
print("Number of iterations % s" % Iterations)
|
|
| 250 |
print("Step of iteration % s" % Step)
|
|
| 251 |
print("Method of resolution % s" % Method)
|
|
| 254 | 252 |
|
| 255 | 253 |
MyData = np.zeros(Number, dtype=cl_array.vec.float8) |
| 256 | 254 |
|
| ... | ... | |
| 261 | 259 |
if Id==Device: |
| 262 | 260 |
PlatForm=platform |
| 263 | 261 |
XPU=device |
| 264 |
print "CPU/GPU selected: ",device.name.lstrip()
|
|
| 262 |
print("CPU/GPU selected: ",device.name.lstrip())
|
|
| 265 | 263 |
HasXPU=True |
| 266 | 264 |
Id+=1 |
| 267 | 265 |
|
| 268 | 266 |
if HasXPU==False: |
| 269 |
print "No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1)
|
|
| 267 |
print("No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1))
|
|
| 270 | 268 |
sys.exit() |
| 271 | 269 |
|
| 272 | 270 |
# Je cree le contexte et la queue pour son execution |
| ... | ... | |
| 274 | 272 |
ctx = cl.Context([XPU]) |
| 275 | 273 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
| 276 | 274 |
except: |
| 277 |
print "Crash during context creation"
|
|
| 275 |
print("Crash during context creation")
|
|
| 278 | 276 |
|
| 279 | 277 |
|
| 280 | 278 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build() |
| ... | ... | |
| 291 | 289 |
clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes) |
| 292 | 290 |
#clData = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyData) |
| 293 | 291 |
|
| 294 |
print 'Tous au meme endroit'
|
|
| 292 |
print('Tous au meme endroit')
|
|
| 295 | 293 |
|
| 296 | 294 |
# MyRoutines.SplutterPoints(queue,(Number,1),None,clDataIn,np.float32(SizeOfBox),np.float32(Velocity),np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
| 297 | 295 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clData,np.float32(SizeOfBox),np.float32(Velocity),np.uint32(110271),np.uint32(250173)) |
| 298 | 296 |
|
| 299 |
print 'Tous distribues'
|
|
| 297 |
print('Tous distribues')
|
|
| 300 | 298 |
|
| 301 | 299 |
cl.enqueue_copy(queue, MyData, clData) |
| 302 | 300 |
t0=np.array([[MyData[0][0],MyData[0][1],MyData[0][2]]]) |
| ... | ... | |
| 304 | 302 |
tL=np.array([[MyData[-1][0],MyData[-1][1],MyData[-1][2]]]) |
| 305 | 303 |
|
| 306 | 304 |
time_start=time.time() |
| 307 |
for i in xrange(Iterations):
|
|
| 305 |
for i in range(Iterations): |
|
| 308 | 306 |
if Method=="RungeKutta": |
| 309 | 307 |
CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clData,np.float32(Step)) |
| 310 | 308 |
else: |
| ... | ... | |
| 314 | 312 |
t0=np.append(t0,[MyData[0][0],MyData[0][1],MyData[0][2]]) |
| 315 | 313 |
t1=np.append(t1,[MyData[1][0],MyData[1][1],MyData[1][2]]) |
| 316 | 314 |
tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]]) |
| 317 |
print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Iterations)
|
|
| 315 |
print("\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Iterations))
|
|
| 318 | 316 |
|
| 319 | 317 |
t0=np.transpose(np.reshape(t0,(Iterations+1,3))) |
| 320 | 318 |
t1=np.transpose(np.reshape(t1,(Iterations+1,3))) |
Formats disponibles : Unified diff