@@ -44,8 +44,6 @@ def __init__(self, buffer_obj, ptr, size, mr):
44
44
45
45
def close (self , stream = None ):
46
46
if self .ptr and self .mr is not None :
47
- if stream is None :
48
- stream = default_stream ()
49
47
self .mr .deallocate (self .ptr , self .size , stream )
50
48
self .ptr = 0
51
49
self .mr = None
@@ -71,8 +69,8 @@ def close(self, stream: Stream = None):
71
69
Parameters
72
70
----------
73
71
stream : Stream, optional
74
- The stream object to use for asynchronous deallocation. If not set ,
75
- the current default is to the default stream .
72
+ The stream object to use for asynchronous deallocation. If None ,
73
+ the behavior depends on the underlying memory resource .
76
74
"""
77
75
self ._mnff .close (stream )
78
76
@@ -256,9 +254,10 @@ def allocate(self, size: int, stream: Stream = None) -> Buffer:
256
254
----------
257
255
size : int
258
256
The size of the buffer to allocate, in bytes.
259
- stream : object , optional
257
+ stream : Stream , optional
260
258
The stream on which to perform the allocation asynchronously.
261
- If None, allocation is synchronous.
259
+ If None, it is up to each memory resource implementation to decide
260
+ and document the behavior.
262
261
263
262
Returns
264
263
-------
@@ -274,13 +273,14 @@ def deallocate(self, ptr: DevicePointerT, size: int, stream: Stream = None):
274
273
275
274
Parameters
276
275
----------
277
- ptr : object
276
+ ptr : :obj:`~_memory.DevicePointerT`
278
277
The pointer or handle to the buffer to deallocate.
279
278
size : int
280
279
The size of the buffer to deallocate, in bytes.
281
- stream : object , optional
280
+ stream : Stream , optional
282
281
The stream on which to perform the deallocation asynchronously.
283
- If None, deallocation is synchronous.
282
+ If None, it is up to each memory resource implementation to decide
283
+ and document the behavior.
284
284
"""
285
285
...
286
286
@@ -309,68 +309,144 @@ def device_id(self) -> int:
309
309
...
310
310
311
311
312
- class _DefaultAsyncMempool (MemoryResource ):
312
+ class DeviceMemoryResource (MemoryResource ):
313
+ """Create a device memory resource that uses the driver's stream-ordered memory pool.
314
+
315
+ Parameters
316
+ ----------
317
+ device_id : int
318
+ Device ordinal for which a memory resource is constructed. The mempool that is
319
+ set to *current* on ``device_id`` is used. If no mempool is set to current yet,
320
+ the driver would use the *default* mempool on the device.
321
+ """
322
+
313
323
__slots__ = ("_dev_id" ,)
314
324
315
- def __init__ (self , dev_id : int ):
316
- self ._handle = handle_return (driver .cuDeviceGetMemPool (dev_id ))
317
- self ._dev_id = dev_id
325
+ def __init__ (self , device_id : int ):
326
+ self ._handle = handle_return (driver .cuDeviceGetMemPool (device_id ))
327
+ self ._dev_id = device_id
318
328
319
329
def allocate (self , size : int , stream : Stream = None ) -> Buffer :
330
+ """Allocate a buffer of the requested size.
331
+
332
+ Parameters
333
+ ----------
334
+ size : int
335
+ The size of the buffer to allocate, in bytes.
336
+ stream : Stream, optional
337
+ The stream on which to perform the allocation asynchronously.
338
+ If None, an internal stream is used.
339
+
340
+ Returns
341
+ -------
342
+ Buffer
343
+ The allocated buffer object, which is accessible on the device that this memory
344
+ resource was created for.
345
+ """
320
346
if stream is None :
321
347
stream = default_stream ()
322
348
ptr = handle_return (driver .cuMemAllocFromPoolAsync (size , self ._handle , stream .handle ))
323
349
return Buffer ._init (ptr , size , self )
324
350
325
351
def deallocate (self , ptr : DevicePointerT , size : int , stream : Stream = None ):
352
+ """Deallocate a buffer previously allocated by this resource.
353
+
354
+ Parameters
355
+ ----------
356
+ ptr : :obj:`~_memory.DevicePointerT`
357
+ The pointer or handle to the buffer to deallocate.
358
+ size : int
359
+ The size of the buffer to deallocate, in bytes.
360
+ stream : Stream, optional
361
+ The stream on which to perform the deallocation asynchronously.
362
+ If None, an internal stream is used.
363
+ """
326
364
if stream is None :
327
365
stream = default_stream ()
328
366
handle_return (driver .cuMemFreeAsync (ptr , stream .handle ))
329
367
330
368
@property
331
369
def is_device_accessible (self ) -> bool :
370
+ """bool: this memory resource provides device-accessible buffers."""
332
371
return True
333
372
334
373
@property
335
374
def is_host_accessible (self ) -> bool :
375
+ """bool: this memory resource does not provides host-accessible buffers."""
336
376
return False
337
377
338
378
@property
339
379
def device_id (self ) -> int :
380
+ """int: the associated device ordinal."""
340
381
return self ._dev_id
341
382
342
383
343
- class _DefaultPinnedMemorySource (MemoryResource ):
384
+ class LegacyPinnedMemoryResource (MemoryResource ):
385
+ """Create a pinned memory resource that uses legacy cuMemAllocHost/cudaMallocHost
386
+ APIs.
387
+ """
388
+
344
389
def __init__ (self ):
345
390
# TODO: support flags from cuMemHostAlloc?
346
391
self ._handle = None
347
392
348
393
def allocate (self , size : int , stream : Stream = None ) -> Buffer :
394
+ """Allocate a buffer of the requested size.
395
+
396
+ Parameters
397
+ ----------
398
+ size : int
399
+ The size of the buffer to allocate, in bytes.
400
+ stream : Stream, optional
401
+ Currently ignored
402
+
403
+ Returns
404
+ -------
405
+ Buffer
406
+ The allocated buffer object, which is accessible on both host and device.
407
+ """
349
408
ptr = handle_return (driver .cuMemAllocHost (size ))
350
409
return Buffer ._init (ptr , size , self )
351
410
352
411
def deallocate (self , ptr : DevicePointerT , size : int , stream : Stream = None ):
412
+ """Deallocate a buffer previously allocated by this resource.
413
+
414
+ Parameters
415
+ ----------
416
+ ptr : :obj:`~_memory.DevicePointerT`
417
+ The pointer or handle to the buffer to deallocate.
418
+ size : int
419
+ The size of the buffer to deallocate, in bytes.
420
+ stream : Stream, optional
421
+ The stream on which to perform the deallocation asynchronously.
422
+ If None, no synchronization would happen.
423
+ """
424
+ if stream :
425
+ stream .sync ()
353
426
handle_return (driver .cuMemFreeHost (ptr ))
354
427
355
428
@property
356
429
def is_device_accessible (self ) -> bool :
430
+ """bool: this memory resource provides device-accessible buffers."""
357
431
return True
358
432
359
433
@property
360
434
def is_host_accessible (self ) -> bool :
435
+ """bool: this memory resource provides host-accessible buffers."""
361
436
return True
362
437
363
438
@property
364
439
def device_id (self ) -> int :
440
+ """This memory resource is not bound to any GPU."""
365
441
raise RuntimeError ("a pinned memory resource is not bound to any GPU" )
366
442
367
443
368
444
class _SynchronousMemoryResource (MemoryResource ):
369
445
__slots__ = ("_dev_id" ,)
370
446
371
- def __init__ (self , dev_id ):
447
+ def __init__ (self , device_id ):
372
448
self ._handle = None
373
- self ._dev_id = dev_id
449
+ self ._dev_id = device_id
374
450
375
451
def allocate (self , size , stream = None ) -> Buffer :
376
452
ptr = handle_return (driver .cuMemAlloc (size ))
@@ -393,7 +469,3 @@ def is_host_accessible(self) -> bool:
393
469
@property
394
470
def device_id (self ) -> int :
395
471
return self ._dev_id
396
-
397
-
398
- DeviceMemoryResource = _DefaultAsyncMempool
399
- LegacyPinnedMemoryResource = _DefaultPinnedMemorySource
0 commit comments