Class: OpenCL::Program

Inherits:
FFI::ManagedStruct
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(ptr, retain = true) ⇒ Program

Creates a new Program and retains it if specified and aplicable



1363
1364
1365
1366
1367
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1363

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/opencl_ruby_ffi/Program.rb', line 227

def method_missing(m, *a, &b)
  m_string = m.to_s
  k = nil
  begin
    k = self.create_kernel(m_string)
  rescue OpenCL::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



1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1378

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}"
  OpenCL.error_check( error )
end

Instance Method Details

#binariesObject

Returns the binaries associated to the Program for each Device. Returns an Array of tuple [ Device, String ]



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/opencl_ruby_ffi/Program.rb', line 330

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, OpenCL::Program::BINARIES, total_size, bin_array, nil)
  OpenCL.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 ]



288
289
290
291
292
293
294
295
296
297
# File 'lib/opencl_ruby_ffi/Program.rb', line 288

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, OpenCL::Program::BINARY_TYPE, ptr.size, ptr, nil)
    OpenCL.error_check(error)
    [dev, OpenCL::Program::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



360
361
362
# File 'lib/opencl_ruby_ffi/Program.rb', line 360

def build(options = { }, &block)
  OpenCL.build_program(self, options, &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)



264
265
266
267
268
269
270
271
272
273
# File 'lib/opencl_ruby_ffi/Program.rb', line 264

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, OpenCL::Program::BUILD_GLOBAL_VARIABLE_TOTAL_SIZE, ptr.size, ptr, nil)
    OpenCL.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 ]



315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/opencl_ruby_ffi/Program.rb', line 315

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, OpenCL::Program::BUILD_LOG, 0, nil, ptr1)
    OpenCL.error_check(error)
    ptr2 = FFI::MemoryPointer::new( ptr1.read_size_t )
    error = OpenCL.clGetProgramBuildInfo(self, dev, OpenCL::Program::BUILD_LOG, ptr1.read_size_t, ptr2, nil)
    OpenCL.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 ]



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/opencl_ruby_ffi/Program.rb', line 300

def build_options(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, OpenCL::Program::BUILD_OPTIONS, 0, nil, ptr1)
    OpenCL.error_check(error)
    ptr2 = FFI::MemoryPointer::new( ptr1.read_size_t )
    error = OpenCL.clGetProgramBuildInfo(self, dev, OpenCL::Program::BUILD_OPTIONS, ptr1.read_size_t, ptr2, nil)
    OpenCL.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 ]



276
277
278
279
280
281
282
283
284
285
# File 'lib/opencl_ruby_ffi/Program.rb', line 276

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, OpenCL::Program::BUILD_STATUS, ptr.size, ptr, nil)
    OpenCL.error_check(error)
    [dev, OpenCL::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



377
378
379
# File 'lib/opencl_ruby_ffi/Program.rb', line 377

def compile(options = {}, &block)
  return OpenCL.compile_program(self, options, &block)
end

#contextObject

Returns the Context the Program is associated to



382
383
384
385
386
387
# File 'lib/opencl_ruby_ffi/Program.rb', line 382

def context
  ptr = FFI::MemoryPointer::new( Context )
  error = OpenCL.clGetProgramInfo(self, OpenCL::Program::CONTEXT, Context.size, ptr, nil)
  OpenCL.error_check(error)
  return OpenCL::Context::new( ptr.read_pointer )
end

#create_kernel(name) ⇒ Object

Returns the Kernel corresponding the the specified name in the Program



412
413
414
# File 'lib/opencl_ruby_ffi/Program.rb', line 412

def create_kernel( name )
  return OpenCL.create_kernel( self, name )
end

#devicesObject

Returns the Array of Device the Program is associated with



401
402
403
404
405
406
407
408
409
# File 'lib/opencl_ruby_ffi/Program.rb', line 401

def devices
  n = self.num_devices
  ptr2 = FFI::MemoryPointer::new( Device, n )
  error = OpenCL.clGetProgramInfo(self, OpenCL::Program::DEVICES, Device.size*n, ptr2, nil)
  OpenCL.error_check(error)
  return ptr2.get_array_of_pointer(0, n).collect { |device_ptr|
    OpenCL::Device::new(device_ptr)
  }
end

#kernel_namesObject

Returns an Array of String representing the Kernel names inside the Program



249
250
251
252
253
254
255
256
257
258
# File 'lib/opencl_ruby_ffi/Program.rb', line 249

def kernel_names
  kernel_names_size = FFI::MemoryPointer::new( :size_t )
  error = OpenCL.clGetProgramInfo( self, OpenCL::Program::KERNEL_NAMES, 0, nil, kernel_names_size)
  OpenCL.error_check(error)
  k_names = FFI::MemoryPointer::new( kernel_names_size.read_size_t )
  error = OpenCL.clGetProgramInfo( self, OpenCL::Program::KERNEL_NAMES, kernel_names_size.read_size_t, k_names, nil)
  OpenCL.error_check(error)
  k_names_string = k_names.read_string
  returns k_names_string.split(";")
end

#kernelsObject

Returns an Array of Kernel corresponding to the kernels defined inside the Program



417
418
419
# File 'lib/opencl_ruby_ffi/Program.rb', line 417

def kernels
  return OpenCL.create_kernels_in_program( self )
end

#orig_method_missingObject



223
# File 'lib/opencl_ruby_ffi/Program.rb', line 223

alias_method :orig_method_missing, :method_missing

#propObject

:method: reference_count() Returns the reference counter for this Program



396
397
398
# File 'lib/opencl_ruby_ffi/Program.rb', line 396

%w( NUM_DEVICES REFERENCE_COUNT ).each { |prop|
  eval OpenCL.get_info("Program", :cl_uint, prop)
}

#to_sObject



1369
1370
1371
1372
1373
1374
1375
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1369

def to_s
  if self.respond_to?(:name) then
    return self.name
  else
    return super
  end
end