Class: OpenCL::Program

Inherits:
ExtendedStruct show all
Includes:
OpenCL12, OpenCL20, OpenCL21, OpenCL22
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,
lib/opencl_ruby_ffi/khr/spir.rb,
lib/opencl_ruby_ffi/intel/unofficial.rb

Overview

Maps the cl_program object of OpenCL

Defined Under Namespace

Modules: OpenCL12, OpenCL20, OpenCL21, OpenCL22 Classes: BinaryType

Instance Method Summary collapse

Methods included from OpenCL22

#scope_global_ctors_present, #scope_global_dtors_present, #set_release_callback, #set_specialization_constant

Methods included from OpenCL21

#il

Methods included from OpenCL20

#build_global_variable_total_size

Methods included from OpenCL12

#binary_type, #compile, #num_kernels

Methods inherited from ExtendedStruct

register_extension

Constructor Details

#initialize(ptr, retain = true) ⇒ Program

Creates a new Program and retains it if specified and aplicable



1391
1392
1393
1394
1395
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1391

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/opencl_ruby_ffi/Program.rb', line 294

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

Instance Method Details

#binariesObject

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/opencl_ruby_ffi/Program.rb', line 344

def binaries
  sizes = self.binary_sizes
  bin_array = MemoryPointer::new( :pointer, sizes.length )
  total_size = 0
  pointers = []
  sizes.each_with_index { |s, i|
    total_size += s
    pointers[i] = MemoryPointer::new(s)
    bin_array[i].write_pointer(pointers[i])
  }
  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[i], pointers[i].read_bytes(s)]
  }
  return bins
end

#binary_sizesObject

Returns the OpenCL::Program::binary_sizes info

Returns:

  • an Array of size_t



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

get_info_array("Program", :size_t, "binary_sizes")

#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, 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



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

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



392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/opencl_ruby_ffi/Program.rb', line 392

def build_log(devs = nil)
  devs = self.devices if not devs
  devs = [devs].flatten
  return devs.collect { |dev|
    ptr1 = MemoryPointer::new( :size_t, 1)
    error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_LOG, 0, nil, ptr1)
    error_check(error)
    ptr2 = 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 ]



377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/opencl_ruby_ffi/Program.rb', line 377

def build_options(devs = nil)
  devs = self.devices if not devs
  devs = [devs].flatten
  return devs.collect { |dev|
    ptr1 = MemoryPointer::new( :size_t, 1)
    error = OpenCL.clGetProgramBuildInfo(self, dev, BUILD_OPTIONS, 0, nil, ptr1)
    error_check(error)
    ptr2 = 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 ]



365
366
367
368
369
370
371
372
373
374
# File 'lib/opencl_ruby_ffi/Program.rb', line 365

def build_status(devs = nil)
  devs = self.devices if not devs
  devs = [devs].flatten
  ptr = 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

#contextObject

Returns the Context the Program is associated to



315
316
317
318
319
320
321
322
# File 'lib/opencl_ruby_ffi/Program.rb', line 315

def context
  @_context ||= begin
    ptr = MemoryPointer::new( Context )
    error = OpenCL.clGetProgramInfo(self, CONTEXT, Context.size, ptr, nil)
    error_check(error)
    Context::new( ptr.read_pointer )
  end
end

#create_kernel(name) ⇒ Object

Returns the Kernel corresponding the the specified name in the Program



422
423
424
# File 'lib/opencl_ruby_ffi/Program.rb', line 422

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

#debug_info_intelObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opencl_ruby_ffi/intel/unofficial.rb', line 75

def debug_info_intel
  sizes = self.debug_info_sizes_intel
  bin_array = MemoryPointer::new( :pointer, sizes.length )
  total_size = 0
  sizes.each_with_index { |s, i|
    total_size += s
    pointers[i] = MemoryPointer::new(s)
    bin_array[i].write_pointer(pointers[i])
  }
  error = OpenCL.clGetProgramInfo(self, DEBUG_INFO_INTEL, total_size, bin_array, nil)
  error_check(error)
  bins = []
  devs = self.devices
  sizes.each_with_index { |s, i|
    bins.push [devs[i], pointers[i].read_bytes(s)]
  }
  return bins
end

#debug_info_sizes_intelObject

Returns the OpenCL::Program::debug_info_sizes_intel info

Returns:

  • an Array of size_t



73
# File 'lib/opencl_ruby_ffi/intel/unofficial.rb', line 73

get_info_array("Program", :size_t, "debug_info_sizes_intel")

#devicesObject

Returns the Array of Device the Program is associated with



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/opencl_ruby_ffi/Program.rb', line 328

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

#inspectObject



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/opencl_ruby_ffi/Program.rb', line 278

def inspect
  success = false
  build_status.each { |d,s|
    success |= true if s.to_i == BuildStatus::SUCCESS
  }
  begin
    return "#<#{self.class.name}: #{success ? kernel_names : ""}>"
  rescue
    return "#<#{self.class.name}: >"
  end
end

#kernel_namesObject



431
432
433
# File 'lib/opencl_ruby_ffi/Program.rb', line 431

def kernel_names
  return kernels.collect(&:name)
end

#kernelsObject

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



427
428
429
# File 'lib/opencl_ruby_ffi/Program.rb', line 427

def kernels
  return OpenCL.create_kernels_in_program( self )
end

#num_devicesObject

Returns the OpenCL::Program::num_devices info

Returns:

  • cl_uint



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

get_info("Program", :cl_uint, "num_devices", true)

#orig_method_missingObject



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

alias_method :orig_method_missing, :method_missing

#platformObject

Returns the Platform associated with the Program



310
311
312
# File 'lib/opencl_ruby_ffi/Program.rb', line 310

def platform
  @_platform ||= self.context.platform
end

#reference_countObject

Returns the OpenCL::Program::reference_count info

Returns:

  • cl_uint



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

get_info("Program", :cl_uint, "reference_count")

#sourceObject

Returns the OpenCL::Program::source info

Returns:

  • string



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

get_info("Program", :string, "source")

Constant Summary collapse

REFERENCE_COUNT =
0x1160
CONTEXT =
0x1161
NUM_DEVICES =
0x1162
DEVICES =
0x1163
SOURCE =
0x1164
BINARY_SIZES =
0x1165
BINARIES =
0x1166
NUM_KERNELS =
0x1167
KERNEL_NAMES =
0x1168
IL =
0x1169
SCOPE_GLOBAL_CTORS_PRESENT =
0x116A
SCOPE_GLOBAL_DTORS_PRESENT =
0x116B
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
DEBUG_INFO_INTEL =
0x4100
DEBUG_INFO_SIZES_INTEL =
0x4101