Class: SGC::Cuda::CudaFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/cuda/runtime/function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ CudaFunction

Create an instance to function name.



42
43
44
# File 'lib/cuda/runtime/function.rb', line 42

def initialize(name)
    @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'lib/cuda/runtime/function.rb', line 38

def name
  @name
end

Class Method Details

.configure(grid_dim, block_dim, shared_mem_size = 0, stream = 0) ⇒ Class

Configure the settings for the next kernel launch.

Parameters:

  • grid_dim (Dim3)

    The 3D grid dimensions x, y, z to launch.

  • block_dim (Dim3)

    The 3D block dimensions x, y, z to launch.

  • shared_mem_size (Integer) (defaults to: 0)

    Number of bytes of dynamic shared memory for each thread block.

  • stream (Integer, CudaStream) (defaults to: 0)

    The stream to launch this kernel function on. Setting stream to anything other than an instance of CudaStream will execute on the default stream 0.

Returns:

  • (Class)

    This class.



82
83
84
85
86
87
88
89
90
# File 'lib/cuda/runtime/function.rb', line 82

def self.configure(grid_dim, block_dim, shared_mem_size = 0, stream = 0)
    s = Pvt::parse_stream(stream)
    status = API::cudaConfigureCall(grid_dim.to_api, block_dim.to_api, shared_mem_size, s)
    Pvt::handle_error(status, "Failed to configure kernel function launch settings.\n" +
        "* #{grid_dim.x} x #{grid_dim.y} x #{grid_dim.z} grid\n" +
        "* #{block_dim.x} x #{block_dim.y} x #{block_dim.z} blocks\n" +
        "* shared memory size = #{shared_mem_size}")
    self
end

.load_lib(name) ⇒ Class

Load a dynamic library with name from dynamic library path.

Parameters:

  • name (String)

    The name of the dynamic library to load. For library libcudart.so, its name is cudart.

Returns:

  • (Class)

    This class.

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/cuda/runtime/function.rb', line 125

def self.load_lib(name)
    raise NotImplementedError
end

.load_lib_file(path) ⇒ Class

Load a dynamic library from the given path.

Parameters:

  • path (String)

    The path of the dynamic library to load.

Returns:

  • (Class)

    This class.



133
134
135
136
# File 'lib/cuda/runtime/function.rb', line 133

def self.load_lib_file(path)
    @@libs << DL::dlopen(path)
    self
end

.setup(*args) ⇒ Class

Set the argument list of subsequent kernel function launch.

Parameters:

  • *args (Array)

    The list of arguments to pass to the kernel.

Returns:

  • (Class)

    This class.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cuda/runtime/function.rb', line 96

def self.setup(*args)
    offset = 0
    args.each do |x|
        case x
            when Fixnum
                p = FFI::MemoryPointer.new(:int).write_int(x)
                size = 4
            when Float
                p = FFI::MemoryPointer.new(:float).write_float(x)
                size = 4
            when SGC::Memory::MemoryPointer
                p = x.ref
                size = FFI::TypeDefs[:pointer].size
            else
                raise TypeError, "Invalid type of kernel parameters #{x}."
        end
        offset = align_up(offset, size)
        status = API::cudaSetupArgument(p, size, offset)
        Pvt::handle_error(status, "Failed to setup kernel argument for #{x}.")
        offset += size
    end
    self
end

.unload_all_libsClass

Unload all the loaded dynamic libraries.

Returns:

  • (Class)

    This class.



141
142
143
144
145
146
147
# File 'lib/cuda/runtime/function.rb', line 141

def self.unload_all_libs
    @@libs.each do |h|
        h.close
    end
    @@libs = []
    self
end

Instance Method Details

#attributesCudaFunctionAttributes

Returns The attributes of this kernel function.

Returns:



48
49
50
51
52
53
# File 'lib/cuda/runtime/function.rb', line 48

def attributes
    a = CudaFunctionAttributes.new
    status = API::cudaFuncGetAttributes(a.to_ptr, @name)
    Pvt::handle_error(status, "Failed to query function attributes.")
    a
end

#cache_config=(conf) ⇒ Object

Set the preferred cache configuration to use for next launch on this kernel function.

Parameters:



58
59
60
61
# File 'lib/cuda/runtime/function.rb', line 58

def cache_config=(conf)
    status = API::cudaFuncSetCacheConfig(@name, conf)
    Pvt::handle_error(status, "Failed to set function cache config: config = #{conf}.")
end

#launchClass

Launch this kernel function with pre-configured settings.

Returns:

  • (Class)

    This class.

See Also:



68
69
70
71
72
# File 'lib/cuda/runtime/function.rb', line 68

def launch
    status = API::cudaLaunch(@name)
    Pvt::handle_error(status, "Failed to launch kernel function: name = #{@name}.")
    self
end