… | |
… | |
20 | typedef cl_mem OpenCL__Image3D_ornull; |
20 | typedef cl_mem OpenCL__Image3D_ornull; |
21 | typedef cl_sampler OpenCL__Sampler; |
21 | typedef cl_sampler OpenCL__Sampler; |
22 | typedef cl_program OpenCL__Program; |
22 | typedef cl_program OpenCL__Program; |
23 | typedef cl_kernel OpenCL__Kernel; |
23 | typedef cl_kernel OpenCL__Kernel; |
24 | typedef cl_event OpenCL__Event; |
24 | typedef cl_event OpenCL__Event; |
|
|
25 | typedef cl_event OpenCL__UserEvent; |
|
|
26 | |
|
|
27 | typedef SV *FUTURE; |
|
|
28 | |
|
|
29 | /*****************************************************************************/ |
|
|
30 | |
|
|
31 | /* up to two temporary buffers */ |
|
|
32 | static void * |
|
|
33 | tmpbuf (size_t size) |
|
|
34 | { |
|
|
35 | static int idx; |
|
|
36 | static void *buf [2]; |
|
|
37 | static size_t len [2]; |
|
|
38 | |
|
|
39 | idx ^= 1; |
|
|
40 | |
|
|
41 | if (len [idx] < size) |
|
|
42 | { |
|
|
43 | free (buf [idx]); |
|
|
44 | len [idx] = ((size + 31) & ~4095) + 4096 - 32; |
|
|
45 | buf [idx] = malloc (len [idx]); |
|
|
46 | } |
|
|
47 | |
|
|
48 | return buf [idx]; |
|
|
49 | } |
|
|
50 | |
|
|
51 | /*****************************************************************************/ |
25 | |
52 | |
26 | typedef struct |
53 | typedef struct |
27 | { |
54 | { |
28 | IV iv; |
55 | IV iv; |
29 | const char *name; |
56 | const char *name; |
… | |
… | |
63 | }; |
90 | }; |
64 | |
91 | |
65 | return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); |
92 | return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); |
66 | } |
93 | } |
67 | |
94 | |
|
|
95 | /*****************************************************************************/ |
|
|
96 | |
|
|
97 | static cl_int last_error; |
|
|
98 | |
68 | #define FAIL(name,res) \ |
99 | #define FAIL(name,err) \ |
69 | croak ("cl" # name ": %s", err2str (res)); |
100 | croak ("cl" # name ": %s", err2str (last_error = err)); |
70 | |
101 | |
71 | #define NEED_SUCCESS(name,args) \ |
102 | #define NEED_SUCCESS(name,args) \ |
72 | do { \ |
103 | do { \ |
73 | cl_int res = cl ## name args; \ |
104 | cl_int res = cl ## name args; \ |
74 | \ |
105 | \ |
75 | if (res) \ |
106 | if (res) \ |
76 | FAIL (name, res); \ |
107 | FAIL (name, res); \ |
77 | } while (0) |
108 | } while (0) |
78 | |
109 | |
|
|
110 | /*****************************************************************************/ |
|
|
111 | |
79 | #define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr) |
112 | #define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr) |
80 | #define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr)) |
113 | #define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr)) |
81 | |
114 | |
82 | /*TODO*/ |
115 | static void * |
83 | #define EVENT_LIST(items,count) cl_uint event_list_count = 0; cl_event *event_list_ptr = 0 |
116 | SvPTROBJ (const char *func, const char *svname, SV *sv, const char *pkg) |
|
|
117 | { |
|
|
118 | if (SvROK (sv) && sv_derived_from (sv, pkg)) |
|
|
119 | return (void *)SvIV (SvRV (sv)); |
|
|
120 | |
|
|
121 | croak ("%s: %s is not of type %s", func, svname, pkg); |
|
|
122 | } |
|
|
123 | |
|
|
124 | /*****************************************************************************/ |
|
|
125 | |
|
|
126 | static cl_event * |
|
|
127 | event_list (SV **items, int count) |
|
|
128 | { |
|
|
129 | cl_event *list = tmpbuf (sizeof (cl_event) * count); |
|
|
130 | |
|
|
131 | while (count--) |
|
|
132 | list [count] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); |
|
|
133 | |
|
|
134 | return list; |
|
|
135 | } |
|
|
136 | |
|
|
137 | #define EVENT_LIST(items,count) \ |
|
|
138 | cl_uint event_list_count = (count); \ |
|
|
139 | cl_event *event_list_ptr = event_list (&ST (items), event_list_count) |
84 | |
140 | |
85 | #define INFO(class) \ |
141 | #define INFO(class) \ |
86 | { \ |
142 | { \ |
87 | size_t size; \ |
143 | size_t size; \ |
88 | SV *sv; \ |
144 | SV *sv; \ |
… | |
… | |
94 | SvCUR_set (sv, size); \ |
150 | SvCUR_set (sv, size); \ |
95 | NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \ |
151 | NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \ |
96 | XPUSHs (sv); \ |
152 | XPUSHs (sv); \ |
97 | } |
153 | } |
98 | |
154 | |
99 | static void * |
|
|
100 | SvPTROBJ (const char *func, const char *svname, SV *sv, const char *pkg) |
|
|
101 | { |
|
|
102 | if (SvROK (sv) && sv_derived_from (sv, pkg)) |
|
|
103 | return (void *)SvIV (SvRV (sv)); |
|
|
104 | |
|
|
105 | croak ("%s: %s is not of type %s", func, svname, pkg); |
|
|
106 | } |
|
|
107 | |
|
|
108 | static void * |
|
|
109 | tmpbuf (size_t size) |
|
|
110 | { |
|
|
111 | static void *buf; |
|
|
112 | static size_t len; |
|
|
113 | |
|
|
114 | if (len < size) |
|
|
115 | { |
|
|
116 | free (buf); |
|
|
117 | len = ((size + 31) & ~4095) + 4096 - 32; |
|
|
118 | buf = malloc (len); |
|
|
119 | } |
|
|
120 | |
|
|
121 | return buf; |
|
|
122 | } |
|
|
123 | |
|
|
124 | MODULE = OpenCL PACKAGE = OpenCL |
155 | MODULE = OpenCL PACKAGE = OpenCL |
125 | |
156 | |
126 | PROTOTYPES: ENABLE |
157 | PROTOTYPES: ENABLE |
127 | |
158 | |
128 | BOOT: |
159 | BOOT: |
129 | { |
160 | { |
130 | HV *stash = gv_stashpv ("OpenCL", 1); |
161 | HV *stash = gv_stashpv ("OpenCL", 1); |
131 | static const ivstr *civ, const_iv[] = { |
162 | static const ivstr *civ, const_iv[] = { |
132 | { sizeof (cl_char ), "SIZEOF_CHAR" }, |
163 | { sizeof (cl_char ), "SIZEOF_CHAR" }, |
133 | { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, |
164 | { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, |
134 | { sizeof (cl_short ), "SIZEOF_SHORT" }, |
165 | { sizeof (cl_short ), "SIZEOF_SHORT" }, |
135 | { sizeof (cl_ushort), "SIZEOF_USHORT"}, |
166 | { sizeof (cl_ushort), "SIZEOF_USHORT" }, |
136 | { sizeof (cl_int ), "SIZEOF_INT" }, |
167 | { sizeof (cl_int ), "SIZEOF_INT" }, |
137 | { sizeof (cl_uint ), "SIZEOF_UINT" }, |
168 | { sizeof (cl_uint ), "SIZEOF_UINT" }, |
138 | { sizeof (cl_long ), "SIZEOF_LONG" }, |
169 | { sizeof (cl_long ), "SIZEOF_LONG" }, |
139 | { sizeof (cl_ulong ), "SIZEOF_ULONG" }, |
170 | { sizeof (cl_ulong ), "SIZEOF_ULONG" }, |
140 | { sizeof (cl_half ), "SIZEOF_HALF" }, |
171 | { sizeof (cl_half ), "SIZEOF_HALF" }, |
141 | { sizeof (cl_float ), "SIZEOF_FLOAT" }, |
172 | { sizeof (cl_float ), "SIZEOF_FLOAT" }, |
|
|
173 | { sizeof (cl_double), "SIZEOF_DOUBLE" }, |
142 | #include "constiv.h" |
174 | #include "constiv.h" |
143 | }; |
175 | }; |
144 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
176 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
145 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
177 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
146 | } |
178 | } |
147 | |
179 | |
|
|
180 | cl_int |
|
|
181 | errno () |
|
|
182 | CODE: |
|
|
183 | errno = last_error; |
|
|
184 | |
148 | const char * |
185 | const char * |
149 | err2str (cl_int err) |
186 | err2str (cl_int err) |
150 | |
187 | |
151 | const char * |
188 | const char * |
152 | enum2str (cl_uint value) |
189 | enum2str (cl_uint value) |
… | |
… | |
167 | for (i = 0; i < count; ++i) |
204 | for (i = 0; i < count; ++i) |
168 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); |
205 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); |
169 | } |
206 | } |
170 | |
207 | |
171 | void |
208 | void |
172 | context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT) |
209 | context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
173 | PPCODE: |
210 | PPCODE: |
174 | { |
211 | { |
175 | cl_int res; |
212 | cl_int res; |
176 | cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); |
213 | cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); |
177 | |
214 | |
… | |
… | |
214 | for (i = 0; i < count; ++i) |
251 | for (i = 0; i < count; ++i) |
215 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
252 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
216 | } |
253 | } |
217 | |
254 | |
218 | void |
255 | void |
219 | context_from_type_simple (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_DEFAULT) |
256 | context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
220 | PPCODE: |
257 | PPCODE: |
221 | { |
258 | { |
222 | cl_int res; |
259 | cl_int res; |
223 | cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 }; |
260 | cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 }; |
224 | cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res); |
261 | cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res); |
… | |
… | |
235 | info (OpenCL::Device this, cl_device_info name) |
272 | info (OpenCL::Device this, cl_device_info name) |
236 | PPCODE: |
273 | PPCODE: |
237 | INFO (Device) |
274 | INFO (Device) |
238 | |
275 | |
239 | void |
276 | void |
240 | context_simple (OpenCL::Device this) |
277 | context (OpenCL::Device this, FUTURE properties = 0, FUTURE notify = 0) |
241 | PPCODE: |
278 | PPCODE: |
242 | { |
279 | { |
243 | cl_int res; |
280 | cl_int res; |
244 | cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res); |
281 | cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res); |
245 | |
282 | |
… | |
… | |
260 | info (OpenCL::Context this, cl_context_info name) |
297 | info (OpenCL::Context this, cl_context_info name) |
261 | PPCODE: |
298 | PPCODE: |
262 | INFO (Context) |
299 | INFO (Context) |
263 | |
300 | |
264 | void |
301 | void |
265 | command_queue_simple (OpenCL::Context this, OpenCL::Device device) |
302 | queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0) |
266 | PPCODE: |
303 | PPCODE: |
267 | { |
304 | { |
268 | cl_int res; |
305 | cl_int res; |
269 | cl_command_queue queue = clCreateCommandQueue (this, device, 0, &res); |
306 | cl_command_queue queue = clCreateCommandQueue (this, device, properties, &res); |
270 | |
307 | |
271 | if (res) |
308 | if (res) |
272 | FAIL (CreateCommandQueue, res); |
309 | FAIL (CreateCommandQueue, res); |
273 | |
310 | |
274 | XPUSH_NEW_OBJ ("OpenCL::Queue", queue); |
311 | XPUSH_NEW_OBJ ("OpenCL::Queue", queue); |
|
|
312 | } |
|
|
313 | |
|
|
314 | void |
|
|
315 | user_event (OpenCL::Context this) |
|
|
316 | PPCODE: |
|
|
317 | { |
|
|
318 | cl_int res; |
|
|
319 | cl_event ev = clCreateUserEvent (this, &res); |
|
|
320 | |
|
|
321 | if (res) |
|
|
322 | FAIL (CreateUserevent, res); |
|
|
323 | |
|
|
324 | XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); |
275 | } |
325 | } |
276 | |
326 | |
277 | void |
327 | void |
278 | buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) |
328 | buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) |
279 | PPCODE: |
329 | PPCODE: |
… | |
… | |
579 | cl_event ev = 0; |
629 | cl_event ev = 0; |
580 | size_t *gwo = 0, *gws, *lws = 0; |
630 | size_t *gwo = 0, *gws, *lws = 0; |
581 | int gws_len; |
631 | int gws_len; |
582 | size_t *lists; |
632 | size_t *lists; |
583 | int i; |
633 | int i; |
584 | EVENT_LIST (2, items - 2); |
634 | EVENT_LIST (5, items - 5); |
585 | |
635 | |
586 | if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV) |
636 | if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV) |
587 | croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference"); |
637 | croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference"); |
588 | |
638 | |
589 | gws_len = AvFILLp (SvRV (global_work_size)) + 1; |
639 | gws_len = AvFILLp (SvRV (global_work_size)) + 1; |
… | |
… | |
789 | set_float (OpenCL::Kernel this, cl_uint idx, cl_float value) |
839 | set_float (OpenCL::Kernel this, cl_uint idx, cl_float value) |
790 | CODE: |
840 | CODE: |
791 | clSetKernelArg (this, idx, sizeof (value), &value); |
841 | clSetKernelArg (this, idx, sizeof (value), &value); |
792 | |
842 | |
793 | void |
843 | void |
|
|
844 | set_double (OpenCL::Kernel this, cl_uint idx, cl_double value) |
|
|
845 | CODE: |
|
|
846 | clSetKernelArg (this, idx, sizeof (value), &value); |
|
|
847 | |
|
|
848 | void |
794 | set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value) |
849 | set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value) |
795 | CODE: |
850 | CODE: |
796 | clSetKernelArg (this, idx, sizeof (value), &value); |
851 | clSetKernelArg (this, idx, sizeof (value), &value); |
797 | |
852 | |
798 | void |
853 | void |
… | |
… | |
835 | void |
890 | void |
836 | wait (OpenCL::Event this) |
891 | wait (OpenCL::Event this) |
837 | CODE: |
892 | CODE: |
838 | clWaitForEvents (1, &this); |
893 | clWaitForEvents (1, &this); |
839 | |
894 | |
|
|
895 | MODULE = OpenCL PACKAGE = OpenCL::UserEvent |
|
|
896 | |
|
|
897 | void |
|
|
898 | set_status (OpenCL::UserEvent this, cl_int execution_status) |
|
|
899 | CODE: |
|
|
900 | clSetUserEventStatus (this, execution_status); |
|
|
901 | |