Skip to content

Commit 54eda64

Browse files
leetcodezvgvassilev
authored andcommitted
Allow clad to run with c++11.
1 parent 900a31a commit 54eda64

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

include/clad/Differentiator/ArrayRef.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ template <typename T> class array_ref {
3535
: m_arr(a.ptr()), m_size(a.size()) {}
3636

3737
/// Operator for conversion from array_ref<T> to T*.
38-
constexpr CUDA_HOST_DEVICE operator T*() { return m_arr; }
38+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE operator T*() { return m_arr; }
3939
/// Operator for conversion from array_ref<T> to const T*.
4040
constexpr CUDA_HOST_DEVICE operator const T*() const { return m_arr; }
4141

@@ -47,7 +47,8 @@ template <typename T> class array_ref {
4747
return *this;
4848
}
4949

50-
constexpr CUDA_HOST_DEVICE array_ref<T>& operator=(const array_ref<T>& a) {
50+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE array_ref<T>&
51+
operator=(const array_ref<T>& a) {
5152
if (this == &a)
5253
return *this;
5354
assert(m_size == a.size());
@@ -67,18 +68,20 @@ template <typename T> class array_ref {
6768
/// Returns the size of the underlying array
6869
constexpr CUDA_HOST_DEVICE std::size_t size() const { return m_size; }
6970
constexpr CUDA_HOST_DEVICE PUREFUNC T* ptr() const { return m_arr; }
70-
constexpr CUDA_HOST_DEVICE PUREFUNC T*& ptr_ref() { return m_arr; }
71+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE PUREFUNC T*& ptr_ref() { return m_arr; }
7172
/// Returns an array_ref to a part of the underlying array starting at
7273
/// offset and having the specified size
73-
constexpr CUDA_HOST_DEVICE array_ref<T> slice(std::size_t offset,
74-
std::size_t size) {
74+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE array_ref<T> slice(std::size_t offset,
75+
std::size_t size) {
7576
assert((offset >= 0) && (offset + size <= m_size) &&
7677
"Window is outside array. Please provide an offset and size "
7778
"inside the array size.");
7879
return array_ref<T>(&m_arr[offset], size);
7980
}
8081
/// Returns the reference to the underlying array
81-
constexpr CUDA_HOST_DEVICE PUREFUNC T& operator*() { return *m_arr; }
82+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE PUREFUNC T& operator*() {
83+
return *m_arr;
84+
}
8285

8386
// Arithmetic overloads
8487
/// Divides the arrays element wise
@@ -254,7 +257,8 @@ template <typename T> class array_ref {
254257
template <typename T>
255258
constexpr CUDA_HOST_DEVICE array_ref(const array_ref<T>& other)
256259
: m_arr(other.ptr()), m_size(other.size()) {}
257-
template <typename T> constexpr CUDA_HOST_DEVICE operator array_ref<T>() {
260+
template <typename T>
261+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE operator array_ref<T>() {
258262
return array_ref<T>((T*)(m_arr), m_size);
259263
}
260264
[[nodiscard]] constexpr CUDA_HOST_DEVICE void* ptr() const { return m_arr; }

include/clad/Differentiator/CladConfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#ifndef CLAD_CONFIG_H
44
#define CLAD_CONFIG_H
55

6+
#if __cplusplus >= 201402L
7+
#define CLAD_CONSTEXPR_CXX14 constexpr
8+
#else
9+
#define CLAD_CONSTEXPR_CXX14 inline
10+
#endif
11+
612
#include <cstdlib>
713
#include <memory>
814

include/clad/Differentiator/Differentiator.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ T& back(
259259
template <bool EnablePadding, class... Rest, class F, class... Args,
260260
class... fArgTypes,
261261
typename std::enable_if<EnablePadding, bool>::type = true>
262-
constexpr CUDA_HOST_DEVICE return_type_t<F>
262+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE return_type_t<F>
263263
execute_with_default_args(list<Rest...>, F f, list<fArgTypes...>,
264264
CUDA_ARGS CUDA_REST_ARGS Args&&... args) {
265265
#if defined(__CUDACC__) && !defined(__CUDA_ARCH__)
@@ -286,7 +286,7 @@ T& back(
286286
template <bool EnablePadding, class... Rest, class F, class... Args,
287287
class... fArgTypes,
288288
typename std::enable_if<!EnablePadding, bool>::type = true>
289-
constexpr return_type_t<F>
289+
CLAD_CONSTEXPR_CXX14 return_type_t<F>
290290
execute_with_default_args(list<Rest...>, F f, list<fArgTypes...>,
291291
CUDA_ARGS CUDA_REST_ARGS Args&&... args) {
292292
#if defined(__CUDACC__) && !defined(__CUDA_ARCH__)
@@ -316,7 +316,7 @@ T& back(
316316
template <bool EnablePadding, class... Rest, class ReturnType, class C,
317317
class Obj, class... Args, class... fArgTypes,
318318
typename std::enable_if<!EnablePadding, bool>::type = true>
319-
constexpr auto
319+
CLAD_CONSTEXPR_CXX14 auto
320320
execute_with_default_args(list<Rest...>, ReturnType C::*f, Obj&& obj,
321321
list<fArgTypes...>,
322322
Args&&... args) -> return_type_t<decltype(f)> {
@@ -414,8 +414,9 @@ T& back(
414414

415415
template <typename... Args, class FnType = CladFunctionType>
416416
typename std::enable_if<!std::is_same<FnType, NoFunction*>::value,
417-
return_type_t<F>>::type constexpr CUDA_HOST_DEVICE
418-
execute(Args&&... args) const {
417+
return_type_t<F>>::type
418+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE
419+
execute(Args&&... args) const {
419420
if (!m_Function)
420421
return static_cast<return_type_t<F>>(return_type_t<F>());
421422
if (m_CUDAkernel) {
@@ -425,9 +426,9 @@ T& back(
425426
// here static_cast is used to achieve perfect forwarding
426427
#ifdef __CUDACC__
427428
return execute_helper(m_Function, m_CUDAkernel, dim3(0), dim3(0),
428-
static_cast<Args>(args)...);
429+
std::forward<Args>(args)...);
429430
#else
430-
return execute_helper(m_Function, static_cast<Args>(args)...);
431+
return execute_helper(m_Function, std::forward<Args>(args)...);
431432
#endif
432433
}
433434

@@ -463,12 +464,13 @@ T& back(
463464
}
464465

465466
template <typename... Args>
466-
constexpr CUDA_HOST_DEVICE auto operator()(Args&&... args) const {
467+
constexpr CUDA_HOST_DEVICE auto operator()(Args&&... args) const
468+
-> decltype(this->execute(std::forward<Args>(args)...)) {
467469
return execute(std::forward<Args>(args)...);
468470
}
469471

470472
/// Return the string representation for the generated derivative.
471-
constexpr const char* getCode() const {
473+
CLAD_CONSTEXPR_CXX14 const char* getCode() const {
472474
if (m_Code)
473475
return m_Code;
474476
return "<invalid>";
@@ -498,7 +500,7 @@ T& back(
498500
private:
499501
/// Helper function for executing non-member derived functions.
500502
template <class Fn, class... Args>
501-
constexpr CUDA_HOST_DEVICE return_type_t<CladFunctionType>
503+
CLAD_CONSTEXPR_CXX14 CUDA_HOST_DEVICE return_type_t<CladFunctionType>
502504
execute_helper(Fn f, CUDA_ARGS Args&&... args) const {
503505
// `static_cast` is required here for perfect forwarding.
504506
#if defined(__CUDACC__)
@@ -541,7 +543,7 @@ T& back(
541543
class = typename std::enable_if<std::is_same<
542544
typename std::decay<Obj>::type, C>::value>::type,
543545
class... Args>
544-
constexpr return_type_t<CladFunctionType>
546+
CLAD_CONSTEXPR_CXX14 return_type_t<CladFunctionType>
545547
execute_helper(ReturnType C::*f, Obj&& obj, Args&&... args) const {
546548
// `static_cast` is required here for perfect forwarding.
547549
return execute_with_default_args<EnablePadding>(
@@ -554,7 +556,7 @@ T& back(
554556
/// will be used and derived function will be called through the object
555557
/// saved in `CladFunction`.
556558
template <class ReturnType, class C, class... Args>
557-
constexpr return_type_t<CladFunctionType>
559+
CLAD_CONSTEXPR_CXX14 return_type_t<CladFunctionType>
558560
execute_helper(ReturnType C::*f, Args&&... args) const {
559561
// `static_cast` is required here for perfect forwarding.
560562
return execute_with_default_args<EnablePadding>(

0 commit comments

Comments
 (0)