Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Function pointer example
  • Loading branch information
Ivan Butygin committed Feb 28, 2020
commit 08f9c1effa2399a70522fc29fc7d5c604bf7e3f9
12 changes: 12 additions & 0 deletions sdc/_concurrent_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ void deleteiter_int_hashmap(void* obj)
delete static_cast<int_hashmap_iters*>(obj);
}

using funcptr_t = int32_t(*)(int32_t,int32_t,int32_t);
int32_t test_funcptr(funcptr_t func, int32_t a, int32_t b)
{
int32_t res = 0;
for (int i = 0; i < 10; ++i)
{
res += func(a, b, i);
}
return res;
}

PyMODINIT_FUNC PyInit_hconcurrent_hash()
{
Expand Down Expand Up @@ -118,6 +128,8 @@ PyMODINIT_FUNC PyInit_hconcurrent_hash()
REGISTER(iterkey_int_hashmap)
REGISTER(iterval_int_hashmap)
REGISTER(deleteiter_int_hashmap)

REGISTER(test_funcptr)
#undef REGISTER
return m;
}
Expand Down
27 changes: 25 additions & 2 deletions sdc/concurrent_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import sdc

from numba import types, typing, generated_jit
from numba.extending import models, register_model
from numba.extending import lower_builtin, overload_method, overload, intrinsic
from numba.extending import lower_builtin, overload_method, overload, intrinsic, register_jitable

from llvmlite import ir as lir
import llvmlite.binding as ll
Expand All @@ -45,6 +44,8 @@
ll.add_symbol('iterval_int_hashmap', hconcurrent_hash.iterval_int_hashmap)
ll.add_symbol('deleteiter_int_hashmap', hconcurrent_hash.deleteiter_int_hashmap)

ll.add_symbol('test_funcptr', hconcurrent_hash.test_funcptr)

_create_int_hashmap = types.ExternalFunction("create_int_hashmap",
types.voidptr())
_delete_int_hashmap = types.ExternalFunction("delete_int_hashmap",
Expand All @@ -65,6 +66,9 @@
_deleteiter_int_hashmap = types.ExternalFunction("deleteiter_int_hashmap",
types.void(types.voidptr))

_test_funcptr = types.ExternalFunction("test_funcptr",
types.int32(types.voidptr,types.int32,types.int32))


def create_int_hashmap():
pass
Expand Down Expand Up @@ -102,6 +106,10 @@ def deleteiter_int_hashmap():
pass


def test_funcptr():
pass


@overload(create_int_hashmap)
def create_int_hashmap_overload():
return lambda: _create_int_hashmap()
Expand Down Expand Up @@ -145,3 +153,18 @@ def iterval_int_hashmap_overload(h):
@overload(deleteiter_int_hashmap)
def deleteiter_int_hashmap_overload(h):
return lambda h: _deleteiter_int_hashmap(h)



@register_jitable
def sink(*args):
args[0]

@overload(test_funcptr)
def test_funcptr_overload(a,b,c):
def func(a,b,c):
res = _test_funcptr(a,b,c)
sink(a,b,c)
return res

return func
20 changes: 17 additions & 3 deletions sdc/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,14 @@ def test_impl():
def test_tbb(self):
import sdc.concurrent_hash

def test_impl():
@numba.cfunc("int32(int32, int32, int32)")
def callback(x, y, z):
return x + y + z

global funcptr
funcptr = callback.address

def test_impl1():
h = sdc.concurrent_hash.create_int_hashmap()

sdc.concurrent_hash.addelem_int_hashmap(h, 1, 2)
Expand All @@ -1814,8 +1821,15 @@ def test_impl():
sdc.concurrent_hash.deleteiter_int_hashmap(it)
sdc.concurrent_hash.delete_int_hashmap(h)

hpat_func = self.jit(test_impl)
hpat_func()
hpat_func1 = self.jit(test_impl1)
hpat_func1()

def test_impl2():
r = sdc.concurrent_hash.test_funcptr(funcptr, 2, 3)
print('res', r)

hpat_func2 = self.jit(test_impl2)
hpat_func2()


if __name__ == "__main__":
Expand Down