Skip to content

Repository files navigation

SwiftCU

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 SystemSwift VersionCUDA VersionSupported
Linux6.012.*
Windows 116.012.*

Installation

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")

Documentation

Docc generated for Swift wrapped API SwiftCU

CUDA runtime CUDART

Examples

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

Simple Package overview

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])]),])

Example build without package/env flags

swift build -Xswiftc-L/usr/local/cuda/lib64 -Xswiftc-I/usr/local/cuda/include

Testing

Current version of SwiftCU is tested with Swift 6.0 development branch using swift-testing package and CUDA v12.5

About

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.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages