cuda.inl.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_CUDAINL_HPP
  44. #define OPENCV_CORE_CUDAINL_HPP
  45. #include "opencv2/core/cuda.hpp"
  46. //! @cond IGNORED
  47. namespace cv { namespace cuda {
  48. //===================================================================================
  49. // GpuMat
  50. //===================================================================================
  51. inline
  52. GpuMat::GpuMat(Allocator* allocator_)
  53. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  54. {}
  55. inline
  56. GpuMat::GpuMat(int rows_, int cols_, int type_, Allocator* allocator_)
  57. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  58. {
  59. if (rows_ > 0 && cols_ > 0)
  60. create(rows_, cols_, type_);
  61. }
  62. inline
  63. GpuMat::GpuMat(Size size_, int type_, Allocator* allocator_)
  64. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  65. {
  66. if (size_.height > 0 && size_.width > 0)
  67. create(size_.height, size_.width, type_);
  68. }
  69. // WARNING: unreachable code using Ninja
  70. #if defined _MSC_VER && _MSC_VER >= 1920
  71. #pragma warning(push)
  72. #pragma warning(disable: 4702)
  73. #endif
  74. inline
  75. GpuMat::GpuMat(int rows_, int cols_, int type_, Scalar s_, Allocator* allocator_)
  76. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  77. {
  78. if (rows_ > 0 && cols_ > 0)
  79. {
  80. create(rows_, cols_, type_);
  81. setTo(s_);
  82. }
  83. }
  84. inline
  85. GpuMat::GpuMat(Size size_, int type_, Scalar s_, Allocator* allocator_)
  86. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  87. {
  88. if (size_.height > 0 && size_.width > 0)
  89. {
  90. create(size_.height, size_.width, type_);
  91. setTo(s_);
  92. }
  93. }
  94. #if defined _MSC_VER && _MSC_VER >= 1920
  95. #pragma warning(pop)
  96. #endif
  97. inline
  98. GpuMat::GpuMat(const GpuMat& m)
  99. : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data), refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), allocator(m.allocator)
  100. {
  101. if (refcount)
  102. CV_XADD(refcount, 1);
  103. }
  104. inline
  105. GpuMat::GpuMat(InputArray arr, Allocator* allocator_) :
  106. flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  107. {
  108. upload(arr);
  109. }
  110. inline
  111. GpuMat::~GpuMat()
  112. {
  113. release();
  114. }
  115. inline
  116. GpuMat& GpuMat::operator =(const GpuMat& m)
  117. {
  118. if (this != &m)
  119. {
  120. GpuMat temp(m);
  121. swap(temp);
  122. }
  123. return *this;
  124. }
  125. inline
  126. void GpuMat::create(Size size_, int type_)
  127. {
  128. create(size_.height, size_.width, type_);
  129. }
  130. inline
  131. void GpuMat::swap(GpuMat& b)
  132. {
  133. std::swap(flags, b.flags);
  134. std::swap(rows, b.rows);
  135. std::swap(cols, b.cols);
  136. std::swap(step, b.step);
  137. std::swap(data, b.data);
  138. std::swap(datastart, b.datastart);
  139. std::swap(dataend, b.dataend);
  140. std::swap(refcount, b.refcount);
  141. std::swap(allocator, b.allocator);
  142. }
  143. inline
  144. GpuMat GpuMat::clone() const
  145. {
  146. GpuMat m;
  147. copyTo(m);
  148. return m;
  149. }
  150. // WARNING: unreachable code using Ninja
  151. #if defined _MSC_VER && _MSC_VER >= 1920
  152. #pragma warning(push)
  153. #pragma warning(disable: 4702)
  154. #endif
  155. inline
  156. void GpuMat::copyTo(OutputArray dst, InputArray mask) const
  157. {
  158. copyTo(dst, mask, Stream::Null());
  159. }
  160. #if defined _MSC_VER && _MSC_VER >= 1920
  161. #pragma warning(pop)
  162. #endif
  163. inline
  164. GpuMat& GpuMat::setTo(Scalar s)
  165. {
  166. return setTo(s, Stream::Null());
  167. }
  168. inline
  169. GpuMat& GpuMat::setTo(Scalar s, InputArray mask)
  170. {
  171. return setTo(s, mask, Stream::Null());
  172. }
  173. // WARNING: unreachable code using Ninja
  174. #if defined _MSC_VER && _MSC_VER >= 1920
  175. #pragma warning(push)
  176. #pragma warning(disable: 4702)
  177. #endif
  178. inline
  179. void GpuMat::convertTo(OutputArray dst, int rtype) const
  180. {
  181. convertTo(dst, rtype, Stream::Null());
  182. }
  183. inline
  184. void GpuMat::convertTo(OutputArray dst, int rtype, double alpha, double beta) const
  185. {
  186. convertTo(dst, rtype, alpha, beta, Stream::Null());
  187. }
  188. #if defined _MSC_VER && _MSC_VER >= 1920
  189. #pragma warning(pop)
  190. #endif
  191. inline
  192. void GpuMat::convertTo(OutputArray dst, int rtype, double alpha, Stream& stream) const
  193. {
  194. convertTo(dst, rtype, alpha, 0.0, stream);
  195. }
  196. inline
  197. void GpuMat::assignTo(GpuMat& m, int _type) const
  198. {
  199. if (_type < 0)
  200. m = *this;
  201. else
  202. convertTo(m, _type);
  203. }
  204. inline
  205. uchar* GpuMat::ptr(int y)
  206. {
  207. CV_DbgAssert( (unsigned)y < (unsigned)rows );
  208. return data + step * y;
  209. }
  210. inline
  211. const uchar* GpuMat::ptr(int y) const
  212. {
  213. CV_DbgAssert( (unsigned)y < (unsigned)rows );
  214. return data + step * y;
  215. }
  216. template<typename _Tp> inline
  217. _Tp* GpuMat::ptr(int y)
  218. {
  219. return (_Tp*)ptr(y);
  220. }
  221. template<typename _Tp> inline
  222. const _Tp* GpuMat::ptr(int y) const
  223. {
  224. return (const _Tp*)ptr(y);
  225. }
  226. template <class T> inline
  227. GpuMat::operator PtrStepSz<T>() const
  228. {
  229. return PtrStepSz<T>(rows, cols, (T*)data, step);
  230. }
  231. template <class T> inline
  232. GpuMat::operator PtrStep<T>() const
  233. {
  234. return PtrStep<T>((T*)data, step);
  235. }
  236. inline
  237. GpuMat GpuMat::row(int y) const
  238. {
  239. return GpuMat(*this, Range(y, y+1), Range::all());
  240. }
  241. inline
  242. GpuMat GpuMat::col(int x) const
  243. {
  244. return GpuMat(*this, Range::all(), Range(x, x+1));
  245. }
  246. inline
  247. GpuMat GpuMat::rowRange(int startrow, int endrow) const
  248. {
  249. return GpuMat(*this, Range(startrow, endrow), Range::all());
  250. }
  251. inline
  252. GpuMat GpuMat::rowRange(Range r) const
  253. {
  254. return GpuMat(*this, r, Range::all());
  255. }
  256. inline
  257. GpuMat GpuMat::colRange(int startcol, int endcol) const
  258. {
  259. return GpuMat(*this, Range::all(), Range(startcol, endcol));
  260. }
  261. inline
  262. GpuMat GpuMat::colRange(Range r) const
  263. {
  264. return GpuMat(*this, Range::all(), r);
  265. }
  266. inline
  267. GpuMat GpuMat::operator ()(Range rowRange_, Range colRange_) const
  268. {
  269. return GpuMat(*this, rowRange_, colRange_);
  270. }
  271. inline
  272. GpuMat GpuMat::operator ()(Rect roi) const
  273. {
  274. return GpuMat(*this, roi);
  275. }
  276. inline
  277. bool GpuMat::isContinuous() const
  278. {
  279. return (flags & Mat::CONTINUOUS_FLAG) != 0;
  280. }
  281. inline
  282. size_t GpuMat::elemSize() const
  283. {
  284. return CV_ELEM_SIZE(flags);
  285. }
  286. inline
  287. size_t GpuMat::elemSize1() const
  288. {
  289. return CV_ELEM_SIZE1(flags);
  290. }
  291. inline
  292. int GpuMat::type() const
  293. {
  294. return CV_MAT_TYPE(flags);
  295. }
  296. inline
  297. int GpuMat::depth() const
  298. {
  299. return CV_MAT_DEPTH(flags);
  300. }
  301. inline
  302. int GpuMat::channels() const
  303. {
  304. return CV_MAT_CN(flags);
  305. }
  306. inline
  307. size_t GpuMat::step1() const
  308. {
  309. return step / elemSize1();
  310. }
  311. inline
  312. Size GpuMat::size() const
  313. {
  314. return Size(cols, rows);
  315. }
  316. inline
  317. bool GpuMat::empty() const
  318. {
  319. return data == 0;
  320. }
  321. inline
  322. void* GpuMat::cudaPtr() const
  323. {
  324. return data;
  325. }
  326. static inline
  327. GpuMat createContinuous(int rows, int cols, int type)
  328. {
  329. GpuMat m;
  330. createContinuous(rows, cols, type, m);
  331. return m;
  332. }
  333. static inline
  334. void createContinuous(Size size, int type, OutputArray arr)
  335. {
  336. createContinuous(size.height, size.width, type, arr);
  337. }
  338. static inline
  339. GpuMat createContinuous(Size size, int type)
  340. {
  341. GpuMat m;
  342. createContinuous(size, type, m);
  343. return m;
  344. }
  345. static inline
  346. void ensureSizeIsEnough(Size size, int type, OutputArray arr)
  347. {
  348. ensureSizeIsEnough(size.height, size.width, type, arr);
  349. }
  350. static inline
  351. void swap(GpuMat& a, GpuMat& b)
  352. {
  353. a.swap(b);
  354. }
  355. //===================================================================================
  356. // GpuMatND
  357. //===================================================================================
  358. inline
  359. GpuMatND::GpuMatND() :
  360. flags(0), dims(0), data(nullptr), offset(0)
  361. {
  362. }
  363. inline
  364. GpuMatND::GpuMatND(SizeArray _size, int _type) :
  365. flags(0), dims(0), data(nullptr), offset(0)
  366. {
  367. create(std::move(_size), _type);
  368. }
  369. inline
  370. void GpuMatND::swap(GpuMatND& m) noexcept
  371. {
  372. std::swap(*this, m);
  373. }
  374. inline
  375. bool GpuMatND::isContinuous() const
  376. {
  377. return (flags & Mat::CONTINUOUS_FLAG) != 0;
  378. }
  379. inline
  380. bool GpuMatND::isSubmatrix() const
  381. {
  382. return (flags & Mat::SUBMATRIX_FLAG) != 0;
  383. }
  384. inline
  385. size_t GpuMatND::elemSize() const
  386. {
  387. return CV_ELEM_SIZE(flags);
  388. }
  389. inline
  390. size_t GpuMatND::elemSize1() const
  391. {
  392. return CV_ELEM_SIZE1(flags);
  393. }
  394. inline
  395. bool GpuMatND::empty() const
  396. {
  397. return data == nullptr;
  398. }
  399. inline
  400. bool GpuMatND::external() const
  401. {
  402. return !empty() && data_.use_count() == 0;
  403. }
  404. inline
  405. uchar* GpuMatND::getDevicePtr() const
  406. {
  407. return data + offset;
  408. }
  409. inline
  410. size_t GpuMatND::total() const
  411. {
  412. size_t p = 1;
  413. for(auto s : size)
  414. p *= s;
  415. return p;
  416. }
  417. inline
  418. size_t GpuMatND::totalMemSize() const
  419. {
  420. return size[0] * step[0];
  421. }
  422. inline
  423. int GpuMatND::type() const
  424. {
  425. return CV_MAT_TYPE(flags);
  426. }
  427. //===================================================================================
  428. // HostMem
  429. //===================================================================================
  430. inline
  431. HostMem::HostMem(AllocType alloc_type_)
  432. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  433. {
  434. }
  435. inline
  436. HostMem::HostMem(const HostMem& m)
  437. : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data), refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), alloc_type(m.alloc_type)
  438. {
  439. if( refcount )
  440. CV_XADD(refcount, 1);
  441. }
  442. inline
  443. HostMem::HostMem(int rows_, int cols_, int type_, AllocType alloc_type_)
  444. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  445. {
  446. if (rows_ > 0 && cols_ > 0)
  447. create(rows_, cols_, type_);
  448. }
  449. inline
  450. HostMem::HostMem(Size size_, int type_, AllocType alloc_type_)
  451. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  452. {
  453. if (size_.height > 0 && size_.width > 0)
  454. create(size_.height, size_.width, type_);
  455. }
  456. inline
  457. HostMem::HostMem(InputArray arr, AllocType alloc_type_)
  458. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  459. {
  460. arr.getMat().copyTo(*this);
  461. }
  462. inline
  463. HostMem::~HostMem()
  464. {
  465. release();
  466. }
  467. inline
  468. HostMem& HostMem::operator =(const HostMem& m)
  469. {
  470. if (this != &m)
  471. {
  472. HostMem temp(m);
  473. swap(temp);
  474. }
  475. return *this;
  476. }
  477. inline
  478. void HostMem::swap(HostMem& b)
  479. {
  480. std::swap(flags, b.flags);
  481. std::swap(rows, b.rows);
  482. std::swap(cols, b.cols);
  483. std::swap(step, b.step);
  484. std::swap(data, b.data);
  485. std::swap(datastart, b.datastart);
  486. std::swap(dataend, b.dataend);
  487. std::swap(refcount, b.refcount);
  488. std::swap(alloc_type, b.alloc_type);
  489. }
  490. inline
  491. HostMem HostMem::clone() const
  492. {
  493. HostMem m(size(), type(), alloc_type);
  494. createMatHeader().copyTo(m);
  495. return m;
  496. }
  497. inline
  498. void HostMem::create(Size size_, int type_)
  499. {
  500. create(size_.height, size_.width, type_);
  501. }
  502. inline
  503. Mat HostMem::createMatHeader() const
  504. {
  505. return Mat(size(), type(), data, step);
  506. }
  507. inline
  508. bool HostMem::isContinuous() const
  509. {
  510. return (flags & Mat::CONTINUOUS_FLAG) != 0;
  511. }
  512. inline
  513. size_t HostMem::elemSize() const
  514. {
  515. return CV_ELEM_SIZE(flags);
  516. }
  517. inline
  518. size_t HostMem::elemSize1() const
  519. {
  520. return CV_ELEM_SIZE1(flags);
  521. }
  522. inline
  523. int HostMem::type() const
  524. {
  525. return CV_MAT_TYPE(flags);
  526. }
  527. inline
  528. int HostMem::depth() const
  529. {
  530. return CV_MAT_DEPTH(flags);
  531. }
  532. inline
  533. int HostMem::channels() const
  534. {
  535. return CV_MAT_CN(flags);
  536. }
  537. inline
  538. size_t HostMem::step1() const
  539. {
  540. return step / elemSize1();
  541. }
  542. inline
  543. Size HostMem::size() const
  544. {
  545. return Size(cols, rows);
  546. }
  547. inline
  548. bool HostMem::empty() const
  549. {
  550. return data == 0;
  551. }
  552. static inline
  553. void swap(HostMem& a, HostMem& b)
  554. {
  555. a.swap(b);
  556. }
  557. //===================================================================================
  558. // Stream
  559. //===================================================================================
  560. inline
  561. Stream::Stream(const Ptr<Impl>& impl)
  562. : impl_(impl)
  563. {
  564. }
  565. //===================================================================================
  566. // Event
  567. //===================================================================================
  568. inline
  569. Event::Event(const Ptr<Impl>& impl)
  570. : impl_(impl)
  571. {
  572. }
  573. //===================================================================================
  574. // Initialization & Info
  575. //===================================================================================
  576. // WARNING: unreachable code using Ninja
  577. #if defined _MSC_VER && _MSC_VER >= 1920
  578. #pragma warning(push)
  579. #pragma warning(disable: 4702)
  580. #endif
  581. inline
  582. bool TargetArchs::has(int major, int minor)
  583. {
  584. return hasPtx(major, minor) || hasBin(major, minor);
  585. }
  586. inline
  587. bool TargetArchs::hasEqualOrGreater(int major, int minor)
  588. {
  589. return hasEqualOrGreaterPtx(major, minor) || hasEqualOrGreaterBin(major, minor);
  590. }
  591. inline
  592. DeviceInfo::DeviceInfo()
  593. {
  594. device_id_ = getDevice();
  595. }
  596. #if defined _MSC_VER && _MSC_VER >= 1920
  597. #pragma warning(pop)
  598. #endif
  599. inline
  600. DeviceInfo::DeviceInfo(int device_id)
  601. {
  602. CV_Assert( device_id >= 0 && device_id < getCudaEnabledDeviceCount() );
  603. device_id_ = device_id;
  604. }
  605. // WARNING: unreachable code using Ninja
  606. #if defined _MSC_VER && _MSC_VER >= 1920
  607. #pragma warning(push)
  608. #pragma warning(disable: 4702)
  609. #endif
  610. inline
  611. int DeviceInfo::deviceID() const
  612. {
  613. return device_id_;
  614. }
  615. inline
  616. size_t DeviceInfo::freeMemory() const
  617. {
  618. size_t _totalMemory = 0, _freeMemory = 0;
  619. queryMemory(_totalMemory, _freeMemory);
  620. return _freeMemory;
  621. }
  622. inline
  623. size_t DeviceInfo::totalMemory() const
  624. {
  625. size_t _totalMemory = 0, _freeMemory = 0;
  626. queryMemory(_totalMemory, _freeMemory);
  627. return _totalMemory;
  628. }
  629. inline
  630. bool DeviceInfo::supports(FeatureSet feature_set) const
  631. {
  632. int version = majorVersion() * 10 + minorVersion();
  633. return version >= feature_set;
  634. }
  635. #if defined _MSC_VER && _MSC_VER >= 1920
  636. #pragma warning(pop)
  637. #endif
  638. }} // namespace cv { namespace cuda {
  639. //===================================================================================
  640. // Mat
  641. //===================================================================================
  642. namespace cv {
  643. inline
  644. Mat::Mat(const cuda::GpuMat& m)
  645. : flags(0), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows)
  646. {
  647. m.download(*this);
  648. }
  649. }
  650. //! @endcond
  651. #endif // OPENCV_CORE_CUDAINL_HPP