From 077852ed2acd183e13a01f547faee5108a18df29 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 01:26:17 +0800 Subject: [PATCH 01/13] simple speedup from stl algorithms --- src/collider/src/collider.cpp | 2 +- src/polygon/src/polygon.cpp | 3 +- src/utilities/include/par.h | 64 ++++++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/collider/src/collider.cpp b/src/collider/src/collider.cpp index b9253d256..58a565edf 100644 --- a/src/collider/src/collider.cpp +++ b/src/collider/src/collider.cpp @@ -287,7 +287,7 @@ SparseIndices Collider::Collisions(const VecDH& queriesIn) const { {thrust::pair(nullptr, nullptr), counts.ptrD(), nodeBBox_.ptrD(), internalChildren_.ptrD()})); // compute start index for each query and total count - exclusive_scan(policy, counts.begin(), counts.end(), counts.begin()); + exclusive_scan(policy, counts.begin(), counts.end(), counts.begin(), 0, std::plus()); SparseIndices queryTri(counts.back()); // actually recording collisions for_each_n(policy, zip(queriesIn.cbegin(), countAt(0)), queriesIn.size(), diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 0683ece83..f86577844 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -15,6 +15,7 @@ #include "polygon.h" #include +#include #include #include #include @@ -784,7 +785,7 @@ class Monotones { starts.push_back(v); } } - std::sort(starts.begin(), starts.end(), cmp); + std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); std::vector skipped; VertItr insertAt = monotones_.begin(); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index e606f623c..1da9f92f2 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -25,6 +25,9 @@ #include #include +#include +#include + #if MANIFOLD_PAR == 'O' #include #define MANIFOLD_PAR_NS omp @@ -143,7 +146,6 @@ THRUST_DYNAMIC_BACKEND_VOID(gather) THRUST_DYNAMIC_BACKEND_VOID(scatter) THRUST_DYNAMIC_BACKEND_VOID(for_each) THRUST_DYNAMIC_BACKEND_VOID(for_each_n) -THRUST_DYNAMIC_BACKEND_VOID(sort) THRUST_DYNAMIC_BACKEND_VOID(stable_sort) THRUST_DYNAMIC_BACKEND_VOID(fill) THRUST_DYNAMIC_BACKEND_VOID(sequence) @@ -152,7 +154,6 @@ THRUST_DYNAMIC_BACKEND_VOID(stable_sort_by_key) THRUST_DYNAMIC_BACKEND_VOID(copy) THRUST_DYNAMIC_BACKEND_VOID(transform) THRUST_DYNAMIC_BACKEND_VOID(inclusive_scan) -THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan) THRUST_DYNAMIC_BACKEND_VOID(uninitialized_fill) THRUST_DYNAMIC_BACKEND_VOID(uninitialized_copy) THRUST_DYNAMIC_BACKEND_VOID(copy_n) @@ -165,9 +166,6 @@ THRUST_DYNAMIC_BACKEND(binary_search, bool) // void implies that the user have to specify the return type in the template // argument, as we are unable to deduce it THRUST_DYNAMIC_BACKEND(remove, void) -THRUST_DYNAMIC_BACKEND(copy_if, void) -THRUST_DYNAMIC_BACKEND(remove_if, void) -THRUST_DYNAMIC_BACKEND(unique, void) THRUST_DYNAMIC_BACKEND(find, void) THRUST_DYNAMIC_BACKEND(find_if, void) THRUST_DYNAMIC_BACKEND(reduce_by_key, void) @@ -175,4 +173,60 @@ THRUST_DYNAMIC_BACKEND(transform_reduce, void) THRUST_DYNAMIC_BACKEND(lower_bound, void) THRUST_DYNAMIC_BACKEND(gather_if, void) +#if MANIFOLD_PAR == 'T' && !MANIFOLD_USE_CUDA +// these are faster when compiled with gcc +template +Ret remove_if(ExecutionPolicy policy, Args... args) { + if (policy == ExecutionPolicy::Seq) + return std::remove_if(args...); + else + return std::remove_if(std::execution::par_unseq, args...); +} +template +Ret unique(ExecutionPolicy policy, Args... args) { + if (policy == ExecutionPolicy::Seq) + return std::unique(args...); + else + return std::unique(std::execution::par_unseq, args...); +} +template +void sort(ExecutionPolicy policy, Args... args) { + if (policy == ExecutionPolicy::Seq) + return std::sort(args...); + else + return std::sort(std::execution::par_unseq, args...); +} +template +void exclusive_scan(ExecutionPolicy policy, Args... args) { + // https://github.com/llvm/llvm-project/issues/59810 + std::exclusive_scan(args...); +} +template +OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, + InputIterator1 last, InputIterator2 stencil, + OutputIterator result, Predicate pred) { + if (policy == ExecutionPolicy::Seq) + return thrust::copy_if(thrust::cpp::par, first, last, stencil, result, + pred); + else + return thrust::copy_if(first, last, stencil, result, pred); +} +template +OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, + InputIterator1 last, OutputIterator result, + Predicate pred) { + if (policy == ExecutionPolicy::Seq) + return std::copy_if(first, last, result, pred); + else + return std::copy_if(std::execution::par_unseq, first, last, result, pred); +} +#else +THRUST_DYNAMIC_BACKEND(remove_if, void) +THRUST_DYNAMIC_BACKEND(unique, void) +THRUST_DYNAMIC_BACKEND_VOID(sort) +THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan) +THRUST_DYNAMIC_BACKEND(copy_if, void) +#endif } // namespace manifold From 8709bf7b20648ecfc338f9708fd0b274c4a9b113 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 01:53:41 +0800 Subject: [PATCH 02/13] fix build --- src/polygon/src/polygon.cpp | 6 ++++++ src/utilities/include/par.h | 5 ++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index f86577844..6542b92de 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -15,7 +15,9 @@ #include "polygon.h" #include +#if MANIFOLD_PAR == 'T' #include +#endif #include #include #include @@ -785,7 +787,11 @@ class Monotones { starts.push_back(v); } } +#if MANIFOLD_PAR == 'T' std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); +#else + std::sort(starts.begin(), starts.end(), cmp); +#endif std::vector skipped; VertItr insertAt = monotones_.begin(); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index 1da9f92f2..de750c1df 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -25,14 +25,13 @@ #include #include -#include -#include - #if MANIFOLD_PAR == 'O' #include #define MANIFOLD_PAR_NS omp #elif MANIFOLD_PAR == 'T' #include +#include +#include #define MANIFOLD_PAR_NS tbb #else #define MANIFOLD_PAR_NS cpp From d1055a742a8668556099f69ea90303a8381ee3a2 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 02:00:47 +0800 Subject: [PATCH 03/13] std::list allocator optimization --- src/polygon/src/polygon.cpp | 41 ++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 6542b92de..62d68a7a0 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "optional_assert.h" @@ -306,14 +307,16 @@ class Monotones { private: struct VertAdj; - typedef std::list::iterator VertItr; + typedef std::pmr::list::iterator VertItr; struct EdgePair; - typedef std::list::iterator PairItr; + typedef std::pmr::list::iterator PairItr; enum VertType { Start, WestSide, EastSide, Merge, End, Skip }; - std::list monotones_; // sweep-line list of verts - std::list activePairs_; // west to east list of monotone edge pairs - std::list inactivePairs_; // completed monotones + std::pmr::monotonic_buffer_resource mbr; + std::pmr::polymorphic_allocator pa{&mbr}; + std::pmr::list monotones_{pa}; // sweep-line list of verts + std::pmr::list activePairs_{pa}; // west to east list of monotone edge pairs + std::pmr::list inactivePairs_{pa}; // completed monotones float precision_; // a triangle of this height or less is degenerate /** @@ -395,7 +398,7 @@ class Monotones { class Triangulator { public: Triangulator(VertItr vert, float precision) : precision_(precision) { - reflex_chain_.push(vert); + reflex_chain_.push_back(vert); other_side_ = vert; } int NumTriangles() const { return triangles_output_; } @@ -410,14 +413,14 @@ class Monotones { */ void ProcessVert(const VertItr vi, bool onRight, bool last, std::vector &triangles) { - VertItr v_top = reflex_chain_.top(); + VertItr v_top = reflex_chain_.back(); if (reflex_chain_.size() < 2) { - reflex_chain_.push(vi); + reflex_chain_.push_back(vi); onRight_ = onRight; return; } - reflex_chain_.pop(); - VertItr vj = reflex_chain_.top(); + reflex_chain_.pop_back(); + VertItr vj = reflex_chain_.back(); if (onRight_ == onRight && !last) { // This only creates enough triangles to ensure the reflex chain is // still reflex. @@ -426,13 +429,13 @@ class Monotones { while (ccw == (onRight_ ? 1 : -1) || ccw == 0) { AddTriangle(triangles, vi, vj, v_top); v_top = vj; - reflex_chain_.pop(); + reflex_chain_.pop_back(); if (reflex_chain_.empty()) break; - vj = reflex_chain_.top(); + vj = reflex_chain_.back(); ccw = CCW(vi->pos, vj->pos, v_top->pos, precision_); } - reflex_chain_.push(v_top); - reflex_chain_.push(vi); + reflex_chain_.push_back(v_top); + reflex_chain_.push_back(vi); } else { // This branch empties the reflex chain and switches sides. It must be // used for the last point, as it will output all the triangles @@ -441,19 +444,19 @@ class Monotones { onRight_ = !onRight_; VertItr v_last = v_top; while (!reflex_chain_.empty()) { - vj = reflex_chain_.top(); + vj = reflex_chain_.back(); AddTriangle(triangles, vi, v_last, vj); v_last = vj; - reflex_chain_.pop(); + reflex_chain_.pop_back(); } - reflex_chain_.push(v_top); - reflex_chain_.push(vi); + reflex_chain_.push_back(v_top); + reflex_chain_.push_back(vi); other_side_ = v_top; } } private: - std::stack reflex_chain_; + std::vector reflex_chain_; VertItr other_side_; // The end vertex across from the reflex chain bool onRight_; // The side the reflex chain is on int triangles_output_ = 0; From 7ebf452e694722197bcf2143322c910c8fb4b1d9 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 02:28:02 +0800 Subject: [PATCH 04/13] added documentation --- src/utilities/include/par.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index de750c1df..00c98b2b6 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -209,6 +209,7 @@ OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, return thrust::copy_if(thrust::cpp::par, first, last, stencil, result, pred); else + // note: this is not a typo, see https://github.com/NVIDIA/thrust/issues/1977 return thrust::copy_if(first, last, stencil, result, pred); } template Date: Sun, 23 Jul 2023 02:16:27 +0800 Subject: [PATCH 05/13] fix weird formatting issues --- src/collider/src/collider.cpp | 3 ++- src/polygon/src/polygon.cpp | 6 +++--- src/utilities/include/par.h | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/collider/src/collider.cpp b/src/collider/src/collider.cpp index 58a565edf..ff90041b9 100644 --- a/src/collider/src/collider.cpp +++ b/src/collider/src/collider.cpp @@ -287,7 +287,8 @@ SparseIndices Collider::Collisions(const VecDH& queriesIn) const { {thrust::pair(nullptr, nullptr), counts.ptrD(), nodeBBox_.ptrD(), internalChildren_.ptrD()})); // compute start index for each query and total count - exclusive_scan(policy, counts.begin(), counts.end(), counts.begin(), 0, std::plus()); + exclusive_scan(policy, counts.begin(), counts.end(), counts.begin(), 0, + std::plus()); SparseIndices queryTri(counts.back()); // actually recording collisions for_each_n(policy, zip(queriesIn.cbegin(), countAt(0)), queriesIn.size(), diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 62d68a7a0..65e37f233 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -20,10 +20,10 @@ #endif #include #include +#include #include #include #include -#include #include "optional_assert.h" @@ -314,8 +314,8 @@ class Monotones { std::pmr::monotonic_buffer_resource mbr; std::pmr::polymorphic_allocator pa{&mbr}; - std::pmr::list monotones_{pa}; // sweep-line list of verts - std::pmr::list activePairs_{pa}; // west to east list of monotone edge pairs + std::pmr::list monotones_{pa}; // sweep-line list of verts + std::pmr::list activePairs_{pa}; // west to east monotone edges std::pmr::list inactivePairs_{pa}; // completed monotones float precision_; // a triangle of this height or less is degenerate diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index 00c98b2b6..d95d21caf 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -30,6 +30,7 @@ #define MANIFOLD_PAR_NS omp #elif MANIFOLD_PAR == 'T' #include + #include #include #define MANIFOLD_PAR_NS tbb @@ -209,7 +210,8 @@ OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, return thrust::copy_if(thrust::cpp::par, first, last, stencil, result, pred); else - // note: this is not a typo, see https://github.com/NVIDIA/thrust/issues/1977 + // note: this is not a typo, see + // https://github.com/NVIDIA/thrust/issues/1977 return thrust::copy_if(first, last, stencil, result, pred); } template Date: Sun, 23 Jul 2023 14:27:38 +0800 Subject: [PATCH 06/13] try fixing MacOS build it seems that Apple Clang 14 does not support pstl --- src/polygon/src/polygon.cpp | 4 ++-- src/utilities/include/par.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 65e37f233..5e4f35ce7 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -15,7 +15,7 @@ #include "polygon.h" #include -#if MANIFOLD_PAR == 'T' +#if MANIFOLD_PAR == 'T' && !(__APPLE__) #include #endif #include @@ -790,7 +790,7 @@ class Monotones { starts.push_back(v); } } -#if MANIFOLD_PAR == 'T' +#if MANIFOLD_PAR == 'T' && !(__APPLE__) std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); #else std::sort(starts.begin(), starts.end(), cmp); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index d95d21caf..574266032 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -173,7 +173,7 @@ THRUST_DYNAMIC_BACKEND(transform_reduce, void) THRUST_DYNAMIC_BACKEND(lower_bound, void) THRUST_DYNAMIC_BACKEND(gather_if, void) -#if MANIFOLD_PAR == 'T' && !MANIFOLD_USE_CUDA +#if MANIFOLD_PAR == 'T' && !MANIFOLD_USE_CUDA && !(__APPLE__) // these are faster when compiled with gcc template Ret remove_if(ExecutionPolicy policy, Args... args) { From ac4384e40359a6d1efaa54a69fb8c4c457c6030c Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 14:39:43 +0800 Subject: [PATCH 07/13] fix again --- src/polygon/src/polygon.cpp | 4 ++-- src/utilities/include/par.h | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 5e4f35ce7..c500a3410 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -15,7 +15,7 @@ #include "polygon.h" #include -#if MANIFOLD_PAR == 'T' && !(__APPLE__) +#if MANIFOLD_PAR == 'T' && !(_APPLE_) #include #endif #include @@ -790,7 +790,7 @@ class Monotones { starts.push_back(v); } } -#if MANIFOLD_PAR == 'T' && !(__APPLE__) +#if MANIFOLD_PAR == 'T' && !(_APPLE_) std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); #else std::sort(starts.begin(), starts.end(), cmp); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index 574266032..658f39d7e 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -32,7 +32,9 @@ #include #include +#if !(_APPLE_) #include +#endif #define MANIFOLD_PAR_NS tbb #else #define MANIFOLD_PAR_NS cpp @@ -173,7 +175,7 @@ THRUST_DYNAMIC_BACKEND(transform_reduce, void) THRUST_DYNAMIC_BACKEND(lower_bound, void) THRUST_DYNAMIC_BACKEND(gather_if, void) -#if MANIFOLD_PAR == 'T' && !MANIFOLD_USE_CUDA && !(__APPLE__) +#if MANIFOLD_PAR == 'T' && !MANIFOLD_USE_CUDA && !(_APPLE_) // these are faster when compiled with gcc template Ret remove_if(ExecutionPolicy policy, Args... args) { From 0e74a54eb0982ea2e678194abdbb0ba7445a047d Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 16:17:40 +0800 Subject: [PATCH 08/13] fix macos build --- src/polygon/src/polygon.cpp | 8 ++++++-- src/utilities/include/par.h | 4 +--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index c500a3410..eed39cdaa 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -15,12 +15,16 @@ #include "polygon.h" #include -#if MANIFOLD_PAR == 'T' && !(_APPLE_) +#if MANIFOLD_PAR == 'T' #include #endif #include #include +#if !_APPLE_ #include +#else +#include +#endif #include #include #include @@ -790,7 +794,7 @@ class Monotones { starts.push_back(v); } } -#if MANIFOLD_PAR == 'T' && !(_APPLE_) +#if MANIFOLD_PAR == 'T' std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); #else std::sort(starts.begin(), starts.end(), cmp); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index 658f39d7e..42b789d44 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -32,9 +32,7 @@ #include #include -#if !(_APPLE_) #include -#endif #define MANIFOLD_PAR_NS tbb #else #define MANIFOLD_PAR_NS cpp @@ -175,7 +173,7 @@ THRUST_DYNAMIC_BACKEND(transform_reduce, void) THRUST_DYNAMIC_BACKEND(lower_bound, void) THRUST_DYNAMIC_BACKEND(gather_if, void) -#if MANIFOLD_PAR == 'T' && !MANIFOLD_USE_CUDA && !(_APPLE_) +#if MANIFOLD_PAR == 'T' // these are faster when compiled with gcc template Ret remove_if(ExecutionPolicy policy, Args... args) { From 7e0f2e4fe4fb01c385a2d20cfd6a9eb1ed64dde6 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 21:21:46 +0800 Subject: [PATCH 09/13] fix apple --- src/polygon/src/polygon.cpp | 2 +- src/utilities/include/par.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index eed39cdaa..1f1fd1faa 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -794,7 +794,7 @@ class Monotones { starts.push_back(v); } } -#if MANIFOLD_PAR == 'T' +#if MANIFOLD_PAR == 'T' && !(_APPLE_) std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); #else std::sort(starts.begin(), starts.end(), cmp); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index 42b789d44..e85b61891 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -173,7 +173,7 @@ THRUST_DYNAMIC_BACKEND(transform_reduce, void) THRUST_DYNAMIC_BACKEND(lower_bound, void) THRUST_DYNAMIC_BACKEND(gather_if, void) -#if MANIFOLD_PAR == 'T' +#if MANIFOLD_PAR == 'T' && !(_APPLE) // these are faster when compiled with gcc template Ret remove_if(ExecutionPolicy policy, Args... args) { From 70e4b7d8aa98484029f23bb18955dfdaa7d2335f Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 21:56:44 +0800 Subject: [PATCH 10/13] clean up using macro --- src/utilities/include/par.h | 127 +++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index e85b61891..9637956b2 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -139,63 +139,34 @@ inline ExecutionPolicy autoPolicy(int size) { thrust::NAME(thrust::cpp::par, args...); \ } -THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each) -THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each_n) - -THRUST_DYNAMIC_BACKEND_VOID(gather) -THRUST_DYNAMIC_BACKEND_VOID(scatter) -THRUST_DYNAMIC_BACKEND_VOID(for_each) -THRUST_DYNAMIC_BACKEND_VOID(for_each_n) -THRUST_DYNAMIC_BACKEND_VOID(stable_sort) -THRUST_DYNAMIC_BACKEND_VOID(fill) -THRUST_DYNAMIC_BACKEND_VOID(sequence) -THRUST_DYNAMIC_BACKEND_VOID(sort_by_key) -THRUST_DYNAMIC_BACKEND_VOID(stable_sort_by_key) -THRUST_DYNAMIC_BACKEND_VOID(copy) -THRUST_DYNAMIC_BACKEND_VOID(transform) -THRUST_DYNAMIC_BACKEND_VOID(inclusive_scan) -THRUST_DYNAMIC_BACKEND_VOID(uninitialized_fill) -THRUST_DYNAMIC_BACKEND_VOID(uninitialized_copy) -THRUST_DYNAMIC_BACKEND_VOID(copy_n) - -THRUST_DYNAMIC_BACKEND(all_of, bool) -THRUST_DYNAMIC_BACKEND(is_sorted, bool) -THRUST_DYNAMIC_BACKEND(reduce, void) -THRUST_DYNAMIC_BACKEND(count_if, int) -THRUST_DYNAMIC_BACKEND(binary_search, bool) -// void implies that the user have to specify the return type in the template -// argument, as we are unable to deduce it -THRUST_DYNAMIC_BACKEND(remove, void) -THRUST_DYNAMIC_BACKEND(find, void) -THRUST_DYNAMIC_BACKEND(find_if, void) -THRUST_DYNAMIC_BACKEND(reduce_by_key, void) -THRUST_DYNAMIC_BACKEND(transform_reduce, void) -THRUST_DYNAMIC_BACKEND(lower_bound, void) -THRUST_DYNAMIC_BACKEND(gather_if, void) - #if MANIFOLD_PAR == 'T' && !(_APPLE) -// these are faster when compiled with gcc -template -Ret remove_if(ExecutionPolicy policy, Args... args) { - if (policy == ExecutionPolicy::Seq) - return std::remove_if(args...); - else - return std::remove_if(std::execution::par_unseq, args...); -} -template -Ret unique(ExecutionPolicy policy, Args... args) { - if (policy == ExecutionPolicy::Seq) - return std::unique(args...); - else - return std::unique(std::execution::par_unseq, args...); -} -template -void sort(ExecutionPolicy policy, Args... args) { - if (policy == ExecutionPolicy::Seq) - return std::sort(args...); - else - return std::sort(std::execution::par_unseq, args...); -} +// sometimes stl variant is faster +#define STL_DYNAMIC_BACKEND(NAME, RET) \ + template \ + Ret NAME(ExecutionPolicy policy, Args... args) { \ + switch (policy) { \ + case ExecutionPolicy::ParUnseq: \ + case ExecutionPolicy::Par: \ + return std::NAME(std::execution::par_unseq, args...); \ + case ExecutionPolicy::Seq: \ + break; \ + } \ + return std::NAME(args...); \ + } +#define STL_DYNAMIC_BACKEND_VOID(NAME) \ + template \ + void NAME(ExecutionPolicy policy, Args... args) { \ + switch (policy) { \ + case ExecutionPolicy::ParUnseq: \ + case ExecutionPolicy::Par: \ + std::NAME(std::execution::par_unseq, args...); \ + break; \ + case ExecutionPolicy::Seq: \ + std::NAME(args...); \ + break; \ + } \ + } + template void exclusive_scan(ExecutionPolicy policy, Args... args) { // https://github.com/llvm/llvm-project/issues/59810 @@ -225,10 +196,48 @@ OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, return std::copy_if(std::execution::par_unseq, first, last, result, pred); } #else -THRUST_DYNAMIC_BACKEND(remove_if, void) -THRUST_DYNAMIC_BACKEND(unique, void) -THRUST_DYNAMIC_BACKEND_VOID(sort) +#define STL_DYNAMIC_BACKEND(NAME, RET) THRUST_DYNAMIC_BACKEND(NAME, RET) +#define STL_DYNAMIC_BACKEND_VOID(NAME) THRUST_DYNAMIC_BACKEND(NAME) + THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan) THRUST_DYNAMIC_BACKEND(copy_if, void) #endif + +THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each) +THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each_n) + +THRUST_DYNAMIC_BACKEND_VOID(gather) +THRUST_DYNAMIC_BACKEND_VOID(scatter) +THRUST_DYNAMIC_BACKEND_VOID(for_each) +THRUST_DYNAMIC_BACKEND_VOID(for_each_n) +THRUST_DYNAMIC_BACKEND_VOID(sequence) +THRUST_DYNAMIC_BACKEND_VOID(sort_by_key) +THRUST_DYNAMIC_BACKEND_VOID(stable_sort_by_key) +THRUST_DYNAMIC_BACKEND_VOID(transform) +THRUST_DYNAMIC_BACKEND_VOID(uninitialized_fill) +THRUST_DYNAMIC_BACKEND_VOID(uninitialized_copy) +THRUST_DYNAMIC_BACKEND_VOID(stable_sort) +THRUST_DYNAMIC_BACKEND_VOID(fill) +THRUST_DYNAMIC_BACKEND_VOID(copy) +THRUST_DYNAMIC_BACKEND_VOID(inclusive_scan) +THRUST_DYNAMIC_BACKEND_VOID(copy_n) +STL_DYNAMIC_BACKEND_VOID(sort) + +// void implies that the user have to specify the return type in the template +// argument, as we are unable to deduce it +THRUST_DYNAMIC_BACKEND(transform_reduce, void) +THRUST_DYNAMIC_BACKEND(gather_if, void) +THRUST_DYNAMIC_BACKEND(reduce_by_key, void) +THRUST_DYNAMIC_BACKEND(lower_bound, void) +THRUST_DYNAMIC_BACKEND(remove, void) +THRUST_DYNAMIC_BACKEND(find, void) +THRUST_DYNAMIC_BACKEND(find_if, void) +THRUST_DYNAMIC_BACKEND(all_of, bool) +THRUST_DYNAMIC_BACKEND(is_sorted, bool) +THRUST_DYNAMIC_BACKEND(reduce, void) +THRUST_DYNAMIC_BACKEND(count_if, int) +THRUST_DYNAMIC_BACKEND(binary_search, bool) +STL_DYNAMIC_BACKEND(remove_if, void) +STL_DYNAMIC_BACKEND(unique, void) + } // namespace manifold From 150af38950c789154b21a8e7b38922753da92d27 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 21:58:08 +0800 Subject: [PATCH 11/13] fix apple? --- src/polygon/src/polygon.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 1f1fd1faa..3ff7c906c 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -20,10 +20,10 @@ #endif #include #include -#if !_APPLE_ -#include -#else +#if _APPLE_ #include +#else +#include #endif #include #include From 50a5acc24d3047646af1810a76333076e60040d9 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jul 2023 22:06:52 +0800 Subject: [PATCH 12/13] small fix... --- src/utilities/include/par.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index 9637956b2..e0b9ffaf2 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -197,7 +197,7 @@ OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, } #else #define STL_DYNAMIC_BACKEND(NAME, RET) THRUST_DYNAMIC_BACKEND(NAME, RET) -#define STL_DYNAMIC_BACKEND_VOID(NAME) THRUST_DYNAMIC_BACKEND(NAME) +#define STL_DYNAMIC_BACKEND_VOID(NAME) THRUST_DYNAMIC_BACKEND_VOID(NAME) THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan) THRUST_DYNAMIC_BACKEND(copy_if, void) From ff589c0052a879c6dc851ddbd813e3718d4737c0 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 24 Jul 2023 14:18:30 +0800 Subject: [PATCH 13/13] fix apple (again...) --- src/polygon/src/polygon.cpp | 19 +++++++++++++------ src/utilities/include/par.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/polygon/src/polygon.cpp b/src/polygon/src/polygon.cpp index 3ff7c906c..a2519c70b 100644 --- a/src/polygon/src/polygon.cpp +++ b/src/polygon/src/polygon.cpp @@ -20,9 +20,7 @@ #endif #include #include -#if _APPLE_ -#include -#else +#if !__APPLE__ #include #endif #include @@ -311,16 +309,25 @@ class Monotones { private: struct VertAdj; - typedef std::pmr::list::iterator VertItr; struct EdgePair; - typedef std::pmr::list::iterator PairItr; enum VertType { Start, WestSide, EastSide, Merge, End, Skip }; +#if __APPLE__ + typedef std::list::iterator VertItr; + typedef std::list::iterator PairItr; + + std::list monotones_; // sweep-line list of verts + std::list activePairs_; // west to east monotone edges + std::list inactivePairs_; // completed monotones +#else + typedef std::pmr::list::iterator VertItr; + typedef std::pmr::list::iterator PairItr; std::pmr::monotonic_buffer_resource mbr; std::pmr::polymorphic_allocator pa{&mbr}; std::pmr::list monotones_{pa}; // sweep-line list of verts std::pmr::list activePairs_{pa}; // west to east monotone edges std::pmr::list inactivePairs_{pa}; // completed monotones +#endif float precision_; // a triangle of this height or less is degenerate /** @@ -794,7 +801,7 @@ class Monotones { starts.push_back(v); } } -#if MANIFOLD_PAR == 'T' && !(_APPLE_) +#if MANIFOLD_PAR == 'T' && !(__APPLE__) std::sort(std::execution::par_unseq, starts.begin(), starts.end(), cmp); #else std::sort(starts.begin(), starts.end(), cmp); diff --git a/src/utilities/include/par.h b/src/utilities/include/par.h index e0b9ffaf2..1a850c0d7 100644 --- a/src/utilities/include/par.h +++ b/src/utilities/include/par.h @@ -139,7 +139,7 @@ inline ExecutionPolicy autoPolicy(int size) { thrust::NAME(thrust::cpp::par, args...); \ } -#if MANIFOLD_PAR == 'T' && !(_APPLE) +#if MANIFOLD_PAR == 'T' && !(__APPLE__) // sometimes stl variant is faster #define STL_DYNAMIC_BACKEND(NAME, RET) \ template \