Skip to content
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

Merged
merged 9 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
7 changes: 5 additions & 2 deletions python/ionpy/Builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def load(self, filename: str):
if ret != 0:
raise Exception('Invalid operation')

def run(self, port_map: PortMap):
ret = ion_builder_run_with_port_map(self.obj, port_map.obj)
def run(self, port_map: PortMap = None):
if port_map is None:
ret = ion_builder_run(self.obj)
else:
ret = ion_builder_run_with_port_map(self.obj, port_map.obj)
if ret != 0:
raise Exception('Invalid operation')
67 changes: 66 additions & 1 deletion python/ionpy/Port.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@
ion_port_create,
ion_port_create_with_index,
ion_port_destroy,

ion_port_bind_i8,
ion_port_bind_i16,
ion_port_bind_i32,
ion_port_bind_i64,

ion_port_bind_u1,
ion_port_bind_u8,
ion_port_bind_u16,
ion_port_bind_u32,
ion_port_bind_u64,

ion_port_bind_f32,
ion_port_bind_f64,
ion_port_bind_buffer
)

from .Type import Type

from .Buffer import Buffer

class Port:
def __init__(self,
Expand Down Expand Up @@ -39,3 +54,53 @@ def __getitem__(self, index):
def __del__(self):
if self.obj: # check not nullptr
ion_port_destroy(self.obj)


# should use numpy type?
def bind_i8(self, v: int):
Copy link
Collaborator

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 inspecting self.type and self.dim.

if ion_port_bind_i8(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_i16(self, v: int):
if ion_port_bind_i16(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_i32(self, v: int):
if ion_port_bind_i32(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_i64(self, v: int):
if ion_port_bind_i64(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_u1(self, v: bool):
if ion_port_bind_u1(self.obj, ctypes.byref(v))!= 0:
raise Exception('Invalid operation')

def bind_u8(self, v: int):
if ion_port_bind_u8(self.obj, ctypes.byref(v))!= 0:
raise Exception('Invalid operation')

def bind_u16(self, v: int):
if ion_port_bind_u16(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_u32(self, v: int):
if ion_port_bind_u32(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_u64(self, v: int):
if ion_port_bind_u64(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_f32(self, v: float):
if ion_port_bind_f32(self.obj,ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_f64(self, v: float):
if ion_port_bind_f64(self.obj, ctypes.byref(v))!= 0:
raise Exception('Invalid operation')

def bind_buffer(self, buffer: Buffer):
if ion_port_bind_buffer(self.obj, buffer.obj) != 0:
raise Exception('Invalid operation')
68 changes: 66 additions & 2 deletions python/ionpy/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions python/test/test_binding.py
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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case for scalar parameter bind?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Loading