Add test to use numpy.fromfunc.
Depends on numpy branch "ufunc-from-function-pointer" in git://github.com/jayvius/numpy.git
This commit is contained in:
parent
150f497ed0
commit
b5893d4ebb
3 changed files with 52 additions and 10 deletions
|
|
@ -151,11 +151,12 @@ class ParallelUFunc(CDefinition):
|
||||||
for t in range(ThreadCount):
|
for t in range(ThreadCount):
|
||||||
cur_ctxt = contexts[t].as_struct(Context)
|
cur_ctxt = contexts[t].as_struct(Context)
|
||||||
total_completed += cur_ctxt.completed
|
total_completed += cur_ctxt.completed
|
||||||
self.debug(cur_ctxt.id, 'completed', cur_ctxt.completed)
|
# self.debug(cur_ctxt.id, 'completed', cur_ctxt.completed)
|
||||||
|
|
||||||
with self.ifelse( total_completed == N ) as ifelse:
|
with self.ifelse( total_completed == N ) as ifelse:
|
||||||
with ifelse.then():
|
with ifelse.then():
|
||||||
self.debug("All is well!")
|
# self.debug("All is well!")
|
||||||
|
pass # keep quite if all is well
|
||||||
with ifelse.otherwise():
|
with ifelse.otherwise():
|
||||||
self.debug("ERROR: race occurred! Trigger segfault")
|
self.debug("ERROR: race occurred! Trigger segfault")
|
||||||
self.unreachable()
|
self.unreachable()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ from parallel_vectorize import *
|
||||||
|
|
||||||
|
|
||||||
class Work_D_D(CDefinition):
|
class Work_D_D(CDefinition):
|
||||||
_name_ = 'copy_d_d'
|
_name_ = 'work_d_d'
|
||||||
_retty_ = C.double
|
_retty_ = C.double
|
||||||
_argtys_ = [
|
_argtys_ = [
|
||||||
('inval', C.double),
|
('inval', C.double),
|
||||||
|
|
@ -47,11 +47,6 @@ class Tester(CDefinition):
|
||||||
ArgCount = 2
|
ArgCount = 2
|
||||||
WorkCount = 10000
|
WorkCount = 10000
|
||||||
|
|
||||||
# parallel_ufunc = self.depends(ParallelUFuncPosix,
|
|
||||||
# ThreadCount=ThreadCount)
|
|
||||||
# ufunc_core = self.depends(UFuncCore_D_D)
|
|
||||||
# worker = self.depends(Work_D_D)
|
|
||||||
|
|
||||||
sppufunc = self.depends(SpecializedParallelUFunc,
|
sppufunc = self.depends(SpecializedParallelUFunc,
|
||||||
PUFuncDef = ParallelUFuncPosix,
|
PUFuncDef = ParallelUFuncPosix,
|
||||||
CoreDef = UFuncCore_D_D,
|
CoreDef = UFuncCore_D_D,
|
||||||
|
|
@ -123,11 +118,11 @@ def main():
|
||||||
# run
|
# run
|
||||||
print('run')
|
print('run')
|
||||||
exe = CExecutor(module)
|
exe = CExecutor(module)
|
||||||
exe.engine.get_pointer_to_function(fntester)
|
|
||||||
func = exe.get_ctype_function(fntester, 'void')
|
func = exe.get_ctype_function(fntester, 'void')
|
||||||
|
|
||||||
func()
|
func()
|
||||||
|
# Will not reach here is race condition occurred
|
||||||
|
print('Good')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
46
test_parallel_vectorize_numpy.py
Normal file
46
test_parallel_vectorize_numpy.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
from test_parallel_vectorize import *
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module = Module.new(__name__)
|
||||||
|
sppufunc = SpecializedParallelUFunc.define(module,
|
||||||
|
PUFuncDef = ParallelUFuncPosix,
|
||||||
|
CoreDef = UFuncCore_D_D,
|
||||||
|
Func = Work_D_D,
|
||||||
|
FuncName = Work_D_D._name_,
|
||||||
|
ThreadCount = 2)
|
||||||
|
module.verify()
|
||||||
|
|
||||||
|
mpm = PassManager.new()
|
||||||
|
pmbuilder = PassManagerBuilder.new()
|
||||||
|
pmbuilder.opt_level = 3
|
||||||
|
pmbuilder.populate(mpm)
|
||||||
|
|
||||||
|
mpm.run(module)
|
||||||
|
# print module
|
||||||
|
|
||||||
|
# run
|
||||||
|
|
||||||
|
exe = CExecutor(module)
|
||||||
|
funcptr = exe.engine.get_pointer_to_function(sppufunc)
|
||||||
|
print("Function pointer: %x" % funcptr)
|
||||||
|
|
||||||
|
ptr_t = long # py2 only
|
||||||
|
|
||||||
|
typenum = np.dtype(np.double).num
|
||||||
|
ufunc = np.fromfunc([ptr_t(funcptr)], [[typenum, typenum]], 1, 1, [None])
|
||||||
|
|
||||||
|
x = np.linspace(0., 10., 1000)
|
||||||
|
x.dtype=np.double
|
||||||
|
# print x
|
||||||
|
ans = ufunc(x)
|
||||||
|
# print ans
|
||||||
|
|
||||||
|
if not ( ans == x/2.345 ).all():
|
||||||
|
raise ValueError('Computation failed')
|
||||||
|
else:
|
||||||
|
print('Good')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue