Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit f78c4b2

Browse files
authored
numpy-like copy (#581)
* numpy-like copy * Fix dtype initial * fix impl bool+number * small fix * fix tests
1 parent 14bda78 commit f78c4b2

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

sdc/functions/numpy_like.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def astype(self, dtype):
4848
pass
4949

5050

51+
def copy(self):
52+
pass
53+
54+
5155
def isnan(self):
5256
pass
5357

@@ -125,6 +129,39 @@ def sdc_astype_number_impl(self, dtype):
125129
ty_checker.raise_exc(self.dtype, 'str or type', 'self.dtype')
126130

127131

132+
@sdc_overload(copy)
133+
def sdc_copy_overload(self):
134+
"""
135+
Intel Scalable Dataframe Compiler Developer Guide
136+
*************************************************
137+
Parallel replacement of numpy.copy.
138+
139+
.. only:: developer
140+
Test: python -m sdc.runtests sdc.tests.test_sdc_numpy -k copy
141+
"""
142+
143+
if not isinstance(self, types.Array):
144+
return None
145+
146+
dtype = self.dtype
147+
if isinstance(dtype, (types.Number, types.Boolean, bool)):
148+
def sdc_copy_number_impl(self):
149+
length = len(self)
150+
res = numpy.empty(length, dtype=dtype)
151+
for i in prange(length):
152+
res[i] = self[i]
153+
154+
return res
155+
156+
return sdc_copy_number_impl
157+
158+
if isinstance(dtype, types.npytypes.UnicodeCharSeq):
159+
def sdc_copy_string_impl(self):
160+
return self.copy()
161+
162+
return sdc_copy_string_impl
163+
164+
128165
@sdc_overload(notnan)
129166
def sdc_isnan_overload(self):
130167
"""

sdc/tests/test_sdc_numpy.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,57 @@ def sdc_impl(a):
188188
with self.subTest(data=case):
189189
np.testing.assert_array_equal(sdc_func(a), ref_impl(a))
190190

191+
def test_copy(self):
192+
def ref_impl(a):
193+
return np.copy(a)
194+
195+
def sdc_impl(a):
196+
return numpy_like.copy(a)
197+
198+
sdc_func = self.jit(sdc_impl)
199+
200+
cases = [[5, 2, 0, 333, -4], [3.3, 5.4, np.nan, 7.9, np.nan], [True, False, True], ['a', 'vv', 'o12oo']]
201+
for case in cases:
202+
a = np.array(case)
203+
with self.subTest(data=case):
204+
np.testing.assert_array_equal(sdc_func(a), ref_impl(a))
205+
206+
def test_copy_int(self):
207+
def ref_impl():
208+
a = np.array([5, 2, 0, 333, -4])
209+
return np.copy(a)
210+
211+
def sdc_impl():
212+
a = np.array([5, 2, 0, 333, -4])
213+
return numpy_like.copy(a)
214+
215+
sdc_func = self.jit(sdc_impl)
216+
np.testing.assert_array_equal(sdc_func(), ref_impl())
217+
218+
def test_copy_bool(self):
219+
def ref_impl():
220+
a = np.array([True, False, True])
221+
return np.copy(a)
222+
223+
def sdc_impl():
224+
a = np.array([True, False, True])
225+
return numpy_like.copy(a)
226+
227+
sdc_func = self.jit(sdc_impl)
228+
np.testing.assert_array_equal(sdc_func(), ref_impl())
229+
230+
@unittest.skip("Numba doesn't have string array")
231+
def test_copy_str(self):
232+
def ref_impl():
233+
a = np.array(['a', 'vv', 'o12oo'])
234+
return np.copy(a)
235+
236+
def sdc_impl():
237+
a = np.array(['a', 'vv', 'o12oo'])
238+
return numpy_like.copy(a)
239+
240+
sdc_func = self.jit(sdc_impl)
241+
np.testing.assert_array_equal(sdc_func(), ref_impl())
191242

192243
class TestArrayReductions(TestCase):
193244

sdc/tests/tests_perf/test_perf_numpy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ def _test_case(self, cases, name, total_data_length, data_num=1, input_data=test
9999
CE(type_='Numba', code='data.astype(np.int64)', jitted=True),
100100
CE(type_='SDC', code='sdc.functions.numpy_like.astype(data, np.int64)', jitted=True),
101101
], usecase_params='data'),
102+
TC(name='copy', size=[10 ** 7], call_expr=[
103+
CE(type_='Python', code='np.copy(data)', jitted=False),
104+
CE(type_='Numba', code='np.copy(data)', jitted=True),
105+
CE(type_='SDC', code='sdc.functions.numpy_like.copy(data)', jitted=True),
106+
], usecase_params='data'),
102107
TC(name='isnan', size=[10 ** 7], call_expr=[
103108
CE(type_='Python', code='np.isnan(data)', jitted=False),
104109
CE(type_='Numba', code='np.isnan(data)', jitted=True),

0 commit comments

Comments
 (0)