SwiftCU is a wrapper for CUDA runtime API's (exposed as cxxCU) with extra utilities for device management, memory ops and kernel execution, along with a robust suite of tests. Repo is tested on newest (v12.5) CUDA runtime API on both Linux and Windows.
| Operating System | Swift Version | CUDA Version | Supported |
|---|---|---|---|
| Linux | 6.0 | 12.* | ✅ |
| Windows 11 | 6.0 | 12.* | ✅ |
To include SwiftCU in your Swift project, add the following line to your Package.swift file:
.package(url:"https://github.com/machineko/SwiftCU", branch:"main")Docc generated for Swift wrapped API SwiftCU
CUDA runtime CUDART
import SwiftCU
letdeviceStatus=CUDADevice(index:0).setDevice()if deviceStatus {print("Device initialized successfully")}else{print("Failed to initialize device")}varpointer:UnsafeMutableRawPointer?letallocationStatus= pointer.cudaMemoryAllocate(1024)if allocationStatus.isSuccessful {print("Memory allocated successfully")}else{print("Memory allocation failed")}letkernel=CUDAKernel(functionPointer: pointerToCUDAKernel)letlaunchStatus= kernel.launch(arguments: kernelArgs, blockDim: blockDim, gridDim: gridDim)if launchStatus.isSuccessful {print("Kernel launched successfully")}else{print("Kernel launch failed")}For more usage examples check SwiftCUTests
import PackageDescription
import Foundation
letpackageDir=URL(fileURLWithPath: #file).deletingLastPathComponent().path
#if os(Windows)letcuPath=ProcessInfo.processInfo.environment["CUDA_HOME"]??"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.5"letcuIncludePath="-I\(cuPath)\\include"#elseif os(Linux)letcuPath=ProcessInfo.processInfo.environment["CUDA_HOME"]??"/usr/local/cuda"letcuIncludePath="-I\(cuPath)/include"#elsefatalError("OS not supported \(os)")#endifletpackage=Package(
name:"SwiftCU-example",
dependencies:[.package(url:"https://github.com/machineko/SwiftCU", branch:"main"),],
targets:[.target(
name:"cudaKernel",
publicHeadersPath:"include",
cxxSettings:[.headerSearchPath(cuIncludePath)]),.executableTarget(
name:"SwiftCU-example",
dependencies:["cudaKernel",.product(name:"SwiftCU",package:"SwiftCU"),],
swiftSettings:[.interoperabilityMode(.Cxx),.unsafeFlags(["-Xcc", cuIncludePath])]),])swift build -Xswiftc-L/usr/local/cuda/lib64 -Xswiftc-I/usr/local/cuda/includeCurrent version of SwiftCU is tested with Swift 6.0 development branch using swift-testing package and CUDA v12.5