TestCompact.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. TestCompact::TestCompact(std::string testName_, NCVTestSourceProvider<Ncv32u> &src_,
  44. Ncv32u length_, Ncv32u badElem_, Ncv32u badElemPercentage_)
  45. :
  46. NCVTestProvider(testName_),
  47. src(src_),
  48. length(length_),
  49. badElem(badElem_),
  50. badElemPercentage(badElemPercentage_ > 100 ? 100 : badElemPercentage_)
  51. {
  52. }
  53. bool TestCompact::toString(std::ofstream &strOut)
  54. {
  55. strOut << "length=" << length << std::endl;
  56. strOut << "badElem=" << badElem << std::endl;
  57. strOut << "badElemPercentage=" << badElemPercentage << std::endl;
  58. return true;
  59. }
  60. bool TestCompact::init()
  61. {
  62. return true;
  63. }
  64. bool TestCompact::process()
  65. {
  66. NCVStatus ncvStat;
  67. bool rcode = false;
  68. NCVVectorAlloc<Ncv32u> h_vecSrc(*this->allocatorCPU.get(), this->length);
  69. ncvAssertReturn(h_vecSrc.isMemAllocated(), false);
  70. NCVVectorAlloc<Ncv32u> d_vecSrc(*this->allocatorGPU.get(), this->length);
  71. ncvAssertReturn(d_vecSrc.isMemAllocated(), false);
  72. NCVVectorAlloc<Ncv32u> h_vecDst(*this->allocatorCPU.get(), this->length);
  73. ncvAssertReturn(h_vecDst.isMemAllocated(), false);
  74. NCVVectorAlloc<Ncv32u> d_vecDst(*this->allocatorGPU.get(), this->length);
  75. ncvAssertReturn(d_vecDst.isMemAllocated(), false);
  76. NCVVectorAlloc<Ncv32u> h_vecDst_d(*this->allocatorCPU.get(), this->length);
  77. ncvAssertReturn(h_vecDst_d.isMemAllocated(), false);
  78. NCV_SET_SKIP_COND(this->allocatorGPU.get()->isCounting());
  79. NCV_SKIP_COND_BEGIN
  80. ncvAssertReturn(this->src.fill(h_vecSrc), false);
  81. for (Ncv32u i=0; i<this->length; i++)
  82. {
  83. Ncv32u tmp = (h_vecSrc.ptr()[i]) & 0xFF;
  84. tmp = tmp * 99 / 255;
  85. if (tmp < this->badElemPercentage)
  86. {
  87. h_vecSrc.ptr()[i] = this->badElem;
  88. }
  89. }
  90. NCV_SKIP_COND_END
  91. NCVVectorAlloc<Ncv32u> h_dstLen(*this->allocatorCPU.get(), 1);
  92. ncvAssertReturn(h_dstLen.isMemAllocated(), false);
  93. Ncv32u bufSize;
  94. ncvStat = nppsStCompactGetSize_32u(this->length, &bufSize, this->devProp);
  95. ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
  96. NCVVectorAlloc<Ncv8u> d_tmpBuf(*this->allocatorGPU.get(), bufSize);
  97. ncvAssertReturn(d_tmpBuf.isMemAllocated(), false);
  98. Ncv32u h_outElemNum_h = 0;
  99. NCV_SKIP_COND_BEGIN
  100. ncvStat = h_vecSrc.copySolid(d_vecSrc, 0);
  101. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  102. ncvStat = nppsStCompact_32u(d_vecSrc.ptr(), this->length,
  103. d_vecDst.ptr(), h_dstLen.ptr(), this->badElem,
  104. d_tmpBuf.ptr(), bufSize, this->devProp);
  105. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  106. ncvStat = d_vecDst.copySolid(h_vecDst_d, 0);
  107. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  108. ncvStat = nppsStCompact_32u_host(h_vecSrc.ptr(), this->length, h_vecDst.ptr(), &h_outElemNum_h, this->badElem);
  109. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  110. NCV_SKIP_COND_END
  111. //bit-to-bit check
  112. bool bLoopVirgin = true;
  113. NCV_SKIP_COND_BEGIN
  114. if (h_dstLen.ptr()[0] != h_outElemNum_h)
  115. {
  116. bLoopVirgin = false;
  117. }
  118. else
  119. {
  120. for (Ncv32u i=0; bLoopVirgin && i < h_outElemNum_h; i++)
  121. {
  122. if (h_vecDst.ptr()[i] != h_vecDst_d.ptr()[i])
  123. {
  124. bLoopVirgin = false;
  125. }
  126. }
  127. }
  128. NCV_SKIP_COND_END
  129. if (bLoopVirgin)
  130. {
  131. rcode = true;
  132. }
  133. return rcode;
  134. }
  135. bool TestCompact::deinit()
  136. {
  137. return true;
  138. }