Open3D (C++ API)  0.18.0+252c867
ParallelScan.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <tbb/parallel_for.h>
11 #include <tbb/parallel_scan.h>
12 
13 // clang-format off
14 #if TBB_INTERFACE_VERSION >= 10000
15  #ifdef OPEN3D_USE_ONEAPI_PACKAGES
16  #include <oneapi/dpl/execution>
17  #include <oneapi/dpl/numeric>
18  #else
19  // Check if the C++ standard library implements parallel algorithms
20  // and use this over parallelstl to avoid conflicts.
21  // Clang does not implement it so far, so checking for C++17 is not sufficient.
22  #ifdef __cpp_lib_parallel_algorithm
23  #include <execution>
24  #include <numeric>
25  #else
26  #include <pstl/execution>
27  #include <pstl/numeric>
28  // parallelstl incorrectly assumes MSVC to unconditionally implement
29  // parallel algorithms even if __cpp_lib_parallel_algorithm is not
30  // defined. So manually include the header which pulls all
31  // "pstl::execution" definitions into the "std" namespace.
32  #if __PSTL_CPP17_EXECUTION_POLICIES_PRESENT
33  #include <pstl/internal/glue_execution_defs.h>
34  #endif
35  #endif
36  #endif
37 #endif
38 
39 // clang-format on
40 
41 namespace open3d {
42 namespace utility {
43 
44 namespace {
45 template <class Tin, class Tout>
46 class ScanSumBody {
47  Tout sum;
48  const Tin* in;
49  Tout* const out;
50 
51 public:
52  ScanSumBody(Tout* out_, const Tin* in_) : sum(0), in(in_), out(out_) {}
53  Tout get_sum() const { return sum; }
54 
55  template <class Tag>
56  void operator()(const tbb::blocked_range<size_t>& r, Tag) {
57  Tout temp = sum;
58  for (size_t i = r.begin(); i < r.end(); ++i) {
59  temp = temp + in[i];
60  if (Tag::is_final_scan()) out[i] = temp;
61  }
62  sum = temp;
63  }
64  ScanSumBody(ScanSumBody& b, tbb::split) : sum(0), in(b.in), out(b.out) {}
65  void reverse_join(ScanSumBody& a) { sum = a.sum + sum; }
66  void assign(ScanSumBody& b) { sum = b.sum; }
67 };
68 } // namespace
69 
70 template <class Tin, class Tout>
71 void InclusivePrefixSum(const Tin* first, const Tin* last, Tout* out) {
72 #if TBB_INTERFACE_VERSION >= 10000
73  // use parallelstl if we have TBB 2018 or later
74 #ifdef OPEN3D_USE_ONEAPI_PACKAGES
75  std::inclusive_scan(oneapi::dpl::execution::par_unseq, first, last, out);
76 
77 #else
78  std::inclusive_scan(std::execution::par_unseq, first, last, out);
79 #endif
80 #else
81  ScanSumBody<Tin, Tout> body(out, first);
82  size_t n = std::distance(first, last);
83  tbb::parallel_scan(tbb::blocked_range<size_t>(0, n), body);
84 #endif
85 }
86 
87 } // namespace utility
88 } // namespace open3d
void InclusivePrefixSum(const Tin *first, const Tin *last, Tout *out)
Definition: ParallelScan.h:71
Definition: PinholeCameraIntrinsic.cpp:16