Skip to content

Commit

Permalink
add python
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinyu Li authored and Xinyu Li committed Oct 24, 2024
1 parent 31f566c commit ca0d995
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 32 deletions.
131 changes: 100 additions & 31 deletions python/ionpy/Port.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@
ion_port_bind_f32,
ion_port_bind_f64,
ion_port_bind_buffer,
ion_port_bind_buffer_array
ion_port_bind_buffer_array,

ion_port_bind_i8_array,
ion_port_bind_i16_array,
ion_port_bind_i32_array,
ion_port_bind_i64_array,

ion_port_bind_u1_array,
ion_port_bind_u8_array,
ion_port_bind_u16_array,
ion_port_bind_u32_array,
ion_port_bind_u64_array,

ion_port_bind_f32_array,
ion_port_bind_f64_array,
)

from .Type import Type
Expand Down Expand Up @@ -62,38 +76,93 @@ def __del__(self):
if self.obj: # check not nullptr
ion_port_destroy(self.obj)

def bind(self, v: Union[int, float, Buffer, List[Buffer]]):
def bind(self, v: Union[int, float, Buffer, List[Union[Buffer, int, float]]]):
if self.dim == 0:
if self.bind_value is None:
self.bind_value = np.ctypeslib.as_ctypes_type(self.type.to_dtype())(v)
else:
self.bind_value.value = v
# scalar
if self.type.code_ == TypeCode.Int:
if self.type.bits_ == 8 and ion_port_bind_i8(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 16 and ion_port_bind_i16(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 32 and ion_port_bind_i32(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 64 and ion_port_bind_i64(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.code_ == TypeCode.Uint:
if self.type.bits_ == 1 and ion_port_bind_u1(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 8 and ion_port_bind_u8(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 16 and ion_port_bind_u16(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 32 and ion_port_bind_u32(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 64 and ion_port_bind_u64(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.code_ == TypeCode.Float:
if self.type.bits_ == 32 and ion_port_bind_f32(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 64 and ion_port_bind_f64(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if type(v) is not list:
if self.bind_value is None:
self.bind_value = np.ctypeslib.as_ctypes_type(self.type.to_dtype())(v)
else:
self.bind_value.value = v

if self.type.code_ == TypeCode.Int:
if self.type.bits_ == 8 and ion_port_bind_i8_array(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 16 and ion_port_bind_i16(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 32 and ion_port_bind_i32(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 64 and ion_port_bind_i64(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.code_ == TypeCode.Uint:
if self.type.bits_ == 1 and ion_port_bind_u1(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 8 and ion_port_bind_u8(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 16 and ion_port_bind_u16(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 32 and ion_port_bind_u32(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 64 and ion_port_bind_u64(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
elif self.type.code_ == TypeCode.Float:
if self.type.bits_ == 32 and ion_port_bind_f32(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 64 and ion_port_bind_f64(self.obj, ctypes.byref(self.bind_value)) != 0:
raise Exception('Invalid operation')
else:
# scalar array
c_arr = None
if self.type.code_ == TypeCode.Int:
if self.type.bits_ == 8:
c_arr = (ctypes.c_int8 * len(v))(*v)
if ion_port_bind_i8_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 16:
c_arr = (ctypes.c_int16 * len(v))(*v)
if ion_port_bind_i16_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 32 :
c_arr = (ctypes.c_int32 * len(v))(*v)
if ion_port_bind_i32_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
elif self.type.bits_ == 64:
c_arr = (ctypes.c_int64 * len(v))(*v)
if ion_port_bind_i64_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
elif self.type.code_ == TypeCode.Uint:
if self.type.bits_ == 1:
c_arr = (ctypes.c_bool * len(v))(*v)
if ion_port_bind_u1_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 8 :
c_arr = (ctypes.c_uint8 * len(v))(*v)
if ion_port_bind_u8_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 16:
c_arr = (ctypes.c_uint16 * len(v))(*v)
if ion_port_bind_u16_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 32:
c_arr = (ctypes.c_uint32* len(v))(*v)
if ion_port_bind_u32_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 64:
c_arr = (ctypes.c_uint64 * len(v))(*v)
if ion_port_bind_u64_array(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
elif self.type.code_ == TypeCode.Float:
if self.type.bits_ == 32:
c_arr = (ctypes.c_float * len(v))(*v)
if ion_port_bind_f32(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')
if self.type.bits_ == 64:
c_arr = (ctypes.c_double * len(v))(*v)
if ion_port_bind_f64(self.obj, c_arr, len(v)) != 0:
raise Exception('Invalid operation')

self.bind_value = c_arr

# vector
else:

Expand Down
58 changes: 57 additions & 1 deletion python/ionpy/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,67 @@ class c_builder_compile_option_t(ctypes.Structure):
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_t, double*;
# int ion_port_bind_f64(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_i8_array(ion_port_t, int8_t*, int);
ion_port_bind_i8_array = ion_core.ion_port_bind_i8_array
ion_port_bind_i8_array.restype = ctypes.c_int
ion_port_bind_i8_array.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int8), ctypes.c_int ]

# int ion_port_bind_i16_array(ion_port_t, int16_t*, int);
ion_port_bind_i16_array = ion_core.ion_port_bind_i16_array
ion_port_bind_i16_array.restype = ctypes.c_int
ion_port_bind_i16_array.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int16), ctypes.c_int ]

# int ion_port_bind_i32_array(ion_port_t, int32_t*, int);
ion_port_bind_i32_array = ion_core.ion_port_bind_i32_array
ion_port_bind_i32_array.restype = ctypes.c_int
ion_port_bind_i32_array.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int32), ctypes.c_int ]

# int ion_port_bind_i64_array(ion_port_t, int64_t*, int);
ion_port_bind_i64_array = ion_core.ion_port_bind_i64_array
ion_port_bind_i64_array.restype = ctypes.c_int
ion_port_bind_i64_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_int64), ctypes.c_int ]

# int ion_port_map_set_u1_array(ion_port_t, bool*, int);
ion_port_bind_u1_array = ion_core.ion_port_bind_u1_array
ion_port_bind_u1_array.restype = ctypes.c_int
ion_port_bind_u1_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_bool), ctypes.c_int ]

# int ion_port_bind_u8_array(ion_port_t, uint8_t*, int);
ion_port_bind_u8_array = ion_core.ion_port_bind_u8_array
ion_port_bind_u8_array.restype = ctypes.c_int
ion_port_bind_u8_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint8), ctypes.c_int ]

# int ion_port_bind_u16_array(ion_port_t, uint16_t*, int);
ion_port_bind_u16_array = ion_core.ion_port_bind_u16_array
ion_port_bind_u16_array.restype = ctypes.c_int
ion_port_bind_u16_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint16), ctypes.c_int ]

# int ion_port_bind_u32_array(ion_port_t, uint32_t*, int);
ion_port_bind_u32_array = ion_core.ion_port_bind_u32_array
ion_port_bind_u32_array.restype = ctypes.c_int
ion_port_bind_u32_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint32), ctypes.c_int ]

# int ion_port_bind_u64_array(ion_port_t, uint64_t*, int);
ion_port_bind_u64_array = ion_core.ion_port_bind_u64_array
ion_port_bind_u64_array.restype = ctypes.c_int
ion_port_bind_u64_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint64), ctypes.c_int ]

# int ion_port_bind_f32_array(ion_port_t, float*, int);
ion_port_bind_f32_array = ion_core.ion_port_bind_f32_array
ion_port_bind_f32_array.restype = ctypes.c_int
ion_port_bind_f32_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_float), ctypes.c_int ]

# int ion_port_bind_f64_array(ion_port_t, double*, int);
ion_port_bind_f64_array = ion_core.ion_port_bind_f64_array
ion_port_bind_f64_array.restype = ctypes.c_int
ion_port_bind_f64_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_double), ctypes.c_int ]


# 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
Expand Down

0 comments on commit ca0d995

Please sign in to comment.