Class CudaTypeMapper
- Namespace
- DotCompute.Backends.CUDA.Compilation
- Assembly
- DotCompute.Backends.CUDA.dll
Maps C# types to CUDA C++ types for kernel code generation.
public static class CudaTypeMapper
- Inheritance
-
CudaTypeMapper
- Inherited Members
Remarks
This mapper handles: - Primitive types (int, float, double, etc.) - Pointer types (Span<T> → T*) - Vector types (Vector2, Vector3, Vector4 → float2, float3, float4) - Custom structs (MemoryPackable message types)
Type mapping follows CUDA's type system and ensures ABI compatibility between C# and CUDA C++.
Methods
GenerateCudaStruct(Type, bool)
Generates a CUDA struct definition from a C# type.
public static string GenerateCudaStruct(Type csType, bool includeMembers = true)
Parameters
csTypeTypeThe C# type to generate a struct for.
includeMembersboolWhether to include struct members (default: true).
Returns
- string
The CUDA struct definition.
Remarks
This generates a CUDA struct that matches the memory layout of the C# type. The struct uses explicit layout attributes if present, ensuring ABI compatibility.
GetCudaParameterDeclaration(ParameterInfo)
Gets the CUDA C++ parameter declaration for a method parameter.
public static string GetCudaParameterDeclaration(ParameterInfo parameter)
Parameters
parameterParameterInfoThe parameter info.
Returns
- string
The CUDA parameter declaration (e.g., "float* data").
GetCudaParameterDeclaration(Type, string, bool)
Gets the CUDA C++ parameter declaration from type and name.
public static string GetCudaParameterDeclaration(Type parameterType, string parameterName, bool isReadOnly = false)
Parameters
parameterTypeTypeThe parameter type.
parameterNamestringThe parameter name.
isReadOnlyboolWhether the parameter is read-only.
Returns
- string
The CUDA parameter declaration (e.g., "const float* data").
GetCudaType(Type)
Gets the CUDA C++ type name for a given C# type.
public static string GetCudaType(Type csType)
Parameters
csTypeTypeThe C# type to map.
Returns
- string
The CUDA C++ type name.
Exceptions
- ArgumentNullException
Thrown when
csTypeis null.- NotSupportedException
Thrown when the type cannot be mapped to a CUDA type.
GetCudaTypeSize(string)
Gets the size in bytes of a CUDA type.
public static int GetCudaTypeSize(string cudaTypeName)
Parameters
cudaTypeNamestringThe CUDA type name (e.g., "float", "int").
Returns
- int
The size in bytes.
GetMemoryPackSerializedSize(Type)
Gets the serialized size in bytes for a MemoryPack message type.
public static int GetMemoryPackSerializedSize(Type messageType)
Parameters
messageTypeTypeThe message type.
Returns
- int
The maximum serialized size in bytes.
Remarks
MemoryPack serialization format:
- Header: 256 bytes (MessageId, MessageType, Timestamp, etc.)
- Payload: Up to 65,536 bytes (configurable max) Total: 65,792 bytes per message
GetRequiredCudaHeader(Type)
Checks if a type requires special CUDA includes.
public static string? GetRequiredCudaHeader(Type csType)
Parameters
csTypeTypeThe C# type.
Returns
- string
The required CUDA header file, or null if no special header is needed.
IsTypeSupported(Type)
Checks if a type is supported for CUDA kernel parameters.
public static bool IsTypeSupported(Type csType)
Parameters
csTypeTypeThe C# type to check.
Returns
- bool
True if the type is supported; otherwise, false.