Skip to content

Commit f094679

Browse files
authored
using container.flat instead explicit datatype casting in dpnp_array() (#863)
1 parent e7a16d2 commit f094679

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

dpnp/dpnp_algo/dpnp_algo.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ cpdef utils.dpnp_descriptor dpnp_array(object obj, object dtype=None):
108108
if not cpython.PySequence_Check(obj):
109109
raise TypeError(f"DPNP dpnp_array(): Unsupported non-sequence obj={type(obj)}")
110110

111-
obj_shape, elem_dtype = utils.get_shape_dtype(obj)
111+
obj_shape, obj_dtype = utils.get_shape_dtype(obj)
112112
if dtype is not None:
113113
""" Set type from parameter. result might be empty array """
114114
result = utils_py.create_output_descriptor_py(obj_shape, dtype, None)
@@ -117,7 +117,7 @@ cpdef utils.dpnp_descriptor dpnp_array(object obj, object dtype=None):
117117
""" Empty object (ex. empty list) and no type provided """
118118
result = utils_py.create_output_descriptor_py(obj_shape, None, None)
119119
else:
120-
result = utils_py.create_output_descriptor_py(obj_shape, elem_dtype, None)
120+
result = utils_py.create_output_descriptor_py(obj_shape, obj_dtype, None)
121121

122122
utils.container_copy(result.get_pyobj(), obj)
123123

dpnp/dpnp_utils/dpnp_algo_utils.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ cpdef find_common_type(object x1_obj, object x2_obj)
9999
Find common type of 2 input objects
100100
"""
101101

102-
cdef long container_copy(dparray dst, input_obj, size_t dst_idx=*) except -1
102+
cdef long container_copy(object dst_obj, object src_obj, size_t dst_idx=*) except -1
103103
"""
104104
Copy values to `dst` by iterating element by element in `input_obj`
105105
"""

dpnp/dpnp_utils/dpnp_algo_utils.pyx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,16 @@ cpdef dp2nd_array(arr):
175175
"""Convert dparray to ndarray"""
176176
return dpnp.asnumpy(arr) if isinstance(arr, dparray) else arr
177177

178-
cdef long container_copy(dparray dst, input_obj, size_t dst_idx=0) except -1:
179-
cdef elem_dtype = dst.dtype
178+
cdef long container_copy(object dst_obj, object src_obj, size_t dst_idx = 0) except -1:
179+
cdef elem_dtype = dst_obj.dtype
180180

181-
for elem_value in input_obj:
181+
for elem_value in src_obj:
182182
if isinstance(elem_value, (list, tuple)):
183-
dst_idx = container_copy(dst, elem_value, dst_idx)
183+
dst_idx = container_copy(dst_obj, elem_value, dst_idx)
184184
elif issubclass(type(elem_value), (numpy.ndarray, dparray)):
185-
dst_idx = container_copy(dst, elem_value, dst_idx)
185+
dst_idx = container_copy(dst_obj, elem_value, dst_idx)
186186
else:
187-
if elem_dtype == numpy.float64:
188-
( < double * > dst.get_data())[dst_idx] = elem_value
189-
elif elem_dtype == numpy.float32:
190-
( < float * > dst.get_data())[dst_idx] = elem_value
191-
elif elem_dtype == numpy.int64:
192-
( < long * > dst.get_data())[dst_idx] = elem_value
193-
elif elem_dtype == numpy.int32:
194-
( < int * > dst.get_data())[dst_idx] = elem_value
195-
elif elem_dtype == numpy.bool_ or elem_dtype == numpy.bool:
196-
(< cpp_bool * > dst.get_data())[dst_idx] = elem_value
197-
elif elem_dtype == numpy.complex64:
198-
(< cpp_complex[float] * > dst.get_data())[dst_idx] = elem_value
199-
elif elem_dtype == numpy.complex128:
200-
(< cpp_complex[double] * > dst.get_data())[dst_idx] = elem_value
201-
else:
202-
checker_throw_type_error("container_copy", elem_dtype)
203-
187+
dst_obj.flat[dst_idx] = elem_value
204188
dst_idx += 1
205189

206190
return dst_idx

0 commit comments

Comments
 (0)