Skip to main content

singe_cuda/types/
memory.rs

1use num_enum::{IntoPrimitive, TryFromPrimitive};
2use singe_core::impl_enum_display;
3use singe_cuda_sys::driver;
4
5singe_core::native_enum! {
6    /// Pointer information
7    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
8    #[repr(u32)]
9    #[non_exhaustive]
10    pub enum PointerAttribute: driver::CUpointer_attribute {
11        /// Returns in `*data` an integer representing the locality domain ordinal of the memory allocation, or -1 if the allocation is not localized to a locality domain.
12        LocalityDomainOrdinal = CU_POINTER_ATTRIBUTE_LOCALITY_DOMAIN_ORDINAL,
13        /// The `CUcontext` on which a pointer was allocated or registered.
14        Context = CU_POINTER_ATTRIBUTE_CONTEXT,
15        /// The [CUmemorytype](https://docs.nvidia.com/cuda/archive/13.4.2/cuda-driver-api/cuda_driver_api/group__CUDA__TYPES.html#group__cuda__types_1ga8a114cc994ad2e865c44ef3838eaec72) describing the physical location of a pointer.
16        MemoryType = CU_POINTER_ATTRIBUTE_MEMORY_TYPE,
17        /// The address at which a pointer's memory may be accessed on the device.
18        DevicePointer = CU_POINTER_ATTRIBUTE_DEVICE_POINTER,
19        /// The address at which a pointer's memory may be accessed on the host.
20        HostPointer = CU_POINTER_ATTRIBUTE_HOST_POINTER,
21        /// A pair of tokens for use with the nv-p2p.h Linux kernel interface.
22        P2pTokens = CU_POINTER_ATTRIBUTE_P2P_TOKENS,
23        /// Synchronize every synchronous memory operation initiated on this region.
24        SyncMemops = CU_POINTER_ATTRIBUTE_SYNC_MEMOPS,
25        /// A process-wide unique ID for an allocated memory region.
26        BufferId = CU_POINTER_ATTRIBUTE_BUFFER_ID,
27        /// Indicates if the pointer points to managed memory.
28        IsManaged = CU_POINTER_ATTRIBUTE_IS_MANAGED,
29        /// A device ordinal of a device on which a pointer was allocated or registered.
30        DeviceOrdinal = CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL,
31        /// Reports whether the allocation supports legacy CUDA IPC memory handles.
32        IsLegacyCudaIpcCapable = CU_POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE,
33        /// Starting address for this requested pointer.
34        RangeStartAddr = CU_POINTER_ATTRIBUTE_RANGE_START_ADDR,
35        /// Size of the address range for this requested pointer.
36        RangeSize = CU_POINTER_ATTRIBUTE_RANGE_SIZE,
37        /// 1 if this pointer is in a valid address range that is mapped to a backing allocation, 0 otherwise.
38        Mapped = CU_POINTER_ATTRIBUTE_MAPPED,
39        /// Bitmask of allowed [CUmemAllocationHandleType](https://docs.nvidia.com/cuda/archive/13.4.2/cuda-driver-api/cuda_driver_api/group__CUDA__TYPES.html#group__cuda__types_1ga450a23153d86fce0afe30e25d63caef9) for this allocation.
40        AllowedHandleTypes = CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES,
41        /// 1 if the memory this pointer is referencing can be used with the `GPUDirect` RDMA API.
42        IsGpuDirectRdmaCapable = CU_POINTER_ATTRIBUTE_IS_GPU_DIRECT_RDMA_CAPABLE,
43        /// Returns the access flags the device associated with the current context has on the corresponding memory referenced by the pointer given.
44        AccessFlags = CU_POINTER_ATTRIBUTE_ACCESS_FLAGS,
45        /// Returns the mempool handle for the allocation if it was allocated from a mempool.
46        ///
47        /// Otherwise returns NULL.
48        MempoolHandle = CU_POINTER_ATTRIBUTE_MEMPOOL_HANDLE,
49        /// Size of the actual underlying mapping that the pointer belongs to.
50        MappingSize = CU_POINTER_ATTRIBUTE_MAPPING_SIZE,
51        /// The start address of the mapping that the pointer belongs to.
52        MappingBaseAddr = CU_POINTER_ATTRIBUTE_MAPPING_BASE_ADDR,
53        /// A process-wide unique id corresponding to the physical allocation the pointer belongs to.
54        MemoryBlockId = CU_POINTER_ATTRIBUTE_MEMORY_BLOCK_ID,
55    }
56}
57
58impl_enum_display!(PointerAttribute, {
59    Self::LocalityDomainOrdinal => "CU_POINTER_ATTRIBUTE_LOCALITY_DOMAIN_ORDINAL",
60    Self::Context => "CU_POINTER_ATTRIBUTE_CONTEXT",
61    Self::MemoryType => "CU_POINTER_ATTRIBUTE_MEMORY_TYPE",
62    Self::DevicePointer => "CU_POINTER_ATTRIBUTE_DEVICE_POINTER",
63    Self::HostPointer => "CU_POINTER_ATTRIBUTE_HOST_POINTER",
64    Self::P2pTokens => "CU_POINTER_ATTRIBUTE_P2P_TOKENS",
65    Self::SyncMemops => "CU_POINTER_ATTRIBUTE_SYNC_MEMOPS",
66    Self::BufferId => "CU_POINTER_ATTRIBUTE_BUFFER_ID",
67    Self::IsManaged => "CU_POINTER_ATTRIBUTE_IS_MANAGED",
68    Self::DeviceOrdinal => "CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL",
69    Self::IsLegacyCudaIpcCapable => "CU_POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE",
70    Self::RangeStartAddr => "CU_POINTER_ATTRIBUTE_RANGE_START_ADDR",
71    Self::RangeSize => "CU_POINTER_ATTRIBUTE_RANGE_SIZE",
72    Self::Mapped => "CU_POINTER_ATTRIBUTE_MAPPED",
73    Self::AllowedHandleTypes => "CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES",
74    Self::IsGpuDirectRdmaCapable => "CU_POINTER_ATTRIBUTE_IS_GPU_DIRECT_RDMA_CAPABLE",
75    Self::AccessFlags => "CU_POINTER_ATTRIBUTE_ACCESS_FLAGS",
76    Self::MempoolHandle => "CU_POINTER_ATTRIBUTE_MEMPOOL_HANDLE",
77    Self::MappingSize => "CU_POINTER_ATTRIBUTE_MAPPING_SIZE",
78    Self::MappingBaseAddr => "CU_POINTER_ATTRIBUTE_MAPPING_BASE_ADDR",
79    Self::MemoryBlockId => "CU_POINTER_ATTRIBUTE_MEMORY_BLOCK_ID",
80});
81
82singe_core::native_enum! {
83    /// Memory types
84    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
85    #[repr(u32)]
86    #[non_exhaustive]
87    pub enum MemoryType: driver::CUmemorytype {
88        /// Host memory.
89        Host = CU_MEMORYTYPE_HOST,
90        /// Device memory.
91        Device = CU_MEMORYTYPE_DEVICE,
92        /// Array memory.
93        Array = CU_MEMORYTYPE_ARRAY,
94        /// Unified device or host memory.
95        Unified = CU_MEMORYTYPE_UNIFIED,
96    }
97}
98
99impl_enum_display!(MemoryType, {
100    Self::Host => "CU_MEMORYTYPE_HOST",
101    Self::Device => "CU_MEMORYTYPE_DEVICE",
102    Self::Array => "CU_MEMORYTYPE_ARRAY",
103    Self::Unified => "CU_MEMORYTYPE_UNIFIED",
104});
105
106singe_core::native_enum! {
107    /// Memory advise values
108    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
109    #[repr(u32)]
110    #[non_exhaustive]
111    pub enum MemoryAdvise: driver::CUmem_advise {
112        /// Data will mostly be read and only occasionally be written to.
113        SetReadMostly = CU_MEM_ADVISE_SET_READ_MOSTLY,
114        /// Undoes the effect of [`MemoryAdvise::SetReadMostly`].
115        UnsetReadMostly = CU_MEM_ADVISE_UNSET_READ_MOSTLY,
116        /// Set the preferred location for the data as the specified device.
117        SetPreferredLocation = CU_MEM_ADVISE_SET_PREFERRED_LOCATION,
118        /// Clear the preferred location for the data.
119        UnsetPreferredLocation = CU_MEM_ADVISE_UNSET_PREFERRED_LOCATION,
120        /// Data will be accessed by the specified device, so prevent page faults as much as possible.
121        SetAccessedBy = CU_MEM_ADVISE_SET_ACCESSED_BY,
122        /// Let the Unified Memory subsystem decide on the page faulting policy for the specified device.
123        UnsetAccessedBy = CU_MEM_ADVISE_UNSET_ACCESSED_BY,
124    }
125}
126
127impl_enum_display!(MemoryAdvise, {
128    Self::SetReadMostly => "CU_MEM_ADVISE_SET_READ_MOSTLY",
129    Self::UnsetReadMostly => "CU_MEM_ADVISE_UNSET_READ_MOSTLY",
130    Self::SetPreferredLocation => "CU_MEM_ADVISE_SET_PREFERRED_LOCATION",
131    Self::UnsetPreferredLocation => "CU_MEM_ADVISE_UNSET_PREFERRED_LOCATION",
132    Self::SetAccessedBy => "CU_MEM_ADVISE_SET_ACCESSED_BY",
133    Self::UnsetAccessedBy => "CU_MEM_ADVISE_UNSET_ACCESSED_BY",
134});
135
136singe_core::native_enum! {
137    /// Attribute queried for a managed-memory address range.
138    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
139    #[repr(u32)]
140    #[non_exhaustive]
141    pub enum MemoryRangeAttribute: driver::CUmem_range_attribute {
142        /// Whether the range will mostly be read and only occasionally be written to.
143        ReadMostly = CU_MEM_RANGE_ATTRIBUTE_READ_MOSTLY,
144        /// The preferred location of the range.
145        PreferredLocation = CU_MEM_RANGE_ATTRIBUTE_PREFERRED_LOCATION,
146        /// The devices that have [`MemoryAdvise::SetAccessedBy`] set for the range.
147        AccessedBy = CU_MEM_RANGE_ATTRIBUTE_ACCESSED_BY,
148        /// The last location to which the range was prefetched.
149        LastPrefetchLocation = CU_MEM_RANGE_ATTRIBUTE_LAST_PREFETCH_LOCATION,
150    }
151}
152
153impl_enum_display!(MemoryRangeAttribute, {
154    Self::ReadMostly => "CU_MEM_RANGE_ATTRIBUTE_READ_MOSTLY",
155    Self::PreferredLocation => "CU_MEM_RANGE_ATTRIBUTE_PREFERRED_LOCATION",
156    Self::AccessedBy => "CU_MEM_RANGE_ATTRIBUTE_ACCESSED_BY",
157    Self::LastPrefetchLocation => "CU_MEM_RANGE_ATTRIBUTE_LAST_PREFETCH_LOCATION",
158});
159
160singe_core::native_enum! {
161    /// Cache performance hint for the hit and miss portions of an access-policy window.
162    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
163    #[repr(u32)]
164    #[non_exhaustive]
165    pub enum AccessProperty: driver::CUaccessProperty {
166        /// Normal cache persistence.
167        Normal = CU_ACCESS_PROPERTY_NORMAL,
168        /// Streaming access is less likely to persit from cache.
169        Streaming = CU_ACCESS_PROPERTY_STREAMING,
170        /// Persisting access is more likely to persist in cache.
171        Persisting = CU_ACCESS_PROPERTY_PERSISTING,
172    }
173}
174
175impl_enum_display!(AccessProperty, {
176    Self::Normal => "CU_ACCESS_PROPERTY_NORMAL",
177    Self::Streaming => "CU_ACCESS_PROPERTY_STREAMING",
178    Self::Persisting => "CU_ACCESS_PROPERTY_PERSISTING",
179});