vigra - vigra/multi_opencl.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

multi_opencl.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2004 by Ullrich Koethe */
4 /* Copyright 2011-2011 by Michael Tesch */
5 /* */
6 /* This file is part of the VIGRA computer vision library. */
7 /* The VIGRA Website is */
8 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
9 /* Please direct questions, bug reports, and contributions to */
10 /* ullrich.koethe@iwr.uni-heidelberg.de or */
11 /* vigra@informatik.uni-hamburg.de */
12 /* */
13 /* Permission is hereby granted, free of charge, to any person */
14 /* obtaining a copy of this software and associated documentation */
15 /* files (the "Software"), to deal in the Software without */
16 /* restriction, including without limitation the rights to use, */
17 /* copy, modify, merge, publish, distribute, sublicense, and/or */
18 /* sell copies of the Software, and to permit persons to whom the */
19 /* Software is furnished to do so, subject to the following */
20 /* conditions: */
21 /* */
22 /* The above copyright notice and this permission notice shall be */
23 /* included in all copies or substantial portions of the */
24 /* Software. */
25 /* */
26 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
27 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
28 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
29 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
30 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
31 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
32 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
33 /* OTHER DEALINGS IN THE SOFTWARE. */
34 /* */
35 /************************************************************************/
36
37 #ifndef VIGRA_OPENCL_HXX
38 #define VIGRA_OPENCL_HXX
39
40 #include "numerictraits.hxx"
41
42 #ifdef __APPLE__
43 #include <OpenCL/opencl.h>
44 #else
45 #include <CL/opencl.h>
46 #endif
47
48 namespace vigra {
49
50 /********************************************************/
51 /* */
52 /* NumericTraits */
53 /* */
54 /********************************************************/
55
56 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
57
58 #define VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(basetype, n) \
59 template<> \
60 struct NumericTraits< basetype##n > \
61 { \
62 typedef basetype##n Type; \
63 typedef Type Promote; \
64 typedef Type UnsignedPromote; \
65 typedef Type RealPromote; \
66 typedef std::complex<Type> ComplexPromote; \
67 typedef basetype ValueType; \
68 \
69 typedef VigraFalseType isIntegral; \
70 typedef VigraFalseType isScalar; \
71 typedef typename NumericTraits<ValueType>::isSigned isSigned; \
72 typedef VigraFalseType isOrdered; \
73 typedef typename NumericTraits<ValueType>::isComplex isComplex; \
74 \
75 static Type zero() { Type x; bzero(&x, sizeof(x)); return x; } \
76 static Type one() { Type x = {{1}}; return x; } \
77 static Type nonZero() { return one(); } \
78 \
79 static Promote toPromote(Type const & v) { return v; } \
80 static Type fromPromote(Promote const & v) { return v; } \
81 static Type fromRealPromote(RealPromote v) { return v; } \
82 }
83
84 #define VIGRA_OPENCL_VECTYPEN_REAL_TRAITS(basetype, n) \
85 template<> \
86 struct NumericTraits< basetype##n > \
87 { \
88 typedef basetype##n Type; \
89 typedef Type Promote; \
90 typedef Type UnsignedPromote; \
91 typedef Type RealPromote; \
92 typedef std::complex<Type> ComplexPromote; \
93 typedef basetype ValueType; \
94 \
95 typedef VigraFalseType isIntegral; \
96 typedef VigraFalseType isScalar; \
97 typedef typename NumericTraits<ValueType>::isSigned isSigned; \
98 typedef VigraFalseType isOrdered; \
99 typedef typename NumericTraits<ValueType>::isComplex isComplex; \
100 \
101 static Type zero() { Type x; bzero(&x, sizeof(x)); return x; } \
102 static Type one() { Type x = {{1}}; return x; } \
103 static Type nonZero() { return one(); } \
104 static Type epsilon() { Type x; x.x = NumericTraits<ValueType>::epsilon(); return x; } \
105 static Type smallestPositive() { Type x; x.x = NumericTraits<ValueType>::smallestPositive(); return x; } \
106 \
107 static Promote toPromote(Type const & v) { return v; } \
108 static Type fromPromote(Promote const & v) { return v; } \
109 static Type fromRealPromote(RealPromote v) { return v; } \
110 }
111
112 /// \todo - fix one() - maybe with .hi and .lo accessors?
113
114 #define VIGRA_OPENCL_VECN_TRAITS(n) \
115 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_char, n); \
116 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_uchar, n); \
117 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_short, n); \
118 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_ushort, n); \
119 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_int, n); \
120 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_uint, n); \
121 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_long, n); \
122 VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_ulong, n); \
123 VIGRA_OPENCL_VECTYPEN_REAL_TRAITS(cl_float, n); \
124 VIGRA_OPENCL_VECTYPEN_REAL_TRAITS(cl_double, n);
125
126 VIGRA_OPENCL_VECN_TRAITS(2);
127 VIGRA_OPENCL_VECN_TRAITS(3);
128 //VIGRA_OPENCL_VECN_TRAITS(4); // cl_type4 is the same as cl_type3
129 VIGRA_OPENCL_VECN_TRAITS(8);
130 VIGRA_OPENCL_VECN_TRAITS(16);
131
132 #undef VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS
133 #undef VIGRA_OPENCL_VECTYPEN_REAL_TRAITS
134 #undef VIGRA_OPENCL_VECN_TRAITS
135
136 /** \todo looks like the windows CL/cl_platform.h does signed/unsigned
137 * strangely, so that the signed properties may not have the right
138 * NumericalTraits::isSigned -- not sure if there's a reason for that.
139 */
140
141
142 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
143
144 /********************************************************/
145 /* */
146 /* SquareRootTraits */
147 /* */
148 /********************************************************/
149
150 /********************************************************/
151 /* */
152 /* NormTraits */
153 /* */
154 /********************************************************/
155
156 #if 0
157 template<>
158 struct NormTraits<fftw_complex>
159 {
160 typedef fftw_complex Type;
161 typedef fftw_real SquaredNormType;
162 typedef fftw_real NormType;
163 };
164
165 template<class Real>
166 struct NormTraits<FFTWComplex<Real> >
167 {
168 typedef FFTWComplex<Real> Type;
169 typedef typename Type::SquaredNormType SquaredNormType;
170 typedef typename Type::NormType NormType;
171 };
172 #endif
173
174 /********************************************************/
175 /* */
176 /* PromoteTraits */
177 /* */
178 /********************************************************/
179
180 #if 0
181 template<class T>
182 struct CanSkipInitialization<std::complex<T> >
183 {
184 typedef typename CanSkipInitialization<T>::type type;
185 static const bool value = type::asBool;
186 };
187 #endif
188
189 /********************************************************/
190 /* */
191 /* multi_math */
192 /* */
193 /********************************************************/
194
195 namespace multi_math {
196
197 /// \todo !
198
199 /** OpenCL 1.1 [6.2] - Convert operators */
200 /** OpenCL 1.1 [6.3] - Scalar/vector math operators */
201
202 /** OpenCL 1.1 [6.11.2] - Math Built-in Functions */
203 /** OpenCL 1.1 [6.11.3] - Integer Built-in Functions */
204 /** OpenCL 1.1 [6.11.4] - Common Built-in Functions */
205 /** OpenCL 1.1 [6.11.5] - Geometric Built-in Functions */
206 /** OpenCL 1.1 [6.11.6] - Relational Built-in Functions */
207 /** OpenCL 1.1 [6.11.7] - Vector Data Load/Store Built-in Functions */
208
209 /** OpenCL 1.1 [6.11.12] - Misc Vector Built-in Functions */
210
211 /** OpenCL 1.1 [6.11.12] - Image Read and Write Built-in Functions */
212
213
214 } // namespace multi_math
215
216 /********************************************************/
217 /* */
218 /* Channel Accessors */
219 /* */
220 /********************************************************/
221
222 /** \addtogroup DataAccessors
223 */
224 //@{
225 /** \defgroup OpenCL-Accessors Accessors for OpenCL types
226
227 Encapsulate access to members of OpenCL vector types.
228
229
230 <b>\#include</b> <vigra/multi_opencl.hxx>
231
232 OpenCL 1.1 [6.1.7] - Vector Components
233
234 - cl_TYPE2Accessor_x
235 - cl_TYPE2Accessor_y
236 - cl_TYPE2Accessor_s0
237 - cl_TYPE2Accessor_s1
238
239 - cl_TYPE2WriteAccessor_x
240 - cl_TYPE2WriteAccessor_y
241 - cl_TYPE2WriteAccessor_s0
242 - cl_TYPE2WriteAccessor_s1
243
244 - cl_TYPE3Accessor_x
245 - cl_TYPE3Accessor_y
246 - cl_TYPE3Accessor_z
247 - cl_TYPE3Accessor_s0
248 - cl_TYPE3Accessor_s1
249 - cl_TYPE3Accessor_s2
250
251 - cl_TYPE3WriteAccessor_x
252 - cl_TYPE3WriteAccessor_y
253 - cl_TYPE3WriteAccessor_z
254 - cl_TYPE3WriteAccessor_s0
255 - cl_TYPE3WriteAccessor_s1
256 - ...
257
258 where TYPE is one of {char, uchar, short, ushort, int, uint, long, ulong, float, double }
259
260 For example:
261
262 \code
263
264 #include <vigra/multi_opencl.hxx>
265
266 MultiArrayView<2, cl_double3 > dataView = ...;
267
268 vigra::FindMinMax<double> minmax;
269 vigra::inspectMultiArray(srcMultiArrayRange(dataView, cl_double3Accessor_z()), minmax);
270 std::cout << "range of .z: " << minmax.min << " - " << minmax.max;
271
272 \endcode
273 */
274 //@{
275 /**
276 \class cl_charNAccessor_COMP
277
278 access the first component.
279
280 \class cl_TYPE3WriteAccessor_s1
281
282 access the second component.
283
284 \class cl_TYPE3WriteAccessor_s2
285
286 access the third component.
287 */
288
289 //@}
290 //@}
291
292 #define VIGRA_OPENCL_TYPE_ACCESSOR(basetype, n, NTH) \
293 class basetype##n##Accessor_##NTH \
294 { \
295 public: \
296 /** The accessor's value type. */ \
297 typedef NumericTraits< basetype##n >::ValueType value_type; \
298 \
299 /** Read component at iterator position. */ \
300 template <class ITERATOR> \
301 value_type operator()(ITERATOR const & i) const { \
302 return (*i).NTH; \
303 } \
304 \
305 /** Read component at offset from iterator position. */ \
306 template <class ITERATOR, class DIFFERENCE> \
307 value_type operator()(ITERATOR const & i, DIFFERENCE d) const { \
308 return i[d].NTH; \
309 } \
310 \
311 /** Write component at iterator position from a scalar. */ \
312 template <class ITERATOR> \
313 void set(value_type const & v, ITERATOR const & i) const { \
314 (*i).NTH = v; \
315 } \
316 \
317 /** Write component at offset from iterator position from a scalar. */ \
318 template <class ITERATOR, class DIFFERENCE> \
319 void set(value_type const & v, ITERATOR const & i, DIFFERENCE d) const { \
320 i[d].NTH = v; \
321 } \
322 \
323 /** Write component at iterator position into a scalar. */ \
324 template <class R, class ITERATOR> \
325 void set(FFTWComplex<R> const & v, ITERATOR const & i) const { \
326 *i = v.NTH; \
327 } \
328 \
329 /** Write component at offset from iterator position into a scalar. */ \
330 template <class R, class ITERATOR, class DIFFERENCE> \
331 void set(FFTWComplex<R> const & v, ITERATOR const & i, DIFFERENCE d) const { \
332 i[d] = v.NTH; \
333 } \
334 }; \
335 class basetype##n##WriteAccessor_##NTH \
336 : public basetype##n##Accessor_##NTH \
337 { \
338 public: \
339 /** The accessor's value type. */ \
340 typedef NumericTraits< basetype##n >::ValueType value_type; \
341 \
342 /** Write component at iterator position. */ \
343 template <class ITERATOR> \
344 void set(value_type const & v, ITERATOR const & i) const { \
345 (*i).NTH = v; \
346 } \
347 \
348 /** Write component at offset from iterator position. */ \
349 template <class ITERATOR, class DIFFERENCE> \
350 void set(value_type const & v, ITERATOR const & i, DIFFERENCE d) const { \
351 i[d].NTH = v; \
352 } \
353 }
354
355 #define VIGRA_OPENCL_TYPE2_ACCESSORS(basetype) \
356 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, s0); \
357 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, s1); \
358 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, x); \
359 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, y);
360
361 #define VIGRA_OPENCL_TYPE3_ACCESSORS(basetype) \
362 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, s0); \
363 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, s1); \
364 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, s2); \
365 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, x); \
366 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, y); \
367 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, z);
368
369 #define VIGRA_OPENCL_TYPE4_ACCESSORS(basetype) \
370 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s0); \
371 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s1); \
372 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s2); \
373 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s3); \
374 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, x); \
375 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, y); \
376 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, z); \
377 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, w);
378
379 #define VIGRA_OPENCL_TYPE8_ACCESSORS(basetype) \
380 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s0); \
381 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s1); \
382 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s2); \
383 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s3); \
384 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s4); \
385 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s5); \
386 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s6); \
387 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s7); \
388 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s8);
389
390 #define VIGRA_OPENCL_TYPE16_ACCESSORS(basetype) \
391 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s0); \
392 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s1); \
393 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s2); \
394 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s3); \
395 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s4); \
396 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s5); \
397 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s6); \
398 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s7); \
399 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s8); \
400 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sa); \
401 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sb); \
402 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sc); \
403 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sd); \
404 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, se); \
405 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sf); \
406 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sA); \
407 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sB); \
408 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sC); \
409 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sD); \
410 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sE); \
411 VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sF);
412
413 /// \todo figure out half (.hi .lo, .even .odd) and other odd-sized accessors
414
415 #define VIGRA_OPENCL_ACCESSORS(basetype) \
416 VIGRA_OPENCL_TYPE2_ACCESSORS(basetype); \
417 VIGRA_OPENCL_TYPE3_ACCESSORS(basetype); \
418 VIGRA_OPENCL_TYPE4_ACCESSORS(basetype); \
419 VIGRA_OPENCL_TYPE8_ACCESSORS(basetype); \
420 VIGRA_OPENCL_TYPE16_ACCESSORS(basetype);
421
422 VIGRA_OPENCL_ACCESSORS(cl_char);
423 VIGRA_OPENCL_ACCESSORS(cl_uchar);
424 VIGRA_OPENCL_ACCESSORS(cl_short);
425 VIGRA_OPENCL_ACCESSORS(cl_ushort);
426 VIGRA_OPENCL_ACCESSORS(cl_int);
427 VIGRA_OPENCL_ACCESSORS(cl_uint);
428 VIGRA_OPENCL_ACCESSORS(cl_long);
429 VIGRA_OPENCL_ACCESSORS(cl_ulong);
430 VIGRA_OPENCL_ACCESSORS(cl_float);
431 VIGRA_OPENCL_ACCESSORS(cl_double);
432
433 #undef VIGRA_OPENCL_TYPE_ACCESSOR
434 #undef VIGRA_OPENCL_TYPE2_ACCESSORS
435 #undef VIGRA_OPENCL_TYPE3_ACCESSORS
436 #undef VIGRA_OPENCL_TYPE4_ACCESSORS
437 #undef VIGRA_OPENCL_TYPE8_ACCESSORS
438 #undef VIGRA_OPENCL_TYPE16_ACCESSORS
439 #undef VIGRA_OPENCL_ACCESSORS
440
441 } // namespace vigra
442
443 #endif // VIGRA_OPENCL_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Mon Nov 18 2013)