Skip to content

Commit

Permalink
Optimization ANTILOOP macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Sep 30, 2024
1 parent b76acd2 commit e1715c2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 49 deletions.
11 changes: 4 additions & 7 deletions omnn/math/Fraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "PrincipalSurd.h"
#include "i.h"
#include "VarHost.h"

#include <rt/antiloop.hpp>

#include <utility>

#include <boost/lexical_cast.hpp>
Expand Down Expand Up @@ -116,13 +119,7 @@ namespace math {
return;
}

OptimizationLoopDetect<Fraction> 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 {
Expand Down
13 changes: 6 additions & 7 deletions omnn/math/Product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
#include "pi.h"
#include "PrincipalSurd.h"
#include "VarHost.h"

#include <rt/antiloop.hpp>

#include <type_traits>

#include <boost/multiprecision/cpp_int.hpp>


namespace omnn{
namespace math {

Expand Down Expand Up @@ -253,13 +258,7 @@ namespace math {
if (!optimizations || optimized)
return;
MarkAsOptimized();
OptimizationLoopDetect<Product> 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 = [&] {
Expand Down
17 changes: 5 additions & 12 deletions omnn/math/Sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
#include "VarHost.h"
#include "Cache.h"

#include "rt/cl.h"
#include <rt/antiloop.hpp>
#include <rt/cl.h>
#include <rt/each.hpp>

//TODO:
//import std;
#include <algorithm>
#include <cmath>
#if __has_include(<execution>)
#include <execution>
#endif
#include <functional>
#include <future>
#include <map>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -293,13 +292,7 @@ namespace
if (isOptimizing)
return;

OptimizationLoopDetect<Sum> 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);

Expand Down
22 changes: 0 additions & 22 deletions omnn/math/Valuable.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,28 +703,6 @@ template <const double I>
const Valuable vf<I>::val = I;
#endif

template <typename ValueT>
class OptimizationLoopDetect {
static thread_local std::unordered_set<ValueT> 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 <typename ValueT>
thread_local std::unordered_set<ValueT> OptimizationLoopDetect<ValueT>::LoopDetectionStack;

} // namespace math
} // namespace omnn
Expand Down
45 changes: 45 additions & 0 deletions omnn/rt/antiloop.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include <unordered_set>

namespace omnn::rt {

template <typename ValueT>
class OptimizationLoopDetect {
static thread_local std::unordered_set<ValueT> 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 <typename ValueT>
thread_local std::unordered_set<ValueT> OptimizationLoopDetect<ValueT>::LoopDetectionStack;

}

#if !defined(NDEBUG) && !defined(NOOMDEBUG)
#define ANTILOOP(Type) \
::omnn::rt::OptimizationLoopDetect<Type> antilooper(*this); \
if (antilooper.isLoopDetected()) { \
std::cout << "Loop of optimizating detected in " << *this << std::endl; \
return; \
}
#else
#define ANTILOOP(Type) \
::omnn::rt::OptimizationLoopDetect<Type> antilooper(*this); \
if (antilooper.isLoopDetected()) { \
return; \
}
#endif
9 changes: 8 additions & 1 deletion omnn/rt/each.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#pragma once
#if __has_include(<execution>)
#include <execution>
#define SEQ std::execution::seq,
#define PAR std::execution::par,
#elif __has_include(<experimental/execution>)
#include <experimental/execution>
#define SEQ std::execution::seq,
#define PAR std::execution::par,
#else
#define SEQ
#define PAR
#endif

#include "tasq.h"
Expand All @@ -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
Expand Down

0 comments on commit e1715c2

Please sign in to comment.