From e1715c23d0302c98907c62df71c5f6a331c78ba5 Mon Sep 17 00:00:00 2001 From: Sergii Kryvonos Date: Mon, 30 Sep 2024 21:29:13 +0200 Subject: [PATCH] Optimization ANTILOOP macro --- omnn/math/Fraction.cpp | 11 ++++------- omnn/math/Product.cpp | 13 ++++++------ omnn/math/Sum.cpp | 17 +++++----------- omnn/math/Valuable.h | 22 --------------------- omnn/rt/antiloop.hpp | 45 ++++++++++++++++++++++++++++++++++++++++++ omnn/rt/each.hpp | 9 ++++++++- 6 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 omnn/rt/antiloop.hpp diff --git a/omnn/math/Fraction.cpp b/omnn/math/Fraction.cpp index 53cfdbcd1..a201f3673 100644 --- a/omnn/math/Fraction.cpp +++ b/omnn/math/Fraction.cpp @@ -10,6 +10,9 @@ #include "PrincipalSurd.h" #include "i.h" #include "VarHost.h" + +#include + #include #include @@ -116,13 +119,7 @@ namespace math { return; } - OptimizationLoopDetect antilooper(*this); - if (antilooper.isLoopDetected()) { -#if !defined(NDEBUG) && !defined(NOOMDEBUG) - LOG_AND_IMPLEMENT("Loop of optimizating detected in " << *this); -#endif - return; - } + ANTILOOP(Fraction) bool reoptimize_the_fraction; do { diff --git a/omnn/math/Product.cpp b/omnn/math/Product.cpp index 8c6843ebb..cb741e9e7 100644 --- a/omnn/math/Product.cpp +++ b/omnn/math/Product.cpp @@ -13,9 +13,14 @@ #include "pi.h" #include "PrincipalSurd.h" #include "VarHost.h" + +#include + #include + #include + namespace omnn{ namespace math { @@ -253,13 +258,7 @@ namespace math { if (!optimizations || optimized) return; MarkAsOptimized(); - OptimizationLoopDetect antilooper(*this); - if (antilooper.isLoopDetected()) { -#if !defined(NDEBUG) && !defined(NOOMDEBUG) - LOG_AND_IMPLEMENT("Loop of optimizating detected in " << *this); -#endif - return; - } + ANTILOOP(Product) auto isPlusMinus = YesNoMaybe::Maybe; auto IsPlusMinus = [&] { diff --git a/omnn/math/Sum.cpp b/omnn/math/Sum.cpp index 75dd64573..ade7a4003 100644 --- a/omnn/math/Sum.cpp +++ b/omnn/math/Sum.cpp @@ -16,15 +16,14 @@ #include "VarHost.h" #include "Cache.h" -#include "rt/cl.h" +#include +#include +#include //TODO: //import std; #include #include -#if __has_include() -#include -#endif #include #include #include @@ -151,7 +150,7 @@ namespace if (members.size() > Thr) it = std::find( #ifndef __APPLE__ - std::execution::par, + PAR #endif members.begin(), members.end(), item); else @@ -293,13 +292,7 @@ namespace if (isOptimizing) return; - OptimizationLoopDetect antilooper(*this); - if (antilooper.isLoopDetected()) { -#if !defined(NDEBUG) && !defined(NOOMDEBUG) - LOG_AND_IMPLEMENT("Loop of optimizating detected in " << *this); -#endif - return; - } + ANTILOOP(Sum) Optimizing o(*this); diff --git a/omnn/math/Valuable.h b/omnn/math/Valuable.h index 3b9f1dce7..79da6039c 100644 --- a/omnn/math/Valuable.h +++ b/omnn/math/Valuable.h @@ -703,28 +703,6 @@ template const Valuable vf::val = I; #endif -template -class OptimizationLoopDetect { - static thread_local std::unordered_set LoopDetectionStack; - bool isLoop; - const ValueT* value; - -public: - OptimizationLoopDetect(const ValueT& value) { - auto emplaced = LoopDetectionStack.emplace(value); - this->value = &*emplaced.first; - isLoop = !emplaced.second; - } - - ~OptimizationLoopDetect() { - if (!isLoop) - LoopDetectionStack.erase(*value); - } - - auto isLoopDetected() const { return isLoop; } -}; -template -thread_local std::unordered_set OptimizationLoopDetect::LoopDetectionStack; } // namespace math } // namespace omnn diff --git a/omnn/rt/antiloop.hpp b/omnn/rt/antiloop.hpp new file mode 100644 index 000000000..e121335c5 --- /dev/null +++ b/omnn/rt/antiloop.hpp @@ -0,0 +1,45 @@ +#pragma once +#include + +namespace omnn::rt { + +template +class OptimizationLoopDetect { + static thread_local std::unordered_set LoopDetectionStack; + bool isLoop; + const ValueT* value; + +public: + OptimizationLoopDetect(const ValueT& value) { + auto emplaced = LoopDetectionStack.emplace(value); + this->value = &*emplaced.first; + isLoop = !emplaced.second; + } + + ~OptimizationLoopDetect() { + if (!isLoop) + LoopDetectionStack.erase(*value); + } + + auto isLoopDetected() const { return isLoop; } +}; + +template +thread_local std::unordered_set OptimizationLoopDetect::LoopDetectionStack; + +} + +#if !defined(NDEBUG) && !defined(NOOMDEBUG) +#define ANTILOOP(Type) \ + ::omnn::rt::OptimizationLoopDetect antilooper(*this); \ + if (antilooper.isLoopDetected()) { \ + std::cout << "Loop of optimizating detected in " << *this << std::endl; \ + return; \ + } +#else +#define ANTILOOP(Type) \ + ::omnn::rt::OptimizationLoopDetect antilooper(*this); \ + if (antilooper.isLoopDetected()) { \ + return; \ + } +#endif diff --git a/omnn/rt/each.hpp b/omnn/rt/each.hpp index ceba2d180..f3190d61c 100644 --- a/omnn/rt/each.hpp +++ b/omnn/rt/each.hpp @@ -1,8 +1,15 @@ #pragma once #if __has_include() #include +#define SEQ std::execution::seq, +#define PAR std::execution::par, #elif __has_include() #include +#define SEQ std::execution::seq, +#define PAR std::execution::par, +#else +#define SEQ +#define PAR #endif #include "tasq.h" @@ -15,7 +22,7 @@ void peach(T&& c, F&& f) { auto b = std::begin(c), e = std::end(c); std::for_each( #ifndef __APPLE__ - std::execution::par, + PAR #endif b, e, f); #endif