parallel_reduce.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_reduce.h"
  17. #include "../sys/regression.h"
  18. namespace embree
  19. {
  20. struct parallel_reduce_regression_test : public RegressionTest
  21. {
  22. parallel_reduce_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. /* sequentially calculate sum of squares */
  32. size_t sum0 = 0;
  33. for (size_t i=0; i<N; i++) {
  34. sum0 += i*i;
  35. }
  36. /* parallel calculation of sum of squares */
  37. for (size_t m=0; m<M; m++)
  38. {
  39. size_t sum1 = parallel_reduce( size_t(0), size_t(N), size_t(1024), size_t(0), [&](const range<size_t>& r) -> size_t
  40. {
  41. size_t s = 0;
  42. for (size_t i=r.begin(); i<r.end(); i++)
  43. s += i*i;
  44. return s;
  45. },
  46. [](const size_t v0, const size_t v1) {
  47. return v0+v1;
  48. });
  49. passed = sum0 == sum1;
  50. }
  51. }
  52. return passed;
  53. }
  54. };
  55. parallel_reduce_regression_test parallel_reduce_regression("parallel_reduce_regression_test");
  56. }