@@ -253,6 +253,71 @@ def copy_from_usm_ndarray_to_usm_ndarray(dst, src):
253
253
copy_same_shape (dst , src_same_shape )
254
254
255
255
256
+ def copy (usm_ary , order = "K" ):
257
+ """
258
+ Creates a copy of given instance of `usm_ndarray`.
259
+
260
+ Memory layour of the copy is controlled by `order` keyword,
261
+ following NumPy's conventions. The `order` keywords can be
262
+ one of the following:
263
+
264
+ "C": C-contiguous memory layout
265
+ "F": Fotrant-contiguous memory layout
266
+ "A": Fotrant-contiguous if the input array is
267
+ F-contiguous, and C-contiguous otherwise
268
+ "K": match the layout of `usm_ary` as closely
269
+ as possible.
270
+
271
+ """
272
+ if not isinstance (usm_ary , dpt .usm_ndarray ):
273
+ return TypeError (
274
+ "Expected object of type dpt.usm_ndarray, got {}" .format (
275
+ type (usm_ary )
276
+ )
277
+ )
278
+ copy_order = "C"
279
+ if order == "C" :
280
+ pass
281
+ elif order == "F" :
282
+ copy_order = order
283
+ elif order == "A" :
284
+ if usm_ary .flags & 2 :
285
+ copy_order = "F"
286
+ elif order == "K" :
287
+ if usm_ary .flags & 2 :
288
+ copy_order = "F"
289
+ else :
290
+ raise ValueError (
291
+ "Unrecognized value of the order keyword. "
292
+ "Recognized values are 'A', 'C', 'F', or 'K'"
293
+ )
294
+ c_contig = usm_ary .flags & 1
295
+ f_contig = usm_ary .flags & 2
296
+ R = dpt .usm_ndarray (
297
+ usm_ary .shape ,
298
+ dtype = usm_ary .dtype ,
299
+ buffer = usm_ary .usm_type ,
300
+ order = copy_order ,
301
+ buffer_ctor_kwargs = {"queue" : usm_ary .sycl_queue },
302
+ )
303
+ if order == "K" and (not c_contig and not f_contig ):
304
+ original_strides = usm_ary .strides
305
+ ind = sorted (
306
+ range (usm_ary .ndim ),
307
+ key = lambda i : abs (original_strides [i ]),
308
+ reverse = True ,
309
+ )
310
+ new_strides = tuple (R .strides [ind [i ]] for i in ind )
311
+ R = dpt .usm_ndarray (
312
+ usm_ary .shape ,
313
+ dtype = usm_ary .dtype ,
314
+ buffer = R .usm_data ,
315
+ strides = new_strides ,
316
+ )
317
+ copy_same_dtype (R , usm_ary )
318
+ return R
319
+
320
+
256
321
def astype (usm_ary , newdtype , order = "K" , casting = "unsafe" , copy = True ):
257
322
"""
258
323
astype(usm_array, new_dtype, order="K", casting="unsafe", copy=True)
@@ -267,6 +332,11 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
267
332
type (usm_ary )
268
333
)
269
334
)
335
+ if not isinstance (order , str ) or order not in ["A" , "C" , "F" , "K" ]:
336
+ raise ValueError (
337
+ "Unrecognized value of the order keyword. "
338
+ "Recognized values are 'A', 'C', 'F', or 'K'"
339
+ )
270
340
ary_dtype = usm_ary .dtype
271
341
target_dtype = np .dtype (newdtype )
272
342
if not np .can_cast (ary_dtype , target_dtype , casting = casting ):
@@ -294,6 +364,11 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
294
364
elif order == "K" :
295
365
if usm_ary .flags & 2 :
296
366
copy_order = "F"
367
+ else :
368
+ raise ValueError (
369
+ "Unrecognized value of the order keyword. "
370
+ "Recognized values are 'A', 'C', 'F', or 'K'"
371
+ )
297
372
R = dpt .usm_ndarray (
298
373
usm_ary .shape ,
299
374
dtype = target_dtype ,
0 commit comments