opencl_ruby_ffi 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ using OpenCLRefinements if RUBY_VERSION.scan(/\d+/).collect(&:to_i).first >= 2
2
+ module OpenCL
3
+
4
+ module StreamDSL
5
+
6
+ class Stream
7
+
8
+ def initialize(queue)
9
+ @queue = queue
10
+ @tail_events = nil
11
+ end
12
+
13
+ def <<(commands)
14
+ events = []
15
+ [commands].flatten.each { |c|
16
+ events.push ( c.enqueue( queue, @tail_events ) )
17
+ }
18
+ @tail_events = events
19
+ return self
20
+ end
21
+
22
+ end
23
+
24
+ class Command
25
+
26
+ def initialize(type, options = {})
27
+ @_type = type
28
+ @_options = options
29
+ @_event = nil
30
+ @_func = nil
31
+ @_params = []
32
+ end
33
+
34
+ def enqueue(queue, event_wait_list = nil)
35
+ if options and options[:event_wait_list] then
36
+ @_event_wait_list = [options[:event_wait_list]].flatten
37
+ else
38
+ @_event_wait_list = nil
39
+ end
40
+ if event_wait_list then
41
+ if @_event_wait_list then
42
+ @_event_wait_list = @_event_wait_list + [event_wait_list].flatten
43
+ else
44
+ @_event_wait_list = [event_wait_list].flatten
45
+ end
46
+ end
47
+ opts = @_options.dup
48
+ opts[:event_wait_list] = @_event_wait_list
49
+ @_event, @_result = @_enqueue_proc.call( queue, *@_param, opts, &@f_unc )
50
+ end
51
+
52
+ def result
53
+ return @_result
54
+ end
55
+
56
+ end
57
+
58
+ def self.command_constructor( name, const, function, params)
59
+ params_array = params + ["options = {}"]
60
+ params_array += ["&func"]
61
+ p_variables = params.collect { |p| "@_params.push(@#{p})" }
62
+ return <<EOF
63
+ class #{name} < Command
64
+
65
+ def initialize( #{params_array.join(", ")}, &func )
66
+ super(CommandType::#{const}, options)
67
+ #{i_variables.join("\n ")}
68
+ @_func = func
69
+ @_enqueue_proc = OpenCL.method(:enqueue_#{function})
70
+ end
71
+
72
+ end
73
+ EOF
74
+ end
75
+
76
+ eval command_constructor( :NDRangeKernel, :NDRANGE_KERNEL, :ndrange_kernel, [ :buffer, :global_work_size ] )
77
+ eval command_constructor( :Task, :TASK, :task, [ :kernel ] )
78
+ eval command_constructor( :NativeKernel, :NATIVE_KERNEL, :native_kernel, [] )
79
+ eval command_constructor( :ReadBuffer, :READ_BUFFER, :read_buffer, [ :buffer, :ptr ] )
80
+ eval command_constructor( :WriteBuffer, :WRITE_BUFFER, :write_buffer, [ :buffer, :ptr ] )
81
+ eval command_constructor( :CopyBuffer, :COPY_BUFFER, :copy_buffer, [ :src_buffer, :dst_buffer ] )
82
+ eval command_constructor( :ReadImage, :READ_IMAGE, :read_image, [ :image, :ptr ] )
83
+ eval command_constructor( :WriteImage, :WRITE_IMAGE, :write_image, [ :image, :ptr ] )
84
+ eval command_constructor( :CopyImage, :COPY_IMAGE, :copy_image, [ :src_image, :dst_image ] )
85
+ eval command_constructor( :CopyImageToBuffer, :COPY_IMAGE_TO_BUFFER, :copy_image_to_buffer, [ :src_image, :dst_buffer ] )
86
+ eval command_constructor( :CopyBufferToImage, :COPY_BUFFER_TO_IMAGE, :copy_buffer_to_image, [ :src_buffer, :dst_image ] )
87
+ eval command_constructor( :MapBuffer, :MAP_BUFFER, :map_buffer, [ :buffer, :map_flags ] )
88
+ eval command_constructor( :MapImage, :MAP_IMAGE, :map_image, [ :image, :map_flags ] )
89
+ eval command_constructor( :UnmapMemObject, :UNMAP_MEM_OBJECT, :unmap_mem_object, [ :mem_obj, :mapped_ptr ] )
90
+
91
+ class Marker < Command
92
+
93
+ def initialize( events = [] )
94
+ super(CommandType::MARKER, nil)
95
+ @events = events
96
+ @_enqueue_proc = OpenCL.method(:enqueue_marker)
97
+ end
98
+
99
+ end
100
+
101
+ eval command_constructor( :AcquireGLObjects, :ACQUIRE_GL_OBJECTS, :acquire_gl_objects, [ :mem_objects ] )
102
+ eval command_constructor( :ReleaseGLObjects, :RELEASE_GL_OBJECTS, :release_gl_objects, [ :mem_objects ] )
103
+ eval command_constructor( :ReadBufferRect, :READ_BUFFER_RECT, :read_buffer_rect, [ :buffer, :ptr, :region ] )
104
+ eval command_constructor( :WriteBufferRect, :WRITE_BUFFER_RECT, :write_buffer_rect, [ :buffer, :ptr, :region ] )
105
+ eval command_constructor( :CopyBufferRect, :COPY_BUFFER_RECT, :copy_buffer_rect, [ :src_buffer, :dst_buffer, :region ] )
106
+
107
+ class Barrier < Command
108
+
109
+ def initialize( events = [] )
110
+ super(CommandType::BARRIER, nil)
111
+ @events = events
112
+ @_enqueue_proc = OpenCL.method(:enqueue_barrier)
113
+ end
114
+
115
+ end
116
+
117
+ eval command_constructor( :FillBuffer, :FILL_BUFFER, :fill_buffer, [ :buffer, :pattern ] )
118
+ eval command_constructor( :FillImage, :FILL_IMAGE, :fill_image, [ :image, :fill_color ] )
119
+ eval command_constructor( :SVMFree, :SVM_FREE, :svm_free, [ :svm_pointers ] )
120
+ eval command_constructor( :SVMMemcpy, :SVM_MEMCPY, :svm_memcpy, [ :dst_ptr, :src_ptr, :size ] )
121
+ eval command_constructor( :SVMMemFill, :SVM_MEMFILL, :svm_memfill, [ :svm_ptr, :pattern, :size ] )
122
+ eval command_constructor( :SVMMap, :SVM_MAP, :svm_map, [ :svm_ptr, :size, :map_flags ] )
123
+ eval command_constructor( :SVMUnmap, :SVM_UNMAP, :svm_unmap, [ :svm_ptr ] )
124
+
125
+ end
126
+
127
+ end