@@ -351,6 +351,9 @@ cdef class usm_ndarray:
351
351
return mem.queue
352
352
353
353
cdef c_dpctl.DPCTLSyclQueueRef get_queue_ref(self ) except * :
354
+ """
355
+ Returns a copy of DPCTLSyclQueueRef associated with array
356
+ """
354
357
cdef c_dpctl.SyclQueue q = self .get_sycl_queue()
355
358
cdef c_dpctl.DPCTLSyclQueueRef QRef = q.get_queue_ref()
356
359
cdef c_dpctl.DPCTLSyclQueueRef QRefCopy = NULL
@@ -1316,15 +1319,24 @@ cdef api void UsmNDArray_SetWritableFlag(usm_ndarray arr, int flag):
1316
1319
arr_fl |= (USM_ARRAY_WRITABLE if flag else 0 )
1317
1320
arr.flags_ = arr_fl
1318
1321
1319
- cdef api object UsmNDArray_MakeFromMemory (
1322
+ cdef api object UsmNDArray_MakeSimpleFromMemory (
1320
1323
int nd, const Py_ssize_t * shape, int typenum,
1321
1324
c_dpmem._Memory mobj, Py_ssize_t offset, char order
1322
1325
):
1323
- """ Create usm_ndarray.
1324
-
1325
- Equivalent to usm_ndarray(
1326
- _make_tuple(nd, shape), dtype=_make_dtype(typenum),
1327
- buffer=mobj, offset=offset)
1326
+ """ Create contiguous usm_ndarray.
1327
+
1328
+ Args:
1329
+ nd: number of dimensions (non-negative)
1330
+ shape: array of nd non-negative array's sizes along each dimension
1331
+ typenum: array elemental type number
1332
+ ptr: pointer to the start of allocation
1333
+ QRef: DPCTLSyclQueueRef associated with the allocation
1334
+ offset: distance between element with zero multi-index and the
1335
+ start of allocation
1336
+ oder: Memory layout of the array. Use 'C' for C-contiguous or
1337
+ row-major layout; 'F' for F-contiguous or column-major layout
1338
+ Returns:
1339
+ Created usm_ndarray instance
1328
1340
"""
1329
1341
cdef object shape_tuple = _make_int_tuple(nd, < Py_ssize_t * > shape)
1330
1342
cdef usm_ndarray arr = usm_ndarray(
@@ -1337,17 +1349,25 @@ cdef api object UsmNDArray_MakeFromMemory(
1337
1349
return arr
1338
1350
1339
1351
1340
- cdef api object UsmNDArray_MakeFromPtr (
1352
+ cdef api object UsmNDArray_MakeSimpleFromPtr (
1341
1353
size_t nelems,
1342
1354
int typenum,
1343
1355
c_dpctl.DPCTLSyclUSMRef ptr,
1344
1356
c_dpctl.DPCTLSyclQueueRef QRef,
1345
1357
object owner
1346
1358
):
1347
- """ Create usm_ndarray from pointer.
1348
-
1349
- Argument owner=None implies transert of USM allocation ownership
1350
- to create array object.
1359
+ """ Create 1D contiguous usm_ndarray from pointer.
1360
+
1361
+ Args:
1362
+ nelems: number of elements in array
1363
+ typenum: array elemental type number
1364
+ ptr: pointer to the start of allocation
1365
+ QRef: DPCTLSyclQueueRef associated with the allocation
1366
+ owner: Python object managing lifetime of USM allocation.
1367
+ Value None implies transfer of USM allocation ownership
1368
+ to the created array object.
1369
+ Returns:
1370
+ Created usm_ndarray instance
1351
1371
"""
1352
1372
cdef size_t itemsize = type_bytesize(typenum)
1353
1373
cdef size_t nbytes = itemsize * nelems
@@ -1360,3 +1380,108 @@ cdef api object UsmNDArray_MakeFromPtr(
1360
1380
buffer = mobj
1361
1381
)
1362
1382
return arr
1383
+
1384
+ cdef api object UsmNDArray_MakeFromPtr(
1385
+ int nd,
1386
+ const Py_ssize_t * shape,
1387
+ int typenum,
1388
+ const Py_ssize_t * strides,
1389
+ c_dpctl.DPCTLSyclUSMRef ptr,
1390
+ c_dpctl.DPCTLSyclQueueRef QRef,
1391
+ Py_ssize_t offset,
1392
+ object owner
1393
+ ):
1394
+ """
1395
+ General usm_ndarray constructor from externally made USM-allocation.
1396
+
1397
+ Args:
1398
+ nd: number of dimensions (non-negative)
1399
+ shape: array of nd non-negative array's sizes along each dimension
1400
+ typenum: array elemental type number
1401
+ strides: array of nd strides along each dimension in elements
1402
+ ptr: pointer to the start of allocation
1403
+ QRef: DPCTLSyclQueueRef associated with the allocation
1404
+ offset: distance between element with zero multi-index and the
1405
+ start of allocation
1406
+ owner: Python object managing lifetime of USM allocation.
1407
+ Value None implies transfer of USM allocation ownership
1408
+ to the created array object.
1409
+ Returns:
1410
+ Created usm_ndarray instance
1411
+ """
1412
+ cdef size_t itemsize = type_bytesize(typenum)
1413
+ cdef int err = 0
1414
+ cdef size_t nelems = 1
1415
+ cdef Py_ssize_t min_disp = 0
1416
+ cdef Py_ssize_t max_disp = 0
1417
+ cdef Py_ssize_t step_ = 0
1418
+ cdef Py_ssize_t dim_ = 0
1419
+ cdef it = 0
1420
+ cdef c_dpmem._Memory mobj
1421
+ cdef usm_ndarray arr
1422
+ cdef object obj_shape
1423
+ cdef object obj_strides
1424
+
1425
+ if (nd < 0 ):
1426
+ raise ValueError (" Dimensionality must be non-negative" )
1427
+ if (ptr is NULL or QRef is NULL ):
1428
+ raise ValueError (
1429
+ " Non-null USM allocation pointer and QRef are expected"
1430
+ )
1431
+ if (nd == 0 ):
1432
+ # case of 0d scalars
1433
+ mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
1434
+ ptr, itemsize, QRef, memory_owner = owner
1435
+ )
1436
+ arr = usm_ndarray(
1437
+ tuple (),
1438
+ dtype = _make_typestr(typenum),
1439
+ buffer = mobj
1440
+ )
1441
+ return arr
1442
+ if (shape is NULL or strides is NULL ):
1443
+ raise ValueError (" Both shape and stride vectors are required" )
1444
+ for it in range (nd):
1445
+ dim_ = shape[it]
1446
+ if dim_ < 0 :
1447
+ raise ValueError (
1448
+ f" Dimension along axis {it} must be non-negative"
1449
+ )
1450
+ nelems *= dim_
1451
+ if dim_ > 0 :
1452
+ step_ = strides[it]
1453
+ if step_ > 0 :
1454
+ max_disp += step_ * (dim_ - 1 )
1455
+ else :
1456
+ min_disp += step_ * (dim_ - 1 )
1457
+
1458
+ obj_shape = _make_int_tuple(nd, shape)
1459
+ obj_strides = _make_int_tuple(nd, strides)
1460
+ if nelems == 0 :
1461
+ mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
1462
+ ptr, itemsize, QRef, memory_owner = owner
1463
+ )
1464
+ arr = usm_ndarray(
1465
+ obj_shape,
1466
+ dtype = _make_typestr(typenum),
1467
+ strides = obj_strides,
1468
+ buffer = mobj,
1469
+ offset = 0
1470
+ )
1471
+ return arr
1472
+ if offset + min_disp < 0 :
1473
+ raise ValueError (
1474
+ " Given shape, strides and offset reference out-of-bound memory"
1475
+ )
1476
+ nbytes = itemsize * (offset + max_disp + 1 )
1477
+ mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
1478
+ ptr, nbytes, QRef, memory_owner = owner
1479
+ )
1480
+ arr = usm_ndarray(
1481
+ obj_shape,
1482
+ dtype = _make_typestr(typenum),
1483
+ strides = obj_strides,
1484
+ buffer = mobj,
1485
+ offset = offset
1486
+ )
1487
+ return arr
0 commit comments