Class: OpenCL::Context

Inherits:
FFI::ManagedStruct
  • Object
show all
Defined in:
lib/opencl_ruby_ffi/Context.rb,
lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb

Overview

Maps the cl_context object of OpenCL

Constant Summary collapse

REFERENCE_COUNT =

:stopdoc:

0x1080
DEVICES =
0x1081
PROPERTIES =
0x1082
NUM_DEVICES =
0x1083
PLATFORM =
0x1084
INTEROP_USER_SYNC =
0x1085
MEMORY_INITIALIZE_KHR =
0x200E
TERMINATE_KHR =
0x2010

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, retain = true) ⇒ Context

Creates a new Context and retains it if specified and aplicable



1100
1101
1102
1103
1104
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1100

def initialize(ptr, retain = true)
  super(ptr)
  OpenCL.clRetainContext(ptr) if retain
  #STDERR.puts "Allocating Context: #{ptr}"
end

Class Method Details

.create_program_with_built_in_kernels(device_list, kernel_names) ⇒ Object

Creates a Program from a list of built in kernel names

Attributes

  • device_list - an Array of Device to create the program for

  • kernel_names - a single or an Array of String representing the kernel names



339
340
341
# File 'lib/opencl_ruby_ffi/Context.rb', line 339

def self.create_program_with_built_in_kernels( device_list, kernel_names )
  return OpenCL.create_program_with_built_in_kernels(self, device_list, kernel_names )
end

.release(ptr) ⇒ Object

method called at Context deletion, releases the object if aplicable



1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1115

def self.release(ptr)
  #STDERR.puts "Releasing Context: #{ptr}"
  #ref_count = FFI::MemoryPointer::new( :cl_uint ) 
  #OpenCL.clGetContextInfo(ptr, OpenCL::Context::REFERENCE_COUNT, ref_count.size, ref_count, nil)
  #STDERR.puts "reference counter: #{ref_count.read_cl_uint}"
  error = OpenCL.clReleaseContext(ptr)
  #STDERR.puts "Object released! #{error}"
  OpenCL.error_check( error )
end

Instance Method Details

#create_buffer(size, options = {}) ⇒ Object

Creates a Buffer in the Context

Attributes

  • size - size of the Buffer to be created

  • options - a hash containing named options

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer

  • :host_ptr - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use



148
149
150
# File 'lib/opencl_ruby_ffi/Context.rb', line 148

def create_buffer( size, options = {} )
  return OpenCL.create_buffer( self, size, options )
end

#create_command_queue(device, options = {}) ⇒ Object

Creates a CommandQueue in Context targeting the specified Device

Attributes

  • device - the Device targetted by the CommandQueue being created

  • options - a hash containing named options

Options

  • :properties - a single or an Array of :cl_command_queue_properties

  • :size - the size of the command queue ( if ON_DEVICE is specified in the properties ) 2.0+ only



133
134
135
# File 'lib/opencl_ruby_ffi/Context.rb', line 133

def create_command_queue( device, options = {} )
  return OpenCL.create_command_queue( self, device, options )
end

#create_from_GL_buffer(bufobj, options = {}) ⇒ Object

Creates a Buffer in the Context from an opengl buffer

Attributes

  • bufobj - opengl buffer object

  • options - a hash containing named options

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image



162
163
164
# File 'lib/opencl_ruby_ffi/Context.rb', line 162

def create_from_GL_buffer( bufobj, options = {} )
  return OpenCL.create_from_GL_buffer( self, bufobj, options )
end

#create_from_GL_render_buffer(renderbuffer, options = {}) ⇒ Object

Creates an Image in the Context from an OpenGL render buffer

Attributes

  • renderbuf - opengl render buffer

  • options - a hash containing named options

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image



176
177
178
# File 'lib/opencl_ruby_ffi/Context.rb', line 176

def create_from_GL_render_buffer( renderbuffer, options = {} )
  return OpenCL.create_from_GL_render_buffer( self, renderbuffer, options )
end

#create_from_GL_texture(texture_target, texture, options = {}) ⇒ Object

Creates an Image in the Context from an OpenGL texture

Attributes

  • texture_target - a :GLenum defining the image type of texture

  • texture - a :GLuint specifying the name of the texture

  • options - a hash containing named options

Options

  • :miplevel - a :GLint specifying the mipmap level to be used (default 0)

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image



192
193
194
# File 'lib/opencl_ruby_ffi/Context.rb', line 192

def create_from_GL_texture( texture_target, texture, options = {} )
  return OpenCL.create_from_GL_texture( self, texture_target, texture, options )
end

#create_from_GL_texture_2D(texture_target, texture, options = {}) ⇒ Object

Creates an Image in the Context from an OpenGL 2D texture

Attributes

  • texture_target - a :GLenum defining the image type of texture

  • texture - a :GLuint specifying the name of the texture

  • options - a hash containing named options

Options

  • :miplevel - a :GLint specifying the mipmap level to be used (default 0)

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image



208
209
210
# File 'lib/opencl_ruby_ffi/Context.rb', line 208

def create_from_GL_texture_2D( texture_target, texture, options = {} )
  return OpenCL.create_from_GL_texture_2D( self, texture_target, texture, options = {} )
end

#create_from_GL_texture_3D(texture_target, texture, options = {}) ⇒ Object

Creates an Image in the Context from an OpenGL 3D texture

Attributes

  • texture_target - a :GLenum defining the image type of texture

  • texture - a :GLuint specifying the name of the texture

  • options - a hash containing named options

Options

  • :miplevel - a :GLint specifying the mipmap level to be used (default 0)

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image



224
225
226
# File 'lib/opencl_ruby_ffi/Context.rb', line 224

def create_from_GL_texture_3D( texture_target, texture, options = {} )
  return OpenCL.create_from_GL_texture_3D( self, texture_target, texture, options )
end

#create_image(format, desc, options = {}) ⇒ Object

Creates an Image in the Context

Attributes

  • format - an ImageFormat

  • options - an ImageDesc

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer

  • :host_ptr - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use



239
240
241
# File 'lib/opencl_ruby_ffi/Context.rb', line 239

def create_image( format, desc, options = {} )
  return OpenCL.create_image( self, format, desc, options )
end

#create_image_1D(format, width, options = {}) ⇒ Object

Creates a 1D Image in the Context

Attributes

  • format - an ImageFormat

  • width - width of the image

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer

  • :host_ptr - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use



254
255
256
# File 'lib/opencl_ruby_ffi/Context.rb', line 254

def create_image_1D( format, width, options = {} )
  return OpenCL.create_image_1D( self, format, width, options )
end

#create_image_2D(format, width, height, options = {}) ⇒ Object

Creates a 2D Image in the Context

Attributes

  • format - an ImageFormat

  • width - width of the image

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer

  • :host_ptr - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use

  • :row_pitch - if provided the row_pitch of data in host_ptr



270
271
272
# File 'lib/opencl_ruby_ffi/Context.rb', line 270

def create_image_2D( format, width, height, options = {} )
  return OpenCL.create_image_2D( self, format, width, height, options )
end

#create_image_3D(format, width, height, depth, options = {}) ⇒ Object

Creates a 3D Image in the Context

Attributes

  • format - an ImageFormat

  • width - width of the image

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer

  • :host_ptr - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use

  • :row_pitch - if provided the row_pitch of data in host_ptr

  • :slice_pitch - if provided the slice_pitch of data in host_ptr



287
288
289
# File 'lib/opencl_ruby_ffi/Context.rb', line 287

def create_image_3D( format, width, height, depth, options = {} )
  return OpenCL.create_image_3D( self, format, width, height, depth, options )
end

#create_pipe(pipe_packet_size, pipe_max_packets, opts = {}) ⇒ Object

Creates a Pipe in the Context

Attributes

  • pipe_packet_size - size of a packet in the Pipe

  • pipe_max_packets - size of the Pipe in packet

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer



376
377
378
# File 'lib/opencl_ruby_ffi/Context.rb', line 376

def create_pipe( pipe_packet_size, pipe_max_packets, opts = {} )
  return OpenCL.create_pipe( self, pipe_packet_size, pipe_max_packets, opts )
end

#create_program_with_binary(device_list, binaries) ⇒ Object

Creates a Program from binary

Attributes

  • device_list - an Array of Device to create the program for. Can throw an OpenCL::Invalid value if the number of supplied devices is different from the number of supplied binaries.

  • binaries - Array of binaries



329
330
331
# File 'lib/opencl_ruby_ffi/Context.rb', line 329

def create_program_with_binary( device_list, binaries)
   return OpenCL.create_program_with_binary(self, device_list, binaries)
end

#create_program_with_source(strings) ⇒ Object

Creates a Program from sources in the Context

Attributes

  • strings - a single or an Array of String repesenting the program source code



348
349
350
# File 'lib/opencl_ruby_ffi/Context.rb', line 348

def create_program_with_source( strings )
  return OpenCL.create_program_with_source(self, strings)
end

#create_sampler(options = {}) ⇒ Object

Creates a Sampler in the Context

Options

  • :normalized_coords - a :cl_bool specifying if the image coordinates are normalized

  • :addressing_mode - a :cl_addressing_mode specifying how out-of-range image coordinates are handled when reading from an image

  • :filter_mode - a :cl_filter_mode specifying the type of filter that must be applied when reading an image

  • :mip_filter_mode - the filtering mode to use if using mimaps (default CL_FILTER_NONE, requires cl_khr_mipmap_image)

  • :lod_min - floating point value representing the minimal LOD (default 0.0f, requires cl_khr_mipmap_image)

  • :lod_max - floating point value representing the maximal LOD (default MAXFLOAT, requires cl_khr_mipmap_image)



362
363
364
# File 'lib/opencl_ruby_ffi/Context.rb', line 362

def create_sampler( options = {} )
  return OpenCL.create_sampler( self, options )
end

#create_user_eventObject

Creates a user Event in the Context



302
303
304
# File 'lib/opencl_ruby_ffi/Context.rb', line 302

def create_user_event
  return OpenCL.create_user_event(self)
end

#devicesObject

Returns an Array of Device associated to the Context



89
90
91
92
93
94
95
96
97
# File 'lib/opencl_ruby_ffi/Context.rb', line 89

def devices
  n = self.num_devices
  ptr2 = FFI::MemoryPointer::new( Device, n )
  error = OpenCL.clGetContextInfo(self, OpenCL::Context::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

#get_info_arrayObject

:method: properties the Array of :cl_context_properties used to create the Context



65
# File 'lib/opencl_ruby_ffi/Context.rb', line 65

eval OpenCL.get_info_array("Context", :cl_context_properties, "PROPERTIES")

Links a set of compiled programs for all device in the Context, or a subset of devices

Attributes

  • input_programs - a single or an Array of Program

  • 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

  • :options - a String containing the options to use for the build

  • :user_data - a Pointer (or convertible to Pointer using to_ptr) to the memory area to pass to the callback



319
320
321
# File 'lib/opencl_ruby_ffi/Context.rb', line 319

def link_program( input_programs, options = {}, &block)
  return OpenCL.link_program(self, input_programs, options, &block)
end

#num_devicesObject

Returns the number of devices associated to the Context



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/opencl_ruby_ffi/Context.rb', line 73

def num_devices
  d_n = 0
  ptr = FFI::MemoryPointer::new( :size_t )
  error = OpenCL.clGetContextInfo(self, OpenCL::Context::DEVICES, 0, nil, ptr)
  OpenCL.error_check(error)
  d_n = ptr.read_size_t / Platform.size
#      else
#        ptr = FFI::MemoryPointer::new( :cl_uint )
#        error = OpenCL.clGetContextInfo(self, OpenCL::Context::NUM_DEVICES, ptr.size, ptr, nil)
#        OpenCL.error_check(error)
#        d_n = ptr.read_cl_uint
#      end
  return d_n
end

#platformObject

Returns the platform associated to the Context



68
69
70
# File 'lib/opencl_ruby_ffi/Context.rb', line 68

def platform
  self.devices.first.platform
end

#propObject

:method: reference_count Returns the reference count of the Context



58
59
60
# File 'lib/opencl_ruby_ffi/Context.rb', line 58

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

#supported_image_formats(image_type, options = {}) ⇒ Object

Returns an Array of ImageFormat that are supported for a given image type in the Context

Attributes

  • image_type - a :cl_mem_object_type specifying the type of Image being queried

  • options - a hash containing named options

Options

  • :flags - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/opencl_ruby_ffi/Context.rb', line 108

def supported_image_formats( image_type, options = {} )
  flags = OpenCL.get_flags( options )
  num_image_formats = FFI::MemoryPointer::new( :cl_uint )
  error = OpenCL.clGetSupportedImageFormats( self, flags, image_type, 0, nil, num_image_formats )
  OpenCL.error_check(error)
  num_entries = num_image_formats.read_cl_uint
  image_formats = FFI::MemoryPointer::new( ImageFormat, num_entries )
  error = OpenCL.clGetSupportedImageFormats( self, flags, image_type, num_entries, image_formats, nil )
  OpenCL.error_check(error)
  return num_entries.times.collect { |i|
    OpenCL::ImageFormat::from_pointer( image_formats + i * ImageFormat.size )
  }
end

#svm_alloc(size, options = {}) ⇒ Object

Creates an SVMPointer pointing to an SVM area of memory in the Context

Attributes

  • size - the size of the mmemory area to allocate

  • options - a hash containing named options

Options

  • :alignment - imposes the minimum alignment in byte



390
391
392
# File 'lib/opencl_ruby_ffi/Context.rb', line 390

def svm_alloc(size, options = {})
  return OpenCL.svm_alloc( self, size, options)
end

#svm_free(svm_pointer) ⇒ Object

Frees an SVMPointer

Attributes

  • svm_pointer - the SVMPointer to deallocate



399
400
401
# File 'lib/opencl_ruby_ffi/Context.rb', line 399

def svm_free(svm_pointer)
  return OpenCL.svm_free(self, svm_pointer)
end

#to_sObject



1106
1107
1108
1109
1110
1111
1112
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1106

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