Skip to content

Commit

Permalink
Mark clad::array_ref methods as constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailMihov committed Oct 21, 2024
1 parent d4d0fb7 commit 155aa7a
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions include/clad/Differentiator/ArrayRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ template <typename T> class array_ref {
array_ref() = default;
/// Constructor to store the pointer to and size of an array supplied by the
/// user
CUDA_HOST_DEVICE array_ref(T* arr, std::size_t size)
constexpr CUDA_HOST_DEVICE array_ref(T* arr, std::size_t size)
: m_arr(arr), m_size(size) {}
/// Constructor for arrays having size equal to 1 or non pointer types to
/// store their addresses
CUDA_HOST_DEVICE array_ref(T* a) : m_arr(a), m_size(1) {}
constexpr CUDA_HOST_DEVICE array_ref(T* a) : m_arr(a), m_size(1) {}
/// Constructor for clad::array types
CUDA_HOST_DEVICE array_ref(array<T>& a) : m_arr(a.ptr()), m_size(a.size()) {}
constexpr CUDA_HOST_DEVICE array_ref(array<T>& a)
: m_arr(a.ptr()), m_size(a.size()) {}

/// Operator for conversion from array_ref<T> to T*.
CUDA_HOST_DEVICE operator T*() { return m_arr; }
constexpr CUDA_HOST_DEVICE operator T*() { return m_arr; }
/// Operator for conversion from array_ref<T> to const T*.
CUDA_HOST_DEVICE operator const T*() const { return m_arr; }
constexpr CUDA_HOST_DEVICE operator const T*() const { return m_arr; }

template <typename U>
CUDA_HOST_DEVICE array_ref<T>& operator=(const array<U>& a) {
Expand All @@ -46,25 +47,26 @@ template <typename T> class array_ref {
return *this;
}
template <typename U>
CUDA_HOST_DEVICE array_ref<T>& operator=(const array_ref<T>& a) {
constexpr CUDA_HOST_DEVICE array_ref<T>& operator=(const array_ref<T>& a) {
m_arr = a.ptr();
m_size = a.size();
return *this;
}
/// Returns the size of the underlying array
CUDA_HOST_DEVICE std::size_t size() const { return m_size; }
CUDA_HOST_DEVICE PUREFUNC T* ptr() const { return m_arr; }
CUDA_HOST_DEVICE PUREFUNC T*& ptr_ref() { return m_arr; }
constexpr CUDA_HOST_DEVICE std::size_t size() const { return m_size; }
constexpr CUDA_HOST_DEVICE PUREFUNC T* ptr() const { return m_arr; }
constexpr CUDA_HOST_DEVICE PUREFUNC T*& ptr_ref() { return m_arr; }
/// Returns an array_ref to a part of the underlying array starting at
/// offset and having the specified size
CUDA_HOST_DEVICE array_ref<T> slice(std::size_t offset, std::size_t size) {
constexpr CUDA_HOST_DEVICE array_ref<T> slice(std::size_t offset,
std::size_t size) {
assert((offset >= 0) && (offset + size <= m_size) &&
"Window is outside array. Please provide an offset and size "
"inside the array size.");
return array_ref<T>(&m_arr[offset], size);
}
/// Returns the reference to the underlying array
CUDA_HOST_DEVICE PUREFUNC T& operator*() { return *m_arr; }
constexpr CUDA_HOST_DEVICE PUREFUNC T& operator*() { return *m_arr; }

// Arithmetic overloads
/// Divides the arrays element wise
Expand Down Expand Up @@ -171,7 +173,7 @@ template <typename T> class array_ref {

/// Multiplies the arrays element wise
template <typename T, typename U>
CUDA_HOST_DEVICE
constexpr CUDA_HOST_DEVICE
array_expression<const array_ref<T>&, BinaryMul, const array_ref<U>&>
operator*(const array_ref<T>& Ar, const array_ref<U>& Br) {
assert(Ar.size() == Br.size() &&
Expand All @@ -183,7 +185,7 @@ CUDA_HOST_DEVICE

/// Adds the arrays element wise
template <typename T, typename U>
CUDA_HOST_DEVICE
constexpr CUDA_HOST_DEVICE
array_expression<const array_ref<T>&, BinaryAdd, const array_ref<U>&>
operator+(const array_ref<T>& Ar, const array_ref<U>& Br) {
assert(Ar.size() == Br.size() &&
Expand All @@ -195,7 +197,7 @@ CUDA_HOST_DEVICE

/// Subtracts the arrays element wise
template <typename T, typename U>
CUDA_HOST_DEVICE
constexpr CUDA_HOST_DEVICE
array_expression<const array_ref<T>&, BinarySub, const array_ref<U>&>
operator-(const array_ref<T>& Ar, const array_ref<U>& Br) {
assert(
Expand All @@ -208,7 +210,7 @@ CUDA_HOST_DEVICE

/// Divides the arrays element wise
template <typename T, typename U>
CUDA_HOST_DEVICE
constexpr CUDA_HOST_DEVICE
array_expression<const array_ref<T>&, BinaryDiv, const array_ref<U>&>
operator/(const array_ref<T>& Ar, const array_ref<U>& Br) {
assert(Ar.size() == Br.size() &&
Expand All @@ -221,55 +223,55 @@ CUDA_HOST_DEVICE
/// Multiplies array_ref by a scalar
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryMul, U>
constexpr CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryMul, U>
operator*(const array_ref<T>& Ar, U a) {
return array_expression<const array_ref<T>&, BinaryMul, U>(Ar, a);
}

/// Multiplies array_ref by a scalar (reverse order)
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryMul, U>
constexpr CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryMul, U>
operator*(U a, const array_ref<T>& Ar) {
return array_expression<const array_ref<T>&, BinaryMul, U>(Ar, a);
}

/// Divides array_ref by a scalar
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryDiv, U>
constexpr CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryDiv, U>
operator/(const array_ref<T>& Ar, U a) {
return array_expression<const array_ref<T>&, BinaryDiv, U>(Ar, a);
}

/// Adds array_ref by a scalar
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryAdd, U>
constexpr CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryAdd, U>
operator+(const array_ref<T>& Ar, U a) {
return array_expression<const array_ref<T>&, BinaryAdd, U>(Ar, a);
}

/// Adds array_ref by a scalar (reverse order)
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryAdd, U>
constexpr CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinaryAdd, U>
operator+(U a, const array_ref<T>& Ar) {
return array_expression<const array_ref<T>&, BinaryAdd, U>(Ar, a);
}

/// Subtracts array_ref by a scalar
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinarySub, U>
constexpr CUDA_HOST_DEVICE array_expression<const array_ref<T>&, BinarySub, U>
operator-(const array_ref<T>& Ar, U a) {
return array_expression<const array_ref<T>&, BinarySub, U>(Ar, a);
}

/// Subtracts array_ref by a scalar (reverse order)
template <typename T, typename U,
typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
CUDA_HOST_DEVICE array_expression<U, BinarySub, const array_ref<T>&>
constexpr CUDA_HOST_DEVICE array_expression<U, BinarySub, const array_ref<T>&>
operator-(U a, const array_ref<T>& Ar) {
return array_expression<U, BinarySub, const array_ref<T>&>(a, Ar);
}
Expand Down Expand Up @@ -303,16 +305,18 @@ operator-(U a, const array_ref<T>& Ar) {
template <typename T, class = typename std::enable_if<
std::is_pointer<T>::value ||
std::is_same<T, std::nullptr_t>::value>::type>
CUDA_HOST_DEVICE array_ref(T arr, std::size_t size = 1)
constexpr CUDA_HOST_DEVICE array_ref(T arr, std::size_t size = 1)
: m_arr((void*)arr), m_size(size) {}
template <typename T>
CUDA_HOST_DEVICE array_ref(const array_ref<T>& other)
constexpr CUDA_HOST_DEVICE array_ref(const array_ref<T>& other)
: m_arr(other.ptr()), m_size(other.size()) {}
template <typename T> CUDA_HOST_DEVICE operator array_ref<T>() {
template <typename T> constexpr CUDA_HOST_DEVICE operator array_ref<T>() {
return array_ref<T>((T*)(m_arr), m_size);
}
CUDA_HOST_DEVICE void* ptr() const { return m_arr; }
CUDA_HOST_DEVICE std::size_t size() const { return m_size; }
[[nodiscard]] constexpr CUDA_HOST_DEVICE void* ptr() const { return m_arr; }
[[nodiscard]] constexpr CUDA_HOST_DEVICE std::size_t size() const {
return m_size;
}
};
// NOLINTEND(*-pointer-arithmetic)
} // namespace clad
Expand Down

0 comments on commit 155aa7a

Please sign in to comment.