ocl.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the OpenCV Foundation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_OPENCL_HPP
  42. #define OPENCV_OPENCL_HPP
  43. #include "opencv2/core.hpp"
  44. namespace cv { namespace ocl {
  45. //! @addtogroup core_opencl
  46. //! @{
  47. CV_EXPORTS_W bool haveOpenCL();
  48. CV_EXPORTS_W bool useOpenCL();
  49. CV_EXPORTS_W bool haveAmdBlas();
  50. CV_EXPORTS_W bool haveAmdFft();
  51. CV_EXPORTS_W void setUseOpenCL(bool flag);
  52. CV_EXPORTS_W void finish();
  53. CV_EXPORTS bool haveSVM();
  54. class CV_EXPORTS Context;
  55. class CV_EXPORTS Device;
  56. class CV_EXPORTS Kernel;
  57. class CV_EXPORTS Program;
  58. class CV_EXPORTS ProgramSource;
  59. class CV_EXPORTS Queue;
  60. class CV_EXPORTS PlatformInfo;
  61. class CV_EXPORTS Image2D;
  62. class CV_EXPORTS Device
  63. {
  64. public:
  65. Device();
  66. explicit Device(void* d);
  67. Device(const Device& d);
  68. Device& operator = (const Device& d);
  69. ~Device();
  70. void set(void* d);
  71. enum
  72. {
  73. TYPE_DEFAULT = (1 << 0),
  74. TYPE_CPU = (1 << 1),
  75. TYPE_GPU = (1 << 2),
  76. TYPE_ACCELERATOR = (1 << 3),
  77. TYPE_DGPU = TYPE_GPU + (1 << 16),
  78. TYPE_IGPU = TYPE_GPU + (1 << 17),
  79. TYPE_ALL = 0xFFFFFFFF
  80. };
  81. String name() const;
  82. String extensions() const;
  83. bool isExtensionSupported(const String& extensionName) const;
  84. String version() const;
  85. String vendorName() const;
  86. String OpenCL_C_Version() const;
  87. String OpenCLVersion() const;
  88. int deviceVersionMajor() const;
  89. int deviceVersionMinor() const;
  90. String driverVersion() const;
  91. void* ptr() const;
  92. int type() const;
  93. int addressBits() const;
  94. bool available() const;
  95. bool compilerAvailable() const;
  96. bool linkerAvailable() const;
  97. enum
  98. {
  99. FP_DENORM=(1 << 0),
  100. FP_INF_NAN=(1 << 1),
  101. FP_ROUND_TO_NEAREST=(1 << 2),
  102. FP_ROUND_TO_ZERO=(1 << 3),
  103. FP_ROUND_TO_INF=(1 << 4),
  104. FP_FMA=(1 << 5),
  105. FP_SOFT_FLOAT=(1 << 6),
  106. FP_CORRECTLY_ROUNDED_DIVIDE_SQRT=(1 << 7)
  107. };
  108. int doubleFPConfig() const;
  109. int singleFPConfig() const;
  110. int halfFPConfig() const;
  111. bool endianLittle() const;
  112. bool errorCorrectionSupport() const;
  113. enum
  114. {
  115. EXEC_KERNEL=(1 << 0),
  116. EXEC_NATIVE_KERNEL=(1 << 1)
  117. };
  118. int executionCapabilities() const;
  119. size_t globalMemCacheSize() const;
  120. enum
  121. {
  122. NO_CACHE=0,
  123. READ_ONLY_CACHE=1,
  124. READ_WRITE_CACHE=2
  125. };
  126. int globalMemCacheType() const;
  127. int globalMemCacheLineSize() const;
  128. size_t globalMemSize() const;
  129. size_t localMemSize() const;
  130. enum
  131. {
  132. NO_LOCAL_MEM=0,
  133. LOCAL_IS_LOCAL=1,
  134. LOCAL_IS_GLOBAL=2
  135. };
  136. int localMemType() const;
  137. bool hostUnifiedMemory() const;
  138. bool imageSupport() const;
  139. bool imageFromBufferSupport() const;
  140. uint imagePitchAlignment() const;
  141. uint imageBaseAddressAlignment() const;
  142. /// deprecated, use isExtensionSupported() method (probably with "cl_khr_subgroups" value)
  143. bool intelSubgroupsSupport() const;
  144. size_t image2DMaxWidth() const;
  145. size_t image2DMaxHeight() const;
  146. size_t image3DMaxWidth() const;
  147. size_t image3DMaxHeight() const;
  148. size_t image3DMaxDepth() const;
  149. size_t imageMaxBufferSize() const;
  150. size_t imageMaxArraySize() const;
  151. enum
  152. {
  153. UNKNOWN_VENDOR=0,
  154. VENDOR_AMD=1,
  155. VENDOR_INTEL=2,
  156. VENDOR_NVIDIA=3
  157. };
  158. int vendorID() const;
  159. // FIXIT
  160. // dev.isAMD() doesn't work for OpenCL CPU devices from AMD OpenCL platform.
  161. // This method should use platform name instead of vendor name.
  162. // After fix restore code in arithm.cpp: ocl_compare()
  163. inline bool isAMD() const { return vendorID() == VENDOR_AMD; }
  164. inline bool isIntel() const { return vendorID() == VENDOR_INTEL; }
  165. inline bool isNVidia() const { return vendorID() == VENDOR_NVIDIA; }
  166. int maxClockFrequency() const;
  167. int maxComputeUnits() const;
  168. int maxConstantArgs() const;
  169. size_t maxConstantBufferSize() const;
  170. size_t maxMemAllocSize() const;
  171. size_t maxParameterSize() const;
  172. int maxReadImageArgs() const;
  173. int maxWriteImageArgs() const;
  174. int maxSamplers() const;
  175. size_t maxWorkGroupSize() const;
  176. int maxWorkItemDims() const;
  177. void maxWorkItemSizes(size_t*) const;
  178. int memBaseAddrAlign() const;
  179. int nativeVectorWidthChar() const;
  180. int nativeVectorWidthShort() const;
  181. int nativeVectorWidthInt() const;
  182. int nativeVectorWidthLong() const;
  183. int nativeVectorWidthFloat() const;
  184. int nativeVectorWidthDouble() const;
  185. int nativeVectorWidthHalf() const;
  186. int preferredVectorWidthChar() const;
  187. int preferredVectorWidthShort() const;
  188. int preferredVectorWidthInt() const;
  189. int preferredVectorWidthLong() const;
  190. int preferredVectorWidthFloat() const;
  191. int preferredVectorWidthDouble() const;
  192. int preferredVectorWidthHalf() const;
  193. size_t printfBufferSize() const;
  194. size_t profilingTimerResolution() const;
  195. static const Device& getDefault();
  196. protected:
  197. struct Impl;
  198. Impl* p;
  199. };
  200. class CV_EXPORTS Context
  201. {
  202. public:
  203. Context();
  204. explicit Context(int dtype);
  205. ~Context();
  206. Context(const Context& c);
  207. Context& operator = (const Context& c);
  208. bool create();
  209. bool create(int dtype);
  210. size_t ndevices() const;
  211. const Device& device(size_t idx) const;
  212. Program getProg(const ProgramSource& prog,
  213. const String& buildopt, String& errmsg);
  214. void unloadProg(Program& prog);
  215. static Context& getDefault(bool initialize = true);
  216. void* ptr() const;
  217. friend void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  218. bool useSVM() const;
  219. void setUseSVM(bool enabled);
  220. struct Impl;
  221. Impl* p;
  222. };
  223. class CV_EXPORTS Platform
  224. {
  225. public:
  226. Platform();
  227. ~Platform();
  228. Platform(const Platform& p);
  229. Platform& operator = (const Platform& p);
  230. void* ptr() const;
  231. static Platform& getDefault();
  232. friend void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  233. protected:
  234. struct Impl;
  235. Impl* p;
  236. };
  237. /** @brief Attaches OpenCL context to OpenCV
  238. @note
  239. OpenCV will check if available OpenCL platform has platformName name, then assign context to
  240. OpenCV and call `clRetainContext` function. The deviceID device will be used as target device and
  241. new command queue will be created.
  242. @param platformName name of OpenCL platform to attach, this string is used to check if platform is available to OpenCV at runtime
  243. @param platformID ID of platform attached context was created for
  244. @param context OpenCL context to be attached to OpenCV
  245. @param deviceID ID of device, must be created from attached context
  246. */
  247. CV_EXPORTS void attachContext(const String& platformName, void* platformID, void* context, void* deviceID);
  248. /** @brief Convert OpenCL buffer to UMat
  249. @note
  250. OpenCL buffer (cl_mem_buffer) should contain 2D image data, compatible with OpenCV. Memory
  251. content is not copied from `clBuffer` to UMat. Instead, buffer handle assigned to UMat and
  252. `clRetainMemObject` is called.
  253. @param cl_mem_buffer source clBuffer handle
  254. @param step num of bytes in single row
  255. @param rows number of rows
  256. @param cols number of cols
  257. @param type OpenCV type of image
  258. @param dst destination UMat
  259. */
  260. CV_EXPORTS void convertFromBuffer(void* cl_mem_buffer, size_t step, int rows, int cols, int type, UMat& dst);
  261. /** @brief Convert OpenCL image2d_t to UMat
  262. @note
  263. OpenCL `image2d_t` (cl_mem_image), should be compatible with OpenCV UMat formats. Memory content
  264. is copied from image to UMat with `clEnqueueCopyImageToBuffer` function.
  265. @param cl_mem_image source image2d_t handle
  266. @param dst destination UMat
  267. */
  268. CV_EXPORTS void convertFromImage(void* cl_mem_image, UMat& dst);
  269. // TODO Move to internal header
  270. void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  271. class CV_EXPORTS Queue
  272. {
  273. public:
  274. Queue();
  275. explicit Queue(const Context& c, const Device& d=Device());
  276. ~Queue();
  277. Queue(const Queue& q);
  278. Queue& operator = (const Queue& q);
  279. bool create(const Context& c=Context(), const Device& d=Device());
  280. void finish();
  281. void* ptr() const;
  282. static Queue& getDefault();
  283. /// @brief Returns OpenCL command queue with enable profiling mode support
  284. const Queue& getProfilingQueue() const;
  285. struct Impl; friend struct Impl;
  286. inline Impl* getImpl() const { return p; }
  287. protected:
  288. Impl* p;
  289. };
  290. class CV_EXPORTS KernelArg
  291. {
  292. public:
  293. enum { LOCAL=1, READ_ONLY=2, WRITE_ONLY=4, READ_WRITE=6, CONSTANT=8, PTR_ONLY = 16, NO_SIZE=256 };
  294. KernelArg(int _flags, UMat* _m, int wscale=1, int iwscale=1, const void* _obj=0, size_t _sz=0);
  295. KernelArg();
  296. static KernelArg Local() { return KernelArg(LOCAL, 0); }
  297. static KernelArg PtrWriteOnly(const UMat& m)
  298. { return KernelArg(PTR_ONLY+WRITE_ONLY, (UMat*)&m); }
  299. static KernelArg PtrReadOnly(const UMat& m)
  300. { return KernelArg(PTR_ONLY+READ_ONLY, (UMat*)&m); }
  301. static KernelArg PtrReadWrite(const UMat& m)
  302. { return KernelArg(PTR_ONLY+READ_WRITE, (UMat*)&m); }
  303. static KernelArg ReadWrite(const UMat& m, int wscale=1, int iwscale=1)
  304. { return KernelArg(READ_WRITE, (UMat*)&m, wscale, iwscale); }
  305. static KernelArg ReadWriteNoSize(const UMat& m, int wscale=1, int iwscale=1)
  306. { return KernelArg(READ_WRITE+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  307. static KernelArg ReadOnly(const UMat& m, int wscale=1, int iwscale=1)
  308. { return KernelArg(READ_ONLY, (UMat*)&m, wscale, iwscale); }
  309. static KernelArg WriteOnly(const UMat& m, int wscale=1, int iwscale=1)
  310. { return KernelArg(WRITE_ONLY, (UMat*)&m, wscale, iwscale); }
  311. static KernelArg ReadOnlyNoSize(const UMat& m, int wscale=1, int iwscale=1)
  312. { return KernelArg(READ_ONLY+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  313. static KernelArg WriteOnlyNoSize(const UMat& m, int wscale=1, int iwscale=1)
  314. { return KernelArg(WRITE_ONLY+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  315. static KernelArg Constant(const Mat& m);
  316. template<typename _Tp> static KernelArg Constant(const _Tp* arr, size_t n)
  317. { return KernelArg(CONSTANT, 0, 1, 1, (void*)arr, n); }
  318. int flags;
  319. UMat* m;
  320. const void* obj;
  321. size_t sz;
  322. int wscale, iwscale;
  323. };
  324. class CV_EXPORTS Kernel
  325. {
  326. public:
  327. Kernel();
  328. Kernel(const char* kname, const Program& prog);
  329. Kernel(const char* kname, const ProgramSource& prog,
  330. const String& buildopts = String(), String* errmsg=0);
  331. ~Kernel();
  332. Kernel(const Kernel& k);
  333. Kernel& operator = (const Kernel& k);
  334. bool empty() const;
  335. bool create(const char* kname, const Program& prog);
  336. bool create(const char* kname, const ProgramSource& prog,
  337. const String& buildopts, String* errmsg=0);
  338. int set(int i, const void* value, size_t sz);
  339. int set(int i, const Image2D& image2D);
  340. int set(int i, const UMat& m);
  341. int set(int i, const KernelArg& arg);
  342. template<typename _Tp> int set(int i, const _Tp& value)
  343. { return set(i, &value, sizeof(value)); }
  344. template<typename _Tp0>
  345. Kernel& args(const _Tp0& a0)
  346. {
  347. set(0, a0); return *this;
  348. }
  349. template<typename _Tp0, typename _Tp1>
  350. Kernel& args(const _Tp0& a0, const _Tp1& a1)
  351. {
  352. int i = set(0, a0); set(i, a1); return *this;
  353. }
  354. template<typename _Tp0, typename _Tp1, typename _Tp2>
  355. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2)
  356. {
  357. int i = set(0, a0); i = set(i, a1); set(i, a2); return *this;
  358. }
  359. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3>
  360. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3)
  361. {
  362. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); return *this;
  363. }
  364. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4>
  365. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2,
  366. const _Tp3& a3, const _Tp4& a4)
  367. {
  368. int i = set(0, a0); i = set(i, a1); i = set(i, a2);
  369. i = set(i, a3); set(i, a4); return *this;
  370. }
  371. template<typename _Tp0, typename _Tp1, typename _Tp2,
  372. typename _Tp3, typename _Tp4, typename _Tp5>
  373. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2,
  374. const _Tp3& a3, const _Tp4& a4, const _Tp5& a5)
  375. {
  376. int i = set(0, a0); i = set(i, a1); i = set(i, a2);
  377. i = set(i, a3); i = set(i, a4); set(i, a5); return *this;
  378. }
  379. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  380. typename _Tp4, typename _Tp5, typename _Tp6>
  381. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  382. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6)
  383. {
  384. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3);
  385. i = set(i, a4); i = set(i, a5); set(i, a6); return *this;
  386. }
  387. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  388. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7>
  389. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  390. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7)
  391. {
  392. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3);
  393. i = set(i, a4); i = set(i, a5); i = set(i, a6); set(i, a7); return *this;
  394. }
  395. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4,
  396. typename _Tp5, typename _Tp6, typename _Tp7, typename _Tp8>
  397. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  398. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  399. const _Tp8& a8)
  400. {
  401. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4);
  402. i = set(i, a5); i = set(i, a6); i = set(i, a7); set(i, a8); return *this;
  403. }
  404. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4,
  405. typename _Tp5, typename _Tp6, typename _Tp7, typename _Tp8, typename _Tp9>
  406. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  407. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  408. const _Tp8& a8, const _Tp9& a9)
  409. {
  410. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  411. i = set(i, a6); i = set(i, a7); i = set(i, a8); set(i, a9); return *this;
  412. }
  413. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  414. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  415. typename _Tp8, typename _Tp9, typename _Tp10>
  416. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  417. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  418. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10)
  419. {
  420. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  421. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); set(i, a10); return *this;
  422. }
  423. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  424. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  425. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11>
  426. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  427. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  428. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11)
  429. {
  430. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  431. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); set(i, a11); return *this;
  432. }
  433. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  434. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  435. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12>
  436. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  437. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  438. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  439. const _Tp12& a12)
  440. {
  441. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  442. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  443. set(i, a12); return *this;
  444. }
  445. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  446. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  447. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12,
  448. typename _Tp13>
  449. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  450. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  451. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  452. const _Tp12& a12, const _Tp13& a13)
  453. {
  454. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  455. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  456. i = set(i, a12); set(i, a13); return *this;
  457. }
  458. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  459. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  460. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12,
  461. typename _Tp13, typename _Tp14>
  462. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  463. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  464. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  465. const _Tp12& a12, const _Tp13& a13, const _Tp14& a14)
  466. {
  467. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  468. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  469. i = set(i, a12); i = set(i, a13); set(i, a14); return *this;
  470. }
  471. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  472. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  473. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12,
  474. typename _Tp13, typename _Tp14, typename _Tp15>
  475. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  476. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  477. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  478. const _Tp12& a12, const _Tp13& a13, const _Tp14& a14, const _Tp15& a15)
  479. {
  480. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4); i = set(i, a5);
  481. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  482. i = set(i, a12); i = set(i, a13); i = set(i, a14); set(i, a15); return *this;
  483. }
  484. /** @brief Run the OpenCL kernel.
  485. @param dims the work problem dimensions. It is the length of globalsize and localsize. It can be either 1, 2 or 3.
  486. @param globalsize work items for each dimension. It is not the final globalsize passed to
  487. OpenCL. Each dimension will be adjusted to the nearest integer divisible by the corresponding
  488. value in localsize. If localsize is NULL, it will still be adjusted depending on dims. The
  489. adjusted values are greater than or equal to the original values.
  490. @param localsize work-group size for each dimension.
  491. @param sync specify whether to wait for OpenCL computation to finish before return.
  492. @param q command queue
  493. */
  494. bool run(int dims, size_t globalsize[],
  495. size_t localsize[], bool sync, const Queue& q=Queue());
  496. bool runTask(bool sync, const Queue& q=Queue());
  497. /** @brief Similar to synchronized run() call with returning of kernel execution time
  498. * Separate OpenCL command queue may be used (with CL_QUEUE_PROFILING_ENABLE)
  499. * @return Execution time in nanoseconds or negative number on error
  500. */
  501. int64 runProfiling(int dims, size_t globalsize[], size_t localsize[], const Queue& q=Queue());
  502. size_t workGroupSize() const;
  503. size_t preferedWorkGroupSizeMultiple() const;
  504. bool compileWorkGroupSize(size_t wsz[]) const;
  505. size_t localMemSize() const;
  506. void* ptr() const;
  507. struct Impl;
  508. protected:
  509. Impl* p;
  510. };
  511. class CV_EXPORTS Program
  512. {
  513. public:
  514. Program();
  515. Program(const ProgramSource& src,
  516. const String& buildflags, String& errmsg);
  517. Program(const Program& prog);
  518. Program& operator = (const Program& prog);
  519. ~Program();
  520. bool create(const ProgramSource& src,
  521. const String& buildflags, String& errmsg);
  522. bool read(const String& buf, const String& buildflags);
  523. bool write(String& buf) const;
  524. const ProgramSource& source() const;
  525. void* ptr() const;
  526. String getPrefix() const;
  527. static String getPrefix(const String& buildflags);
  528. struct Impl;
  529. inline Impl* getImpl() const { return (Impl*)p; }
  530. protected:
  531. Impl* p;
  532. };
  533. class CV_EXPORTS ProgramSource
  534. {
  535. public:
  536. typedef uint64 hash_t; // deprecated
  537. ProgramSource();
  538. explicit ProgramSource(const String& module, const String& name, const String& codeStr, const String& codeHash);
  539. explicit ProgramSource(const String& prog); // deprecated
  540. explicit ProgramSource(const char* prog); // deprecated
  541. ~ProgramSource();
  542. ProgramSource(const ProgramSource& prog);
  543. ProgramSource& operator = (const ProgramSource& prog);
  544. const String& source() const;
  545. hash_t hash() const; // deprecated
  546. struct Impl;
  547. inline Impl* getImpl() const { return (Impl*)p; }
  548. protected:
  549. Impl* p;
  550. };
  551. class CV_EXPORTS PlatformInfo
  552. {
  553. public:
  554. PlatformInfo();
  555. explicit PlatformInfo(void* id);
  556. ~PlatformInfo();
  557. PlatformInfo(const PlatformInfo& i);
  558. PlatformInfo& operator =(const PlatformInfo& i);
  559. String name() const;
  560. String vendor() const;
  561. String version() const;
  562. int deviceNumber() const;
  563. void getDevice(Device& device, int d) const;
  564. protected:
  565. struct Impl;
  566. Impl* p;
  567. };
  568. CV_EXPORTS const char* convertTypeStr(int sdepth, int ddepth, int cn, char* buf);
  569. CV_EXPORTS const char* typeToStr(int t);
  570. CV_EXPORTS const char* memopTypeToStr(int t);
  571. CV_EXPORTS const char* vecopTypeToStr(int t);
  572. CV_EXPORTS const char* getOpenCLErrorString(int errorCode);
  573. CV_EXPORTS String kernelToStr(InputArray _kernel, int ddepth = -1, const char * name = NULL);
  574. CV_EXPORTS void getPlatfomsInfo(std::vector<PlatformInfo>& platform_info);
  575. enum OclVectorStrategy
  576. {
  577. // all matrices have its own vector width
  578. OCL_VECTOR_OWN = 0,
  579. // all matrices have maximal vector width among all matrices
  580. // (useful for cases when matrices have different data types)
  581. OCL_VECTOR_MAX = 1,
  582. // default strategy
  583. OCL_VECTOR_DEFAULT = OCL_VECTOR_OWN
  584. };
  585. CV_EXPORTS int predictOptimalVectorWidth(InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  586. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  587. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray(),
  588. OclVectorStrategy strat = OCL_VECTOR_DEFAULT);
  589. CV_EXPORTS int checkOptimalVectorWidth(const int *vectorWidths,
  590. InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  591. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  592. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray(),
  593. OclVectorStrategy strat = OCL_VECTOR_DEFAULT);
  594. // with OCL_VECTOR_MAX strategy
  595. CV_EXPORTS int predictOptimalVectorWidthMax(InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  596. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  597. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray());
  598. CV_EXPORTS void buildOptionsAddMatrixDescription(String& buildOptions, const String& name, InputArray _m);
  599. class CV_EXPORTS Image2D
  600. {
  601. public:
  602. Image2D();
  603. /**
  604. @param src UMat object from which to get image properties and data
  605. @param norm flag to enable the use of normalized channel data types
  606. @param alias flag indicating that the image should alias the src UMat. If true, changes to the
  607. image or src will be reflected in both objects.
  608. */
  609. explicit Image2D(const UMat &src, bool norm = false, bool alias = false);
  610. Image2D(const Image2D & i);
  611. ~Image2D();
  612. Image2D & operator = (const Image2D & i);
  613. /** Indicates if creating an aliased image should succeed.
  614. Depends on the underlying platform and the dimensions of the UMat.
  615. */
  616. static bool canCreateAlias(const UMat &u);
  617. /** Indicates if the image format is supported.
  618. */
  619. static bool isFormatSupported(int depth, int cn, bool norm);
  620. void* ptr() const;
  621. protected:
  622. struct Impl;
  623. Impl* p;
  624. };
  625. class CV_EXPORTS Timer
  626. {
  627. public:
  628. Timer(const Queue& q);
  629. ~Timer();
  630. void start();
  631. void stop();
  632. uint64 durationNS() const; //< duration in nanoseconds
  633. protected:
  634. struct Impl;
  635. Impl* const p;
  636. private:
  637. Timer(const Timer&); // disabled
  638. Timer& operator=(const Timer&); // disabled
  639. };
  640. CV_EXPORTS MatAllocator* getOpenCLAllocator();
  641. #ifdef __OPENCV_BUILD
  642. namespace internal {
  643. CV_EXPORTS bool isOpenCLForced();
  644. #define OCL_FORCE_CHECK(condition) (cv::ocl::internal::isOpenCLForced() || (condition))
  645. CV_EXPORTS bool isPerformanceCheckBypassed();
  646. #define OCL_PERFORMANCE_CHECK(condition) (cv::ocl::internal::isPerformanceCheckBypassed() || (condition))
  647. CV_EXPORTS bool isCLBuffer(UMat& u);
  648. } // namespace internal
  649. #endif
  650. //! @}
  651. }}
  652. #endif