Skip to content

Commit

Permalink
Update time library and array container indexing
Browse files Browse the repository at this point in the history
Former-commit-id: 0a7ab46
  • Loading branch information
Pencilcaseman committed Apr 25, 2023
1 parent 6561a5d commit f047bd1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
5 changes: 3 additions & 2 deletions librapid/include/librapid/array/arrayContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,14 @@ namespace librapid {
m_shape.ndim());

int64_t index = 0;
int64_t count = 0;
for (int64_t i : {indices...}) {
LIBRAPID_ASSERT(
i >= 0 && i < static_cast<int64_t>(m_shape[index]),
i >= 0 && i < static_cast<int64_t>(m_shape[count]),
"Index {} out of bounds in ArrayContainer::operator() with dimension={}",
i,
m_shape[index]);
index = index * m_shape[index] + i;
index = index * m_shape[count++] + i;
}
return m_storage[index];
}
Expand Down
8 changes: 8 additions & 0 deletions librapid/include/librapid/array/arrayIterator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LIBRAPID_ARRAY_ITERATOR_HPP
#define LIBRAPID_ARRAY_ITERATOR_HPP

namespace librapid::detail {

}

#endif // LIBRAPID_ARRAY_ITERATOR_HPP
12 changes: 6 additions & 6 deletions librapid/include/librapid/array/cudaStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ namespace librapid {
# define CUDA_REF_OPERATOR(OP) \
template<typename LHS, typename RHS> \
auto operator OP(const CudaRef<LHS> &lhs, const RHS &rhs) { \
return lhs.get() op rhs; \
return lhs.get() OP rhs; \
} \
\
template<typename LHS, typename RHS> \
auto operator OP(const LHS &lhs, const CudaRef<RHS> &rhs) { \
return lhs op rhs.get(); \
return lhs OP rhs.get(); \
} \
\
template<typename LHS, typename RHS> \
auto operator OP(const CudaRef<LHS> &lhs, const CudaRef<RHS> &rhs) { \
return lhs.get() op rhs.get(); \
return lhs.get() OP rhs.get(); \
} \
\
template<typename LHS, typename RHS> \
Expand All @@ -70,17 +70,17 @@ namespace librapid {
# define CUDA_REF_OPERATOR_NO_ASSIGN(OP) \
template<typename LHS, typename RHS> \
auto operator OP(const CudaRef<LHS> &lhs, const RHS &rhs) { \
return lhs.get() op rhs; \
return lhs.get() OP rhs; \
} \
\
template<typename LHS, typename RHS> \
auto operator OP(const LHS &lhs, const CudaRef<RHS> &rhs) { \
return lhs op rhs.get(); \
return lhs OP rhs.get(); \
} \
\
template<typename LHS, typename RHS> \
auto operator OP(const CudaRef<LHS> &lhs, const CudaRef<RHS> &rhs) { \
return lhs.get() op rhs.get(); \
return lhs.get() OP rhs.get(); \
}

template<typename T>
Expand Down
31 changes: 27 additions & 4 deletions librapid/include/librapid/utils/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,25 @@ namespace librapid {

template<int64_t scale = time::second>
std::string formatTime(double time, const std::string &format = "{:.3f}") {
double ns = time * scale;
int numUnits = 8;
static std::string prefix[] = {"ns", "µs", "ms", "s", "m", "h", "d", "y"};
static double divisor[] = {1000, 1000, 1000, 60, 60, 24, 365, 1e300};
double ns = time * scale;
int numUnits = 8;

static std::string prefix[] = {
"ns",
#if defined(LIBRAPID_OS_WINDOWS) && defined(LIBRAPID_NO_WINDOWS_H)
"µs",
#else
"us",
#endif
"ms",
"s",
"m",
"h",
"d",
"y"
};

static double divisor[] = {1000, 1000, 1000, 60, 60, 24, 365, 1e300};
for (int i = 0; i < numUnits; ++i) {
if (ns < divisor[i]) return std::operator+(fmt::format(format, ns), prefix[i]);
ns /= divisor[i];
Expand Down Expand Up @@ -112,6 +127,14 @@ namespace librapid {
return (m_end - m_start) / (double)scale;
}

/// Get the average time in a given unit
/// \tparam scale The unit to return the time in
/// \return The average time in the given unit
template<int64_t scale = time::second>
LIBRAPID_NODISCARD double average() const {
return elapsed<scale>() / (double)m_iters;
}

bool isRunning() {
++m_iters;
return now<time::nanosecond>() - m_start < m_targetTime;
Expand Down

0 comments on commit f047bd1

Please sign in to comment.