opencl_ruby_ffi 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,152 @@
1
+ module OpenCL
2
+
3
+
4
+ # Attaches a callback to event that will be called on the given transition
5
+ #
6
+ # ==== Attributes
7
+ #
8
+ # * +event+ - the Event to attach the callback to
9
+ # * +options+ - a hash containing named options
10
+ # * +block+ - a callback invoked when the given event occurs. Signature of the callback is { |Event, :cl_int event_command_exec_status, FFI::Pointer to user_data| ... }
11
+ #
12
+ # ==== Options
13
+ #
14
+ # * +:user_data+ - a Pointer (or convertible to Pointer using to_ptr) to the memory area to pass to the callback
15
+ def self.set_event_callback( event, command_exec_callback_type, options = {}, &proc )
16
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if event.context.platform.version_number < 1.1
17
+ error = OpenCL.clSetEventCallback( event, command_exec_callback_type, proc, options[:user_data] )
18
+ OpenCL.error_check(error)
19
+ return self
20
+ end
21
+
22
+ # Creates a user Event
23
+ #
24
+ # ==== Attributes
25
+ #
26
+ # * +context+ - Context the created Event will be associated to
27
+ def self.create_user_event(context)
28
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if context.platform.version_number < 1.1
29
+ error = FFI::MemoryPointer::new(:cl_int)
30
+ event = OpenCL.clCreateUserEvent(context, error)
31
+ OpenCL.error_check(error.read_cl_int)
32
+ return Event::new(event, false)
33
+ end
34
+
35
+ # Sets the satus of user Event to the given execution status
36
+ def self.set_user_event_status( event, execution_status )
37
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if event.context.platform.version_number < 1.1
38
+ error = OpenCL.clSetUserEventStatus( event, execution_status )
39
+ OpenCL.error_check(error)
40
+ return self
41
+ end
42
+
43
+ # Creates an event from a GL sync object
44
+ #
45
+ # ==== Attributes
46
+ #
47
+ # * +context+ - Context the created Event will be associated to
48
+ # * +sync+ - a :GLsync representing the name of the sync object
49
+ def self.create_event_from_GL_sync_KHR( context, sync )
50
+ error = FFI::MemoryPointer::new(:cl_int)
51
+ event = OpenCL.clCreateEventFromGLsyncKHR(context, sync, error)
52
+ OpenCL.error_check(error.read_cl_int)
53
+ return Event::new(event, false)
54
+ end
55
+
56
+ # Maps the cl_event object
57
+ class Event
58
+
59
+ # Returns the CommandQueue associated with the Event, if it exists
60
+ def command_queue
61
+ ptr = FFI::MemoryPointer.new( CommandQueue )
62
+ error = OpenCL.clGetEventInfo(self, Event::COMMAND_QUEUE, CommandQueue.size, ptr, nil)
63
+ OpenCL.error_check(error)
64
+ pt = ptr.read_pointer
65
+ if pt.null? then
66
+ return nil
67
+ else
68
+ return OpenCL::CommandQueue::new( pt )
69
+ end
70
+ end
71
+
72
+ # Returns the Context associated with the Event
73
+ def context
74
+ ptr = FFI::MemoryPointer.new( Context )
75
+ error = OpenCL.clGetEventInfo(self, Event::CONTEXT, Context.size, ptr, nil)
76
+ OpenCL.error_check(error)
77
+ return OpenCL::Context::new( ptr.read_pointer )
78
+ end
79
+
80
+ # Returns a CommandType corresponding to the type of the command associated with the Event
81
+ eval OpenCL.get_info("Event", :cl_command_type, "COMMAND_TYPE")
82
+
83
+ # Returns a CommandExecutionStatus corresponding to the status of the command associtated with the Event
84
+ def command_execution_status
85
+ ptr = FFI::MemoryPointer.new( :cl_int )
86
+ error = OpenCL.clGetEventInfo(self, OpenCL::Event::COMMAND_EXECUTION_STATUS, ptr.size, ptr, nil )
87
+ OpenCL.error_check(error)
88
+ return OpenCL::CommandExecutionStatus::new( ptr.read_cl_int )
89
+ end
90
+
91
+ ##
92
+ # :method: reference_count()
93
+ # Returns the reference counter of th Event
94
+ eval OpenCL.get_info("Event", :cl_uint, "REFERENCE_COUNT")
95
+
96
+ # Returns the date the command corresponding to Event was queued
97
+ def profiling_command_queued
98
+ ptr = FFI::MemoryPointer.new( :cl_ulong )
99
+ error = OpenCL.clGetEventProfilingInfo(self, OpenCL::PROFILING_COMMAND_QUEUED, ptr.size, ptr, nil )
100
+ OpenCL.error_check(error)
101
+ return ptr.read_cl_ulong
102
+ end
103
+
104
+ # Returns the date the command corresponding to Event was submited
105
+ def profiling_command_submit
106
+ ptr = FFI::MemoryPointer.new( :cl_ulong )
107
+ error = OpenCL.clGetEventProfilingInfo(self, OpenCL::PROFILING_COMMAND_SUBMIT, ptr.size, ptr, nil )
108
+ OpenCL.error_check(error)
109
+ return ptr.read_cl_ulong
110
+ end
111
+
112
+ # Returns the date the command corresponding to Event started
113
+ def profiling_command_start
114
+ ptr = FFI::MemoryPointer.new( :cl_ulong )
115
+ error = OpenCL.clGetEventProfilingInfo(self, OpenCL::PROFILING_COMMAND_START, ptr.size, ptr, nil )
116
+ OpenCL.error_check(error)
117
+ return ptr.read_cl_ulong
118
+ end
119
+
120
+ # Returns the date the command corresponding to Event ended
121
+ def profiling_command_end
122
+ ptr = FFI::MemoryPointer.new( :cl_ulong )
123
+ error = OpenCL.clGetEventProfilingInfo(self, OpenCL::PROFILING_COMMAND_END, ptr.size, ptr, nil )
124
+ OpenCL.error_check(error)
125
+ return ptr.read_cl_ulong
126
+ end
127
+
128
+ # Sets the satus of Event (a user event) to the given execution status
129
+ def set_user_event_status( execution_status )
130
+ return OpenCL.set_user_event_status( self, execution_status )
131
+ end
132
+
133
+ alias :set_status :set_user_event_status
134
+
135
+ # Attaches a callback to the Event that will be called on the given transition
136
+ #
137
+ # ==== Attributes
138
+ #
139
+ # * +options+ - a hash containing named options
140
+ # * +block+ - a callback invoked when the given Event occurs. Signature of the callback is { |Event, :cl_int event_command_exec_status, FFI::Pointer to user_data| ... }
141
+ #
142
+ # ==== Options
143
+ #
144
+ # * +:user_data+ - a Pointer (or convertible to Pointer using to_ptr) to the memory area to pass to the callback
145
+ def set_event_callback( command_exec_callback_type, options={}, &proc )
146
+ return OpenCL.set_event_callback( self, command_exec_callback_type, options={}, &proc )
147
+ end
148
+
149
+ alias :set_callback :set_event_callback
150
+
151
+ end
152
+ end
@@ -0,0 +1,270 @@
1
+ module OpenCL
2
+
3
+ # Creates an Image
4
+ #
5
+ # ==== Attributes
6
+ #
7
+ # * +context+ - Context the created Image will be associated to
8
+ # * +format+ - an ImageFormat
9
+ # * +options+ - an ImageDesc
10
+ #
11
+ # ==== Options
12
+ #
13
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer
14
+ # * +:host_ptr+ - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use
15
+ def self.create_image( context, format, desc, options = {} )
16
+ flags = OpenCL.get_flags( options )
17
+ host_ptr = options[:host_ptr]
18
+ error = FFI::MemoryPointer.new( :cl_int )
19
+ img_ptr = OpenCL.clCreateImage( context, flags, format, desc, host_ptr, error )
20
+ OpenCL.error_check(error.read_cl_int)
21
+ return Image::new(img_ptr, false)
22
+ end
23
+
24
+ # Creates a 1D Image
25
+ #
26
+ # ==== Attributes
27
+ #
28
+ # * +context+ - Context the created Image will be associated to
29
+ # * +format+ - an ImageFormat
30
+ # * +width+ - width of the image
31
+ #
32
+ # ==== Options
33
+ #
34
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer
35
+ # * +:host_ptr+ - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use
36
+ def self.create_image_1D( context, format, width, options )
37
+ if context.platform.version_number > 1.1 then
38
+ desc = OpenCL::ImageDesc::new(OpenCL::Mem::IMAGE1D, width, 0, 0, 0, 0, 0, 0, 0, nil)
39
+ return OpenCL.create_image( context, format, desc, options )
40
+ else
41
+ OpenCL.error_check(OpenCL::Error::INVALID_OPERATION)
42
+ end
43
+ end
44
+
45
+ # Creates a 2D Image
46
+ #
47
+ # ==== Attributes
48
+ #
49
+ # * +context+ - Context the created Image will be associated to
50
+ # * +format+ - an ImageFormat
51
+ # * +width+ - width of the image
52
+ #
53
+ # ==== Options
54
+ #
55
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer
56
+ # * +:host_ptr+ - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use
57
+ # * +:row_pitch+ - if provided the row_pitch of data in host_ptr
58
+ def self.create_image_2D( context, format, width, height, options = {} )
59
+ row_pitch = 0
60
+ row_pitch = options[:row_pitch] if options[:row_pitch]
61
+ if context.platform.version_number > 1.1 then
62
+ desc = OpenCL::ImageDesc::new(OpenCL::Mem::IMAGE2D, width, height, 0, 0, row_pitch, 0, 0, 0, nil)
63
+ return OpenCL.create_image( context, format, desc, options )
64
+ end
65
+ flags = OpenCL.get_flags( options )
66
+ host_ptr = options[:host_ptr]
67
+ error = FFI::MemoryPointer.new( :cl_int )
68
+ img_ptr = OpenCL.clCreateImage2D( context, flags, format, width, heigh, row_pitch, host_ptr, error )
69
+ OpenCL.error_check(error.read_cl_int)
70
+ return Image::new(img_ptr, false)
71
+ end
72
+
73
+ # Creates a 3D Image
74
+ #
75
+ # ==== Attributes
76
+ #
77
+ # * +context+ - Context the created Image will be associated to
78
+ # * +format+ - an ImageFormat
79
+ # * +width+ - width of the image
80
+ #
81
+ # ==== Options
82
+ #
83
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Buffer
84
+ # * +:host_ptr+ - if provided, the Pointer (or convertible to Pointer using to_ptr) to the memory area to use
85
+ # * +:row_pitch+ - if provided the row_pitch of data in host_ptr
86
+ # * +:slice_pitch+ - if provided the slice_pitch of data in host_ptr
87
+ def self.create_image_3D( context, format, width, height, depth, options = {} )
88
+ row_pitch = 0
89
+ row_pitch = options[:row_pitch] if options[:row_pitch]
90
+ slice_pitch = 0
91
+ slice_pitch = options[:slice_pitch] if options[:slice_pitch]
92
+ if context.platform.version_number > 1.1 then
93
+ desc = OpenCL::ImageDesc::new(OpenCL::Mem::IMAGE3D, width, height, depth, 0, row_pitch, slice_pitch, 0, 0, nil)
94
+ return OpenCL.create_image( context, format, desc, flags, data )
95
+ end
96
+ flags = OpenCL.get_flags( options )
97
+ host_ptr = options[:host_ptr]
98
+ error = FFI::MemoryPointer.new( :cl_int )
99
+ img_ptr = OpenCL.clCreateImage3D( context, fs, format, width, heigh, depth, row_pitch, slice_pitch, d, error )
100
+ OpenCL.error_check(error.read_cl_int)
101
+ return Image::new(img_ptr, false)
102
+ end
103
+
104
+ # Creates an Image from an OpenGL render buffer
105
+ #
106
+ # ==== Attributes
107
+ #
108
+ # * +context+ - Context the created Image will be associated to
109
+ # * +renderbuf+ - opengl render buffer
110
+ # * +options+ - a hash containing named options
111
+ #
112
+ # ==== Options
113
+ #
114
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image (default OpenCL::Mem::READ_WRITE)
115
+ def self.create_from_GL_render_buffer( context, renderbuffer, options = {} )
116
+ flags = OpenCL.get_flags( options )
117
+ error = FFI::MemoryPointer.new( :cl_int )
118
+ img = OpenCL.clCreateFromGLRenderBuffer( context, flags, renderbuffer, error )
119
+ OpenCL.error_check(error.read_cl_int)
120
+ return Image::new( img, false )
121
+ end
122
+
123
+ # Creates an Image from an OpenGL texture
124
+ #
125
+ # ==== Attributes
126
+ #
127
+ # * +context+ - Context the created Image will be associated to
128
+ # * +texture_target+ - a :GLenum defining the image type of texture
129
+ # * +texture+ - a :GLuint specifying the name of the texture
130
+ # * +options+ - a hash containing named options
131
+ #
132
+ # ==== Options
133
+ #
134
+ # * +:miplevel+ - a :GLint specifying the mipmap level to be used (default 0)
135
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image (default OpenCL::Mem::READ_WRITE)
136
+ def self.create_from_GL_texture( context, texture_target, texture, options = {} )
137
+ if context.platform.version_number < 1.2 then
138
+ OpenCL.error_check(OpenCL::INVALID_OPERATION)
139
+ end
140
+ flags = OpenCL.get_flags( options )
141
+ miplevel = 0
142
+ miplevel = options[:miplevel] if options[:miplevel]
143
+ error = FFI::MemoryPointer.new( :cl_int )
144
+ img = OpenCL.clCreateFromGLTexture( context, flags, texture_target, miplevel, texture, error )
145
+ OpenCL.error_check(error.read_cl_int)
146
+ return Image::new( img, false )
147
+ end
148
+
149
+ # Creates an Image from an OpenGL 2D texture
150
+ #
151
+ # ==== Attributes
152
+ #
153
+ # * +texture_target+ - a :GLenum defining the image type of texture
154
+ # * +texture+ - a :GLuint specifying the name of the texture
155
+ # * +options+ - a hash containing named options
156
+ #
157
+ # ==== Options
158
+ #
159
+ # * +:miplevel+ - a :GLint specifying the mipmap level to be used (default 0)
160
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image
161
+ def self.create_from_GL_texture_2D( context, texture_target, texture, options = {} )
162
+ if context.platform.version_number > 1.1 then
163
+ return OpenCL.create_from_GL_texture( context, texture_target, texture, options )
164
+ end
165
+ flags = OpenCL.get_flags( options )
166
+ miplevel = 0
167
+ miplevel = options[:miplevel] if options[:miplevel]
168
+ error = FFI::MemoryPointer.new( :cl_int )
169
+ img = OpenCL.clCreateFromGLTexture2D( context, flags, texture_target, miplevel, texture, error )
170
+ OpenCL.error_check(error.read_cl_int)
171
+ return Image::new( img, false )
172
+ end
173
+
174
+ # Creates an Image from an OpenGL 3D texture
175
+ #
176
+ # ==== Attributes
177
+ #
178
+ # * +texture_target+ - a :GLenum defining the image type of texture
179
+ # * +texture+ - a :GLuint specifying the name of the texture
180
+ # * +options+ - a hash containing named options
181
+ #
182
+ # ==== Options
183
+ #
184
+ # * +:miplevel+ - a :GLint specifying the mipmap level to be used (default 0)
185
+ # * +:flags+ - a single or an Array of :cl_mem_flags specifying the flags to be used when creating the Image
186
+ def self.create_from_GL_texture_3D( context, texture_target, texture, options = {} )
187
+ if context.platform.version_number > 1.1 then
188
+ return OpenCL.create_from_GL_texture( context, texture_target, texture, options )
189
+ end
190
+ flags = OpenCL.get_flags( options )
191
+ miplevel = 0
192
+ miplevel = options[:miplevel] if options[:miplevel]
193
+ error = FFI::MemoryPointer.new( :cl_int )
194
+ img = OpenCL.clCreateFromGLTexture3D( context, flags, texture_target, miplevel, texture, error )
195
+ OpenCL.error_check(error.read_cl_int)
196
+ return Image::new( img, false )
197
+ end
198
+
199
+ # Maps the cl_mem OpenCL objects of type CL_MEM_OBJECT_IMAGE*
200
+ class Image
201
+
202
+
203
+ ##
204
+ # :method: element_size
205
+ # Returns the element_size of the Image
206
+
207
+ ##
208
+ # :method: row_pitch
209
+ # Returns the row_pitch of the Image
210
+
211
+ ##
212
+ # :method: slice_pitch
213
+ # Returns the slice_pitch of the Image
214
+
215
+ ##
216
+ # :method: width
217
+ # Returns the width of the Image
218
+
219
+ ##
220
+ # :method: height
221
+ # Returns the height of the Image
222
+
223
+ ##
224
+ # :method: depth
225
+ # Returns the depth of the Image
226
+
227
+ ##
228
+ # :method: array_size
229
+ # Returns the array_size of the Image
230
+ %w( ELEMENT_SIZE ROW_PITCH SLICE_PITCH WIDTH HEIGHT DEPTH ARRAY_SIZE ).each { |prop|
231
+ eval OpenCL.get_info("Image", :size_t, prop)
232
+ }
233
+
234
+ ##
235
+ # :method: num_mip_levels
236
+ # Returns the num_mip_levels of the Image
237
+
238
+ ##
239
+ # :method: num_samples
240
+ # Returns the num_samples of the Image
241
+ %w( NUM_MIP_LEVELS NUM_SAMPLES ).each { |prop|
242
+ eval OpenCL.get_info("Image", :cl_uint, prop)
243
+ }
244
+
245
+ # Returns the ImageDesc corresponding to the Image
246
+ def desc
247
+ return ImageDesc::new( self.type, self.width, self.height, self.depth, self.array_size, self.row_pitch, self.slice_pitch, self.num_mip_levels, self.num_samples, self.buffer )
248
+ end
249
+
250
+ # Returns the ImageFormat corresponding to the image
251
+ def format
252
+ image_format = FFI::MemoryPointer.new( ImageFormat )
253
+ error = OpenCL.clGetImageInfo( self, OpenCL::Image::FORMAT, image_format.size, image_format, nil)
254
+ OpenCL.error_check(error)
255
+ return OpenCL::ImageFormat::from_pointer( image_format )
256
+ end
257
+
258
+ # Returns the associated Buffer if any, nil otherwise
259
+ def buffer
260
+ ptr = FFI::MemoryPointer.new( OpenCL::Buffer )
261
+ error = OpenCL.clGetImageInfo(self, OpenCL::Image::BUFFER, OpenCL::Buffer.size, ptr, nil)
262
+ OpenCL.error_check(error)
263
+ return nil if ptr.null?
264
+ return OpenCL::Buffer::new(ptr.read_pointer)
265
+ end
266
+
267
+ end
268
+
269
+ end
270
+
@@ -0,0 +1,183 @@
1
+ module OpenCL
2
+
3
+ # Creates an Array of Kernel corresponding to the kernels defined inside the Program
4
+ def self.create_kernels_in_program( program )
5
+ num_ptr = FFI::MemoryPointer.new( :cl_uint )
6
+ error = OpenCL. clCreateKernelsInProgram( program, 0, nil, num_ptr )
7
+ OpenCL.error_check(error)
8
+ num_kernels = num_ptr.read_cl_uint
9
+ kernels_ptr = FFI::MemoryPointer.new( OpenCL::Kernel, num_kernels )
10
+ error = OpenCL. clCreateKernelsInProgram( program, num_kernels, kernels_ptr, 0 )
11
+ OpenCL.error_check(error)
12
+ return kernels_ptr.get_array_of_pointer(0, num_kernels).collect { |kernel_ptr|
13
+ OpenCL::Kernel.new(kernel_ptr, false)
14
+ }
15
+ end
16
+
17
+ # Returns the Kernel corresponding the the specified name in the given Program
18
+ def self.create_kernel(program, name)
19
+ pointer_err = FFI::MemoryPointer.new( :cl_int )
20
+ kernel_ptr = OpenCL.clCreateKernel(program, name, pointer_err)
21
+ OpenCL.error_check(pointer_err.read_cl_int)
22
+ return OpenCL::Kernel::new( kernel_ptr, false )
23
+ end
24
+
25
+ # Set the index th argument of Kernel to value. size of value can be specified
26
+ def self.set_kernel_arg( kernel, index, value, size = nil )
27
+ sz = size
28
+ sz = value.class.size if sz == nil
29
+ val = value
30
+ if value.kind_of?(OpenCL::Mem) then
31
+ val = FFI::MemoryPointer.new( OpenCL::Mem )
32
+ val.write_pointer(value.to_ptr)
33
+ end
34
+ error = OpenCL.clSetKernelArg( kernel, index, sz, val )
35
+ OpenCL.error_check(error)
36
+ end
37
+
38
+ # Maps the cl_kernel object
39
+ class Kernel
40
+
41
+ # Maps the logical cl arg object
42
+ class Arg
43
+ # Returns the index of the Arg in the list
44
+ attr_reader :index
45
+ # Returns the Kernel this Arg belongs to
46
+ attr_reader :kernel
47
+
48
+ # Creates a new arg for a Kernel at index
49
+ def initialize( kernel, index )
50
+ @index = index
51
+ @kernel = kernel
52
+ end
53
+
54
+ # Returns an AddressQualifier corresponding to the Arg
55
+ def address_qualifier
56
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if @kernel.context.platform.version_number < 1.2
57
+ ptr = FFI::MemoryPointer.new( :cl_kernel_arg_address_qualifier )
58
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::ADDRESS_QUALIFIER, ptr.size, ptr, nil)
59
+ OpenCL.error_check(error)
60
+ return OpenCL::Kernel::Arg::AddressQualifier::new( ptr.read_cl_kernel_arg_address_qualifier )
61
+ end
62
+
63
+ # Returns an AccessQualifier corresponding to the Arg
64
+ def access_qualifier
65
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if @kernel.context.platform.version_number < 1.2
66
+ ptr = FFI::MemoryPointer.new( :cl_kernel_arg_access_qualifier )
67
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::ACCESS_QUALIFIER, ptr.size, ptr, nil)
68
+ OpenCL.error_check(error)
69
+ return OpenCL::Kernel::Arg::AccessQualifier::new( ptr.read_cl_kernel_arg_access_qualifier )
70
+ end
71
+
72
+ # Returns a TypeQualifier corresponding to the Arg
73
+ def type_qualifier
74
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if @kernel.context.platform.version_number < 1.2
75
+ ptr = FFI::MemoryPointer.new( :cl_kernel_arg_type_qualifier )
76
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::TYPE_QUALIFIER, ptr.size, ptr, nil)
77
+ OpenCL.error_check(error)
78
+ return OpenCL::Kernel::Arg::TypeQualifier::new( ptr.read_cl_kernel_arg_type_qualifier )
79
+ end
80
+
81
+ # Returns a String corresponding to the Arg type name
82
+ def type_name
83
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if @kernel.context.platform.version_number < 1.2
84
+ ptr1 = FFI::MemoryPointer.new( :size_t, 1)
85
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::TYPE_NAME, 0, nil, ptr1)
86
+ OpenCL.error_check(error)
87
+ ptr2 = FFI::MemoryPointer.new( ptr1.read_size_t )
88
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::TYPE_NAME, ptr1.read_size_t, ptr2, nil)
89
+ OpenCL.error_check(error)
90
+ return ptr2.read_string
91
+ end
92
+
93
+ # Returns a String corresponding to the Arg name
94
+ def name
95
+ OpenCL.error_check(OpenCL::INVALID_OPERATION) if @kernel.context.platform.version_number < 1.2
96
+ ptr1 = FFI::MemoryPointer.new( :size_t, 1)
97
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::NAME, 0, nil, ptr1)
98
+ OpenCL.error_check(error)
99
+ ptr2 = FFI::MemoryPointer.new( ptr1.read_size_t )
100
+ error = OpenCL.clGetKernelArgInfo(@kernel, @index, OpenCL::Kernel::Arg::NAME, ptr1.read_size_t, ptr2, nil)
101
+ OpenCL.error_check(error)
102
+ return ptr2.read_string
103
+ end
104
+
105
+ # Sets this Arg to value. The size of value can be specified.
106
+ def set(value, size = nil)
107
+ OpenCL.set_kernel_arg(@kernel, @index, value, size)
108
+ end
109
+
110
+ end
111
+
112
+ # Returns an Array of Arg corresponding to the arguments of the Kernel
113
+ def args
114
+ n = self.num_args
115
+ a = []
116
+ n.times { |i|
117
+ a.push OpenCL::Kernel::Arg::new(self, i)
118
+ }
119
+ return a
120
+ end
121
+
122
+ ##
123
+ # :method: function_name()
124
+ # returns a String correspondig to the Kernel function name
125
+
126
+ ##
127
+ # :method: attributes()
128
+ # returns a String containing the attributes qualifier used at kernel definition
129
+ %w( FUNCTION_NAME ATTRIBUTES ).each { |prop|
130
+ eval OpenCL.get_info("Kernel", :string, prop)
131
+ }
132
+
133
+ ##
134
+ # :method: num_args()
135
+ # Returns the number of arguments for the Kernel
136
+
137
+ ##
138
+ # :method: reference_count
139
+ # Returns the reference counter for the Kernel
140
+ %w( NUM_ARGS REFERENCE_COUNT ).each { |prop|
141
+ eval OpenCL.get_info("Kernel", :cl_uint, prop)
142
+ }
143
+
144
+ # Returns the Context the Kernel is associated with
145
+ def context
146
+ ptr = FFI::MemoryPointer.new( Context )
147
+ error = OpenCL.clGetKernelInfo(self, Kernel::CONTEXT, Context.size, ptr, nil)
148
+ OpenCL.error_check(error)
149
+ return OpenCL::Context::new( ptr.read_pointer )
150
+ end
151
+
152
+ # Returns the Program the Kernel was created from
153
+ def program
154
+ ptr = FFI::MemoryPointer.new( Program )
155
+ error = OpenCL.clGetKernelInfo(self, Kernel::PROGRAM, Program.size, ptr, nil)
156
+ OpenCL.error_check(error)
157
+ return OpenCL::Program::new(ptr.read_pointer)
158
+ end
159
+
160
+ # Set the index th argument of the Kernel to value. The size of value can be specified.
161
+ def set_arg(index, value, size = nil)
162
+ OpenCL.set_kernel_arg(self, index, value, size)
163
+ end
164
+
165
+ # Enqueues the Kernel in the given queue, specifying the global_work_size. Arguments for the kernel are specified afterwards. Last, a hash containing options for enqueuNDrange kernel can be specified
166
+ def enqueue_with_args(command_queue, global_work_size, *args)
167
+ n = self.num_args
168
+ OpenCL.error_check(OpenCL::INVALID_KERNEL_ARGS) if args.length < n
169
+ OpenCL.error_check(OpenCL::INVALID_KERNEL_ARGS) if args.length > n + 1
170
+ if args.length == n + 1
171
+ options = args.last
172
+ else
173
+ options = {}
174
+ end
175
+ n.times { |i|
176
+ self.set_arg(i, args[i])
177
+ }
178
+ command_queue.enqueue_NDrange_kernel(self, global_work_size, options)
179
+ end
180
+
181
+ end
182
+
183
+ end