Coverage for cuda/core/_memory/_legacy.py: 50.00%
32 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-05 01:11 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-05 01:11 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
5from __future__ import annotations
7from typing import TYPE_CHECKING
9if TYPE_CHECKING:
10 from cuda.core._stream import Stream
11 from cuda.core.graph import GraphBuilder
12 from cuda.core.typing import DevicePointerType
14from cuda.core._memory._buffer import Buffer, MemoryResource
15from cuda.core._utils.cuda_utils import ( # type: ignore[attr-defined] # stubgen-pyx omits .pxd cpdef bodies
16 _check_driver_error as raise_if_driver_error,
17)
18from cuda.core._utils.cuda_utils import (
19 driver,
20)
22__all__ = ["LegacyPinnedMemoryResource"]
25class LegacyPinnedMemoryResource(MemoryResource):
26 """Create a pinned memory resource that uses legacy cuMemAllocHost/cudaMallocHost
27 APIs.
28 """
30 # TODO: support creating this MR with flags that are later passed to cuMemHostAlloc?
32 def allocate(self, size: int, *, stream: Stream | GraphBuilder | None = None) -> Buffer:
33 """Allocate a buffer of the requested size.
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.
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.
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 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
54 if stream is not None: 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
55 Stream_accept(stream)
56 if size: 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
57 err, ptr = driver.cuMemAllocHost(size) 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzb
58 raise_if_driver_error(err) 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzb
59 else:
60 ptr = 0 2Rc
61 return Buffer._init(ptr, size, self) 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
63 def deallocate(self, ptr: DevicePointerType, size: int, *, stream: Stream | GraphBuilder | None = None) -> None:
64 """Deallocate a buffer previously allocated by this resource.
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 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{b!c#c|b}b~b$cacbc%ccc'c(cdcecfcgc)c*c+c,c-c.c/c:c;c=c?c@chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
78 if stream is not None: 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{b!c#c|b}b~b$cacbc%ccc'c(cdcecfcgc)c*c+c,c-c.c/c:c;c=c?c@chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
79 Stream_accept(stream).sync() 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{b!c#c|b}b~b$cacbc%ccc'c(cdcecfcgc)c*c+c,c-c.c/c:c;c=c?c@chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
81 if size: 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{b!c#c|b}b~b$cacbc%ccc'c(cdcecfcgc)c*c+c,c-c.c/c:c;c=c?c@chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
82 (err,) = driver.cuMemFreeHost(ptr) 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{b!c#c|b}b~b$cacbc%ccc'c(cdcecfcgc)c*c+c,c-c.c/c:c;c=c?c@chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzb
83 raise_if_driver_error(err) 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{b!c#c|b}b~b$cacbc%ccc'c(cdcecfcgc)c*c+c,c-c.c/c:c;c=c?c@chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzb
85 @property
86 def is_device_accessible(self) -> bool:
87 """bool: this memory resource provides device-accessible buffers."""
88 return True 2a 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 A B C D E F AbG H I J K L M BbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^bN O P _b`b{bScTc|b}b~bUcacbcVcccWcXcdcecfcgcYcZc0c1c2c3c4c5c6c7c8c9chcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcQ R S McNcT U OcV PcQcW X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbRcwbxbybzb
90 @property
91 def is_host_accessible(self) -> bool:
92 """bool: this memory resource provides host-accessible buffers."""
93 return True 2a 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 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 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbyb
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[czb