Class: SGC::CU::CUDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/cuda/driver/device.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.can_access_peer?(dev, peer_dev = nil) ⇒ Boolean

Returns True if device dev may directly access the memory of device peer_dev.

Parameters:

  • dev (CUDevice)

    The device which is to access the memory of the device peer_dev.

  • peer_dev (CUDevice) (defaults to: nil)

    The device which its memory is to be accessed by the device dev.

Returns:

  • (Boolean)

    True if device dev may directly access the memory of device peer_dev.

Since:

  • CUDA 4.0



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cuda/driver/device.rb', line 146

def self.can_access_peer?(dev, peer_dev = nil)
    # TODO: Remove the following workaround for JRuby when the default argument bug is fixed.
    if peer_dev.nil?
        peer_dev = dev
        dev = CUContext.device
    end
    b = FFI::MemoryPointer.new(:int)
    status = API::cuDeviceCanAccessPeer(b, dev.to_api, peer_dev.to_api)
    Pvt::handle_error(status, "Failed to query can access peer.")
    b.read_int == 1 ? true : false
end

.countInteger

Returns The number of CUDA devices.

Returns:

  • (Integer)

    The number of CUDA devices.



37
38
39
40
41
42
# File 'lib/cuda/driver/device.rb', line 37

def self.count
    p = FFI::MemoryPointer.new(:int)
    status = API::cuDeviceGetCount(p)
    Pvt::handle_error(status, "Failed to get device count.")
    p.read_int
end

.get(index) ⇒ CUDevice

Returns The device corresponding to CUDA device index.

Parameters:

  • index (Integer)

    The index (0..CUDevice.count-1) of the device to get.

Returns:

  • (CUDevice)

    The device corresponding to CUDA device index.



47
48
49
50
51
52
# File 'lib/cuda/driver/device.rb', line 47

def self.get(index)
    p = FFI::MemoryPointer.new(:CUDevice)
    status = API::cuDeviceGet(p, index)
    Pvt::handle_error(status, "Failed to get device #{index}.")
    new(p)
end

.malloc(nbytes) ⇒ CUDevicePtr

Allocate nbytes of device memory from the current device.

Parameters:

  • nbytes (Integer)

    The number of bytes to allocate.

Returns:

  • (CUDevicePtr)

    A device pointer to the allocated memory.



133
134
135
136
137
138
# File 'lib/cuda/driver/device.rb', line 133

def self.malloc(nbytes)
    p = FFI::MemoryPointer.new(:CUDevicePtr)
    status = API::cuMemAlloc(p, nbytes)
    Pvt::handle_error(status, "Failed to allocate device memory: size = #{nbytes}.")
    CUDevicePtr.send(:new, p)
end

Instance Method Details

#attribute(attrib) ⇒ Integer

Returns The attribute attrib of this device.

Examples:

dev.attribute(:MAX_THREADS_PER_BLOCK)        #=> 512
dev.attribute(:MULTIPROCESSOR_COUNT)         #=> 30
dev.attribute(:MAX_SHARED_MEMORY_PER_BLOCK)  #=> 16384

Parameters:

Returns:

  • (Integer)

    The attribute attrib of this device.



83
84
85
86
87
88
# File 'lib/cuda/driver/device.rb', line 83

def attribute(attrib)
    p = FFI::MemoryPointer.new(:int)
    status = API::cuDeviceGetAttribute(p, attrib, self.to_api)
    Pvt::handle_error(status, "Failed to query device attribute #{attrib}.")
    p.read_int
end

#compute_capabilityHash{ :major, :minor }

Returns The compute capability of this device.

Examples:

For a device with compute capability 1.3:

dev.compute_capability    #=> { major: 1, minor: 3 }

Returns:

  • (Hash{ :major, :minor })

    The compute capability of this device.



68
69
70
71
72
73
# File 'lib/cuda/driver/device.rb', line 68

def compute_capability
    cap = FFI::MemoryPointer.new(:int, 2)
    status = API::cuDeviceComputeCapability(cap[0], cap[1], self.to_api)
    Pvt::handle_error(status, "Failed to query device compute capability.")
    { major: cap[0].read_int, minor: cap[1].read_int }
end

#nameString

Returns The name of this device with a maximum of 255 characters.

Returns:

  • (String)

    The name of this device with a maximum of 255 characters.



56
57
58
59
60
61
# File 'lib/cuda/driver/device.rb', line 56

def name
    s = FFI::MemoryPointer.new(:char, 256)
    status = API::cuDeviceGetName(s, 256, self.to_api)
    Pvt::handle_error(status, "Failed to get device name.")
    s.read_string
end

#propertiesHash

Returns The properties of this device in a hash with the following keys:

  • :clock_rate

  • :max_grid_size

  • :max_threads_dim

  • :max_threads_per_block

  • :mem_pitch

  • :regs_per_block

  • :shared_mem_per_block

  • :simd_width

  • :texture_align

  • :total_constant_memory.

Returns:

  • (Hash)

    The properties of this device in a hash with the following keys:

    • :clock_rate

    • :max_grid_size

    • :max_threads_dim

    • :max_threads_per_block

    • :mem_pitch

    • :regs_per_block

    • :shared_mem_per_block

    • :simd_width

    • :texture_align

    • :total_constant_memory



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cuda/driver/device.rb', line 102

def properties
    prop = API::CUDevProp.new
    status = API::cuDeviceGetProperties(prop.to_ptr, self.to_api)
    Pvt::handle_error(status, "Failed to get device properties.")
    h = {}
    h[:clock_rate] = prop[:clockRate]
    h[:max_grid_size] = prop[:maxGridSize]
    h[:max_threads_dim] = prop[:maxThreadsDim]
    h[:max_threads_per_block] = prop[:maxThreadsPerBlock]
    h[:mem_pitch] = prop[:memPitch]
    h[:regs_per_block] = prop[:regsPerBlock]
    h[:shared_mem_per_block] = prop[:sharedMemPerBlock]
    h[:simd_width] = prop[:SIMDWidth]
    h[:texture_align] = prop[:textureAlign]
    h[:total_constant_memory] = prop[:totalConstantMemory]
    h
end

#total_memInteger

Returns The total amount of device memory in bytes.

Returns:

  • (Integer)

    The total amount of device memory in bytes.



122
123
124
125
126
127
# File 'lib/cuda/driver/device.rb', line 122

def total_mem
    p = FFI::MemoryPointer.new(:size_t)
    status = API::cuDeviceTotalMem(p, self.to_api)
    Pvt::handle_error(status, "Failed to get device total amount of memory available.")
    API::read_size_t(p)
end