Skip to content

Commit

Permalink
Arrays: Prepare count pointer for the future matrix shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
PMeira committed Jul 10, 2022
1 parent 1e64689 commit 3bb209c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dss/IObj.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def _set_obj(self, idx: int, other):

def _get_obj_array(self, idx: int, pycls):
ptr = self._ffi.new('void***')
cnt = self._ffi.new('int32_t[2]')
cnt = self._ffi.new('int32_t[4]')
self._lib.Obj_GetObjectArray(ptr, cnt, self._ptr, idx)
if not cnt[0]:
self._lib.DSS_Dispose_PPointer(ptr)
Expand Down Expand Up @@ -453,7 +453,7 @@ def __init__(self, api_util, **kwargs):
self._ffi = api_util.ffi

self.pointer = self._ffi.new('void***')
self.count = self._ffi.new('int32_t[2]')
self.count = self._ffi.new('int32_t[4]')
if len(kwargs) == 0:
self._lib.Batch_CreateByClass(self.pointer, self.count, self._cls_idx)
self._check_for_error()
Expand Down
50 changes: 40 additions & 10 deletions dss/_cffi_api_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,51 +220,81 @@ def get_string(self, b):

def get_float64_array(self, func, *args):
ptr = self.ffi.new('double**')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy()
self.lib.DSS_Dispose_PDouble(ptr)

if cnt[3] != 0:
# If the last element is filled, we have a matrix. Otherwise, the
# matrix feature is disabled or the result is indeed a vector
return res.reshape((cnt[2], cnt[3]))

return res



def get_float64_gr_array(self):
ptr, cnt = self.gr_float64_pointers
if cnt[3] != 0:
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy().reshape((cnt[2], cnt[3]))

return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy()

def get_int32_array(self, func, *args):
ptr = self.ffi.new('int32_t**')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy()
self.lib.DSS_Dispose_PInteger(ptr)

if cnt[3] != 0:
# If the last element is filled, we have a matrix. Otherwise, the
# matrix feature is disabled or the result is indeed a vector
return res.reshape((cnt[2], cnt[3]))

return res


def get_ptr_array(self, func, *args):
ptr = self.ffi.new('void***')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * np.dtype(np.uintp).itemsize), dtype=np.uintp).copy()
self.lib.DSS_Dispose_PPointer(ptr)
return res

def get_int32_gr_array(self):
ptr, cnt = self.gr_int32_pointers
if cnt[3] != 0:
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy().reshape((cnt[2], cnt[3]))

return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy()


def get_int8_array(self, func, *args):
ptr = self.ffi.new('int8_t**')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy()
self.lib.DSS_Dispose_PByte(ptr)

if cnt[3] != 0:
# If the last element is filled, we have a matrix. Otherwise, the
# matrix feature is disabled or the result is indeed a vector
return res.reshape((cnt[2], cnt[3]))

return res

def get_int8_gr_array(self):
ptr, cnt = self.gr_int8_pointers
if cnt[3] != 0:
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy().reshape((cnt[2], cnt[3]))

return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy()

def get_string_array(self, func, *args):
ptr = self.ffi.new('char***')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = []
Expand All @@ -283,7 +313,7 @@ def get_string_array(self, func, *args):

def get_string_array2(self, func, *args): # for compatibility with OpenDSSDirect.py
ptr = self.ffi.new('char***')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)

if not cnt[0]:
Expand All @@ -308,7 +338,7 @@ def get_string_array2(self, func, *args): # for compatibility with OpenDSSDirect

def get_float64_array2(self, func, *args):
ptr = self.ffi.new('double**')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = []
Expand All @@ -324,7 +354,7 @@ def get_float64_gr_array2(self):

def get_int32_array2(self, func, *args):
ptr = self.ffi.new('int32_t**')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = None
Expand All @@ -340,7 +370,7 @@ def get_int32_gr_array2(self):

def get_int8_array2(self, func, *args):
ptr = self.ffi.new('int8_t**')
cnt = self.ffi.new('int32_t[2]')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = None
Expand Down
2 changes: 1 addition & 1 deletion dss/dss_capi_ir/IZIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def Extract(self, FileName):
'''
api_util = self._api_util
ptr = api_util.ffi.new('int8_t**')
cnt = api_util.ffi.new('int32_t[2]')
cnt = api_util.ffi.new('int32_t[4]')
if type(FileName) is not bytes:
FileName = FileName.encode(api_util.codec)

Expand Down

0 comments on commit 3bb209c

Please sign in to comment.