-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update/python binding api #185
Changes from 2 commits
0ec0e62
735305c
1cf3cb5
31489ea
c676d93
778cc1f
4fbc447
79408ba
e96fd2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,18 @@ | |
from ctypes.util import find_library | ||
|
||
import os | ||
import platform | ||
|
||
if os.name == 'nt': | ||
if platform.system() == 'Windows': | ||
ion_core_module = find_library('ion-core.dll') | ||
elif os.name == 'posix': | ||
elif platform.system() == 'Darwin': | ||
ion_core_module = 'libion-core.dylib' | ||
elif platform.system() == 'Linux': | ||
ion_core_module = 'libion-core.so' | ||
|
||
# libion-core.so must be in a directory listed in $LD_LIBRARY_PATH. | ||
# ion-core.dll must be in a directory listed in %PATH%. | ||
# libion-core.dylib must be in a directory listed in $DYLD_LIBRARY_PATH. | ||
ion_core = ctypes.cdll.LoadLibrary(ion_core_module) | ||
|
||
class c_ion_type_t(ctypes.Structure): | ||
|
@@ -33,6 +37,7 @@ class c_builder_compile_option_t(ctypes.Structure): | |
c_ion_buffer_t = ctypes.POINTER(ctypes.c_int) | ||
c_ion_port_map_t = ctypes.POINTER(ctypes.c_int) | ||
|
||
c_ion_port_bind_t = ctypes.POINTER(ctypes.c_int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused? |
||
|
||
# int ion_port_create(ion_port_t *, const char *, ion_type_t, int); | ||
ion_port_create = ion_core.ion_port_create | ||
|
@@ -49,6 +54,65 @@ class c_builder_compile_option_t(ctypes.Structure): | |
ion_port_destroy.restype = ctypes.c_int | ||
ion_port_destroy.argtypes = [ c_ion_port_t ] | ||
|
||
# int ion_port_bind_i8(ion_port_t, int8_t); | ||
ion_port_bind_i8 = ion_core.ion_port_bind_i8 | ||
ion_port_bind_i8.restype = ctypes.c_int | ||
ion_port_bind_i8.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int8) ] | ||
|
||
# int ion_port_bind_i16(ion_port_t, int16_t); | ||
ion_port_bind_i16 = ion_core.ion_port_bind_i16 | ||
ion_port_bind_i16.restype = ctypes.c_int | ||
ion_port_bind_i16.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int16) ] | ||
|
||
# int ion_port_bind_i32(ion_port_t, int32_t); | ||
ion_port_bind_i32 = ion_core.ion_port_bind_i32 | ||
ion_port_bind_i32.restype = ctypes.c_int | ||
ion_port_bind_i32.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int32)] | ||
|
||
# int ion_port_bind_i64(ion_port_t, int64_t); | ||
ion_port_bind_i64 = ion_core.ion_port_bind_i64 | ||
ion_port_bind_i64.restype = ctypes.c_int | ||
ion_port_bind_i64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_int64) ] | ||
|
||
# int ion_port_map_set_u1(ion_port_t, bool); | ||
ion_port_bind_u1 = ion_core.ion_port_bind_u1 | ||
ion_port_bind_u1.restype = ctypes.c_int | ||
ion_port_bind_u1.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_bool) ] | ||
|
||
# int ion_port_bind_u8(ion_port_t, uint8_t); | ||
ion_port_bind_u8 = ion_core.ion_port_bind_u8 | ||
ion_port_bind_u8.restype = ctypes.c_int | ||
ion_port_bind_u8.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint8) ] | ||
|
||
# int ion_port_bind_u16(ion_port_t, uint16_t); | ||
ion_port_bind_u16 = ion_core.ion_port_bind_u16 | ||
ion_port_bind_u16.restype = ctypes.c_int | ||
ion_port_bind_u16.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint16) ] | ||
|
||
# int ion_port_bind_u32(ion_port_t, uint32_t); | ||
ion_port_bind_u32 = ion_core.ion_port_bind_u32 | ||
ion_port_bind_u32.restype = ctypes.c_int | ||
ion_port_bind_u32.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint32) ] | ||
|
||
# int ion_port_bind_u64(ion_port_t, uint64_t); | ||
ion_port_bind_u64 = ion_core.ion_port_bind_u64 | ||
ion_port_bind_u64.restype = ctypes.c_int | ||
ion_port_bind_u64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint64) ] | ||
|
||
# int ion_port_bind_f32(ion_port_t, float); | ||
ion_port_bind_f32 = ion_core.ion_port_bind_f32 | ||
ion_port_bind_f32.restype = ctypes.c_int | ||
ion_port_bind_f32.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_float) ] | ||
|
||
# int ion_port_bind_f64(ion_port_map_t, ion_port_t, double); | ||
ion_port_bind_f64 = ion_core.ion_port_bind_f64 | ||
ion_port_bind_f64.restype = ctypes.c_int | ||
ion_port_bind_f64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_double) ] | ||
|
||
# int ion_port_bind_buffer(ion_port_t, ion_buffer_t); | ||
ion_port_bind_buffer = ion_core.ion_port_bind_buffer | ||
ion_port_bind_buffer.restype = ctypes.c_int | ||
ion_port_bind_buffer.argtypes = [c_ion_port_t, c_ion_buffer_t ] | ||
|
||
# int ion_param_create(ion_param_t *, const char *, const char *); | ||
ion_param_create = ion_core.ion_param_create | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from ionpy import Node, Builder, Buffer, PortMap, Port, Param, Type, TypeCode | ||
import numpy as np # TODO: rewrite with pure python | ||
|
||
def test_binding(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test case for scalar parameter bind? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you replicate the C++ testcase for port binding using Python API? I want to keep the same C++ and Python API semantics. |
||
|
||
input_port = Port(name='input', type=Type.from_dtype(np.dtype(np.int32)), dim=2) | ||
value41 = Param(key='v', val='41') | ||
|
||
builder = Builder() | ||
builder.set_target(target='host') | ||
builder.with_bb_module(path='ion-bb-test') | ||
|
||
node = builder.add('test_inc_i32x2').set_iport([input_port]).set_param(params=[ value41, ]) | ||
|
||
idata = np.full((4, 4), fill_value=1, dtype=np.int32) | ||
ibuf = Buffer(array=idata) | ||
|
||
odata = np.full((4, 4), fill_value=0, dtype=np.int32) | ||
obuf = Buffer(array=odata) | ||
|
||
|
||
input_port.bind_buffer(ibuf) | ||
output_port = node.get_port(name='output') | ||
output_port.bind_buffer(obuf) | ||
|
||
# First run | ||
builder.run() | ||
|
||
for y in range(4): | ||
for x in range(4): | ||
assert odata[y][x] == 42 | ||
|
||
# Second | ||
for y in range(4): | ||
for x in range(4): | ||
idata[y][x] = 2 | ||
|
||
builder.run() | ||
|
||
for y in range(4): | ||
for x in range(4): | ||
assert odata[y][x] == 43 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better design to provide a generic
bind
instead of type-specific method (e.g. bind_i8). The appropreate C-API can be determined by inspectingself.type
andself.dim
.