Skip to content

Commit 0216e82

Browse files
committed
BUGFIX / PERF: Fixing bugs in interop functions.
- Call arr._reorder instead of transpose when using row major. - Disable additional copies when is_device is false
1 parent e2ba101 commit 0216e82

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

arrayfire/array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,9 +1198,11 @@ def to_ctype(self, row_major=False, return_shape=False):
11981198
if (self.arr.value == 0):
11991199
raise RuntimeError("Can not call to_ctype on empty array")
12001200

1201-
tmp = transpose(self) if row_major else self
1201+
tmp = self._reorder() if (row_major) else self
1202+
12021203
ctype_type = to_c_type[self.type()] * self.elements()
12031204
res = ctype_type()
1205+
12041206
safe_call(backend.get().af_get_data_ptr(c_pointer(res), self.arr))
12051207
if (return_shape):
12061208
return res, self.dims()

arrayfire/interop.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def _fc_to_af_array(in_ptr, in_shape, in_dtype, is_device=False, copy = True):
2929
"""
3030
res = Array(in_ptr, in_shape, in_dtype, is_device=is_device)
3131

32-
if is_device:
33-
lock_array(res)
34-
pass
32+
if not is_device:
33+
return res
3534

35+
lock_array(res)
3636
return res.copy() if copy else res
3737

3838
def _cc_to_af_array(in_ptr, ndim, in_shape, in_dtype, is_device=False, copy = True):
@@ -41,24 +41,11 @@ def _cc_to_af_array(in_ptr, ndim, in_shape, in_dtype, is_device=False, copy = Tr
4141
"""
4242
if ndim == 1:
4343
return _fc_to_af_array(in_ptr, in_shape, in_dtype, is_device, copy)
44-
elif ndim == 2:
45-
shape = (in_shape[1], in_shape[0])
46-
res = Array(in_ptr, shape, in_dtype, is_device=is_device)
47-
if is_device: lock_array(res)
48-
return reorder(res, 1, 0)
49-
elif ndim == 3:
50-
shape = (in_shape[2], in_shape[1], in_shape[0])
51-
res = Array(in_ptr, shape, in_dtype, is_device=is_device)
52-
if is_device: lock_array(res)
53-
return reorder(res, 2, 1, 0)
54-
elif ndim == 4:
55-
shape = (in_shape[3], in_shape[2], in_shape[1], in_shape[0])
44+
else:
45+
shape = tuple(reversed(in_shape))
5646
res = Array(in_ptr, shape, in_dtype, is_device=is_device)
5747
if is_device: lock_array(res)
58-
return reorder(res, 3, 2, 1, 0)
59-
else:
60-
raise RuntimeError("Unsupported ndim")
61-
48+
return res._reorder()
6249

6350
_nptype_to_aftype = {'b1' : Dtype.b8,
6451
'u1' : Dtype.u8,

0 commit comments

Comments
 (0)