@@ -243,5 +243,82 @@ def setUp(self):
243
243
self .usm_type = "device"
244
244
245
245
246
+ class View :
247
+ def __init__ (self , buf , shape , strides , offset ):
248
+ self .buffer = buf
249
+ self .shape = shape
250
+ self .strides = strides
251
+ self .offset = offset
252
+
253
+ @property
254
+ def __sycl_usm_array_interface__ (self ):
255
+ sua_iface = self .buffer .__sycl_usm_array_interface__
256
+ sua_iface ["offset" ] = self .offset
257
+ sua_iface ["shape" ] = self .shape
258
+ sua_iface ["strides" ] = self .strides
259
+ return sua_iface
260
+
261
+
262
+ class TestMemoryWithView (unittest .TestCase ):
263
+ def test_suai_non_contig_1D (self ):
264
+ """ Test of zero-copy using sycl_usm_array_interface with non-contiguous data """
265
+
266
+ MemoryUSMClass = MemoryUSMShared
267
+ try :
268
+ buf = MemoryUSMClass (32 )
269
+ except :
270
+ self .skipTest ("MemoryUSMShared could not be allocated" )
271
+ host_canary = np .full ((buf .nbytes ,), 77 , dtype = "|u1" )
272
+ buf .copy_from_host (host_canary )
273
+ n1d = 10
274
+ step_1d = 2
275
+ offset = 8
276
+ v = View (buf , shape = (n1d ,), strides = (step_1d ,), offset = offset )
277
+ buf2 = MemoryUSMClass (v )
278
+ expected_nbytes = (
279
+ np .flip (host_canary [offset : offset + n1d * step_1d : step_1d ]).ctypes .data
280
+ + 1
281
+ - host_canary [offset :].ctypes .data
282
+ )
283
+ self .assertEqual (buf2 .nbytes , expected_nbytes )
284
+ inset_canary = np .arange (0 , buf2 .nbytes , dtype = "|u1" )
285
+ buf2 .copy_from_host (inset_canary )
286
+ res = buf .copy_to_host ()
287
+ del buf
288
+ del buf2
289
+ expected_res = host_canary .copy ()
290
+ expected_res [offset : offset + (n1d - 1 ) * step_1d + 1 ] = inset_canary
291
+ self .assertTrue (np .array_equal (res , expected_res ))
292
+
293
+ def test_suai_non_contig_2D (self ):
294
+ MemoryUSMClass = MemoryUSMDevice
295
+ try :
296
+ buf = MemoryUSMClass (20 )
297
+ except :
298
+ self .skipTest ("MemoryUSMShared could not be allocated" )
299
+ host_canary = np .arange (20 , dtype = "|u1" )
300
+ buf .copy_from_host (host_canary )
301
+ shape_2d = (2 , 2 )
302
+ strides_2d = (10 , - 2 )
303
+ offset = 9
304
+ idx = []
305
+ for i0 in range (shape_2d [0 ]):
306
+ for i1 in range (shape_2d [1 ]):
307
+ idx .append (offset + i0 * strides_2d [0 ] + i1 * strides_2d [1 ])
308
+ idx .sort ()
309
+ v = View (buf , shape = shape_2d , strides = strides_2d , offset = offset )
310
+ buf2 = MemoryUSMClass (v )
311
+ expected_nbytes = idx [- 1 ] - idx [0 ] + 1
312
+ self .assertEqual (buf2 .nbytes , expected_nbytes )
313
+ inset_canary = np .full ((buf2 .nbytes ), 255 , dtype = "|u1" )
314
+ buf2 .copy_from_host (inset_canary )
315
+ res = buf .copy_to_host ()
316
+ del buf
317
+ del buf2
318
+ expected_res = host_canary .copy ()
319
+ expected_res [idx [0 ] : idx [- 1 ] + 1 ] = inset_canary
320
+ self .assertTrue (np .array_equal (res , expected_res ))
321
+
322
+
246
323
if __name__ == "__main__" :
247
324
unittest .main ()
0 commit comments