Skip to main content

singe_cuda/types/
function.rs

1use num_enum::{IntoPrimitive, TryFromPrimitive};
2use singe_core::impl_enum_display;
3use singe_cuda_sys::driver;
4
5singe_core::native_enum! {
6    /// Attribute of a compiled kernel function, queried or set per function.
7    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
8    #[repr(u32)]
9    #[non_exhaustive]
10    pub enum FunctionAttribute: driver::CUfunction_attribute {
11        /// The shared memory mode of a function, as a [`SharedMemoryMode`].
12        SharedMemoryMode = CU_FUNC_ATTRIBUTE_SHARED_MEMORY_MODE,
13        /// The maximum number of threads per block, beyond which a launch of the function would fail.
14        ///
15        /// This number depends on both the function and the device on which the function is currently loaded.
16        MaxThreadsPerBlock = CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK,
17        /// The size in bytes of statically-allocated shared memory required by this function.
18        ///
19        /// This does not include dynamically-allocated shared memory requested by the user at runtime.
20        SharedSizeBytes = CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES,
21        /// The size in bytes of user-allocated constant memory required by this function.
22        ConstSizeBytes = CU_FUNC_ATTRIBUTE_CONST_SIZE_BYTES,
23        /// The size in bytes of local memory used by each thread of this function.
24        LocalSizeBytes = CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES,
25        /// The number of registers used by each thread of this function.
26        NumRegs = CU_FUNC_ATTRIBUTE_NUM_REGS,
27        /// The PTX virtual architecture version for which the function was compiled.
28        ///
29        /// This value is the major PTX version times 10 plus the minor PTX version, so a PTX version 1.3 function returns 13.
30        /// Cubins compiled before CUDA 3.0 may report the undefined value 0.
31        PtxVersion = CU_FUNC_ATTRIBUTE_PTX_VERSION,
32        /// The binary architecture version for which the function was compiled.
33        ///
34        /// This value is the major binary version times 10 plus the minor binary version, so a binary version 1.3 function returns 13.
35        /// Legacy cubins without a properly encoded binary architecture version report 10.
36        BinaryVersion = CU_FUNC_ATTRIBUTE_BINARY_VERSION,
37        /// Whether the function was compiled with the `-Xptxas --dlcm=ca` option.
38        CacheModeCa = CU_FUNC_ATTRIBUTE_CACHE_MODE_CA,
39        /// The maximum size in bytes of dynamically-allocated shared memory that can be used by this function.
40        ///
41        /// If the user-specified dynamic shared memory size is larger than this value, the launch will fail.
42        ///
43        /// The default is [`DeviceAttribute::MaxSharedMemoryPerBlock`](crate::device::DeviceAttribute::MaxSharedMemoryPerBlock) minus [`Self::SharedSizeBytes`], or 0 when the static size exceeds that maximum.
44        /// It can be raised to [`DeviceAttribute::MaxSharedMemoryPerBlockOptin`](crate::device::DeviceAttribute::MaxSharedMemoryPerBlockOptin) minus [`Self::SharedSizeBytes`].
45        ///
46        /// This attribute is ignored if the function or launch sets a shared memory mode, such as [`Self::SharedMemoryMode`].
47        ///
48        /// This attribute cannot be used to access oversized shared memory, which only a shared memory mode enables.
49        MaxDynamicSharedSizeBytes = CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
50        /// On devices where the L1 cache and shared memory use the same hardware resources, this sets the shared memory carveout preference, in percent of the total shared memory.
51        ///
52        /// The total is [`DeviceAttribute::MaxSharedMemoryPerMultiprocessor`](crate::device::DeviceAttribute::MaxSharedMemoryPerMultiprocessor).
53        /// This is only a hint, and the driver can choose a different ratio if required to execute the function.
54        PreferredSharedMemoryCarveout = CU_FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT,
55        /// If this attribute is set, the kernel must launch with a valid cluster size specified.
56        ClusterSizeMustBeSet = CU_FUNC_ATTRIBUTE_CLUSTER_SIZE_MUST_BE_SET,
57        /// The required cluster width in blocks.
58        ///
59        /// The values must either all be 0 or all be positive.
60        /// The validity of the cluster dimensions is otherwise checked at launch time.
61        ///
62        /// If the value is set during compile time, it cannot be set at runtime.
63        /// Setting it at runtime returns [`Status::NotPermitted`](crate::error::Status::NotPermitted).
64        RequiredClusterWidth = CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_WIDTH,
65        /// The required cluster height in blocks.
66        ///
67        /// The values must either all be 0 or all be positive.
68        /// The validity of the cluster dimensions is otherwise checked at launch time.
69        ///
70        /// If the value is set during compile time, it cannot be set at runtime.
71        /// Setting it at runtime returns [`Status::NotPermitted`](crate::error::Status::NotPermitted).
72        RequiredClusterHeight = CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_HEIGHT,
73        /// The required cluster depth in blocks.
74        ///
75        /// The values must either all be 0 or all be positive.
76        /// The validity of the cluster dimensions is otherwise checked at launch time.
77        ///
78        /// If the value is set during compile time, it cannot be set at runtime.
79        /// Setting it at runtime returns [`Status::NotPermitted`](crate::error::Status::NotPermitted).
80        RequiredClusterDepth = CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_DEPTH,
81        /// Whether the function can be launched with non-portable cluster size.
82        ///
83        /// 1 is allowed, 0 is disallowed.
84        /// A non-portable cluster size may only function on the specific SKUs the program is tested on.
85        /// The launch might fail if the program is run on a different hardware platform.
86        ///
87        /// An occupancy query for the maximum active clusters checks whether a size can launch on the current device.
88        ///
89        /// A portable cluster size is guaranteed to be functional on all compute capabilities higher than the target compute capability.
90        /// The portable cluster size for `sm_90` is 8 blocks per cluster.
91        /// This value may increase for future compute capabilities.
92        ///
93        /// The specific hardware unit may support higher cluster sizes that are not guaranteed to be portable.
94        NonPortableClusterSizeAllowed = CU_FUNC_ATTRIBUTE_NON_PORTABLE_CLUSTER_SIZE_ALLOWED,
95        /// The block scheduling policy of a function, as a [`ClusterSchedulingPolicy`].
96        ClusterSchedulingPolicyPreference = CU_FUNC_ATTRIBUTE_CLUSTER_SCHEDULING_POLICY_PREFERENCE,
97    }
98}
99
100impl_enum_display!(FunctionAttribute, {
101    Self::SharedMemoryMode => "CU_FUNC_ATTRIBUTE_SHARED_MEMORY_MODE",
102    Self::MaxThreadsPerBlock => "CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK",
103    Self::SharedSizeBytes => "CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES",
104    Self::ConstSizeBytes => "CU_FUNC_ATTRIBUTE_CONST_SIZE_BYTES",
105    Self::LocalSizeBytes => "CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES",
106    Self::NumRegs => "CU_FUNC_ATTRIBUTE_NUM_REGS",
107    Self::PtxVersion => "CU_FUNC_ATTRIBUTE_PTX_VERSION",
108    Self::BinaryVersion => "CU_FUNC_ATTRIBUTE_BINARY_VERSION",
109    Self::CacheModeCa => "CU_FUNC_ATTRIBUTE_CACHE_MODE_CA",
110    Self::MaxDynamicSharedSizeBytes => "CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES",
111    Self::PreferredSharedMemoryCarveout => "CU_FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT",
112    Self::ClusterSizeMustBeSet => "CU_FUNC_ATTRIBUTE_CLUSTER_SIZE_MUST_BE_SET",
113    Self::RequiredClusterWidth => "CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_WIDTH",
114    Self::RequiredClusterHeight => "CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_HEIGHT",
115    Self::RequiredClusterDepth => "CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_DEPTH",
116    Self::NonPortableClusterSizeAllowed => "CU_FUNC_ATTRIBUTE_NON_PORTABLE_CLUSTER_SIZE_ALLOWED",
117    Self::ClusterSchedulingPolicyPreference => "CU_FUNC_ATTRIBUTE_CLUSTER_SCHEDULING_POLICY_PREFERENCE",
118});
119
120singe_core::native_enum! {
121    /// Function cache configurations
122    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
123    #[repr(u32)]
124    #[non_exhaustive]
125    pub enum FunctionCache: driver::CUfunc_cache {
126        /// no preference for shared memory or L1 (default).
127        PreferNone = CU_FUNC_CACHE_PREFER_NONE,
128        /// prefer larger shared memory and smaller L1 cache.
129        PreferShared = CU_FUNC_CACHE_PREFER_SHARED,
130        /// prefer larger L1 cache and smaller shared memory.
131        PreferL1 = CU_FUNC_CACHE_PREFER_L1,
132        /// prefer equal sized L1 cache and shared memory.
133        PreferEqual = CU_FUNC_CACHE_PREFER_EQUAL,
134    }
135}
136
137impl_enum_display!(FunctionCache, {
138    Self::PreferNone => "CU_FUNC_CACHE_PREFER_NONE",
139    Self::PreferShared => "CU_FUNC_CACHE_PREFER_SHARED",
140    Self::PreferL1 => "CU_FUNC_CACHE_PREFER_L1",
141    Self::PreferEqual => "CU_FUNC_CACHE_PREFER_EQUAL",
142});
143
144singe_core::native_enum! {
145    /// Deprecated:
146    ///
147    /// Shared memory configurations
148    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
149    #[repr(u32)]
150    #[deprecated]
151    #[non_exhaustive]
152    pub enum SharedMemoryConfig: driver::CUsharedconfig {
153        /// set default shared memory bank size.
154        DefaultBankSize = CU_SHARED_MEM_CONFIG_DEFAULT_BANK_SIZE,
155        /// set shared memory bank width to four bytes.
156        FourByteBankSize = CU_SHARED_MEM_CONFIG_FOUR_BYTE_BANK_SIZE,
157        /// set shared memory bank width to eight bytes.
158        EightByteBankSize = CU_SHARED_MEM_CONFIG_EIGHT_BYTE_BANK_SIZE,
159    }
160}
161
162impl_enum_display!(SharedMemoryConfig, {
163    Self::DefaultBankSize => "CU_SHARED_MEM_CONFIG_DEFAULT_BANK_SIZE",
164    Self::FourByteBankSize => "CU_SHARED_MEM_CONFIG_FOUR_BYTE_BANK_SIZE",
165    Self::EightByteBankSize => "CU_SHARED_MEM_CONFIG_EIGHT_BYTE_BANK_SIZE",
166});
167
168singe_core::native_enum! {
169    /// Shared memory carveout configurations for a function's [`FunctionAttribute::PreferredSharedMemoryCarveout`] attribute.
170    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
171    #[repr(i32)]
172    #[non_exhaustive]
173    pub enum SharedMemoryCarveout: driver::CUshared_carveout {
174        /// No preference for shared memory or L1 (default).
175        Default = CU_SHAREDMEM_CARVEOUT_DEFAULT,
176        /// Prefer maximum available shared memory, minimum L1 cache.
177        MaxShared = CU_SHAREDMEM_CARVEOUT_MAX_SHARED,
178        /// Prefer maximum available L1 cache, minimum shared memory.
179        MaxL1 = CU_SHAREDMEM_CARVEOUT_MAX_L1,
180    }
181}
182
183impl_enum_display!(SharedMemoryCarveout, {
184    Self::Default => "CU_SHAREDMEM_CARVEOUT_DEFAULT",
185    Self::MaxShared => "CU_SHAREDMEM_CARVEOUT_MAX_SHARED",
186    Self::MaxL1 => "CU_SHAREDMEM_CARVEOUT_MAX_L1",
187});
188
189singe_core::native_enum! {
190    /// Shared memory configuration for a kernel.
191    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
192    #[repr(u32)]
193    #[non_exhaustive]
194    pub enum SharedMemoryMode: driver::CUsharedMemoryMode_enum {
195        /// Uses the function's [`FunctionAttribute::MaxDynamicSharedSizeBytes`] for shared memory on launch.
196        Default = CU_SHARED_MEMORY_MODE_DEFAULT,
197        /// Requires the requested dynamic shared memory size to be portable, within [`DeviceAttribute::MaxSharedMemoryPerBlock`](crate::device::DeviceAttribute::MaxSharedMemoryPerBlock).
198        RequirePortable = CU_SHARED_MEMORY_MODE_REQUIRE_PORTABLE,
199        /// Allows a non-portable dynamic shared memory size, within [`DeviceAttribute::MaxSharedMemoryPerBlockOptin`](crate::device::DeviceAttribute::MaxSharedMemoryPerBlockOptin).
200        AllowNonPortable = CU_SHARED_MEMORY_MODE_ALLOW_NON_PORTABLE,
201        /// Specifies that oversized shared memory configurations may be used (with the limitation of only 8kB L1 cache).
202        AllowOversizedSharedMemory = CU_SHARED_MEMORY_MODE_ALLOW_OVERSIZED_SHARED_MEMORY,
203        /// Specifies that oversized shared memory configurations may be used (with the limitation of only 8kB L1 cache), and prefer an oversized shared memory configuration.
204        PreferOversizedSharedMemory = CU_SHARED_MEMORY_MODE_PREFER_OVERSIZED_SHARED_MEMORY,
205    }
206}
207
208singe_core::native_enum! {
209    /// Cluster scheduling policy.
210    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
211    #[repr(u32)]
212    #[non_exhaustive]
213    pub enum ClusterSchedulingPolicy: driver::CUclusterSchedulingPolicy_enum {
214        /// the default policy.
215        Default = CU_CLUSTER_SCHEDULING_POLICY_DEFAULT,
216        /// spread the blocks within a cluster to the SMs.
217        Spread = CU_CLUSTER_SCHEDULING_POLICY_SPREAD,
218        /// allow the hardware to load-balance the blocks in a cluster to the SMs.
219        LoadBalancing = CU_CLUSTER_SCHEDULING_POLICY_LOAD_BALANCING,
220        /// Spread the blocks within a cluster to the SMs, and prefer placement that prioritizes Distributed Shared Memory locality for Rubin-class GPUs.
221        ///
222        /// This is a preference only.
223        /// The driver may ignore this request when the launch is not eligible, when the device does not support the requested placement behavior, or when other launch constraints take precedence.
224        ///
225        /// This policy is intended for clustered kernels whose performance depends on communication through Distributed Shared Memory among blocks in the same cluster.
226        RubinDsmemLocality = CU_CLUSTER_SCHEDULING_POLICY_RUBIN_DSMEM_LOCALITY,
227    }
228}
229
230impl_enum_display!(SharedMemoryMode, {
231    SharedMemoryMode::Default => "CU_SHARED_MEMORY_MODE_DEFAULT",
232    SharedMemoryMode::RequirePortable => "CU_SHARED_MEMORY_MODE_REQUIRE_PORTABLE",
233    SharedMemoryMode::AllowNonPortable => "CU_SHARED_MEMORY_MODE_ALLOW_NON_PORTABLE",
234    SharedMemoryMode::AllowOversizedSharedMemory => "CU_SHARED_MEMORY_MODE_ALLOW_OVERSIZED_SHARED_MEMORY",
235    SharedMemoryMode::PreferOversizedSharedMemory => "CU_SHARED_MEMORY_MODE_PREFER_OVERSIZED_SHARED_MEMORY",
236});
237
238impl_enum_display!(ClusterSchedulingPolicy, {
239    ClusterSchedulingPolicy::Default => "CU_CLUSTER_SCHEDULING_POLICY_DEFAULT",
240    ClusterSchedulingPolicy::Spread => "CU_CLUSTER_SCHEDULING_POLICY_SPREAD",
241    ClusterSchedulingPolicy::LoadBalancing => "CU_CLUSTER_SCHEDULING_POLICY_LOAD_BALANCING",
242    ClusterSchedulingPolicy::RubinDsmemLocality => "CU_CLUSTER_SCHEDULING_POLICY_RUBIN_DSMEM_LOCALITY",
243});