Class: OpenCL::Context

Inherits:
ExtendedStruct show all
Includes:
KHRGLEvent, KHRGLSharing, KHRTerminateContext, OpenCL11, OpenCL12, OpenCL20, OpenCL21
Defined in:
lib/opencl_ruby_ffi/Context.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/gl_event.rb,
lib/opencl_ruby_ffi/khr/gl_sharing.rb,
lib/opencl_ruby_ffi/khr/d3d10_sharing.rb,
lib/opencl_ruby_ffi/khr/d3d11_sharing.rb,
lib/opencl_ruby_ffi/khr/initalize_memory.rb,
lib/opencl_ruby_ffi/khr/dx9_media_sharing.rb,
lib/opencl_ruby_ffi/khr/terminate_context.rb

Overview

Maps the cl_context object of OpenCL

Defined Under Namespace

Modules: KHRGLEvent, KHRGLSharing, KHRTerminateContext, OpenCL11, OpenCL12, OpenCL20, OpenCL21 Classes: Properties

Constant Summary collapse

REFERENCE_COUNT =
0x1080
DEVICES =
0x1081
PROPERTIES =
0x1082
NUM_DEVICES =
0x1083
PLATFORM =
0x1084
INTEROP_USER_SYNC =
0x1085
D3D10_DEVICE_KHR =
0x4014
D3D10_PREFER_SHARED_RESOURCES_KHR =
0x402C
D3D11_DEVICE_KHR =
0x401D
D3D11_PREFER_SHARED_RESOURCES_KHR =
0x402D
MEMORY_INITIALIZE_KHR =
0x200E
ADAPTER_D3D9_KHR =
0x2025
ADAPTER_D3D9EX_KHR =
0x2026
ADAPTER_DXVA_KHR =
0x2027
TERMINATE_KHR =
0x2032

Instance Method Summary collapse

Methods included from KHRTerminateContext

#terminate_context_khr

Methods included from KHRGLSharing

#get_gl_info_khr

Methods included from KHRGLEvent

#create_event_from_glsync_khr

Methods included from OpenCL21

#create_program_with_il, #set_default_device_command_queue

Methods included from OpenCL20

#create_pipe, #svm_alloc, #svm_free

Methods included from OpenCL12

#create_from_gl_texture, #create_image, #create_program_with_built_in_kernels, #link_program

Methods included from OpenCL11

#create_user_event

Methods inherited from ExtendedStruct

register_extension

Constructor Details

#initialize(ptr, retain = true) ⇒ Context

Creates a new Context and retains it if specified and aplicable



1082
1083
1084
1085
1086
# File 'lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb', line 1082

def initialize(ptr, retain = true)
  super(ptr)
  OpenCL.clRetainContext(ptr) if retain
  #STDERR.puts "Allocating Context: #{ptr}"
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



174
175
176
# File 'lib/opencl_ruby_ffi/Context.rb', line 174

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



159
160
161
# File 'lib/opencl_ruby_ffi/Context.rb', line 159

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

#create_from_gl_buffer(bufobj, options = {}) ⇒ Object Also known as: create_from_GL_buffer

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



188
189
190
# File 'lib/opencl_ruby_ffi/Context.rb', line 188

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

#create_from_gl_renderbuffer(renderbuffer, options = {}) ⇒ Object Also known as: create_from_GL_renderbuffer

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



203
204
205
# File 'lib/opencl_ruby_ffi/Context.rb', line 203

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

#create_from_gl_texture_2d(texture_target, texture, options = {}) ⇒ Object Also known as: create_from_GL_texture_2D

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



220
221
222
# File 'lib/opencl_ruby_ffi/Context.rb', line 220

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 Also known as: create_from_GL_texture_3D

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



237
238
239
# File 'lib/opencl_ruby_ffi/Context.rb', line 237

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_1d(format, width, options = {}) ⇒ Object Also known as: create_image_1D

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



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

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 Also known as: create_image_2D

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 Also known as: create_image_3D

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



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

def create_image_3d( format, width, height, depth, options = {} )
  return OpenCL.create_image_3d( self, format, width, height, depth, options )
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



299
300
301
# File 'lib/opencl_ruby_ffi/Context.rb', line 299

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



308
309
310
# File 'lib/opencl_ruby_ffi/Context.rb', line 308

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)



322
323
324
# File 'lib/opencl_ruby_ffi/Context.rb', line 322

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

#devicesObject

Returns an Array of Device associated to the Context



80
81
82
83
84
85
86
87
88
# File 'lib/opencl_ruby_ffi/Context.rb', line 80

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

#inspectObject



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

def inspect
  return "#<#{self.class.name}: #{self.devices}>"
end

#num_devicesObject

Returns the number of devices associated to the Context



70
71
72
73
74
75
76
77
# File 'lib/opencl_ruby_ffi/Context.rb', line 70

def num_devices
  d_n = 0
  ptr = MemoryPointer::new( :size_t )
  error = OpenCL.clGetContextInfo(self, DEVICES, 0, nil, ptr)
  error_check(error)
  d_n = ptr.read_size_t / Platform.size
  return d_n
end

#platformObject

Returns the platform associated to the Context



120
121
122
# File 'lib/opencl_ruby_ffi/Context.rb', line 120

def platform
  self.devices.first.platform
end

#propertiesObject

the Array of :cl_context_properties used to create the Context



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/opencl_ruby_ffi/Context.rb', line 93

def properties
  ptr1 = MemoryPointer::new( :size_t, 1)
  error = OpenCL.clGetContextInfo(self, PROPERTIES, 0, nil, ptr1)
  error_check(error)
  return [] if ptr1.read_size_t == 0
  ptr2 = MemoryPointer::new( ptr1.read_size_t )
  error = OpenCL.clGetContextInfo(self, PROPERTIES, ptr1.read_size_t, ptr2, nil)
  error_check(error)
  arr = ptr2.get_array_of_cl_context_properties(0, ptr1.read_size_t/ OpenCL.find_type(:cl_context_properties).size)
  return arr if arr.length == 1 and arr[0].to_i == 0
  arr_2 = []
  while arr.length > 2 do
    prop = arr.shift.to_i
    arr_2.push Properties::new(prop)
    return arr_2 if arr.length <= 0
    if prop == Properties::PLATFORM then
      arr_2.push Platform::new(arr.shift)
    else
      arr_2.push arr.shift.to_i
    end
    return arr_2 if arr.length <= 0
  end
  arr_2.push arr.shift.to_i if arr.length > 0
  return arr_2
end

#reference_countObject

Returns the OpenCL::Context::reference_count info

Returns:

  • cl_uint



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

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

#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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/opencl_ruby_ffi/Context.rb', line 133

def supported_image_formats( image_type, options = {} )
  flags = get_flags( options )
  flags = Mem::Flags::READ_WRITE if flags.to_i == 0 #ensure default READ_WRITE, Intel bug.
  num_image_formats = MemoryPointer::new( :cl_uint )
  error = OpenCL.clGetSupportedImageFormats( self, flags, image_type, 0, nil, num_image_formats )
  error_check(error)
  num_entries = num_image_formats.read_cl_uint
  image_formats = MemoryPointer::new( ImageFormat, num_entries )
  error = OpenCL.clGetSupportedImageFormats( self, flags, image_type, num_entries, image_formats, nil )
  error_check(error)
  return num_entries.times.collect { |i|
    ImageFormat::new( image_formats + i * ImageFormat.size )
  }
end