forked from mesham/epython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepython.py
More file actions
587 lines (537 loc) · 16.3 KB
/
epython.py
File metadata and controls
587 lines (537 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#!/usr/bin/python
import os
import stat
import sys
import functools
import thread
import atexit
import os.path
import subprocess
from threading import Thread, Lock, Condition
import inspect
import re
useNumpy=False
if useNumpy: import numpy as np
toepython_pipe_name="/tmp/toepython"
fromepython_pipe_name="/tmp/fromepython"
epythonfile_name="/tmp/pythonkernels.py"
popen=None
ePythonFunctionTable=None
number_of_cores=0
activeCores=None
thisCore=0
active=True
globalVars=[]
outstandingLaunches=[]
schuedulerMutex = Lock()
schedulerCondition = Condition()
ALL_DEVICES=0
EPIPHANY_DEVICE=1
def executeOnCoProcessor():
global popen
for line in iter(popen.stdout.readline, b''):
print line,
popen.stdout.close()
def pingEpython():
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "10\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
raw_data=os.read(rp, 1024)
os.close(rp)
def stopEpython():
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "11\n")
os.close(wp)
def getExportableFunctionTable():
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "9\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
raw_data=os.read(rp, 8192)
os.close(rp)
exportedFunctions={}
if raw_data != "-":
for line in raw_data.splitlines():
exportedFunctions[line.split('>')[0]]=int(line.split('>')[1])
return exportedFunctions
def getTypeFromData(data):
if type(data) is int:
return 0
elif type(data) is float:
return 1
elif type(data) is bool:
return 3
else:
if useNumpy:
if type(data) is np.int:
return 0
elif type(data) is np.float64 or type(data) is np.float32:
return 1
elif type(data) is np.bool:
return 3
print "Error, can not map data to type "+str(type(data))
quit()
def sendrecv(data, pid):
command_to_send="8 "+str(pid)+" "+str(getTypeFromData(data))+" "
command_to_send+="0 "+str(data)+"\n"
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, command_to_send)
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
items = recv_data.split(" ")
if items[0]=="0":
return int(items[2])
if items[0]=="1":
return float(items[2])
if items[0]=="3":
return bool(items[2])
def underlyingSend(data, pid, length, isFunctionPointer=False):
command_to_send="1 "+str(pid)+" "
if isFunctionPointer:
command_to_send+="5 "
else:
if (length > 1):
command_to_send+=str(getTypeFromData(data[0]))+" "
else:
command_to_send+=str(getTypeFromData(data))+" "
command_to_send+="0 "+str(length)+" "
if (length > 1):
i=0
while i<length:
command_to_send+=str(data[i])+" "
i+=1
else:
command_to_send+=str(data)
command_to_send+="\n"
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, command_to_send)
os.close(wp)
def send(data, pid, length=None, isFunctionPointer=False):
length=1
try:
length=len(data)
except TypeError:
pass
underlyingSend(data, pid, length, isFunctionPointer=isFunctionPointer)
def probe(pid):
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "12 "+str(pid)+"\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
items = recv_data.split(" ")
return bool(items[2])
def underlyingRecv(pid):
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "2 "+str(pid)+"\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
items = recv_data.split(" ")
if items[0]=="0":
return int(items[2])
if items[0]=="1":
return float(items[2])
if items[0]=="3":
return bool(items[2])
def recv(pid, len=None):
if len is None:
return underlyingRecv(pid)
else:
toRet=[0]*len
i=0
while i<len:
toRet[i]=underlyingRecv(pid)
i+=1
return toRet
def reduce(data, operator):
command_to_send="6 "
if operator=="sum":
command_to_send+="0 "
elif operator=="min":
command_to_send+="1 "
elif operator=="max":
command_to_send+="2 "
elif operator=="prod":
command_to_send+="3 "
else:
print "Operator "+operator+" not found"
command_to_send+=str(getTypeFromData(data))+" "
command_to_send+="0 "+str(data)+"\n"
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, command_to_send)
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
items = recv_data.split(" ")
if items[0]=="0":
return int(items[2])
if items[0]=="1":
return float(items[2])
if items[0]=="3":
return bool(items[2])
def bcast(data, root):
command_to_send="7 "+str(root)
command_to_send+=str(getTypeFromData(data))+" "
command_to_send+="0 "+str(data)+"\n"
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, command_to_send)
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
items = recv_data.split(" ")
if items[0]=="0":
return int(items[2])
if items[0]=="1":
return float(items[2])
if items[0]=="3":
return bool(items[2])
def numcores():
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "3\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
return int(recv_data)
def coreid():
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "4\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
return int(recv_data)
def sync():
wp=os.open(toepython_pipe_name, os.O_WRONLY)
os.write(wp, "5\n")
os.close(wp)
rp = os.open(fromepython_pipe_name, os.O_RDONLY)
recv_data=os.read(rp, 1024)
os.close(rp)
def ishost():
return True
def isdevice():
return False
def receiveKernelReturnValue(coreId):
length=recv(coreId)
if (length==0):
data=recv(coreId)
else:
data=recv(coreId, length)
activeCores[coreId]=False
schedulerCondition.acquire()
schedulerCondition.notify()
schedulerCondition.release()
return data
class KernelExecutionHandler:
def __init__(self, running_coreids=None, num_scheduled=None):
self.running_coreids=[] if running_coreids is None else running_coreids
self.num_scheduled=0 if num_scheduled is None else num_scheduled
def append(self, handler_to_append):
self.appendRunningCoreIds(handler_to_append.getRunningCoreIds())
self.num_scheduled+=handler_to_append.getNumberScheduled()
def getRunningCoreIds(self):
return sorted(self.running_coreids)
def appendRunningCoreIds(self, cids):
self.running_coreids.extend(cids)
def wait(self):
while (self.num_scheduled > 0) : pass
returnVals=[]
index=0
for pid in sorted(self.running_coreids):
returnVals.append(receiveKernelReturnValue(pid))
index+=1
return returnVals
def wait_any(self):
while True:
while len(self.running_coreids == 0): pass
for pid in self.running_coreids:
if (probe(pid)):
self.running_coreids.remove(pid)
return {pid : receiveKernelReturnValue(pid)}
def number_running(self):
return len(self.running_coreids)
def number_outstanding(self):
return len(self.running_coreids) + self.num_scheduled
def getNumberScheduled(self):
return self.num_scheduled
def setNumberScheduled(self, num_scheduled):
self.num_scheduled=num_scheduled
def test(self):
if self.num_scheduled > 0 : return False
for pid in self.running_coreids:
if not probe(pid): return False
return True
def testKernelCompletion(pid):
if (isinstance(pid, KernelExecutionHandler)):
return pid.test()
else:
return probe(pid)
def waitForKernelCompletion(pid):
if (isinstance(pid, KernelExecutionHandler)):
return pid.wait()
else:
return receiveKernelReturnValue(pid)
def doPhysicalCoprocessorLaunch(pid, function_name, *args):
send(len(args), pid)
send(ePythonFunctionTable[function_name], pid, isFunctionPointer=True)
for arg in args:
try:
send(len(arg), pid)
except TypeError:
send(0, pid)
send(arg, pid)
def waitAll(*args):
results=[]
for handler in args:
results.append(handler.wait())
return results
class OutstandingLaunch:
def __init__(self, num_outstanding, func_name, args):
self.num_outstanding=num_outstanding
self.func_name=func_name
self.args=args
self.handler=None
def setHandler(self, handler):
self.handler=handler
def getHandler(self):
return self.handler
def getFunctionName(self):
return self.func_name
def getArgs(self):
return self.args
class AnyCoresOutstandingLaunch(OutstandingLaunch):
def __init__(self, num_outstanding, func_name, *args):
OutstandingLaunch.__init__(self,num_outstanding,func_name, args)
self.num_outstanding=num_outstanding
def getNumOutstanding(self):
return self.num_outstanding
def setNumOutstanding(self, num_outstanding):
self.num_outstanding=num_outstanding
class SpecificCoresOutstandingLaunch(OutstandingLaunch):
def __init__(self, outstandingpids, func_name, *args):
OutstandingLaunch.__init__(self,len(outstandingpids),func_name, args)
self.outstandingpids=outstandingpids
def getNumOutstanding(self):
return len(self.outstandingpids)
def getOutstandingCores(self):
return self.outstandingpids
def setOutstandingCores(self, outstandingpids):
self.outstandingpids=outstandingpids
def appendCoreId(self, coreid):
self.outstandingpids.append(coreid)
def removeCoreIds(self, listtoremove):
self.outstandingpids = [x for x in self.outstandingpids if x not in listtoremove]
def copy_from_device(var, target=None, async=False):
try:
varId=globalVars.index(var)
return issueKernelLaunches("copyFromGlobal", async, target, None, True if target == None else False, [varId])
except ValueError:
print "Error, can not find global variable " +str(var)+" for copying from the device"
quit()
def define_on_device(var):
pass
def copy_to_device(var, data, target=None, async=False):
try:
varId=globalVars.index(var)
return issueKernelLaunches("copyToGlobal", async, target, None, True if target == None else False, [varId, data])
except ValueError:
print "Error, can not find global variable " +str(var)+" for copying to device"
quit()
def issueKernelLaunches(kernelName, isAsync, myTarget, myAuto, myAll, args):
outstandingLaunch=None
if not myAuto is None:
pidtarget=[]
schuedulerMutex.acquire()
try:
for kernelinstance in range(0,myAuto):
idx=activeCores.index(False)
activeCores[idx]=True
pidtarget.append(idx)
except ValueError:
outstandingLaunch=AnyCoresOutstandingLaunch(myAuto-kernelinstance, kernelName, *args)
schuedulerMutex.release()
elif not myTarget is None:
if isinstance(myTarget, list):
pidtarget=myTarget
else:
pidtarget=[myTarget]
elif myAll:
pidtarget=range(0,number_of_cores)
pidtarget.remove(thisCore)
schuedulerMutex.acquire()
busyPids=[]
runningCoreIds=[]
for pid in pidtarget:
if (not myAuto is None or activeCores[pid] == False):
activeCores[pid]=True
doPhysicalCoprocessorLaunch(pid, kernelName, *args)
runningCoreIds.append(pid)
else:
busyPids.append(pid)
schuedulerMutex.release()
if (len(busyPids) > 0):
outstandingLaunch=SpecificCoresOutstandingLaunch(busyPids, kernelName, *args)
handler=KernelExecutionHandler(runningCoreIds, 0 if outstandingLaunch is None else outstandingLaunch.getNumOutstanding())
if not outstandingLaunch is None:
outstandingLaunch.setHandler(handler)
schuedulerMutex.acquire()
outstandingLaunches.append(outstandingLaunch)
schuedulerMutex.release()
if isAsync:
return handler
else:
return handler.wait()
def offload(test_func=None,async=False,target=None, auto=None, all=True, device=ALL_DEVICES):
if not test_func:
return functools.partial(offload, async=async,target=target, auto=auto, all=all)
@functools.wraps(test_func)
def f(*args, **kwargs):
global outstandingLaunches
isAsync=async
myTarget=target
myAuto=auto
myAll=all
for arg in kwargs:
if (arg == "target"):
myTarget=kwargs[arg]
elif (arg == "async"):
isAsync=kwargs[arg]
elif (arg == "auto"):
myAuto=kwargs[arg]
elif (arg == "all"):
myAll=kwargs[arg]
return issueKernelLaunches(test_func.func_name, isAsync, myTarget, myAuto, myAll, args)
return f
def offload_single(test_func=None, device=ALL_DEVICES):
return offload(test_func=test_func, async=True, target=None, auto=1, all=False, device=device)
def offload_multiple(test_func=None, cores=None, device=ALL_DEVICES):
if cores is None:
print "Error - you must specify the number of device cores to use with the multiple decorator"
quit()
return offload(test_func=test_func, async=True, target=None, auto=cores, all=False, device=device)
def shutdownEpython():
global popen, active, outstandingLaunches
while len(outstandingLaunches) > 0: pass
active=False
schedulerCondition.acquire()
schedulerCondition.notify()
schedulerCondition.release()
for pid in range(0,number_of_cores):
if not pid == thisCore: send(-1,pid)
stopEpython()
popen.wait()
try:
os.remove(toepython_pipe_name)
except OSError:
pass
try:
os.remove(fromepython_pipe_name)
except OSError:
pass
try:
os.remove(epythonfile_name)
except OSError:
pass
def pollScheduler():
while active:
for outstanding in outstandingLaunches:
pidtarget=[]
if (isinstance(outstanding, AnyCoresOutstandingLaunch)):
schuedulerMutex.acquire()
try:
for launchId in range(0,outstanding.getNumOutstanding()):
idx=activeCores.index(False)
activeCores[idx]=True
pidtarget.append(idx)
except ValueError:
pass
schuedulerMutex.release()
if len(pidtarget) > 0:
outstanding.setNumOutstanding(outstanding.getNumOutstanding() - len(pidtarget))
elif (isinstance(outstanding, SpecificCoresOutstandingLaunch)):
schuedulerMutex.acquire()
for launchId in outstanding.getOutstandingCores():
if (activeCores[launchId] == False):
activeCores[launchId]=True
pidtarget.append(launchId)
schuedulerMutex.release()
if len(pidtarget) > 0:
outstanding.removeCoreIds(pidtarget)
if len(pidtarget) > 0:
if outstanding.getNumOutstanding() == 0: outstandingLaunches.remove(outstanding)
for pid in pidtarget:
doPhysicalCoprocessorLaunch(pid, outstanding.getFunctionName(), *outstanding.getArgs())
outstanding.getHandler().appendRunningCoreIds(pidtarget)
outstanding.getHandler().setNumberScheduled(outstanding.getHandler().getNumberScheduled()-len(pidtarget))
break # This enforces strict ordering of processing kernels in the order that they are scheduled
else:
break # This enforces strict ordering of processing kernels in the order that they are scheduled
schedulerCondition.acquire()
schedulerCondition.wait()
schedulerCondition.release()
def initialise():
global globalVars
insideKernel=False
firstAddition=False
runningCoProcessor=False
importCode="import coprocessor\n"
generatedCode=""
kernelsCode=""
global_definitions={}
with open(sys.argv[0], 'rU') as f:
for line in f:
if line.isspace(): continue
if not line.startswith((' ', '\t')):
if (re.search(r'\w+=.+',line)):
global_definitions[line.split('=')[0]]=line.split('=')[1]
if firstAddition and not line.startswith((' ', '\t')):
insideKernel=False
if insideKernel:
if (re.search(r'\s*import .*',line) or re.search(r'\s*from .*',line)):
importCode+=line.lstrip()
else:
kernelsCode+=line
firstAddition=True
if "define_on_device(" in line or "epython.define_on_device(" in line:
var=line.split('(')[1].replace(',',')').split(')')[0]
if not var in globalVars:
globalVars.append(var)
generatedCode+=var+"="+global_definitions[var]+"\nregisterGlobalVariable("+var+")\n"
if "@offload" in line or "@epython.offload" in line:
runningCoProcessor=True
insideKernel=True
firstAddition=False
kernelsCode+="@exportable\n"
if runningCoProcessor:
global popen, ePythonFunctionTable, number_of_cores, activeCores, thisCore
atexit.register(shutdownEpython)
fo = open(epythonfile_name, "wb")
fo.write(importCode+generatedCode+"worker()\n"+kernelsCode);
fo.close()
try:
os.mkfifo(toepython_pipe_name, 0644)
except OSError:
pass
try:
os.mkfifo(fromepython_pipe_name, 0644)
except OSError:
pass
popen = subprocess.Popen("epython -fullpython "+epythonfile_name, shell=True, stdout=subprocess.PIPE, universal_newlines=True, bufsize=1)
thread.start_new_thread(executeOnCoProcessor,())
thread.start_new_thread(pollScheduler,())
pingEpython()
ePythonFunctionTable=getExportableFunctionTable()
number_of_cores=numcores()
thisCore=coreid()
activeCores=[False]*number_of_cores
activeCores[thisCore]=True
initialise()