Class: OpenCL::Program
- Inherits:
-
FFI::ManagedStruct
- Object
- FFI::ManagedStruct
- OpenCL::Program
- Defined in:
- lib/opencl_ruby_ffi/Program.rb,
lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb,
lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb
Overview
Maps the cl_program object of OpenCL
Defined Under Namespace
Classes: BinaryType
Constant Summary collapse
- REFERENCE_COUNT =
:stopdoc:
0x1160- CONTEXT =
0x1161- NUM_DEVICES =
0x1162- DEVICES =
0x1163- SOURCE =
0x1164- BINARY_SIZES =
0x1165- BINARIES =
0x1166- NUM_KERNELS =
0x1167- KERNEL_NAMES =
0x1168- BUILD_STATUS =
0x1181- BUILD_OPTIONS =
0x1182- BUILD_LOG =
0x1183- BINARY_TYPE =
0x1184- BUILD_GLOBAL_VARIABLE_TOTAL_SIZE =
0x1185- BINARY_TYPE_NONE =
0x0- BINARY_TYPE_COMPILED_OBJECT =
0x1- BINARY_TYPE_LIBRARY =
0x2- BINARY_TYPE_EXECUTABLE =
0x4- BINARY_TYPE_INTERMEDIATE =
0x40E1
Class Method Summary collapse
-
.release(ptr) ⇒ Object
method called at Program deletion, releases the object if aplicable.
Instance Method Summary collapse
-
#binaries ⇒ Object
Returns the binaries associated to the Program for each Device.
-
#binary_type(devs = nil) ⇒ Object
Returns the BinaryType for each Device associated to the Program or the Device(s) specified.
-
#build(options = { }, &block) ⇒ Object
Builds (compile and link) the Program created from sources or binary.
-
#build_global_variable_total_size(devs = nil) ⇒ Object
Returns the total amount in byte used by the Program variables in the global address space for the Device(s) specified.
-
#build_log(devs = nil) ⇒ Object
Returns the build log for each Device associated to the Program or the Device(s) specified.
-
#build_options(devs = nil) ⇒ Object
Returns the build options for each Device associated to the Program or the Device(s) specified.
-
#build_status(devs = nil) ⇒ Object
Returns the BuildStatus of the Program for each device associated to the Program or the Device(s) specified.
-
#compile(options = {}, &block) ⇒ Object
Compiles the Program’ sources.
-
#context ⇒ Object
Returns the Context the Program is associated to.
-
#create_kernel(name) ⇒ Object
Returns the Kernel corresponding the the specified name in the Program.
-
#devices ⇒ Object
Returns the Array of Device the Program is associated with.
-
#initialize(ptr, retain = true) ⇒ Program
constructor
Creates a new Program and retains it if specified and aplicable.
-
#kernel_names ⇒ Object
Returns an Array of String representing the Kernel names inside the Program.
-
#kernels ⇒ Object
Returns an Array of Kernel corresponding to the kernels defined inside the Program.
-
#method_missing(m, *a, &b) ⇒ Object
Intercepts a call to a missing method and tries to see if it is defined as a Kernel inside the Program.
- #orig_method_missing ⇒ Object
-
#prop ⇒ Object
:method: reference_count() Returns the reference counter for this Program.
-
#to_s ⇒ Object
:startdoc:.
Constructor Details
#initialize(ptr, retain = true) ⇒ Program
Creates a new Program and retains it if specified and aplicable
3034 3035 3036 3037 3038 |
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 3034 def initialize(ptr, retain = true) super(ptr) OpenCL.clRetainProgram(ptr) if retain #STDERR.puts "Allocating Program: #{ptr}" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a, &b) ⇒ Object
Intercepts a call to a missing method and tries to see if it is defined as a Kernel inside the Program. It then calls the Kernel enqueue_with_args method. Thanks pyopencl (Andreas Klöeckner) for the idea
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 200 def method_missing(m, *a, &b) m_string = m.to_s k = nil begin k = self.create_kernel(m_string) rescue Error k = nil end if k then k.enqueue_with_args(*a, &b) else orig_method_missing(m, *a, &b) end end |
Class Method Details
.release(ptr) ⇒ Object
method called at Program deletion, releases the object if aplicable
3041 3042 3043 3044 3045 3046 3047 3048 3049 |
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 3041 def self.release(ptr) #STDERR.puts "Releasing Program: #{ptr}" #ref_count = FFI::MemoryPointer::new( :cl_uint ) #OpenCL.clGetProgramInfo(ptr, OpenCL::Program::REFERENCE_COUNT, ref_count.size, ref_count, nil) #STDERR.puts "reference counter: #{ref_count.read_cl_uint}" error = OpenCL.clReleaseProgram(ptr) #STDERR.puts "Object released! #{error}" error_check( error ) end |
Instance Method Details
#binaries ⇒ Object
Returns the binaries associated to the Program for each Device. Returns an Array of tuple [ Device, String ]
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 303 def binaries sizes = self.binary_sizes bin_array = FFI::MemoryPointer::new( :pointer, sizes.length ) sizes.length total_size = 0 sizes.each_with_index { |s, i| total_size += s bin_array[i].write_pointer(FFI::MemoryPointer::new(s)) } error = OpenCL.clGetProgramInfo(self, BINARIES, total_size, bin_array, nil) error_check(error) bins = [] devs = self.devices sizes.each_with_index { |s, i| bins.push [devs, bin_array[i].read_pointer.read_bytes(s)] } return bins end |
#binary_type(devs = nil) ⇒ Object
Returns the BinaryType for each Device associated to the Program or the Device(s) specified. Returns an Array of tuple [ Device, BinaryType ]
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 261 def binary_type(devs = nil) devs = self.devices if not devs devs = [devs].flatten ptr = FFI::MemoryPointer::new( :cl_program_binary_type ) return devs.collect { |dev| error = OpenCL.clGetProgramBuildInfo(self, dev, BINARY_TYPE, ptr.size, ptr, nil) error_check(error) [dev, BinaryType::new(ptr.read_cl_program_binary_type)] } end |
#build(options = { }, &block) ⇒ Object
Builds (compile and link) the Program created from sources or binary
Attributes
-
options- a hash containing named options -
block- if provided, a callback invoked when the Program is built. Signature of the callback is { |Program, FFI::Pointer to user_data| … }
Options
-
:device_list- an Array of Device to build the program for -
:user_data- a Pointer (or convertible to Pointer using to_ptr) to the memory area to pass to the callback -
:options- a String containing the options to use for the build
333 334 335 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 333 def build( = { }, &block) OpenCL.build_program(self, , &block) end |
#build_global_variable_total_size(devs = nil) ⇒ Object
Returns the total amount in byte used by the Program variables in the global address space for the Device(s) specified. Returns an Array of tuple [ Device, size ] (2.0 only)
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 237 def build_global_variable_total_size(devs = nil) devs = self.devices if not devs devs = [devs].flatten ptr = FFI::MemoryPointer::new( :size_t ) return devs.collect { |dev| error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_GLOBAL_VARIABLE_TOTAL_SIZE, ptr.size, ptr, nil) error_check(error) [dev, ptr.read_size_t] } end |
#build_log(devs = nil) ⇒ Object
Returns the build log for each Device associated to the Program or the Device(s) specified. Returns an Array of tuple [ Device, String ]
288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 288 def build_log(devs = nil) devs = self.devices if not devs devs = [devs].flatten return devs.collect { |dev| ptr1 = FFI::MemoryPointer::new( :size_t, 1) error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_LOG, 0, nil, ptr1) error_check(error) ptr2 = FFI::MemoryPointer::new( ptr1.read_size_t ) error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_LOG, ptr1.read_size_t, ptr2, nil) error_check(error) [dev, ptr2.read_string] } end |
#build_options(devs = nil) ⇒ Object
Returns the build options for each Device associated to the Program or the Device(s) specified. Returns an Array of tuple [ Device, String ]
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 273 def (devs = nil) devs = self.devices if not devs devs = [devs].flatten return devs.collect { |dev| ptr1 = FFI::MemoryPointer::new( :size_t, 1) error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_OPTIONS, 0, nil, ptr1) error_check(error) ptr2 = FFI::MemoryPointer::new( ptr1.read_size_t ) error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_OPTIONS, ptr1.read_size_t, ptr2, nil) error_check(error) [dev, ptr2.read_string] } end |
#build_status(devs = nil) ⇒ Object
Returns the BuildStatus of the Program for each device associated to the Program or the Device(s) specified. Returns an Array of tuple [ Device, BuildStatus ]
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 249 def build_status(devs = nil) devs = self.devices if not devs devs = [devs].flatten ptr = FFI::MemoryPointer::new( :cl_build_status ) return devs.collect { |dev| error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_STATUS, ptr.size, ptr, nil) error_check(error) [dev, BuildStatus::new(ptr.read_cl_build_status)] } end |
#compile(options = {}, &block) ⇒ Object
Compiles the Program’ sources
Attributes
-
options- a Hash containing named options -
block- if provided, a callback invoked when the Program is compiled. Signature of the callback is { |Program, FFI::Pointer to user_data| … }
Options
-
:device_list- an Array of Device to build the program for -
:user_data- a Pointer (or convertible to Pointer using to_ptr) to the memory area to pass to the callback -
:options- a String containing the options to use for the compilation -
:input_headers- a Hash containing pairs of : String: header_include_name => Program: header
350 351 352 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 350 def compile( = {}, &block) return OpenCL.compile_program(self, , &block) end |
#context ⇒ Object
Returns the Context the Program is associated to
355 356 357 358 359 360 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 355 def context ptr = FFI::MemoryPointer::new( Context ) error = OpenCL.clGetProgramInfo(self, CONTEXT, Context.size, ptr, nil) error_check(error) return Context::new( ptr.read_pointer ) end |
#create_kernel(name) ⇒ Object
Returns the Kernel corresponding the the specified name in the Program
385 386 387 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 385 def create_kernel( name ) return OpenCL.create_kernel( self, name ) end |
#devices ⇒ Object
Returns the Array of Device the Program is associated with
374 375 376 377 378 379 380 381 382 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 374 def devices n = self.num_devices ptr2 = FFI::MemoryPointer::new( Device, n ) error = OpenCL.clGetProgramInfo(self, DEVICES, Device.size*n, ptr2, nil) error_check(error) return ptr2.get_array_of_pointer(0, n).collect { |device_ptr| Device::new(device_ptr) } end |
#kernel_names ⇒ Object
Returns an Array of String representing the Kernel names inside the Program
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 222 def kernel_names kernel_names_size = FFI::MemoryPointer::new( :size_t ) error = OpenCL.clGetProgramInfo( self, KERNEL_NAMES, 0, nil, kernel_names_size) error_check(error) k_names = FFI::MemoryPointer::new( kernel_names_size.read_size_t ) error = OpenCL.clGetProgramInfo( self, KERNEL_NAMES, kernel_names_size.read_size_t, k_names, nil) error_check(error) k_names_string = k_names.read_string returns k_names_string.split(";") end |
#kernels ⇒ Object
Returns an Array of Kernel corresponding to the kernels defined inside the Program
390 391 392 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 390 def kernels return OpenCL.create_kernels_in_program( self ) end |
#orig_method_missing ⇒ Object
196 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 196 alias_method :orig_method_missing, :method_missing |
#prop ⇒ Object
:method: reference_count() Returns the reference counter for this Program
369 370 371 |
# File 'lib/opencl_ruby_ffi/Program.rb', line 369 %w( NUM_DEVICES REFERENCE_COUNT ).each { |prop| eval get_info("Program", :cl_uint, prop) } |
#to_s ⇒ Object
:startdoc:
3052 3053 3054 3055 3056 3057 3058 |
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 3052 def to_s if self.respond_to?(:name) then return self.name else return super end end |