ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
(Generate patch)

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.4 by root, Tue Nov 15 21:13:42 2011 UTC vs.
Revision 1.5 by root, Wed Nov 16 00:35:30 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines