@@ -302,13 +302,16 @@ cdef class _Memory:
302
302
return obj
303
303
304
304
cpdef copy_from_host(self , object obj):
305
- """ Copy content of Python buffer provided by `obj` to instance memory."""
305
+ """
306
+ Copy content of Python buffer provided by `obj` to instance memory.
307
+ """
306
308
cdef const unsigned char [::1 ] host_buf = obj
307
309
cdef Py_ssize_t buf_len = len (host_buf)
308
310
309
311
if (buf_len > self .nbytes):
310
312
raise ValueError (" Source object is too large to be "
311
- " accommodated in {} bytes buffer" .format(self .nbytes))
313
+ " accommodated in {} bytes buffer" .format(
314
+ self .nbytes))
312
315
# call kernel to copy from
313
316
DPCTLQueue_Memcpy(
314
317
self .queue.get_queue_ref(),
@@ -332,7 +335,8 @@ cdef class _Memory:
332
335
333
336
if (src_buf.nbytes > self .nbytes):
334
337
raise ValueError (" Source object is too large to "
335
- " be accommondated in {} bytes buffer" .format(self .nbytes))
338
+ " be accommondated in {} bytes buffer" .format(
339
+ self .nbytes))
336
340
kind = DPCTLUSM_GetPointerType(
337
341
src_buf.p, self .queue.get_sycl_context().get_context_ref())
338
342
if (kind == b' unknown' ):
@@ -364,107 +368,128 @@ cdef class _Memory:
364
368
365
369
@staticmethod
366
370
cdef SyclDevice get_pointer_device(DPCTLSyclUSMRef p, SyclContext ctx):
367
- """ Returns sycl device used to allocate given pointer `p` in given sycl context `ctx`"""
368
- cdef DPCTLSyclDeviceRef dref = DPCTLUSM_GetPointerDevice(p, ctx.get_context_ref())
371
+ """
372
+ Returns sycl device used to allocate given pointer `p` in
373
+ given sycl context `ctx`
374
+ """
375
+ cdef DPCTLSyclDeviceRef dref = DPCTLUSM_GetPointerDevice(
376
+ p, ctx.get_context_ref())
369
377
370
378
return SyclDevice._create(dref)
371
379
372
380
@staticmethod
373
381
cdef bytes get_pointer_type(DPCTLSyclUSMRef p, SyclContext ctx):
374
382
""" Returns USM-type of given pointer `p` in given sycl context `ctx`"""
375
- cdef const char * usm_type = DPCTLUSM_GetPointerType(p, ctx.get_context_ref())
383
+ cdef const char * usm_type = DPCTLUSM_GetPointerType(
384
+ p, ctx.get_context_ref())
376
385
377
386
return < bytes> usm_type
378
387
379
388
380
389
cdef class MemoryUSMShared(_Memory):
381
390
"""
382
- MemoryUSMShared(nbytes, alignment=0, queue=None, copy=False) allocates nbytes of
383
- USM shared memory.
391
+ MemoryUSMShared(nbytes, alignment=0, queue=None, copy=False)
392
+ allocates nbytes of USM shared memory.
384
393
385
394
Non-positive alignments are not used (malloc_shared is used instead).
386
395
For the queue=None cast the `dpctl.SyclQueue()` is used to allocate memory.
387
396
388
- MemoryUSMShared(usm_obj) constructor create instance from `usm_obj` expected to
389
- implement `__sycl_usm_array_interface__` protocol and exposing a contiguous block of
390
- USM memory of USM shared type. Using copy=True to perform a copy if USM type is other
391
- than 'shared'.
397
+ MemoryUSMShared(usm_obj) constructor create instance from `usm_obj`
398
+ expected to implement `__sycl_usm_array_interface__` protocol and exposing
399
+ a contiguous block of USM memory of USM shared type. Using copy=True to
400
+ perform a copy if USM type is other than 'shared'.
392
401
"""
393
- def __cinit__ (self , other , *, Py_ssize_t alignment = 0 , SyclQueue queue = None , int copy = False ):
402
+ def __cinit__ (self , other , *, Py_ssize_t alignment = 0 ,
403
+ SyclQueue queue = None , int copy = False ):
394
404
if (isinstance (other, numbers.Integral)):
395
405
self ._cinit_alloc(alignment, < Py_ssize_t> other, b" shared" , queue)
396
406
else :
397
407
self ._cinit_other(other)
398
408
if (self .get_usm_type() != " shared" ):
399
409
if copy:
400
- self ._cinit_alloc(0 , < Py_ssize_t> self .nbytes, b" shared" , queue)
410
+ self ._cinit_alloc(0 , < Py_ssize_t> self .nbytes,
411
+ b" shared" , queue)
401
412
self .copy_from_device(other)
402
413
else :
403
- raise ValueError (" USM pointer in the argument {} is not a USM shared pointer. "
404
- " Zero-copy operation is not possible with copy=False. "
405
- " Either use copy=True, or use a constructor appropriate for "
406
- " type '{}'" .format(other, self .get_usm_type()))
414
+ raise ValueError (
415
+ " USM pointer in the argument {} is not a "
416
+ " USM shared pointer. "
417
+ " Zero-copy operation is not possible with "
418
+ " copy=False. "
419
+ " Either use copy=True, or use a constructor "
420
+ " appropriate for "
421
+ " type '{}'" .format(other, self .get_usm_type()))
407
422
408
423
def __getbuffer__ (self , Py_buffer *buffer , int flags ):
409
424
self ._getbuffer(buffer , flags)
410
425
411
426
412
427
cdef class MemoryUSMHost(_Memory):
413
428
"""
414
- MemoryUSMHost(nbytes, alignment=0, queue=None, copy=False) allocates nbytes of
415
- USM host memory.
429
+ MemoryUSMHost(nbytes, alignment=0, queue=None, copy=False)
430
+ allocates nbytes of USM host memory.
416
431
417
432
Non-positive alignments are not used (malloc_host is used instead).
418
433
For the queue=None case `dpctl.SyclQueue()` is used to allocate memory.
419
434
420
- MemoryUSMDevice(usm_obj) constructor create instance from `usm_obj` expected to
421
- implement `__sycl_usm_array_interface__` protocol and exposing a contiguous block of
422
- USM memory of USM host type. Using copy=True to perform a copy if USM type is other
423
- than 'host'.
435
+ MemoryUSMDevice(usm_obj) constructor create instance from `usm_obj`
436
+ expected to implement `__sycl_usm_array_interface__` protocol and exposing
437
+ a contiguous block of USM memory of USM host type. Using copy=True to
438
+ perform a copy if USM type is other than 'host'.
424
439
"""
425
- def __cinit__ (self , other , *, Py_ssize_t alignment = 0 , SyclQueue queue = None , int copy = False ):
440
+ def __cinit__ (self , other , *, Py_ssize_t alignment = 0 ,
441
+ SyclQueue queue = None , int copy = False ):
426
442
if (isinstance (other, numbers.Integral)):
427
443
self ._cinit_alloc(alignment, < Py_ssize_t> other, b" host" , queue)
428
444
else :
429
445
self ._cinit_other(other)
430
446
if (self .get_usm_type() != " host" ):
431
447
if copy:
432
- self ._cinit_alloc(0 , < Py_ssize_t> self .nbytes, b" host" , queue)
448
+ self ._cinit_alloc(0 , < Py_ssize_t> self .nbytes,
449
+ b" host" , queue)
433
450
self .copy_from_device(other)
434
451
else :
435
- raise ValueError (" USM pointer in the argument {} is not a USM host pointer. "
436
- " Zero-copy operation is not possible with copy=False. "
437
- " Either use copy=True, or use a constructor appropriate for "
438
- " type '{}'" .format(other, self .get_usm_type()))
452
+ raise ValueError (
453
+ " USM pointer in the argument {} is "
454
+ " not a USM host pointer. "
455
+ " Zero-copy operation is not possible with copy=False. "
456
+ " Either use copy=True, or use a constructor "
457
+ " appropriate for type '{}'" .format(
458
+ other, self .get_usm_type()))
439
459
440
460
def __getbuffer__ (self , Py_buffer *buffer , int flags ):
441
461
self ._getbuffer(buffer , flags)
442
462
443
463
444
464
cdef class MemoryUSMDevice(_Memory):
445
465
"""
446
- MemoryUSMDevice(nbytes, alignment=0, queue=None, copy=False) allocates nbytes of
447
- USM device memory.
466
+ MemoryUSMDevice(nbytes, alignment=0, queue=None, copy=False)
467
+ allocates nbytes of USM device memory.
448
468
449
469
Non-positive alignments are not used (malloc_device is used instead).
450
470
For the queue=None cast the `dpctl.SyclQueue()` is used to allocate memory.
451
471
452
- MemoryUSMDevice(usm_obj) constructor create instance from `usm_obj` expected to
453
- implement `__sycl_usm_array_interface__` protocol and exposing a contiguous block of
454
- USM memory of USM device type. Using copy=True to perform a copy if USM type is other
455
- than 'device'.
472
+ MemoryUSMDevice(usm_obj) constructor create instance from `usm_obj`
473
+ expected to implement `__sycl_usm_array_interface__` protocol and exposing
474
+ a contiguous block of USM memory of USM device type. Using copy=True to
475
+ perform a copy if USM type is other than 'device'.
456
476
"""
457
- def __cinit__ (self , other , *, Py_ssize_t alignment = 0 , SyclQueue queue = None , int copy = False ):
477
+ def __cinit__ (self , other , *, Py_ssize_t alignment = 0 ,
478
+ SyclQueue queue = None , int copy = False ):
458
479
if (isinstance (other, numbers.Integral)):
459
480
self ._cinit_alloc(alignment, < Py_ssize_t> other, b" device" , queue)
460
481
else :
461
482
self ._cinit_other(other)
462
483
if (self .get_usm_type() != " device" ):
463
484
if copy:
464
- self ._cinit_alloc(0 , < Py_ssize_t> self .nbytes, b" device" , queue)
485
+ self ._cinit_alloc(0 , < Py_ssize_t> self .nbytes,
486
+ b" device" , queue)
465
487
self .copy_from_device(other)
466
488
else :
467
- raise ValueError (" USM pointer in the argument {} is not a USM device pointer. "
468
- " Zero-copy operation is not possible with copy=False. "
469
- " Either use copy=True, or use a constructor appropriate for "
470
- " type '{}'" .format(other, self .get_usm_type()))
489
+ raise ValueError (
490
+ " USM pointer in the argument {} is not "
491
+ " a USM device pointer. "
492
+ " Zero-copy operation is not possible with copy=False. "
493
+ " Either use copy=True, or use a constructor "
494
+ " appropriate for type '{}'" .format(
495
+ other, self .get_usm_type()))
0 commit comments