parallel_prefix_sum.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "parallel_prefix_sum.h"
  17. #include "../sys/regression.h"
  18. namespace embree
  19. {
  20. struct parallel_prefix_sum_regression_test : public RegressionTest
  21. {
  22. parallel_prefix_sum_regression_test(const char* name) : RegressionTest(name) {
  23. registerRegressionTest(this);
  24. }
  25. bool run ()
  26. {
  27. bool passed = true;
  28. const size_t M = 10;
  29. for (size_t N=10; N<10000000; N=size_t(2.1*N))
  30. {
  31. /* initialize array with random numbers */
  32. uint32_t sum0 = 0;
  33. std::vector<uint32_t> src(N);
  34. for (size_t i=0; i<N; i++) {
  35. sum0 += src[i] = rand();
  36. }
  37. /* calculate parallel prefix sum */
  38. std::vector<uint32_t> dst(N);
  39. for (auto& v : dst) v = 0;
  40. for (size_t i=0; i<M; i++) {
  41. uint32_t sum1 = parallel_prefix_sum(src,dst,N,0,std::plus<uint32_t>());
  42. passed &= (sum0 == sum1);
  43. }
  44. /* check if prefix sum is correct */
  45. for (size_t i=0, sum=0; i<N; sum+=src[i++])
  46. passed &= ((uint32_t)sum == dst[i]);
  47. }
  48. return passed;
  49. }
  50. };
  51. parallel_prefix_sum_regression_test parallel_prefix_sum_regression("parallel_prefix_sum_regression");
  52. }