Skip to content

Commit

Permalink
include algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
vaithak committed Oct 27, 2023
1 parent da2b40c commit f68b39e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ jobs:
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
# When debugging increase to a suitable value!
timeout-minutes: ${{ github.event.pull_request && 1 || 20 }}
timeout-minutes: ${{ github.event.pull_request && 20 || 20 }}
- name: Prepare code coverage report
if: ${{ success() && (matrix.coverage == true) }}
run: |
Expand Down
50 changes: 27 additions & 23 deletions include/clad/Differentiator/BuiltinDerivatives.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace custom_derivatives{}
#include "clad/Differentiator/ArrayRef.h"
#include "clad/Differentiator/CladConfig.h"

#include <algorithm>
#include <cmath>

namespace clad {
Expand Down Expand Up @@ -139,52 +140,55 @@ CUDA_HOST_DEVICE void fma_pullback(T1 a, T2 b, T3 c, T4 d_y,
*d_c += d_y;
}

template <typename T>
CUDA_HOST_DEVICE ValueAndPushforward<T, T>
min_pushforward(const T& a, const T& b, const T& d_a, const T& d_b) {
template <typename T1, typename T2>
CUDA_HOST_DEVICE ValueAndPushforward<decltype(::std::min(T1(), T2())),
decltype(::std::min(T1(), T2()))>
min_pushforward(const T1& a, const T2& b, const T1& d_a, const T2& d_b) {
return {::std::min(a, b), a < b ? d_a : d_b};
}

template <typename T>
CUDA_HOST_DEVICE ValueAndPushforward<T, T>
max_pushforward(const T& a, const T& b, const T& d_a, const T& d_b) {
template <typename T1, typename T2>
CUDA_HOST_DEVICE ValueAndPushforward<decltype(::std::max(T1(), T2())),
decltype(::std::max(T1(), T2()))>
max_pushforward(const T1& a, const T2& b, const T1& d_a, const T2& d_b) {
return {::std::max(a, b), a < b ? d_b : d_a};
}

template <typename T, typename U>
CUDA_HOST_DEVICE void min_pullback(const T& a, const T& b, U d_y,
clad::array_ref<decltype(T())> d_a,
clad::array_ref<decltype(T())> d_b) {
template <typename T1, typename T2, typename U>
CUDA_HOST_DEVICE void min_pullback(const T1& a, const T2& b, U d_y,
clad::array_ref<decltype(T1())> d_a,
clad::array_ref<decltype(T2())> d_b) {
if (a < b)
*d_a += d_y;
else
*d_b += d_y;
}

template <typename T, typename U>
CUDA_HOST_DEVICE void max_pullback(const T& a, const T& b, U d_y,
clad::array_ref<decltype(T())> d_a,
clad::array_ref<decltype(T())> d_b) {
template <typename T1, typename T2, typename U>
CUDA_HOST_DEVICE void max_pullback(const T1& a, const T2& b, U d_y,
clad::array_ref<decltype(T1())> d_a,
clad::array_ref<decltype(T2())> d_b) {
if (a < b)
*d_b += d_y;
else
*d_a += d_y;
}

#if __cplusplus >= 201703L
template <typename T>
CUDA_HOST_DEVICE ValueAndPushforward<T, T>
clamp_pushforward(const T& v, const T& lo, const T& hi, const T& d_v,
const T& d_lo, const T& d_hi) {
template <typename T1, typename T2, typename T3>
CUDA_HOST_DEVICE ValueAndPushforward<decltype(::std::clamp(T1(), T2(), T3())),
decltype(::std::clamp(T1(), T2(), T3()))>
clamp_pushforward(const T1& v, const T2& lo, const T3& hi, const T1& d_v,
const T2& d_lo, const T3& d_hi) {
return {::std::clamp(v, lo, hi), v < lo ? d_lo : hi < v ? d_hi : d_v};
}

template <typename T, typename U>
CUDA_HOST_DEVICE void clamp_pullback(const T& v, const T& lo, const T& hi,
template <typename T1, typename T2, typename T3, typename U>
CUDA_HOST_DEVICE void clamp_pullback(const T1& v, const T2& lo, const T3& hi,
const U& d_y,
clad::array_ref<decltype(T())> d_v,
clad::array_ref<decltype(T())> d_lo,
clad::array_ref<decltype(T())> d_hi) {
clad::array_ref<decltype(T1())> d_v,
clad::array_ref<decltype(T2())> d_lo,
clad::array_ref<decltype(T3())> d_hi) {
if (v < lo)
*d_lo += d_y;
else if (hi < v)
Expand Down

0 comments on commit f68b39e

Please sign in to comment.