Open3D (C++ API)  0.15.1
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) 2018-2021 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 defined(__cpp_lib_parallel_algorithm) && TBB_INTERFACE_VERSION >= 10000
33#include <execution>
34#include <numeric>
35#endif
36
37namespace open3d {
38namespace utility {
39
40namespace {
41template <class Tin, class Tout>
42class ScanSumBody {
43 Tout sum;
44 const Tin* in;
45 Tout* const out;
46
47public:
48 ScanSumBody(Tout* out_, const Tin* in_) : sum(0), in(in_), out(out_) {}
49 Tout get_sum() const { return sum; }
50
51 template <class Tag>
52 void operator()(const tbb::blocked_range<size_t>& r, Tag) {
53 Tout temp = sum;
54 for (size_t i = r.begin(); i < r.end(); ++i) {
55 temp = temp + in[i];
56 if (Tag::is_final_scan()) out[i] = temp;
57 }
58 sum = temp;
59 }
60 ScanSumBody(ScanSumBody& b, tbb::split) : sum(0), in(b.in), out(b.out) {}
61 void reverse_join(ScanSumBody& a) { sum = a.sum + sum; }
62 void assign(ScanSumBody& b) { sum = b.sum; }
63};
64} // namespace
65
66template <class Tin, class Tout>
67void InclusivePrefixSum(const Tin* first, const Tin* last, Tout* out) {
68#if defined(__cpp_lib_parallel_algorithm) && TBB_INTERFACE_VERSION >= 10000
69 // use stl if we have TBB 2018 or later
70 std::inclusive_scan(std::execution::par_unseq, first, last, out);
71#else
72 ScanSumBody<Tin, Tout> body(out, first);
73 size_t n = std::distance(first, last);
74 tbb::parallel_scan(tbb::blocked_range<size_t>(0, n), body);
75#endif
76}
77
78} // namespace utility
79} // namespace open3d
void InclusivePrefixSum(const Tin *first, const Tin *last, Tout *out)
Definition: ParallelScan.h:67
Definition: PinholeCameraIntrinsic.cpp:35