Home

Awesome

cuda-swift

This project provides a native Swift interface to CUDA with the following modules:

Any machine with CUDA 7.0+ and a CUDA-capable GPU is supported. Xcode Playground is supported as well. Please refer to Usage and Components.

Quick look

Value types

CUDA Driver, Runtime, cuBLAS, and NVRTC (real-time compiler) are wrapped in native Swift types. Warp provides higher level value types, DeviceArray and DeviceValue, with copy-on-write semantics.

import Warp

/// Initialize two arrays on device
var x: DeviceArray<Float> = [1.0, 2.0, 3.0, 4.0, 5.0]
let y: DeviceArray<Float> = [1.0, 2.0, 3.0, 4.0, 5.0]

/// Scalar map operations
x.incrementElements(by: 2) // x => [2.0, 3.0, 4.0, 5.0, 6.0] on device
x.multiplyElements(by: 2) // x => [2.0, 4.0, 6.0, 8.0, 10.0] on device

/// Addition
x.formElementwise(.addition, with: y) // x => [3.0, 6.0, 9.0, 12.0, 15.0] on device

/// Dot product
x • y // => 165.0

/// Sum
x.sum() // => 15

/// Absolute sum
x.sumOfAbsoluteValues() // => 15

/// Transform by 1-place math functions
x.transform(by: .sin)
x.transform(by: .tanh)
x.transform(by: .ceil)

/// Elementwise operation
x.formElementwise(.addition, with: y)
x.formElementwise(.subtraction, with: y)
x.formElementwise(.multiplication, with: y)
x.formElementwise(.division, with: y)

/// Fill with the same value
var z = y
z.fill(with: 10.0)

/// Composite assignment
x.assign(from: .subtraction, left: y, multipliedBy: 100.0, right: z)

Real-time compilation

Compile source string to PTX

import NVRTC
import CUDADriver
import Warp

let source: String =
  + "extern \"C\" __global__ void saxpy(float a, float *x, float *y, float *out, int n) {"
  + "    size_t tid = blockIdx.x * blockDim.x + threadIdx.x;"
  + "    if (tid < n) out[tid] = a * x[tid] + y[tid];"
  + "}";
let ptx = try Compiler.compile(source)

JIT-compile and load PTX using Driver API within a device context

try Device.main.withContext { context in
    let module = try Module(ptx: ptx)
    let function = module.function(named: "saxpy")!
    
    let x: DeviceArray<Float> = [1, 2, 3, 4, 5, 6, 7, 8]
    let y: DeviceArray<Float> = [2, 3, 4, 5, 6, 7, 8, 9]
    var result = DeviceArray<Float>(capacity: 8)

    try function<<<(1, 8)>>>[.float(1.0), .constPointer(to: x), .constPointer(to: y), .pointer(to: &result), .int(8)]
    /// result => [3, 5, 7, 9, 11, 13, 15, 17] on device
}

Package Information

Add a dependency:

.Package(url: "https://github.com/rxwei/cuda-swift", majorVersion: 1)

You may use the Makefile in this repository for you own project. No extra path configuration is needed.

Otherwise, specify the path to your CUDA headers and library at swift build.

macOS

swift build -Xcc -I/usr/local/cuda/include -Xlinker -L/usr/local/cuda/lib

Linux

swift build -Xcc -I/usr/local/cuda/include -Xlinker -L/usr/local/cuda/lib64

Components

Core

Optional

Dependencies

License

MIT License

CUDA is a registered trademark of NVIDIA Corporation.