vigra - vigra/multi_opencl.hxx Source File

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

vigra/multi_opencl.hxx |

    00001 /************************************************************************/
    00002 /*                                                                      */
    00003 /*               Copyright 1998-2004 by Ullrich Koethe                  */
    00004 /*               Copyright 2011-2011 by Michael Tesch                   */
    00005 /*                                                                      */
    00006 /*    This file is part of the VIGRA computer vision library.           */
    00007 /*    The VIGRA Website is                                              */
    00008 /*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
    00009 /*    Please direct questions, bug reports, and contributions to        */
    00010 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
    00011 /*        vigra@informatik.uni-hamburg.de                               */
    00012 /*                                                                      */
    00013 /*    Permission is hereby granted, free of charge, to any person       */
    00014 /*    obtaining a copy of this software and associated documentation    */
    00015 /*    files (the "Software"), to deal in the Software without           */
    00016 /*    restriction, including without limitation the rights to use,      */
    00017 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
    00018 /*    sell copies of the Software, and to permit persons to whom the    */
    00019 /*    Software is furnished to do so, subject to the following          */
    00020 /*    conditions:                                                       */
    00021 /*                                                                      */
    00022 /*    The above copyright notice and this permission notice shall be    */
    00023 /*    included in all copies or substantial portions of the             */
    00024 /*    Software.                                                         */
    00025 /*                                                                      */
    00026 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
    00027 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
    00028 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
    00029 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
    00030 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
    00031 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
    00032 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
    00033 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
    00034 /*                                                                      */
    00035 /************************************************************************/
    00036
    00037 #ifndef VIGRA_OPENCL_HXX
    00038 #define VIGRA_OPENCL_HXX
    00039
    00040 #include "numerictraits.hxx"
    00041
    00042 #ifdef __APPLE__
    00043 #include <OpenCL/opencl.h>
    00044 #else
    00045 #include <CL/opencl.h>
    00046 #endif
    00047
    00048 namespace vigra {
    00049
    00050 /********************************************************/
    00051 /*                                                      */
    00052 /*                      NumericTraits                   */
    00053 /*                                                      */
    00054 /********************************************************/
    00055
    00056 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
    00057
    00058 #define VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(basetype, n)               \
    00059   template<>                                                            \
    00060   struct NumericTraits< basetype##n >                                   \
    00061   {                                                                     \
    00062     typedef basetype##n Type;                                           \
    00063     typedef Type Promote;                                               \
    00064     typedef Type UnsignedPromote;                                       \
    00065     typedef Type RealPromote;                                           \
    00066     typedef std::complex<Type> ComplexPromote;                          \
    00067     typedef basetype ValueType;                                         \
    00068                                                                         \
    00069     typedef VigraFalseType isIntegral;                                  \
    00070     typedef VigraFalseType isScalar;                                    \
    00071     typedef typename NumericTraits<ValueType>::isSigned isSigned;       \
    00072     typedef VigraFalseType isOrdered;                                   \
    00073     typedef typename NumericTraits<ValueType>::isComplex isComplex;     \
    00074                                                                         \
    00075     static Type zero() { Type x; bzero(&x, sizeof(x)); return x; }      \
    00076     static Type one() { Type x = {{1}}; return x; }                     \
    00077     static Type nonZero() { return one(); }                             \
    00078                                                                         \
    00079     static Promote toPromote(Type const & v) { return v; }              \
    00080     static Type fromPromote(Promote const & v) { return v; }            \
    00081     static Type fromRealPromote(RealPromote v) { return v; }            \
    00082   }
    00083
    00084 #define VIGRA_OPENCL_VECTYPEN_REAL_TRAITS(basetype, n)                  \
    00085   template<>                                                            \
    00086   struct NumericTraits< basetype##n >                                   \
    00087   {                                                                     \
    00088     typedef basetype##n Type;                                           \
    00089     typedef Type Promote;                                               \
    00090     typedef Type UnsignedPromote;                                       \
    00091     typedef Type RealPromote;                                           \
    00092     typedef std::complex<Type> ComplexPromote;                          \
    00093     typedef basetype ValueType;                                         \
    00094                                                                         \
    00095     typedef VigraFalseType isIntegral;                                  \
    00096     typedef VigraFalseType isScalar;                                    \
    00097     typedef typename NumericTraits<ValueType>::isSigned isSigned;       \
    00098     typedef VigraFalseType isOrdered;                                   \
    00099     typedef typename NumericTraits<ValueType>::isComplex isComplex;     \
    00100                                                                         \
    00101     static Type zero() { Type x; bzero(&x, sizeof(x)); return x; }      \
    00102     static Type one() { Type x = {{1}}; return x; }                     \
    00103     static Type nonZero() { return one(); }                             \
    00104     static Type epsilon() { Type x; x.x = NumericTraits<ValueType>::epsilon(); return x; } \
    00105     static Type smallestPositive() { Type x; x.x = NumericTraits<ValueType>::smallestPositive(); return x; } \
    00106                                                                         \
    00107     static Promote toPromote(Type const & v) { return v; }              \
    00108     static Type fromPromote(Promote const & v) { return v; }            \
    00109     static Type fromRealPromote(RealPromote v) { return v; }            \
    00110   }
    00111
    00112 /// \todo - fix one() - maybe with .hi and .lo accessors?
    00113
    00114 #define VIGRA_OPENCL_VECN_TRAITS(n)                          \
    00115   VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_char, n);          \
    00116     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_uchar, n);       \
    00117     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_short, n);       \
    00118     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_ushort, n);      \
    00119     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_int, n);         \
    00120     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_uint, n);        \
    00121     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_long, n);        \
    00122     VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS(cl_ulong, n);       \
    00123     VIGRA_OPENCL_VECTYPEN_REAL_TRAITS(cl_float, n);          \
    00124     VIGRA_OPENCL_VECTYPEN_REAL_TRAITS(cl_double, n);
    00125
    00126 VIGRA_OPENCL_VECN_TRAITS(2);
    00127 VIGRA_OPENCL_VECN_TRAITS(3);
    00128 //VIGRA_OPENCL_VECN_TRAITS(4); // cl_type4 is the same as cl_type3
    00129 VIGRA_OPENCL_VECN_TRAITS(8);
    00130 VIGRA_OPENCL_VECN_TRAITS(16);
    00131
    00132 #undef VIGRA_OPENCL_VECTYPEN_INTEGER_TRAITS
    00133 #undef VIGRA_OPENCL_VECTYPEN_REAL_TRAITS
    00134 #undef VIGRA_OPENCL_VECN_TRAITS
    00135
    00136 /** \todo looks like the windows CL/cl_platform.h does signed/unsigned
    00137  *      strangely, so that the signed properties may not have the right
    00138  *      NumericalTraits::isSigned -- not sure if there's a reason for that.
    00139  */
    00140
    00141
    00142 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
    00143
    00144 /********************************************************/
    00145 /*                                                      */
    00146 /*                    SquareRootTraits                  */
    00147 /*                                                      */
    00148 /********************************************************/
    00149
    00150 /********************************************************/
    00151 /*                                                      */
    00152 /*                       NormTraits                     */
    00153 /*                                                      */
    00154 /********************************************************/
    00155
    00156 #if 0
    00157 template<>
    00158 struct NormTraits<fftw_complex>
    00159 {
    00160     typedef fftw_complex Type;
    00161     typedef fftw_real    SquaredNormType;
    00162     typedef fftw_real    NormType;
    00163 };
    00164
    00165 template<class Real>
    00166 struct NormTraits<FFTWComplex<Real> >
    00167 {
    00168     typedef FFTWComplex<Real>  Type;
    00169     typedef typename Type::SquaredNormType   SquaredNormType;
    00170     typedef typename Type::NormType   NormType;
    00171 };
    00172 #endif
    00173
    00174 /********************************************************/
    00175 /*                                                      */
    00176 /*                      PromoteTraits                   */
    00177 /*                                                      */
    00178 /********************************************************/
    00179
    00180 #if 0
    00181 template<class T>
    00182 struct CanSkipInitialization<std::complex<T> >
    00183 {
    00184     typedef typename CanSkipInitialization<T>::type type;
    00185     static const bool value = type::asBool;
    00186 };
    00187 #endif
    00188
    00189 /********************************************************/
    00190 /*                                                      */
    00191 /*                      multi_math                      */
    00192 /*                                                      */
    00193 /********************************************************/
    00194
    00195 namespace multi_math {
    00196
    00197 /// \todo !
    00198
    00199 /**     OpenCL 1.1 [6.2] - Convert operators */
    00200 /**     OpenCL 1.1 [6.3] - Scalar/vector math operators */
    00201
    00202 /**     OpenCL 1.1 [6.11.2] - Math Built-in Functions */
    00203 /**     OpenCL 1.1 [6.11.3] - Integer Built-in Functions */
    00204 /**     OpenCL 1.1 [6.11.4] - Common Built-in Functions */
    00205 /**     OpenCL 1.1 [6.11.5] - Geometric Built-in Functions */
    00206 /**     OpenCL 1.1 [6.11.6] - Relational Built-in Functions */
    00207 /**     OpenCL 1.1 [6.11.7] - Vector Data Load/Store Built-in Functions */
    00208
    00209 /**     OpenCL 1.1 [6.11.12] - Misc Vector Built-in Functions */
    00210
    00211 /**     OpenCL 1.1 [6.11.12] - Image Read and Write Built-in Functions */
    00212
    00213
    00214 } // namespace multi_math
    00215
    00216 /********************************************************/
    00217 /*                                                      */
    00218 /*                  Channel Accessors                   */
    00219 /*                                                      */
    00220 /********************************************************/
    00221
    00222 /** \addtogroup DataAccessors
    00223 */
    00224 //@{
    00225 /** \defgroup OpenCL-Accessors Accessors for OpenCL types
    00226
    00227     Encapsulate access to members of OpenCL vector types.
    00228
    00229
    00230     <b>\#include</b> <vigra/multi_opencl.hxx>
    00231
    00232     OpenCL 1.1 [6.1.7] - Vector Components
    00233
    00234     - cl_TYPE2Accessor_x
    00235     - cl_TYPE2Accessor_y
    00236     - cl_TYPE2Accessor_s0
    00237     - cl_TYPE2Accessor_s1
    00238
    00239     - cl_TYPE2WriteAccessor_x
    00240     - cl_TYPE2WriteAccessor_y
    00241     - cl_TYPE2WriteAccessor_s0
    00242     - cl_TYPE2WriteAccessor_s1
    00243
    00244     - cl_TYPE3Accessor_x
    00245     - cl_TYPE3Accessor_y
    00246     - cl_TYPE3Accessor_z
    00247     - cl_TYPE3Accessor_s0
    00248     - cl_TYPE3Accessor_s1
    00249     - cl_TYPE3Accessor_s2
    00250
    00251     - cl_TYPE3WriteAccessor_x
    00252     - cl_TYPE3WriteAccessor_y
    00253     - cl_TYPE3WriteAccessor_z
    00254     - cl_TYPE3WriteAccessor_s0
    00255     - cl_TYPE3WriteAccessor_s1
    00256     - ...
    00257
    00258     where TYPE is one of {char, uchar, short, ushort, int, uint, long, ulong, float, double }
    00259
    00260     For example:
    00261
    00262     \code
    00263
    00264     #include <vigra/multi_opencl.hxx>
    00265
    00266     MultiArrayView<2, cl_double3 > dataView = ...;
    00267
    00268     vigra::FindMinMax<double> minmax;
    00269     vigra::inspectMultiArray(srcMultiArrayRange(dataView, cl_double3Accessor_z()), minmax);
    00270     std::cout << "range of .z: " << minmax.min << " - " << minmax.max;
    00271
    00272     \endcode
    00273 */
    00274 //@{
    00275 /**
    00276    \class cl_charNAccessor_COMP
    00277
    00278    access the first component.
    00279
    00280    \class cl_TYPE3WriteAccessor_s1
    00281
    00282    access the second component.
    00283
    00284    \class cl_TYPE3WriteAccessor_s2
    00285
    00286    access the third component.
    00287 */
    00288
    00289 //@}
    00290 //@}
    00291
    00292 #define VIGRA_OPENCL_TYPE_ACCESSOR(basetype, n, NTH) \
    00293   class basetype##n##Accessor_##NTH                  \
    00294   {                                                             \
    00295   public:                                                       \
    00296     /** The accessor's value type. */                           \
    00297     typedef NumericTraits< basetype##n >::ValueType value_type; \
    00298                                                                 \
    00299     /** Read component at iterator position. */                 \
    00300     template <class ITERATOR>                                   \
    00301       value_type operator()(ITERATOR const & i) const {         \
    00302       return (*i).NTH;                                          \
    00303     }                                                           \
    00304                                                                 \
    00305     /** Read component at offset from iterator position. */             \
    00306     template <class ITERATOR, class DIFFERENCE>                         \
    00307       value_type operator()(ITERATOR const & i, DIFFERENCE d) const {   \
    00308       return i[d].NTH;                                                  \
    00309     }                                                                   \
    00310                                                                         \
    00311     /** Write component at iterator position from a scalar. */          \
    00312     template <class ITERATOR>                                           \
    00313       void set(value_type const & v, ITERATOR const & i) const {        \
    00314       (*i).NTH = v;                                                     \
    00315     }                                                                   \
    00316                                                                         \
    00317     /** Write component at offset from iterator position from a scalar. */ \
    00318     template <class ITERATOR, class DIFFERENCE>                         \
    00319       void set(value_type const & v, ITERATOR const & i, DIFFERENCE d) const { \
    00320       i[d].NTH = v;                                                     \
    00321     }                                                                   \
    00322                                                                         \
    00323     /** Write component at iterator position into a scalar. */          \
    00324     template <class R, class ITERATOR>                                  \
    00325       void set(FFTWComplex<R> const & v, ITERATOR const & i) const {    \
    00326       *i = v.NTH;                                                       \
    00327     }                                                                   \
    00328                                                                         \
    00329     /** Write component at offset from iterator position into a scalar. */ \
    00330     template <class R, class ITERATOR, class DIFFERENCE>                \
    00331       void set(FFTWComplex<R> const & v, ITERATOR const & i, DIFFERENCE d) const { \
    00332       i[d] = v.NTH;                                                     \
    00333     }                                                                   \
    00334   };                                                                    \
    00335   class basetype##n##WriteAccessor_##NTH                                \
    00336     : public basetype##n##Accessor_##NTH                                \
    00337   {                                                                     \
    00338   public:                                                               \
    00339     /** The accessor's value type. */                                   \
    00340     typedef NumericTraits< basetype##n >::ValueType value_type;         \
    00341                                                                         \
    00342     /** Write component at iterator position. */                        \
    00343     template <class ITERATOR>                                           \
    00344     void set(value_type const & v, ITERATOR const & i) const {          \
    00345       (*i).NTH = v;                                                     \
    00346     }                                                                   \
    00347                                                                         \
    00348     /** Write component at offset from iterator position. */            \
    00349     template <class ITERATOR, class DIFFERENCE>                         \
    00350     void set(value_type const & v, ITERATOR const & i, DIFFERENCE d) const { \
    00351       i[d].NTH = v;                                                     \
    00352     }                                                                   \
    00353   }
    00354
    00355 #define VIGRA_OPENCL_TYPE2_ACCESSORS(basetype)  \
    00356   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, s0);  \
    00357   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, s1);  \
    00358   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, x);   \
    00359   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 2, y);
    00360
    00361 #define VIGRA_OPENCL_TYPE3_ACCESSORS(basetype)  \
    00362   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, s0);  \
    00363   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, s1);  \
    00364   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, s2);  \
    00365   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, x);   \
    00366   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, y);   \
    00367   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 3, z);
    00368
    00369 #define VIGRA_OPENCL_TYPE4_ACCESSORS(basetype)   \
    00370   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s0);   \
    00371   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s1);   \
    00372   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s2);   \
    00373   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, s3);   \
    00374   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, x);    \
    00375   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, y);    \
    00376   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, z);    \
    00377   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 4, w);
    00378
    00379 #define VIGRA_OPENCL_TYPE8_ACCESSORS(basetype)   \
    00380   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s0);   \
    00381   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s1);   \
    00382   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s2);   \
    00383   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s3);   \
    00384   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s4);   \
    00385   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s5);   \
    00386   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s6);   \
    00387   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s7);   \
    00388   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 8, s8);
    00389
    00390 #define VIGRA_OPENCL_TYPE16_ACCESSORS(basetype)   \
    00391   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s0);   \
    00392   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s1);   \
    00393   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s2);   \
    00394   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s3);   \
    00395   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s4);   \
    00396   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s5);   \
    00397   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s6);   \
    00398   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s7);   \
    00399   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, s8);   \
    00400   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sa);   \
    00401   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sb);   \
    00402   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sc);   \
    00403   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sd);   \
    00404   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, se);   \
    00405   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sf);   \
    00406   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sA);   \
    00407   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sB);   \
    00408   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sC);   \
    00409   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sD);   \
    00410   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sE);   \
    00411   VIGRA_OPENCL_TYPE_ACCESSOR(basetype, 16, sF);
    00412
    00413 /// \todo figure out half (.hi .lo, .even .odd) and other odd-sized accessors
    00414
    00415 #define VIGRA_OPENCL_ACCESSORS(basetype)  \
    00416   VIGRA_OPENCL_TYPE2_ACCESSORS(basetype); \
    00417   VIGRA_OPENCL_TYPE3_ACCESSORS(basetype); \
    00418   VIGRA_OPENCL_TYPE4_ACCESSORS(basetype); \
    00419   VIGRA_OPENCL_TYPE8_ACCESSORS(basetype); \
    00420   VIGRA_OPENCL_TYPE16_ACCESSORS(basetype);
    00421
    00422 VIGRA_OPENCL_ACCESSORS(cl_char);
    00423 VIGRA_OPENCL_ACCESSORS(cl_uchar);
    00424 VIGRA_OPENCL_ACCESSORS(cl_short);
    00425 VIGRA_OPENCL_ACCESSORS(cl_ushort);
    00426 VIGRA_OPENCL_ACCESSORS(cl_int);
    00427 VIGRA_OPENCL_ACCESSORS(cl_uint);
    00428 VIGRA_OPENCL_ACCESSORS(cl_long);
    00429 VIGRA_OPENCL_ACCESSORS(cl_ulong);
    00430 VIGRA_OPENCL_ACCESSORS(cl_float);
    00431 VIGRA_OPENCL_ACCESSORS(cl_double);
    00432
    00433 #undef VIGRA_OPENCL_TYPE_ACCESSOR
    00434 #undef VIGRA_OPENCL_TYPE2_ACCESSORS
    00435 #undef VIGRA_OPENCL_TYPE3_ACCESSORS
    00436 #undef VIGRA_OPENCL_TYPE4_ACCESSORS
    00437 #undef VIGRA_OPENCL_TYPE8_ACCESSORS
    00438 #undef VIGRA_OPENCL_TYPE16_ACCESSORS
    00439 #undef VIGRA_OPENCL_ACCESSORS
    00440
    00441 } // namespace vigra
    00442
    00443 #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.9.0 (Tue Nov 6 2012)