Skip to content

Commit 541b063

Browse files
Vedant2005goyalvgvassilev
authored andcommitted
Add a call operator for CladFunction
Fixes #1654
1 parent 334f471 commit 541b063

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

include/clad/Differentiator/Differentiator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ CUDA_HOST_DEVICE void push(tape<T[N], SBO_SIZE, SLAB_SIZE>& to, const U& val) {
444444
return static_cast<return_type_t<F>>(0);
445445
}
446446

447+
template <typename... Args>
448+
constexpr CUDA_HOST_DEVICE auto operator()(Args&&... args) const {
449+
return execute(std::forward<Args>(args)...);
450+
}
451+
447452
/// Return the string representation for the generated derivative.
448453
constexpr const char* getCode() const {
449454
if (m_Code)

test/Features/DiffInterfaceExec.C

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %cladclang -std=c++17 -I%S/../../include %s -o %t
2+
// RUN: %t | %filecheck_exec %s
3+
4+
#include "clad/Differentiator/Differentiator.h"
5+
#include <cstdio>
6+
7+
double f(double x) {
8+
return x * x;
9+
}
10+
11+
int main() {
12+
auto f_diff = clad::differentiate(f);
13+
14+
// Test the new operator() syntax
15+
double res_diff = f_diff(3.0);
16+
printf("Diff result: %.2f\n", res_diff);
17+
// CHECK-EXEC: Diff result: 6.00
18+
19+
// 2. Test gradient (Reverse Mode)
20+
auto f_grad = clad::gradient(f);
21+
double x = 4.0;
22+
double d_x = 0.0;
23+
24+
// Test the new operator() syntax
25+
f_grad(x, &d_x);
26+
27+
printf("Grad result: %.2f\n", d_x);
28+
// CHECK-EXEC: Grad result: 8.00
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)