Skip to content

Commit

Permalink
recognized that using const Vec<BaseType> does reduce the speed drama…
Browse files Browse the repository at this point in the history
…tically. I started removing it. But probably better to use always typename T.
  • Loading branch information
Konrad1991 committed Dec 1, 2023
1 parent 1c5c884 commit 9f80e5c
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 163 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ import(RcppArmadillo)
importFrom(Rcpp,sourceCpp)
importFrom(methods,formalArgs)
importFrom(purrr,map_if)
useDynLib(ast2ast, .registration = TRUE)
2 changes: 1 addition & 1 deletion R/compiling.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ compiler_a2a <- function(f, verbose, reference, R_fct, desired_type, return_type
# nonnull-attribute,null,object-size,pointer-overflow,return,
# returns-nonnull-attribute,shift,signed-integer-overflow,
# unreachable,vla-bound,vptr
Sys.setenv("PKG_CXXFLAGS" = "-DRFCT")
# Sys.setenv("PKG_CXXFLAGS" = "-DRFCT -Wall -Wpedantic")
# -fsanitize=address --param=max-vartrack-size=9000000 -fno-omit-frame-pointer

tryCatch(
Expand Down
7 changes: 3 additions & 4 deletions R/mainclass.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ MA <- R6::R6Class("MA",
self$code[[i]] <- self$get_calls(self$ast[[i]])
}
},
call2char = function() { # some errors {}

call2char = function() {
j <- 1
for (i in seq_along(self$code)) {
temp <- deparse(self$code[[i]], width.cutoff = 500)
Expand All @@ -95,8 +94,8 @@ MA <- R6::R6Class("MA",

self$char[[j]] <- temp
j <- j + 1
self$char[[j]] <- "\n"
j <- j + 1
#self$char[[j]] <- "\n"
#j <- j + 1
}
},
get_vars = function() {
Expand Down
27 changes: 14 additions & 13 deletions inst/include/etr_new/BufferVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ template <typename T, typename R, typename Trait> struct Vec {
template <typename U = R, typename T2>
requires std::is_same_v<U, Borrow<BaseType>>
explicit Vec(const Borrow<T2> &borrowed) : d(borrowed) {d.setMatrix(borrowed.mp);}
template<typename U = R>
requires std::is_same_v<U, Borrow<BaseType>>
explicit Vec(T* ptr, size_t s) : d(ptr, s) { }
template <typename L2, typename R2, binaryFct f, typename OperationTrait>
explicit Vec(BinaryOperation<L2, R2, f, OperationTrait> &inp) : d(inp) {
using TypeTrait = OperationTrait;
Expand Down Expand Up @@ -158,9 +161,7 @@ template <typename T, typename R, typename Trait> struct Vec {
RetType &operator[](size_t idx) { return d[idx]; }
RetType operator[](size_t idx) const { return d[idx]; }

template <typename TD>
requires std::is_same_v<TD, BaseType>
Vec &operator=(const TD inp) {
Vec &operator=(const T inp) {
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
if constexpr (isSubset::value) {
Expand Down Expand Up @@ -242,7 +243,7 @@ template <typename T, typename R, typename Trait> struct Vec {
Buffer<T> temp(otherVec.size());
for (size_t i = 0; i < otherVec.size(); i++)
temp[i] = otherVec[i];
d.moveit(temp);
for (size_t i = 0; i < otherVec.size(); i++) d[i] = temp[i];
} else if constexpr (isBorrowSEXP::value) {
Buffer<T> temp(otherVec.size());
for (size_t i = 0; i < otherVec.size(); i++)
Expand Down Expand Up @@ -290,8 +291,8 @@ template <typename T, typename R, typename Trait> struct Vec {
ass(otherVec.size() == this->size(),
"number of items to replace is not a multiple of replacement length");
Buffer<T> temp(otherVec.size());
for (size_t i = 0; i < otherVec.size(); i++) // issue: what the hell is this
d.moveit(temp);
for (size_t i = 0; i < otherVec.size(); i++) temp[i] = otherVec[i];
for (size_t i = 0; i < otherVec.size(); i++) d[i] = temp[i];
} else if constexpr (isBorrowSEXP::value) {
Buffer<T> temp(otherVec.size());
using RetTypeOtherVec = std::remove_reference<decltype(otherVec.d)>::type::RetType;
Expand Down Expand Up @@ -323,10 +324,6 @@ template <typename T, typename R, typename Trait> struct Vec {
temp[i] = static_cast<BaseType>(otherVec[i]);
}
}
//printType(*d.p);
//printType(d);
//std::cout << d.p -> size() << " " << temp.size() << " " << otherVec.size() << std::endl;
//if(d.p -> size() < temp.size()) d.resize(temp.size());
for (size_t i = 0; i < d.ind.size(); i++) {
d[i % d.ind.size()] = temp[i];
}
Expand All @@ -342,9 +339,13 @@ template <typename T, typename R, typename Trait> struct Vec {
return *this;
}

operator bool() const {
ass(this -> size() == 1, "Error in if: the condition has length > 1");
return static_cast<bool>(d[0]);
//operator bool() const {
// ass(this -> size() == 1, "Error in if: the condition has length > 1");
// return static_cast<bool>(d[0]);
//}

operator double() const {
return d[0];
}

size_t size() const { return d.size(); }
Expand Down
11 changes: 6 additions & 5 deletions inst/include/etr_new/UtilsTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,13 @@ template <typename T, typename BaseTrait> struct BaseStore {
return *this;
if (other.size() > this->sz) {
if(allocated) {
int diff = other.size() - this->sz;
size_t diff = other.size() - this->sz;
this->realloc(this->sz + diff);
} else {
this->realloc(other.size());
}
}
for (int i = 0; i < this->sz; i++) {
for (size_t i = 0; i < this->sz; i++) {
p[i] = other[i];
}
return *this;
Expand Down Expand Up @@ -519,14 +519,14 @@ template <typename T, typename BaseTrait> struct BaseStore {
T &back() { return p[sz]; }
T *data() const { return p; }

void realloc(int new_size) {
void realloc(size_t new_size) {
T *temp;
temp = new T[sz];
for (int i = 0; i < sz; i++)
for (size_t i = 0; i < sz; i++)
temp[i] = p[i];
if(p != nullptr && allocated) delete[] p;
p = new T[new_size];
for (int i = 0; i < sz; i++)
for (size_t i = 0; i < sz; i++)
p[i] = temp[i];
ass(temp != nullptr, "try to delete nullptr");
delete[] temp;
Expand Down Expand Up @@ -634,6 +634,7 @@ template <typename T, typename BorrowTrait> struct Borrow {
using RetType = BaseType;
using Type = T;
using TypeTrait = BorrowTrait;
using CaseTrait = BorrowTrait;
T *p = nullptr;
size_t sz = 0;
size_t capacity = 0;
Expand Down
12 changes: 6 additions & 6 deletions inst/include/etr_new/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ inline SEXP cpp2R(const Vec<BaseType> &res) {
} else {
ret = PROTECT(Rf_allocVector(REALSXP, res.size()));
}
for (int i = 0; i < res.size(); i++) {
for (size_t i = 0; i < res.size(); i++) {
REAL(ret)[i] = res[i];
}
UNPROTECT(1);
Expand Down Expand Up @@ -198,15 +198,15 @@ inline SEXP cpp2R(const Vec<L, R, Trait> &res) {

inline Vec<BaseType> isNA(const Vec<BaseType> &inp) {
Vec<BaseType> res(inp.size());
for (int i = 0; i < res.size(); i++) {
for (size_t i = 0; i < res.size(); i++) {
res[i] = ISNA(inp[i]);
}
return res;
}

inline Vec<BaseType> isInfinite(const Vec<BaseType> &inp) {
Vec<BaseType> res(inp.size());
for (int i = 0; i < res.size(); i++) {
for (size_t i = 0; i < res.size(); i++) {
res[i] = (!R_FINITE(inp[i]) && !ISNA(inp[i]));
}
return res;
Expand All @@ -232,9 +232,9 @@ inline Vec<double> colon(const A& start, const O& end) {
} else {
s = static_cast<BaseType>(start); e = static_cast<BaseType>(end);
}
Vec<BaseType> ret(static_cast<int>(e - s + 1));
for (int i = 0; i < ret.size(); i++) {
ret[i] = start + static_cast<double>(i);
Vec<BaseType> ret(static_cast<size_t>(e - s + 1));
for (size_t i = 0; i < ret.size(); i++) {
ret[i] = s + static_cast<BaseType>(i);
}
return ret;
}
Expand Down
84 changes: 66 additions & 18 deletions inst/include/etr_new/subsetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Subset<R> convertSubset(const Vec<T, R> &obj) {
return Subset<T, SubsetTrait>(obj);
}

template <typename I>
inline void calcInd(const Vec<BaseType> &vec, Indices &ind, const I &idx) {
template <typename T, typename I>
inline void calcInd(T &vec, Indices &ind, const I &idx) {
if constexpr (std::is_same_v<I, bool>) {
if (idx) {
ind.resize(vec.size());
Expand Down Expand Up @@ -89,7 +89,7 @@ requires NotOperation<R>
inline auto subset(Vec<L, R> &vec, const I &idx)
-> Vec<BaseType, Subset<decltype(convert(vec).d), SubsetTrait>> {
Subset<decltype(convert(vec).d), SubsetTrait> sub(vec);
calcInd(vec, sub.ind, idx);
calcInd(vec, sub.ind, idx); // issue: this need ages in run_sum why?
sub.setMatrix(false, 0, 0);
return Vec<BaseType, decltype(convertSubset(vec))>(std::move(sub));
}
Expand Down Expand Up @@ -118,7 +118,7 @@ template <typename L, typename R, typename Trait, typename I>
requires UnaryOrBinaryOperation<R>
inline auto subset(const Vec<L, R, Trait> &vec, const I &idx) -> Vec<BaseType> {
Indices ind;
calcInd(vec, ind, idx);
calcInd(vec, ind, idx); // issue: everytime where const Vec<BaseType> & is used as argument. It needs ages. Because, a normal vector is converted into const Vec<BaseType> --> it is copied entirely. This has to be changed!!! Which type has to be used instead?
Vec<BaseType> ret(ind.size());
for(size_t i = 0; i < ret.size(); i++) ret[i] = vec[ind[i]];
return ret;
Expand Down Expand Up @@ -626,55 +626,103 @@ inline auto subset(const Vec<L, R, Trait> &vec, const IL &idxL, const IR& idxR)
return ret;
}

inline BaseType& at(const Vec<BaseType> &inp, int i) {
// issue: not all "at" cases are catched

template<typename T, typename R>
requires std::is_same_v<R, size_t>
inline BaseType& at(T &inp, R i) {
i--;
ass(inp.d.allocated, "No memory was allocated");
ass(i >= 0, "Error: out of boundaries --> value below 1");
ass(i < inp.d.sz, "Error: out of boundaries --> value beyond size of vector");
ass(i < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[i];
}

inline BaseType &at(const Vec<BaseType> &inp, BaseType i_) {
int i = d2i(i_);
template<typename T>
inline BaseType &at(T &inp, BaseType i_) {
size_t i = d2i(i_);
i--;
ass(inp.d.allocated, "No memory was allocated");
ass(i >= 0, "Error: out of boundaries --> value below 1");
ass(i < inp.d.sz, "Error: out of boundaries --> value beyond size of vector");
ass(i < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[i];
}

inline BaseType &at(const Vec<BaseType> &inp, int r, int c) {
template<typename T>
inline BaseType &at(T &inp, size_t r, size_t c) {
ass(inp.im() == true, "Input is not a matrix!");
r--;
c--;
ass(inp.d.allocated, "No memory was allocated");
ass((c * inp.nr() + r) >= 0, "Error: out of boundaries --> value below 1");
ass((c * inp.nr() + r) < inp.d.sz, "Error: out of boundaries --> value beyond size of vector");
ass((c * inp.nr() + r) < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[c * inp.nr() + r];
}

inline BaseType &at(const Vec<BaseType> &inp, BaseType r_, BaseType c_) {
template<typename T>
inline BaseType &at(T &inp, BaseType r_, BaseType c_) {
ass(inp.im() == true, "Input is not a matrix!");
int r = d2i(r_);
int c = d2i(c_);
size_t r = d2i(r_);
size_t c = d2i(c_);
r--;
c--;
ass(inp.d.allocated, "No memory was allocated");
ass((c * inp.nr() + r) >= 0, "Error: out of boundaries --> value below 1");
ass((c * inp.nr() + r) < inp.d.sz, "Error: out of boundaries --> value beyond size of vector");
ass((c * inp.nr() + r) < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[c * inp.nr() + r];
}

inline BaseType &at(const Vec<BaseType> &&inp, int r, int c) {



template<typename T, typename R>
requires std::is_same_v<R, size_t>
inline BaseType& at(const Vec<BaseType> &inp, R i) {
i--;
ass(i >= 0, "Error: out of boundaries --> value below 1");
ass(i < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[i];
}

inline BaseType &at(const Vec<BaseType> &inp, BaseType i_) {
size_t i = d2i(i_);
i--;
ass(i >= 0, "Error: out of boundaries --> value below 1");
ass(i < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[i];
}

inline BaseType &at(const Vec<BaseType> &inp, size_t r, size_t c) {
ass(inp.im() == true, "Input is not a matrix!");
r--;
c--;
ass(inp.d.allocated, "No memory was allocated");
ass((c * inp.nr() + r) >= 0, "Error: out of boundaries --> value below 1");
ass((c * inp.nr() + r) < inp.d.sz, "Error: out of boundaries --> value beyond size of vector");
ass((c * inp.nr() + r) < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[c * inp.nr() + r];
}

inline BaseType &at(const Vec<BaseType> &inp, BaseType r_, BaseType c_) {
ass(inp.im() == true, "Input is not a matrix!");
size_t r = d2i(r_);
size_t c = d2i(c_);
r--;
c--;
ass((c * inp.nr() + r) >= 0, "Error: out of boundaries --> value below 1");
ass((c * inp.nr() + r) < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[c * inp.nr() + r];
}

inline BaseType &at(const Vec<BaseType> &&inp, size_t r, size_t c) {
ass(inp.im() == true, "Input is not a matrix!");
r--;
c--;
ass((c * inp.nr() + r) >= 0, "Error: out of boundaries --> value below 1");
ass((c * inp.nr() + r) < inp.size(), "Error: out of boundaries --> value beyond size of vector");
return inp.d.p[c * inp.nr() + r];
}



}; // namespace etr

#endif
Loading

0 comments on commit 9f80e5c

Please sign in to comment.