Coverage for cuda/core/_memory/_legacy.py: 96.88%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-10 02:27 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4 

5from __future__ import annotations 

6 

7from typing import TYPE_CHECKING 

8 

9if TYPE_CHECKING: 

10 from cuda.core._stream import Stream 

11 from cuda.core.graph import GraphBuilder 

12 from cuda.core.typing import DevicePointerType 

13 

14from cuda.core._memory._buffer import Buffer, MemoryResource 

15from cuda.core._utils.cuda_utils import ( 

16 _check_driver_error as raise_if_driver_error, 

17) 

18from cuda.core._utils.cuda_utils import ( 

19 driver, 

20) 

21 

22__all__ = ["LegacyPinnedMemoryResource"] 

23 

24 

25class LegacyPinnedMemoryResource(MemoryResource): 

26 """Create a pinned memory resource that uses legacy cuMemAllocHost/cudaMallocHost 

27 APIs. 

28 """ 

29 

30 # TODO: support creating this MR with flags that are later passed to cuMemHostAlloc? 

31 

32 def allocate(self, size: int, *, stream: Stream | GraphBuilder | None = None) -> Buffer: 

33 """Allocate a buffer of the requested size. 

34 

35 ``cuMemAllocHost`` is synchronous, so this resource ignores any 

36 supplied stream. The argument is accepted (and validated when 

37 non-``None``) for interface conformance with stream-ordered 

38 memory resources. 

39 

40 Parameters 

41 ---------- 

42 size : int 

43 The size of the buffer to allocate, in bytes. 

44 stream : Stream, optional 

45 Keyword-only. Validated when provided but otherwise unused. 

46 

47 Returns 

48 ------- 

49 Buffer 

50 The allocated buffer object, which is accessible on both host and device. 

51 """ 

52 from cuda.core._stream import Stream_accept 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

53 

54 if stream is not None: 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

55 Stream_accept(stream) 

56 if size: 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

57 err, ptr = driver.cuMemAllocHost(size) 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtb

58 raise_if_driver_error(err) 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtb

59 else: 

60 ptr = 0 2wc

61 return Buffer._init(ptr, size, self) 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

62 

63 def deallocate(self, ptr: DevicePointerType, size: int, *, stream: Stream | GraphBuilder | None = None) -> None: 

64 """Deallocate a buffer previously allocated by this resource. 

65 

66 Parameters 

67 ---------- 

68 ptr : :obj:`~_memory.DevicePointerType` 

69 The pointer or handle to the buffer to deallocate. 

70 size : int 

71 The size of the buffer to deallocate, in bytes. 

72 stream : Stream, optional 

73 Keyword-only. If provided, ``stream.sync()`` is called before the 

74 host allocation is freed. ``None`` skips the sync. 

75 """ 

76 from cuda.core._stream import Stream_accept 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bRcSc-b.b/bTc:b;bUc=bVcWc?b@b[b]bXcYcZc0c1c2c3c4c5c6c7c8c^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrc9c!c#csctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

77 

78 if stream is not None: 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bRcSc-b.b/bTc:b;bUc=bVcWc?b@b[b]bXcYcZc0c1c2c3c4c5c6c7c8c^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrc9c!c#csctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

79 Stream_accept(stream).sync() 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bRcSc-b.b/bTc:b;bUc=bVcWc?b@b[b]bXcYcZc0c1c2c3c4c5c6c7c8c^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrc9c!c#csctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

80 

81 if size: 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bRcSc-b.b/bTc:b;bUc=bVcWc?b@b[b]bXcYcZc0c1c2c3c4c5c6c7c8c^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrc9c!c#csctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

82 (err,) = driver.cuMemFreeHost(ptr) 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bRcSc-b.b/bTc:b;bUc=bVcWc?b@b[b]bXcYcZc0c1c2c3c4c5c6c7c8c^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrc9c!c#csctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtb

83 raise_if_driver_error(err) 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bRcSc-b.b/bTc:b;bUc=bVcWc?b@b[b]bXcYcZc0c1c2c3c4c5c6c7c8c^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrc9c!c#csctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtb

84 

85 @property 

86 def is_device_accessible(self) -> bool: 

87 """bool: this memory resource provides device-accessible buffers.""" 

88 return True 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G ubH I J K L M N vbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+bO P Q ,bxcyc-b.b/bzc:b;bAc=bBcCc?b@b[b]bDcEcFcGcHcIcJcKcLcMcNcOc^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcPcQcsctcR S T ucvcU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbwcqbrbsbtb

89 

90 @property 

91 def is_host_accessible(self) -> bool: 

92 """bool: this memory resource provides host-accessible buffers.""" 

93 return True 2b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsb

94 

95 @property 

96 def device_id(self) -> int: 

97 """Return -1. Pinned memory is host memory and is not bound to a specific device.""" 

98 return -1 2$ctb