0
|
1 /**
|
|
2 *
|
|
3 */
|
|
4 module opencl.wrapper;
|
|
5
|
|
6 import opencl.error;
|
3
|
7 import opencl.c.cl;
|
0
|
8
|
|
9 // alternate Info getter functions
|
|
10 private alias extern(C) cl_int function(const(void)*, const(void*), cl_uint, size_t, void*, size_t*) Func;
|
|
11
|
|
12 /// abstract base class
|
|
13 abstract class CLWrapper(T, alias infoFunction)
|
|
14 {
|
|
15 protected:
|
|
16 T _object = null;
|
|
17
|
|
18 // should only be used inside here
|
|
19 package T getObject()
|
|
20 {
|
|
21 return _object;
|
|
22 }
|
|
23
|
|
24 // used for all non-array types
|
|
25 T getInfo(T)(cl_uint infoname, Func altFunction = null, cl_device_id device = null)
|
|
26 {
|
|
27 assert(_object !is null);
|
|
28 size_t needed;
|
|
29 cl_int res;
|
|
30
|
|
31 // get number of needed memory
|
|
32 if (altFunction != null && device != null)
|
|
33 res = altFunction(_object, device, infoname, 0, null, &needed);
|
|
34 else
|
|
35 res = infoFunction(_object, infoname, 0, null, &needed);
|
|
36
|
|
37 // error checking
|
|
38 if (res != CL_SUCCESS)
|
|
39 throw new CLException(res);
|
|
40
|
|
41 assert(needed == T.sizeof); // TODO:
|
|
42
|
|
43 T info;
|
|
44
|
|
45 // get actual data
|
|
46 if (altFunction != null && device != null)
|
|
47 res = altFunction(_object, device, infoname, T.sizeof, &info, null);
|
|
48 else
|
|
49 res = infoFunction(_object, infoname, T.sizeof, &info, null);
|
|
50
|
|
51 // error checking
|
|
52 if (res != CL_SUCCESS)
|
|
53 throw new CLException(res);
|
|
54
|
|
55 return info;
|
|
56 }
|
|
57
|
|
58 // helper function for all OpenCL Get*Info functions
|
|
59 // used for all array return types
|
|
60 T[] getArrayInfo(T)(cl_uint infoname, Func altFunction = null, cl_device_id device = null)
|
|
61 {
|
|
62 assert(_object !is null);
|
|
63 size_t needed;
|
|
64 cl_int res;
|
|
65
|
|
66 // get number of needed memory
|
|
67 if (altFunction != null && device != null)
|
|
68 res = altFunction(_object, device, infoname, 0, null, &needed);
|
|
69 else
|
|
70 res = infoFunction(_object, infoname, 0, null, &needed);
|
|
71
|
|
72 // error checking
|
|
73 if (res != CL_SUCCESS)
|
|
74 throw new CLException(res);
|
|
75
|
|
76 auto buffer = new T[needed];
|
|
77
|
|
78 // get actual data
|
|
79 if (altFunction != null && device != null)
|
|
80 res = altFunction(_object, device, infoname, buffer.length, cast(void*)buffer.ptr, null);
|
|
81 else
|
|
82 res = infoFunction(_object, infoname, buffer.length, cast(void*)buffer.ptr, null);
|
|
83
|
|
84 // error checking
|
|
85 if (res != CL_SUCCESS)
|
|
86 throw new CLException(res);
|
|
87
|
|
88 return buffer;
|
|
89 }
|
|
90
|
|
91 string getStringInfo(cl_uint infoname, Func altFunction = null, cl_device_id device = null)
|
|
92 {
|
|
93 return cast(string) getArrayInfo!(char)(infoname, altFunction, device);
|
|
94 }
|
|
95
|
|
96 // static cl_int getInfo(Arg0, Arg1)(Arg0 arg0, Arg1)
|
|
97
|
|
98 public:
|
|
99 this() {}
|
|
100 this(T obj)
|
|
101 {
|
|
102 _object = obj;
|
|
103 }
|
|
104 } |