-
Notifications
You must be signed in to change notification settings - Fork 3
/
oclPyObjGenerator.py
220 lines (193 loc) · 8.28 KB
/
oclPyObjGenerator.py
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
import os
from utilityFunc import splitNameToWords, \
getStructFileName, \
getPyFileName, \
getKernelFileName, \
DS_TYPE_TO_NUMPY_STRING_TYPE
tabSpace = ' '
opj = os.path.join
def getClassDef(strName, inheritCls=[], nIndent=0):
strCls = 'class %s'%(strName)
for idx, cls in enumerate(inheritCls):
if idx == 0:
strCls += '('
strCls += cls
strCls += ')' if idx == (len(inheritCls)-1) else ', '
strCls += ':' + os.linesep
return tabSpace*nIndent + strCls
def getMethodDef(strMethodName, dicArgd={}, nIndent=0):
# Head
strRetHead = 'def %s'%(strMethodName)
strParams = '(self, '
lstVars = dicArgd.get('var', [])
lstVars_Default = dicArgd.get('var_default', [])
args = dicArgd.get('args', None)
argd = dicArgd.get('argd', None)
for var in lstVars:
strParams += var + ', '
for dicVar in lstVars_Default:
for var, default in dicVar.iteritems():
strTemp = var + '=' + str(default) + ', '
strParams += strTemp
if args:
strParams += '*args, '
if argd:
strParams += '**argd, '
strParams = strParams[:-2]
strRetTail = '):' + os.linesep
head = tabSpace*nIndent + strRetHead + strParams + strRetTail
return head
def prepareImport(nIndent=0):
lstImport = ['import os', 'import numpy', 'from oclConfigurar import OCLConfigurar, PREFERRED_GPU',
'from threading import Lock']
strResult = ''
for item in lstImport:
strResult += item + os.linesep
return tabSpace*nIndent + strResult + os.linesep
def prepareNumpyDS(dicDS={}, nIndent=0):
# Should be inside __init__()
strTemp = ''
for k in dicDS.iterkeys():
strTemp += tabSpace * nIndent
strTemp += '%s = numpy.dtype(%s)'%(str(k), str(dicDS[k]))
strTemp += os.linesep
return strTemp
def prepareLock(nIndent=0):
strTemp = ''
strTemp += tabSpace * nIndent + 'lk = Lock()' + os.linesep
return strTemp
def prepareOCLConfigurar(nIndent=0):
return tabSpace*nIndent + "self.oclConfigurar = OCLConfigurar()" + os.linesep
def prepareOCLSetup(clsName, dicNumpyDS={}, nIndent=0):
strDS = ''
for k in dicNumpyDS.iterkeys():
strDS += ' \'' + k + '\'' + ' : ' + '%s.%s,'%(clsName, k)
space = tabSpace*nIndent
test = space + 'self.oclConfigurar.setupContextAndQueue(PREFERRED_GPU)' + os.linesep
test += space + 'dirPath = os.path.dirname(__file__)' + os.linesep
test += space + 'path = os.path.join(dirPath, %s)'%('\''+str(getKernelFileName(clsName))+'\'') + os.linesep
test += space + '%s.lk.acquire()'%(clsName) + os.linesep
test += space + 'try:' + os.linesep
test += space + tabSpace + 'dicRetDS = self.oclConfigurar.setupProgramAndDataStructure(path, [dirPath], {%s})'%(strDS) + os.linesep
test += space + 'finally:' + os.linesep
test += space + tabSpace + '%s.lk.release()'%(clsName) + os.linesep
for k in dicNumpyDS.iterkeys():
test += space + 'self.%s = dicRetDS.get(%s, None)'%(k, '\''+k+'\'') + os.linesep
return test + os.linesep
def prepareOCLFuncImpl(strMethodName, dicArgd={}, nIndent=0):
lstVars = dicArgd.get('var', [])
types = dicArgd.get('types', {})
strRepresArgs = ''
for var in lstVars:
if var in ['gWorkSize', 'lWorkSize']:
strRepresArgs += var + ', '
else:
varTypeInfo = types[var]
if type(varTypeInfo['type']) == dict:
strRepresArgs += '%s.data, '%(var)
else:
strRepresArgs += '%s(%s), '%(DS_TYPE_TO_NUMPY_STRING_TYPE[str(varTypeInfo['type'])], var)
if strRepresArgs == '':
return '' + os.linesep
strRepresArgs = strRepresArgs[:-2]
body = tabSpace*nIndent + 'evt = self.oclConfigurar.callFuncFromProgram(%s, %s)'%('\''+strMethodName+'\'', strRepresArgs) + os.linesep
body += tabSpace*nIndent + 'return evt' + os.linesep
return body
def prepareOCLBufferByDS(nIndent=0):
dicArgs = {'var' : ['dataStructure'],
'var_default' : [{'nSize':0}, {'lstData':[]}]}
strFuncDef = getMethodDef('createBufferData', dicArgs, nIndent)
strFuncDef += tabSpace*(nIndent+1) + 'OCLFunc = self.oclConfigurar.createOCLArrayForInput if nSize == 0 else self.oclConfigurar.createOCLArrayEmpty' + os.linesep
strFuncDef += tabSpace*(nIndent+1) + 'input = lstData if nSize == 0 else nSize' + os.linesep
strFuncDef += tabSpace*(nIndent+1) + 'dataBuffer = OCLFunc(dataStructure, input)' + os.linesep
strFuncDef += tabSpace*(nIndent+1) + 'return dataBuffer' + os.linesep
return strFuncDef + os.linesep
def prepareFuncTail(nIndent=0):
return tabSpace*nIndent + 'pass' + os.linesep + os.linesep
class OCLPyObjGenerator:
def __init__(self, strName, dicNumpyDS, dicKFuncDS, strFolder='out'):
assert (not os.path.isabs(strFolder)), "strFolder should be a relatvie path"
self.strFileName = getPyFileName(strName)
self.strOutputFolder = opj(os.path.dirname(__file__), strFolder)
if not os.path.exists(self.strOutputFolder):
os.makedirs(self.strOutputFolder)
self.strFilePath = opj(self.strOutputFolder, self.strFileName)
self.strRelFolder = strFolder
self.className = strName
self.dicNumpyDS = dicNumpyDS
self.dicKFuncDS = dicKFuncDS
pass
def getObj(self):
import pkgutil
import imp
modules = pkgutil.iter_modules(path=[self.strOutputFolder])
instance = None
for loader, mod_name, ispkg in modules:
if mod_name == self.className:
module_src = imp.load_source(os.path.splitext(self.strFileName)[0], self.strFilePath)
loaded_class = getattr(module_src, self.className)
instance = loaded_class()
break
return instance
def __copyOCLConfigurarToOutput(self):
from shutil import copyfile
outputFilePath = opj(self.strOutputFolder, 'oclConfigurar.py')
copyfile(opj(os.path.dirname(__file__), 'oclConfigurar.py'), outputFilePath)
def generateOCLPyObj(self):
# __init__ file for module loader in folder
with open(opj(self.strOutputFolder, getPyFileName('__init__')), 'w'):
pass
# Copy OCLConfigurar into output folder
self.__copyOCLConfigurarToOutput()
# Generating target file
with open(self.strFilePath, 'w') as fPyObj:
# import
fPyObj.write(prepareImport())
# class
fPyObj.write(getClassDef(self.className))
fPyObj.write(prepareLock(1))
fPyObj.write(prepareNumpyDS(self.dicNumpyDS, 1))
# __init__
fPyObj.write(getMethodDef('__init__', nIndent=1))
fPyObj.write(prepareOCLConfigurar(2))
fPyObj.write(prepareOCLSetup(self.className, self.dicNumpyDS, 2))
fPyObj.write(prepareFuncTail(2))
fPyObj.write(prepareOCLBufferByDS(1))
# methods
for funcName, dicArgs in self.dicKFuncDS.iteritems():
fPyObj.write(getMethodDef(funcName, dicArgs, nIndent=1))
fPyObj.write(prepareOCLFuncImpl(funcName, dicArgs, nIndent=2))
fPyObj.write(prepareFuncTail(2))
pass
# dicArgd = {'var' : ['a', 'b', ...],
# 'var_default' : [{'c':pyobj}],
# 'args' : False/True,
# 'argd' : False/True}
dicTest0 = {'var' : [],
'var_default' : [],
'args' : False,
'argd' : False}
dicTest1 = {'var' : ['a', 'b'],
'var_default' : [],
'args' : False,
'argd' : False}
dicTest2 = {'var' : [],
'var_default' :[{'a':1, 'b':[2,3], 'c':{'d':6}, 'd':None}],
'args' : False,
'argd' : False}
dicTest3 = {'var' : [],
'var_default' :[],
'args' : True,
'argd' : False}
dicTest4 = {'var' : [],
'var_default' : [],
'args' : False,
'argd' : True}
dicTest5 = {'var' : dicTest1['var'],
'var_default' : dicTest2['var_default'],
'args' : dicTest3['args'],
'argd' : dicTest4['argd']}
def testMethodDef():
for idx in xrange(6):
dicData = eval('dicTest'+str(idx))
print getMethodDef(str(idx), dicData)