Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ParallelScan.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2020 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <tbb/parallel_for.h>
30 #include <tbb/parallel_scan.h>
31 
32 #if TBB_INTERFACE_VERSION >= 10000
33 
34 // Check if the C++ standard library implements parallel algorithms
35 // and use this over parallelstl to avoid conflicts.
36 // Clang does not implement it so far, so checking for C++17 is not sufficient.
37 #ifdef __cpp_lib_parallel_algorithm
38 #include <execution>
39 #include <numeric>
40 #else
41 #include <pstl/execution>
42 #include <pstl/numeric>
43 
44 // parallelstl incorrectly assumes MSVC to unconditionally implement
45 // parallel algorithms even if __cpp_lib_parallel_algorithm is not defined.
46 // So manually include the header which pulls all "pstl::execution" definitions
47 // into the "std" namespace.
48 #if __PSTL_CPP17_EXECUTION_POLICIES_PRESENT
49 #include <pstl/internal/glue_execution_defs.h>
50 #endif
51 
52 #endif
53 #endif
54 
55 namespace open3d {
56 namespace utility {
57 
58 namespace {
59 template <class Tin, class Tout>
60 class ScanSumBody {
61  Tout sum;
62  const Tin* in;
63  Tout* const out;
64 
65 public:
66  ScanSumBody(Tout* out_, const Tin* in_) : sum(0), in(in_), out(out_) {}
67  Tout get_sum() const { return sum; }
68 
69  template <class Tag>
70  void operator()(const tbb::blocked_range<size_t>& r, Tag) {
71  Tout temp = sum;
72  for (size_t i = r.begin(); i < r.end(); ++i) {
73  temp = temp + in[i];
74  if (Tag::is_final_scan()) out[i] = temp;
75  }
76  sum = temp;
77  }
78  ScanSumBody(ScanSumBody& b, tbb::split) : sum(0), in(b.in), out(b.out) {}
79  void reverse_join(ScanSumBody& a) { sum = a.sum + sum; }
80  void assign(ScanSumBody& b) { sum = b.sum; }
81 };
82 } // namespace
83 
84 template <class Tin, class Tout>
85 void InclusivePrefixSum(const Tin* first, const Tin* last, Tout* out) {
86 #if TBB_INTERFACE_VERSION >= 10000
87  // use parallelstl if we have TBB 2018 or later
88  std::inclusive_scan(std::execution::par_unseq, first, last, out);
89 #else
90  ScanSumBody<Tin, Tout> body(out, first);
91  size_t n = std::distance(first, last);
92  tbb::parallel_scan(tbb::blocked_range<size_t>(0, n), body);
93 #endif
94 }
95 
96 } // namespace utility
97 } // namespace open3d
void InclusivePrefixSum(const Tin *first, const Tin *last, Tout *out)
Definition: ParallelScan.h:85
Definition: PinholeCameraIntrinsic.cpp:35