<commit_before>//
//  Device.swift
//  CUDA
//
//  Created by <NAME> on 10/19/16.
//
//

import CCUDARuntime
@_exported import struct CUDADriver.ComputeCapability

public struct Device : Equatable {

    public typealias Properties = cudaDeviceProp

    public let index: Int

    public static var count: Int {
        var deviceCount: Int32 = 0
        !!cudaGetDeviceCount(&deviceCount)
        return Int(deviceCount)
    }

    public static var all: [Device] {
        return (0..<count).map { Device(assumingIndex: Int32($0)) }
    }

    public static var current: Device {
        get {
            var index: Int32 = 0
            !!cudaGetDevice(&index)
            return Device(assumingIndex: index)
        }
        set {
            !!cudaSetDevice(Int32(newValue.index))
        }
    }

    /// Unsafely assume the index handle to be the device index
    internal init(assumingIndex index: Int32) {
        self.index = Int(index)
    }

    public init?(atIndex index: Int) {
        guard index >= 0 && index < Device.count else {
            return nil
        }
        self.index = index
    }

    public init?(withProperties properties: Properties) {
        var index: Int32 = 0
        var properties = properties
        do {
            try ensureSuccess(cudaChooseDevice(&index, &properties))
            self.index = Int(index)
        } catch {
            return nil
        }
    }

    public var properties: Properties {
        var prop = Properties()
        !!cudaGetDeviceProperties(&prop, Int32(index))
        return prop
    }

    public var computeCapability: ComputeCapability {
        var major: Int32 = 0
        !!cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, Int32(index))
        var minor: Int32 = 0
        !!cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, Int32(index))
        return ComputeCapability(major: Int(major), minor: Int(minor))
    }

    public static func ==(lhs: Device, rhs: Device) -> Bool {
        return lhs.index == rhs.index
    }
    
}
<commit_msg>Implement 2-way conversion initializers between driver device and runtime device<commit_after>//
//  Device.swift
//  CUDA
//
//  Created by <NAME> on 10/19/16.
//
//

import CCUDARuntime
import struct CUDADriver.Device
@_exported import struct CUDADriver.ComputeCapability

public struct Device : Equatable {

    public typealias Properties = cudaDeviceProp

    public let index: Int

    public static var count: Int {
        var deviceCount: Int32 = 0
        !!cudaGetDeviceCount(&deviceCount)
        return Int(deviceCount)
    }

    public static var all: [Device] {
        return (0..<count).map { Device(assumingIndex: Int32($0)) }
    }

    public static var current: Device {
        get {
            var index: Int32 = 0
            !!cudaGetDevice(&index)
            return Device(assumingIndex: index)
        }
        set {
            !!cudaSetDevice(Int32(newValue.index))
        }
    }

    /// Unsafely assume the index handle to be the device index
    internal init(assumingIndex index: Int32) {
        self.index = Int(index)
    }

    public init?(atIndex index: Int) {
        guard index >= 0 && index < Device.count else {
            return nil
        }
        self.index = index
    }

    public init?(withProperties properties: Properties) {
        var index: Int32 = 0
        var properties = properties
        do {
            try ensureSuccess(cudaChooseDevice(&index, &properties))
            self.index = Int(index)
        } catch {
            return nil
        }
    }

    public var properties: Properties {
        var prop = Properties()
        !!cudaGetDeviceProperties(&prop, Int32(index))
        return prop
    }

    public var computeCapability: ComputeCapability {
        var major: Int32 = 0
        !!cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, Int32(index))
        var minor: Int32 = 0
        !!cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, Int32(index))
        return ComputeCapability(major: Int(major), minor: Int(minor))
    }

    public static func ==(lhs: Device, rhs: Device) -> Bool {
        return lhs.index == rhs.index
    }

    public init(_ driverDevice: CUDADriver.Device) {
        self.init(assumingIndex: Int32(driverDevice.index))
    }

    /// Convert to driver device
    public var driverDevice: CUDADriver.Device {
        return CUDADriver.Device(atIndex: index)!
    }
    
}

/// Conversion from runtime device
public extension CUDADriver.Device {
    public init(_ device: CUDARuntime.Device) {
        self = device.driverDevice
    }

    public var runtimeDevice: CUDARuntime.Device {
        return CUDARuntime.Device(self)
    }
}

{'fraction_non_alphanumeric': 0.09427609427609428, 'fraction_numerical': 0.015782828282828284, 'mean_word_length': 2.246584699453552, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2113709', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1332, 'n_words': 364}