Skip to content

Commit e449ffd

Browse files
author
Kilik Kuo
committed
Merge pull request #11 from kilikkuo/master
Merge from kilikkuo's fork
2 parents ca6c98a + c5ffe29 commit e449ffd

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

oclConfigurar.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self):
1313
self.dicPlatform2Devices = {}
1414

1515
self.context = None
16+
self.curDevice = None
1617
self.queue = None
1718
self.program = None
1819
self.mem_pool = None
@@ -42,10 +43,10 @@ def setupProgramAndDataStructure(self, program, lstIPath=[], dicName2DS={}):
4243
# ('open', numpy.float32)]}
4344
assert (self.context != None), "Setup context seems incorrectly !!"
4445
assert (len(self.context.devices) > 0), "Error, No device for context !!"
45-
device = self.context.devices[0]
46+
self.curDevice = self.context.devices[0]
4647
dicReturnStruct = {}
4748
for k, v in dicName2DS.iteritems():
48-
kObj, k_c_decl = cl.tools.match_dtype_to_c_struct(device, k, v)
49+
kObj, k_c_decl = cl.tools.match_dtype_to_c_struct(self.curDevice, k, v)
4950
retV = cl.tools.get_or_register_dtype(k, kObj)
5051

5152
dicReturnStruct[k] = retV
@@ -64,8 +65,15 @@ def setupProgramAndDataStructure(self, program, lstIPath=[], dicName2DS={}):
6465
def callFuncFromProgram(self, strMethodName, *args, **argd):
6566
methodCall = getattr(self.program, strMethodName)
6667
if methodCall:
68+
if len(args) >= 2 and type(args[1])==tuple and (not args[1]) != True:
69+
wgs = cl.Kernel(self.program, strMethodName).get_work_group_info(
70+
cl.kernel_work_group_info.WORK_GROUP_SIZE, self.curDevice)
71+
local_worksize = reduce(lambda x,y: x*y, args[1])
72+
print 'local size : ', local_worksize
73+
assert wgs >= local_worksize, 'Out of capability, please reduce the local work size for %s()'%(strMethodName)
6774
evt = methodCall(self.queue, *args)
6875
return evt
76+
return None
6977

7078
def getContext(self, device=PREFERRED_GPU):
7179
assert len(self.dicIdx2Platform) > 0, 'No platform for OCL operation'
@@ -104,8 +112,8 @@ def createOCLArrayEmpty(self, stDType, size):
104112
assert size > 0, "Can NOT create array size <= 0"
105113
assert (self.queue != None), " Make sure setup correctly"
106114
# Creat a list which contains element initialized with structure stDType
107-
npArrData = np.zeros(size, dtype=stDType, allocator=self.mem_pool)
108-
clArrData = cl.array.to_device(self.queue, npArrData)
115+
npArrData = np.zeros(size, dtype=stDType)
116+
clArrData = cl.array.to_device(self.queue, npArrData, allocator=self.mem_pool)
109117
return clArrData
110118

111119
def createOCLArrayForInput(self, stDType, lstData):
@@ -114,6 +122,6 @@ def createOCLArrayForInput(self, stDType, lstData):
114122
assert len(lstData) > 0, "Size of input data list = 0"
115123
assert (self.queue != None), " Make sure setup correctly"
116124

117-
arrayData = np.array(lstData, dtype=stDType, allocator=self.mem_pool)
118-
clArrayData = cl.array.to_device(self.queue, arrayData)
125+
arrayData = np.array(lstData, dtype=stDType)
126+
clArrayData = cl.array.to_device(self.queue, arrayData, allocator=self.mem_pool)
119127
return clArrayData

oclPyObjGenerator.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def getMethodDef(strMethodName, dicArgd={}, nIndent=0):
4646
return head
4747

4848
def prepareImport(nIndent=0):
49-
lstImport = ['import os', 'import numpy', 'from oclConfigurar import OCLConfigurar, PREFERRED_GPU']
49+
lstImport = ['import os', 'import numpy', 'from oclConfigurar import OCLConfigurar, PREFERRED_GPU',
50+
'from threading import Lock']
5051
strResult = ''
5152
for item in lstImport:
5253
strResult += item + os.linesep
@@ -61,6 +62,11 @@ def prepareNumpyDS(dicDS={}, nIndent=0):
6162
strTemp += os.linesep
6263
return strTemp
6364

65+
def prepareLock(nIndent=0):
66+
strTemp = ''
67+
strTemp += tabSpace * nIndent + 'lk = Lock()' + os.linesep
68+
return strTemp
69+
6470
def prepareOCLConfigurar(nIndent=0):
6571
return tabSpace*nIndent + "self.oclConfigurar = OCLConfigurar()" + os.linesep
6672

@@ -72,7 +78,11 @@ def prepareOCLSetup(clsName, dicNumpyDS={}, nIndent=0):
7278
test = space + 'self.oclConfigurar.setupContextAndQueue(PREFERRED_GPU)' + os.linesep
7379
test += space + 'dirPath = os.path.dirname(__file__)' + os.linesep
7480
test += space + 'path = os.path.join(dirPath, %s)'%('\''+str(getKernelFileName(clsName))+'\'') + os.linesep
75-
test += space + 'dicRetDS = self.oclConfigurar.setupProgramAndDataStructure(path, [dirPath], {%s})'%(strDS) + os.linesep
81+
test += space + '%s.lk.acquire()'%(clsName) + os.linesep
82+
test += space + 'try:' + os.linesep
83+
test += space + tabSpace + 'dicRetDS = self.oclConfigurar.setupProgramAndDataStructure(path, [dirPath], {%s})'%(strDS) + os.linesep
84+
test += space + 'finally:' + os.linesep
85+
test += space + tabSpace + '%s.lk.release()'%(clsName) + os.linesep
7686
for k in dicNumpyDS.iterkeys():
7787
test += space + 'self.%s = dicRetDS.get(%s, None)'%(k, '\''+k+'\'') + os.linesep
7888
return test + os.linesep
@@ -93,7 +103,8 @@ def prepareOCLFuncImpl(strMethodName, dicArgd={}, nIndent=0):
93103
if strRepresArgs == '':
94104
return '' + os.linesep
95105
strRepresArgs = strRepresArgs[:-2]
96-
body = tabSpace*nIndent + 'self.oclConfigurar.callFuncFromProgram(%s, %s)'%('\''+strMethodName+'\'', strRepresArgs) + os.linesep
106+
body = tabSpace*nIndent + 'evt = self.oclConfigurar.callFuncFromProgram(%s, %s)'%('\''+strMethodName+'\'', strRepresArgs) + os.linesep
107+
body += tabSpace*nIndent + 'return evt' + os.linesep
97108
return body
98109

99110
def prepareOCLBufferByDS(nIndent=0):
@@ -156,6 +167,7 @@ def generateOCLPyObj(self):
156167

157168
# class
158169
fPyObj.write(getClassDef(self.className))
170+
fPyObj.write(prepareLock(1))
159171
fPyObj.write(prepareNumpyDS(self.dicNumpyDS, 1))
160172

161173
# __init__

0 commit comments

Comments
 (0)