ocl.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. inline Impl* getImpl() const { return (Impl*)p; }
  222. //protected:
  223. Impl* p;
  224. };
  225. class CV_EXPORTS Platform
  226. {
  227. public:
  228. Platform();
  229. ~Platform();
  230. Platform(const Platform& p);
  231. Platform& operator = (const Platform& p);
  232. void* ptr() const;
  233. static Platform& getDefault();
  234. friend void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  235. protected:
  236. struct Impl;
  237. Impl* p;
  238. };
  239. /** @brief Attaches OpenCL context to OpenCV
  240. @note
  241. OpenCV will check if available OpenCL platform has platformName name, then assign context to
  242. OpenCV and call `clRetainContext` function. The deviceID device will be used as target device and
  243. new command queue will be created.
  244. @param platformName name of OpenCL platform to attach, this string is used to check if platform is available to OpenCV at runtime
  245. @param platformID ID of platform attached context was created for
  246. @param context OpenCL context to be attached to OpenCV
  247. @param deviceID ID of device, must be created from attached context
  248. */
  249. CV_EXPORTS void attachContext(const String& platformName, void* platformID, void* context, void* deviceID);
  250. /** @brief Convert OpenCL buffer to UMat
  251. @note
  252. OpenCL buffer (cl_mem_buffer) should contain 2D image data, compatible with OpenCV. Memory
  253. content is not copied from `clBuffer` to UMat. Instead, buffer handle assigned to UMat and
  254. `clRetainMemObject` is called.
  255. @param cl_mem_buffer source clBuffer handle
  256. @param step num of bytes in single row
  257. @param rows number of rows
  258. @param cols number of cols
  259. @param type OpenCV type of image
  260. @param dst destination UMat
  261. */
  262. CV_EXPORTS void convertFromBuffer(void* cl_mem_buffer, size_t step, int rows, int cols, int type, UMat& dst);
  263. /** @brief Convert OpenCL image2d_t to UMat
  264. @note
  265. OpenCL `image2d_t` (cl_mem_image), should be compatible with OpenCV UMat formats. Memory content
  266. is copied from image to UMat with `clEnqueueCopyImageToBuffer` function.
  267. @param cl_mem_image source image2d_t handle
  268. @param dst destination UMat
  269. */
  270. CV_EXPORTS void convertFromImage(void* cl_mem_image, UMat& dst);
  271. // TODO Move to internal header
  272. void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  273. class CV_EXPORTS Queue
  274. {
  275. public:
  276. Queue();
  277. explicit Queue(const Context& c, const Device& d=Device());
  278. ~Queue();
  279. Queue(const Queue& q);
  280. Queue& operator = (const Queue& q);
  281. bool create(const Context& c=Context(), const Device& d=Device());
  282. void finish();
  283. void* ptr() const;
  284. static Queue& getDefault();
  285. /// @brief Returns OpenCL command queue with enable profiling mode support
  286. const Queue& getProfilingQueue() const;
  287. struct Impl; friend struct Impl;
  288. inline Impl* getImpl() const { return p; }
  289. protected:
  290. Impl* p;
  291. };
  292. class CV_EXPORTS KernelArg
  293. {
  294. public:
  295. enum { LOCAL=1, READ_ONLY=2, WRITE_ONLY=4, READ_WRITE=6, CONSTANT=8, PTR_ONLY = 16, NO_SIZE=256 };
  296. KernelArg(int _flags, UMat* _m, int wscale=1, int iwscale=1, const void* _obj=0, size_t _sz=0);
  297. KernelArg();
  298. static KernelArg Local() { return KernelArg(LOCAL, 0); }
  299. static KernelArg PtrWriteOnly(const UMat& m)
  300. { return KernelArg(PTR_ONLY+WRITE_ONLY, (UMat*)&m); }
  301. static KernelArg PtrReadOnly(const UMat& m)
  302. { return KernelArg(PTR_ONLY+READ_ONLY, (UMat*)&m); }
  303. static KernelArg PtrReadWrite(const UMat& m)
  304. { return KernelArg(PTR_ONLY+READ_WRITE, (UMat*)&m); }
  305. static KernelArg ReadWrite(const UMat& m, int wscale=1, int iwscale=1)
  306. { return KernelArg(READ_WRITE, (UMat*)&m, wscale, iwscale); }
  307. static KernelArg ReadWriteNoSize(const UMat& m, int wscale=1, int iwscale=1)
  308. { return KernelArg(READ_WRITE+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  309. static KernelArg ReadOnly(const UMat& m, int wscale=1, int iwscale=1)
  310. { return KernelArg(READ_ONLY, (UMat*)&m, wscale, iwscale); }
  311. static KernelArg WriteOnly(const UMat& m, int wscale=1, int iwscale=1)
  312. { return KernelArg(WRITE_ONLY, (UMat*)&m, wscale, iwscale); }
  313. static KernelArg ReadOnlyNoSize(const UMat& m, int wscale=1, int iwscale=1)
  314. { return KernelArg(READ_ONLY+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  315. static KernelArg WriteOnlyNoSize(const UMat& m, int wscale=1, int iwscale=1)
  316. { return KernelArg(WRITE_ONLY+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  317. static KernelArg Constant(const Mat& m);
  318. template<typename _Tp> static KernelArg Constant(const _Tp* arr, size_t n)
  319. { return KernelArg(CONSTANT, 0, 1, 1, (void*)arr, n); }
  320. int flags;
  321. UMat* m;
  322. const void* obj;
  323. size_t sz;
  324. int wscale, iwscale;
  325. };
  326. class CV_EXPORTS Kernel
  327. {
  328. public:
  329. Kernel();
  330. Kernel(const char* kname, const Program& prog);
  331. Kernel(const char* kname, const ProgramSource& prog,
  332. const String& buildopts = String(), String* errmsg=0);
  333. ~Kernel();
  334. Kernel(const Kernel& k);
  335. Kernel& operator = (const Kernel& k);
  336. bool empty() const;
  337. bool create(const char* kname, const Program& prog);
  338. bool create(const char* kname, const ProgramSource& prog,
  339. const String& buildopts, String* errmsg=0);
  340. int set(int i, const void* value, size_t sz);
  341. int set(int i, const Image2D& image2D);
  342. int set(int i, const UMat& m);
  343. int set(int i, const KernelArg& arg);
  344. template<typename _Tp> int set(int i, const _Tp& value)
  345. { return set(i, &value, sizeof(value)); }
  346. template<typename _Tp0>
  347. Kernel& args(const _Tp0& a0)
  348. {
  349. set(0, a0); return *this;
  350. }
  351. template<typename _Tp0, typename _Tp1>
  352. Kernel& args(const _Tp0& a0, const _Tp1& a1)
  353. {
  354. int i = set(0, a0); set(i, a1); return *this;
  355. }
  356. template<typename _Tp0, typename _Tp1, typename _Tp2>
  357. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2)
  358. {
  359. int i = set(0, a0); i = set(i, a1); set(i, a2); return *this;
  360. }
  361. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3>
  362. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3)
  363. {
  364. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); return *this;
  365. }
  366. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4>
  367. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2,
  368. const _Tp3& a3, const _Tp4& a4)
  369. {
  370. int i = set(0, a0); i = set(i, a1); i = set(i, a2);
  371. i = set(i, a3); set(i, a4); return *this;
  372. }
  373. template<typename _Tp0, typename _Tp1, typename _Tp2,
  374. typename _Tp3, typename _Tp4, typename _Tp5>
  375. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2,
  376. const _Tp3& a3, const _Tp4& a4, const _Tp5& a5)
  377. {
  378. int i = set(0, a0); i = set(i, a1); i = set(i, a2);
  379. i = set(i, a3); i = set(i, a4); set(i, a5); return *this;
  380. }
  381. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  382. typename _Tp4, typename _Tp5, typename _Tp6>
  383. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  384. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6)
  385. {
  386. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3);
  387. i = set(i, a4); i = set(i, a5); set(i, a6); return *this;
  388. }
  389. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  390. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7>
  391. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  392. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7)
  393. {
  394. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3);
  395. i = set(i, a4); i = set(i, a5); i = set(i, a6); set(i, a7); return *this;
  396. }
  397. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4,
  398. typename _Tp5, typename _Tp6, typename _Tp7, typename _Tp8>
  399. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  400. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  401. const _Tp8& a8)
  402. {
  403. int i = set(0, a0); i = set(i, a1); i = set(i, a2); i = set(i, a3); i = set(i, a4);
  404. i = set(i, a5); i = set(i, a6); i = set(i, a7); set(i, a8); return *this;
  405. }
  406. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4,
  407. typename _Tp5, typename _Tp6, typename _Tp7, typename _Tp8, typename _Tp9>
  408. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  409. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  410. const _Tp8& a8, const _Tp9& a9)
  411. {
  412. 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);
  413. i = set(i, a6); i = set(i, a7); i = set(i, a8); set(i, a9); return *this;
  414. }
  415. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  416. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  417. typename _Tp8, typename _Tp9, typename _Tp10>
  418. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  419. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  420. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10)
  421. {
  422. 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);
  423. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); set(i, a10); return *this;
  424. }
  425. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  426. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  427. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11>
  428. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  429. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  430. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11)
  431. {
  432. 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);
  433. 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;
  434. }
  435. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  436. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  437. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12>
  438. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  439. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  440. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  441. const _Tp12& a12)
  442. {
  443. 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);
  444. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  445. set(i, a12); return *this;
  446. }
  447. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  448. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  449. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12,
  450. typename _Tp13>
  451. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  452. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  453. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  454. const _Tp12& a12, const _Tp13& a13)
  455. {
  456. 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);
  457. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  458. i = set(i, a12); set(i, a13); return *this;
  459. }
  460. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  461. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  462. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12,
  463. typename _Tp13, typename _Tp14>
  464. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  465. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  466. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  467. const _Tp12& a12, const _Tp13& a13, const _Tp14& a14)
  468. {
  469. 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);
  470. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  471. i = set(i, a12); i = set(i, a13); set(i, a14); return *this;
  472. }
  473. template<typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
  474. typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
  475. typename _Tp8, typename _Tp9, typename _Tp10, typename _Tp11, typename _Tp12,
  476. typename _Tp13, typename _Tp14, typename _Tp15>
  477. Kernel& args(const _Tp0& a0, const _Tp1& a1, const _Tp2& a2, const _Tp3& a3,
  478. const _Tp4& a4, const _Tp5& a5, const _Tp6& a6, const _Tp7& a7,
  479. const _Tp8& a8, const _Tp9& a9, const _Tp10& a10, const _Tp11& a11,
  480. const _Tp12& a12, const _Tp13& a13, const _Tp14& a14, const _Tp15& a15)
  481. {
  482. 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);
  483. i = set(i, a6); i = set(i, a7); i = set(i, a8); i = set(i, a9); i = set(i, a10); i = set(i, a11);
  484. i = set(i, a12); i = set(i, a13); i = set(i, a14); set(i, a15); return *this;
  485. }
  486. /** @brief Run the OpenCL kernel.
  487. @param dims the work problem dimensions. It is the length of globalsize and localsize. It can be either 1, 2 or 3.
  488. @param globalsize work items for each dimension. It is not the final globalsize passed to
  489. OpenCL. Each dimension will be adjusted to the nearest integer divisible by the corresponding
  490. value in localsize. If localsize is NULL, it will still be adjusted depending on dims. The
  491. adjusted values are greater than or equal to the original values.
  492. @param localsize work-group size for each dimension.
  493. @param sync specify whether to wait for OpenCL computation to finish before return.
  494. @param q command queue
  495. */
  496. bool run(int dims, size_t globalsize[],
  497. size_t localsize[], bool sync, const Queue& q=Queue());
  498. bool runTask(bool sync, const Queue& q=Queue());
  499. /** @brief Similar to synchronized run() call with returning of kernel execution time
  500. * Separate OpenCL command queue may be used (with CL_QUEUE_PROFILING_ENABLE)
  501. * @return Execution time in nanoseconds or negative number on error
  502. */
  503. int64 runProfiling(int dims, size_t globalsize[], size_t localsize[], const Queue& q=Queue());
  504. size_t workGroupSize() const;
  505. size_t preferedWorkGroupSizeMultiple() const;
  506. bool compileWorkGroupSize(size_t wsz[]) const;
  507. size_t localMemSize() const;
  508. void* ptr() const;
  509. struct Impl;
  510. protected:
  511. Impl* p;
  512. };
  513. class CV_EXPORTS Program
  514. {
  515. public:
  516. Program();
  517. Program(const ProgramSource& src,
  518. const String& buildflags, String& errmsg);
  519. Program(const Program& prog);
  520. Program& operator = (const Program& prog);
  521. ~Program();
  522. bool create(const ProgramSource& src,
  523. const String& buildflags, String& errmsg);
  524. void* ptr() const;
  525. /**
  526. * @brief Query device-specific program binary.
  527. *
  528. * Returns RAW OpenCL executable binary without additional attachments.
  529. *
  530. * @sa ProgramSource::fromBinary
  531. *
  532. * @param[out] binary output buffer
  533. */
  534. void getBinary(std::vector<char>& binary) const;
  535. struct Impl; friend struct Impl;
  536. inline Impl* getImpl() const { return (Impl*)p; }
  537. protected:
  538. Impl* p;
  539. public:
  540. #ifndef OPENCV_REMOVE_DEPRECATED_API
  541. // TODO Remove this
  542. CV_DEPRECATED bool read(const String& buf, const String& buildflags); // removed, use ProgramSource instead
  543. CV_DEPRECATED bool write(String& buf) const; // removed, use getBinary() method instead (RAW OpenCL binary)
  544. CV_DEPRECATED const ProgramSource& source() const; // implementation removed
  545. CV_DEPRECATED String getPrefix() const; // deprecated, implementation replaced
  546. CV_DEPRECATED static String getPrefix(const String& buildflags); // deprecated, implementation replaced
  547. #endif
  548. };
  549. class CV_EXPORTS ProgramSource
  550. {
  551. public:
  552. typedef uint64 hash_t; // deprecated
  553. ProgramSource();
  554. explicit ProgramSource(const String& module, const String& name, const String& codeStr, const String& codeHash);
  555. explicit ProgramSource(const String& prog); // deprecated
  556. explicit ProgramSource(const char* prog); // deprecated
  557. ~ProgramSource();
  558. ProgramSource(const ProgramSource& prog);
  559. ProgramSource& operator = (const ProgramSource& prog);
  560. const String& source() const; // deprecated
  561. hash_t hash() const; // deprecated
  562. /** @brief Describe OpenCL program binary.
  563. * Do not call clCreateProgramWithBinary() and/or clBuildProgram().
  564. *
  565. * Caller should guarantee binary buffer lifetime greater than ProgramSource object (and any of its copies).
  566. *
  567. * This kind of binary is not portable between platforms in general - it is specific to OpenCL vendor / device / driver version.
  568. *
  569. * @param module name of program owner module
  570. * @param name unique name of program (module+name is used as key for OpenCL program caching)
  571. * @param binary buffer address. See buffer lifetime requirement in description.
  572. * @param size buffer size
  573. * @param buildOptions additional program-related build options passed to clBuildProgram()
  574. * @return created ProgramSource object
  575. */
  576. static ProgramSource fromBinary(const String& module, const String& name,
  577. const unsigned char* binary, const size_t size,
  578. const cv::String& buildOptions = cv::String());
  579. /** @brief Describe OpenCL program in SPIR format.
  580. * Do not call clCreateProgramWithBinary() and/or clBuildProgram().
  581. *
  582. * Supports SPIR 1.2 by default (pass '-spir-std=X.Y' in buildOptions to override this behavior)
  583. *
  584. * Caller should guarantee binary buffer lifetime greater than ProgramSource object (and any of its copies).
  585. *
  586. * Programs in this format are portable between OpenCL implementations with 'khr_spir' extension:
  587. * https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/cl_khr_spir.html
  588. * (but they are not portable between different platforms: 32-bit / 64-bit)
  589. *
  590. * Note: these programs can't support vendor specific extensions, like 'cl_intel_subgroups'.
  591. *
  592. * @param module name of program owner module
  593. * @param name unique name of program (module+name is used as key for OpenCL program caching)
  594. * @param binary buffer address. See buffer lifetime requirement in description.
  595. * @param size buffer size
  596. * @param buildOptions additional program-related build options passed to clBuildProgram()
  597. * (these options are added automatically: '-x spir' and '-spir-std=1.2')
  598. * @return created ProgramSource object.
  599. */
  600. static ProgramSource fromSPIR(const String& module, const String& name,
  601. const unsigned char* binary, const size_t size,
  602. const cv::String& buildOptions = cv::String());
  603. //OpenCL 2.1+ only
  604. //static Program fromSPIRV(const String& module, const String& name,
  605. // const unsigned char* binary, const size_t size,
  606. // const cv::String& buildOptions = cv::String());
  607. struct Impl; friend struct Impl;
  608. inline Impl* getImpl() const { return (Impl*)p; }
  609. protected:
  610. Impl* p;
  611. };
  612. class CV_EXPORTS PlatformInfo
  613. {
  614. public:
  615. PlatformInfo();
  616. explicit PlatformInfo(void* id);
  617. ~PlatformInfo();
  618. PlatformInfo(const PlatformInfo& i);
  619. PlatformInfo& operator =(const PlatformInfo& i);
  620. String name() const;
  621. String vendor() const;
  622. String version() const;
  623. int deviceNumber() const;
  624. void getDevice(Device& device, int d) const;
  625. protected:
  626. struct Impl;
  627. Impl* p;
  628. };
  629. CV_EXPORTS const char* convertTypeStr(int sdepth, int ddepth, int cn, char* buf);
  630. CV_EXPORTS const char* typeToStr(int t);
  631. CV_EXPORTS const char* memopTypeToStr(int t);
  632. CV_EXPORTS const char* vecopTypeToStr(int t);
  633. CV_EXPORTS const char* getOpenCLErrorString(int errorCode);
  634. CV_EXPORTS String kernelToStr(InputArray _kernel, int ddepth = -1, const char * name = NULL);
  635. CV_EXPORTS void getPlatfomsInfo(std::vector<PlatformInfo>& platform_info);
  636. enum OclVectorStrategy
  637. {
  638. // all matrices have its own vector width
  639. OCL_VECTOR_OWN = 0,
  640. // all matrices have maximal vector width among all matrices
  641. // (useful for cases when matrices have different data types)
  642. OCL_VECTOR_MAX = 1,
  643. // default strategy
  644. OCL_VECTOR_DEFAULT = OCL_VECTOR_OWN
  645. };
  646. CV_EXPORTS int predictOptimalVectorWidth(InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  647. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  648. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray(),
  649. OclVectorStrategy strat = OCL_VECTOR_DEFAULT);
  650. CV_EXPORTS int checkOptimalVectorWidth(const int *vectorWidths,
  651. InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  652. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  653. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray(),
  654. OclVectorStrategy strat = OCL_VECTOR_DEFAULT);
  655. // with OCL_VECTOR_MAX strategy
  656. CV_EXPORTS int predictOptimalVectorWidthMax(InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  657. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  658. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray());
  659. CV_EXPORTS void buildOptionsAddMatrixDescription(String& buildOptions, const String& name, InputArray _m);
  660. class CV_EXPORTS Image2D
  661. {
  662. public:
  663. Image2D();
  664. /**
  665. @param src UMat object from which to get image properties and data
  666. @param norm flag to enable the use of normalized channel data types
  667. @param alias flag indicating that the image should alias the src UMat. If true, changes to the
  668. image or src will be reflected in both objects.
  669. */
  670. explicit Image2D(const UMat &src, bool norm = false, bool alias = false);
  671. Image2D(const Image2D & i);
  672. ~Image2D();
  673. Image2D & operator = (const Image2D & i);
  674. /** Indicates if creating an aliased image should succeed.
  675. Depends on the underlying platform and the dimensions of the UMat.
  676. */
  677. static bool canCreateAlias(const UMat &u);
  678. /** Indicates if the image format is supported.
  679. */
  680. static bool isFormatSupported(int depth, int cn, bool norm);
  681. void* ptr() const;
  682. protected:
  683. struct Impl;
  684. Impl* p;
  685. };
  686. class CV_EXPORTS Timer
  687. {
  688. public:
  689. Timer(const Queue& q);
  690. ~Timer();
  691. void start();
  692. void stop();
  693. uint64 durationNS() const; //< duration in nanoseconds
  694. protected:
  695. struct Impl;
  696. Impl* const p;
  697. private:
  698. Timer(const Timer&); // disabled
  699. Timer& operator=(const Timer&); // disabled
  700. };
  701. CV_EXPORTS MatAllocator* getOpenCLAllocator();
  702. #ifdef __OPENCV_BUILD
  703. namespace internal {
  704. CV_EXPORTS bool isOpenCLForced();
  705. #define OCL_FORCE_CHECK(condition) (cv::ocl::internal::isOpenCLForced() || (condition))
  706. CV_EXPORTS bool isPerformanceCheckBypassed();
  707. #define OCL_PERFORMANCE_CHECK(condition) (cv::ocl::internal::isPerformanceCheckBypassed() || (condition))
  708. CV_EXPORTS bool isCLBuffer(UMat& u);
  709. } // namespace internal
  710. #endif
  711. //! @}
  712. }}
  713. #endif