Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Andriychuk committed Nov 18, 2024
1 parent e34f84b commit 7e0901f
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions test/Analyses/ActivityReverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ double f8(double x){
// CHECK-NEXT: }
// CHECK-NEXT: }

double fn9(double x, double const *obs)
double f9(double x, double const *obs)
{
double res = 0.0;
for (int loopIdx0 = 0; loopIdx0 < 2; loopIdx0++) {
Expand All @@ -273,7 +273,7 @@ double fn9(double x, double const *obs)
return res;
}

// CHECK: void fn9_grad(double x, const double *obs, double *_d_x, double *_d_obs) {
// CHECK: void f9_grad(double x, const double *obs, double *_d_x, double *_d_obs) {
// CHECK-NEXT: int loopIdx0 = 0;
// CHECK-NEXT: clad::tape<double> _t1 = {};
// CHECK-NEXT: double _d_res = 0.;
Expand Down Expand Up @@ -304,6 +304,55 @@ double fn9(double x, double const *obs)
// CHECK-NEXT: }


void f10_1(double x, double* t){
t[0] = x;
}

double f10(double x){
double t[3];
f10_1(x, t);
return t[0];
}
// CHECK: void f10_1_pullback(double x, double *t, double *_d_x, double *_d_t);
// CHECK-NEXT: void f10_grad(double x, double *_d_x) {
// CHECK-NEXT: double _d_t[3] = {0};
// CHECK-NEXT: double t[3];
// CHECK-NEXT: f10_1(x, t);
// CHECK-NEXT: _d_t[0] += 1;
// CHECK-NEXT: {
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: f10_1_pullback(x, t, &_r0, _d_t);
// CHECK-NEXT: *_d_x += _r0;
// CHECK-NEXT: }
// CHECK-NEXT: }

double f11_1(double v, double& u){
u = v;
return u;
}

double f11(double x){
double y;
double c = f11_1(x, y);
return y;
}

// CHECK: void f11_1_pullback(double v, double &u, double _d_y, double *_d_v, double *_d_u);
// CHECK-NEXT: void f11_grad(double x, double *_d_x) {
// CHECK-NEXT: double _d_y = 0.;
// CHECK-NEXT: double y;
// CHECK-NEXT: double _t0 = y;
// CHECK-NEXT: double _d_c = 0.;
// CHECK-NEXT: double c = f11_1(x, y);
// CHECK-NEXT: _d_y += 1;
// CHECK-NEXT: {
// CHECK-NEXT: y = _t0;
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: f11_1_pullback(x, _t0, _d_c, &_r0, &_d_y);
// CHECK-NEXT: *_d_x += _r0;
// CHECK-NEXT: }
// CHECK-NEXT: }

#define TEST(F, x) { \
result[0] = 0; \
auto F##grad = clad::gradient<clad::opts::enable_va>(F);\
Expand All @@ -324,9 +373,11 @@ int main(){
TEST(f6, 3);// CHECK-EXEC: {0.00}
TEST(f7, 3);// CHECK-EXEC: {1.00}
TEST(f8, 3);// CHECK-EXEC: {1.00}
auto grad = clad::gradient<clad::opts::enable_va>(fn9);
auto grad = clad::gradient<clad::opts::enable_va>(f9);
grad.execute(3, arr, &dx, darr);
printf("%.2f\n", dx);// CHECK-EXEC: 2.00
TEST(f10, 3);// CHECK-EXEC: {1.00}
TEST(f11, 3);// CHECK-EXEC: {1.00}
}

// CHECK: void f4_1_pullback(double v, double u, double _d_y, double *_d_v, double *_d_u) {
Expand All @@ -344,4 +395,27 @@ int main(){

// CHECK: void f8_1_pullback(double v, double u, double _d_y, double *_d_v, double *_d_u) {
// CHECK-NEXT: *_d_v += _d_y;
// CHECK-NEXT: }

// CHECK: void f10_1_pullback(double x, double *t, double *_d_x, double *_d_t) {
// CHECK-NEXT: double _t0 = t[0];
// CHECK-NEXT: t[0] = x;
// CHECK-NEXT: {
// CHECK-NEXT: t[0] = _t0;
// CHECK-NEXT: double _r_d0 = _d_t[0];
// CHECK-NEXT: _d_t[0] = 0.;
// CHECK-NEXT: *_d_x += _r_d0;
// CHECK-NEXT: }
// CHECK-NEXT: }

// CHECK: void f11_1_pullback(double v, double &u, double _d_y, double *_d_v, double *_d_u) {
// CHECK-NEXT: double _t0 = u;
// CHECK-NEXT: u = v;
// CHECK-NEXT: *_d_u += _d_y;
// CHECK-NEXT: {
// CHECK-NEXT: u = _t0;
// CHECK-NEXT: double _r_d0 = *_d_u;
// CHECK-NEXT: *_d_u = 0.;
// CHECK-NEXT: *_d_v += _r_d0;
// CHECK-NEXT: }
// CHECK-NEXT: }

0 comments on commit 7e0901f

Please sign in to comment.