diff --git a/enzyme/functional_tests_c/differential_pointer_return.c b/enzyme/functional_tests_c/differential_pointer_return.c new file mode 100644 index 000000000000..0f1faf2709e7 --- /dev/null +++ b/enzyme/functional_tests_c/differential_pointer_return.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +#include "test_utils.h" + +#define __builtin_autodiff __enzyme_autodiff +double __enzyme_autodiff(void*, ...); + +double f_read(double* x) { + double product = (*x) * (*x); + return product; +} + +double* g_write(double* x, double product) { + *x = (*x) * product; + return x; +} + +double h_read(double* x) { + return *x; +} + +double readwriteread_helper(double* x) { + double product = f_read(x); + x = g_write(x, product); + double ret = h_read(x); + return ret; +} + +void readwriteread(double*__restrict x, double*__restrict ret) { + *ret = readwriteread_helper(x); +} + +int main(int argc, char** argv) { + double ret = 0; + double dret = 1.0; + double* x = (double*) malloc(sizeof(double)); + double* dx = (double*) malloc(sizeof(double)); + *x = 2.0; + *dx = 0.0; + + __builtin_autodiff(readwriteread, x, dx, &ret, &dret); + + + printf("dx is %f ret is %f\n", *dx, ret); + assert(approx_fp_equality_float(*dx, 3*2.0*2.0, 1e-10)); + return 0; +} diff --git a/enzyme/functional_tests_c/insertsort_sum.c b/enzyme/functional_tests_c/insertsort_sum.c index c5e7cd33d3a0..7c5770041791 100644 --- a/enzyme/functional_tests_c/insertsort_sum.c +++ b/enzyme/functional_tests_c/insertsort_sum.c @@ -3,6 +3,8 @@ #include #include +#include "test_utils.h" + #define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); @@ -67,9 +69,9 @@ int main(int argc, char** argv) { for (int i = 0; i < N; i++) { printf("Diffe for index %d is %f\n", i, d_array[i]); if (i%2 == 0) { - assert(d_array[i] == 0.0); + assert(approx_fp_equality_float(d_array[i], 0.0, 1e-10)); } else { - assert(d_array[i] == 1.0); + assert(approx_fp_equality_float(d_array[i],1.0,1e-10)); } } return 0; diff --git a/enzyme/functional_tests_c/insertsort_sum_alt.c b/enzyme/functional_tests_c/insertsort_sum_alt.c index 944804b6b271..ba92c6c3025a 100644 --- a/enzyme/functional_tests_c/insertsort_sum_alt.c +++ b/enzyme/functional_tests_c/insertsort_sum_alt.c @@ -3,16 +3,10 @@ #include #include +#include "test_utils.h" + #define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); -//float man_max(float* a, float* b) { -// if (*a > *b) { -// return *a; -// } else { -// return *b; -// } -//} - // size of array float* unsorted_array_init(int N) { @@ -37,13 +31,11 @@ void insertion_sort_inner(float* array, int i) { // sums the first half of a sorted array. void insertsort_sum (float*__restrict array, int N, float*__restrict ret) { float sum = 0; - //qsort(array, N, sizeof(float), cmp); for (int i = 1; i < N; i++) { insertion_sort_inner(array, i); } - for (int i = 0; i < N/2; i++) { //printf("Val: %f\n", array[i]); sum += array[i]; @@ -51,22 +43,7 @@ void insertsort_sum (float*__restrict array, int N, float*__restrict ret) { *ret = sum; } - - - int main(int argc, char** argv) { - - - - float a = 2.0; - float b = 3.0; - - - - float da = 0;//(float*) malloc(sizeof(float)); - float db = 0;//(float*) malloc(sizeof(float)); - - float ret = 0; float dret = 1.0; @@ -85,15 +62,11 @@ int main(int argc, char** argv) { for (int i = 0; i < N; i++) { printf("Diffe for index %d is %f\n", i, d_array[i]); if (i%2 == 0) { - assert(d_array[i] == 0.0); + assert(approx_fp_equality_float(d_array[i], 0.0, 1e-10)); } else { - assert(d_array[i] == 1.0); + assert(approx_fp_equality_float(d_array[i], 1.0, 1e-10)); } } - //assert(da == 100*1.0f); - //assert(db == 100*1.0f); - - //printf("hello! %f, res2 %f, da: %f, db: %f\n", ret, ret, da,db); return 0; } diff --git a/enzyme/functional_tests_c/loops.c b/enzyme/functional_tests_c/loops.c index 0c1ffcea6f6d..479843a31306 100644 --- a/enzyme/functional_tests_c/loops.c +++ b/enzyme/functional_tests_c/loops.c @@ -2,8 +2,9 @@ #include #include -#define __builtin_autodiff __enzyme_autodiff +#include "test_utils.h" +#define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); @@ -17,14 +18,6 @@ double __enzyme_autodiff(void*, ...); void compute_loops(float* a, float* b, float* ret) { double sum0 = 0.0; for (int i = 0; i < 100; i++) { - //double sum1 = 0.0; - //for (int j = 0; j < 100; j++) { - // //double sum2 = 0.0; - // //for (int k = 0; k < 100; k++) { - // // sum2 += *a+*b; - // //} - // sum1 += *a+*b; - //} sum0 += *a + *b; } *ret = sum0; @@ -48,13 +41,11 @@ int main(int argc, char** argv) { float ret = 0; float dret = 1.0; - //compute_loops(&a, &b, &ret); - __builtin_autodiff(compute_loops, &a, &da, &b, &db, &ret, &dret); - assert(da == 100*1.0f); - assert(db == 100*1.0f); + assert(approx_fp_equality_float(da, 100*1.0f, 1e-10)); + assert(approx_fp_equality_float(db, 100*1.0f, 1e-10)); printf("hello! %f, res2 %f, da: %f, db: %f\n", ret, ret, da,db); return 0; diff --git a/enzyme/functional_tests_c/loopsdouble.c b/enzyme/functional_tests_c/loopsdouble.c index 67712b2256db..c77b4874751d 100644 --- a/enzyme/functional_tests_c/loopsdouble.c +++ b/enzyme/functional_tests_c/loopsdouble.c @@ -1,24 +1,17 @@ #include #include #include + +#include "test_utils.h" + #define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); -//float man_max(float* a, float* b) { -// if (*a > *b) { -// return *a; -// } else { -// return *b; -// } -//} + void compute_loops(float* a, float* b, float* ret) { double sum0 = 0.0; for (int i = 0; i < 100; i++) { double sum1 = 0.0; for (int j = 0; j < 100; j++) { - //double sum2 = 0.0; - //for (int k = 0; k < 100; k++) { - // sum2 += *a+*b; - //} sum1 += *a+*b; } sum0 += sum1; @@ -26,31 +19,20 @@ void compute_loops(float* a, float* b, float* ret) { *ret = sum0; } - - int main(int argc, char** argv) { - - - float a = 2.0; float b = 3.0; - - - float da = 0;//(float*) malloc(sizeof(float)); - float db = 0;//(float*) malloc(sizeof(float)); - + float da = 0; + float db = 0; float ret = 0; float dret = 1.0; - //compute_loops(&a, &b, &ret); - __builtin_autodiff(compute_loops, &a, &da, &b, &db, &ret, &dret); - - assert(da == 100*100*1.0f); - assert(db == 100*100*1.0f); + assert(approx_fp_equality_float(da, 100*100*1.0f, 1e-10)); + assert(approx_fp_equality_float(db, 100*100*1.0f, 1e-10)); printf("hello! %f, res2 %f, da: %f, db: %f\n", ret, ret, da,db); return 0; diff --git a/enzyme/functional_tests_c/loopstriple.c b/enzyme/functional_tests_c/loopstriple.c index ceb71802a834..8f11c22ee367 100644 --- a/enzyme/functional_tests_c/loopstriple.c +++ b/enzyme/functional_tests_c/loopstriple.c @@ -2,6 +2,8 @@ #include #include +#include "test_utils.h" + #define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); void compute_loops(float* a, float* b, float* ret) { @@ -20,31 +22,21 @@ void compute_loops(float* a, float* b, float* ret) { *ret = sum0; } - - int main(int argc, char** argv) { - - float a = 2.0; float b = 3.0; - - - float da = 0;//(float*) malloc(sizeof(float)); - float db = 0;//(float*) malloc(sizeof(float)); - + float da = 0; + float db = 0; float ret = 0; float dret = 1.0; - //compute_loops(&a, &b, &ret); - __builtin_autodiff(compute_loops, &a, &da, &b, &db, &ret, &dret); - - assert(da == 100*100*100*1.0f); - assert(db == 100*100*100*1.0f); + assert(approx_fp_equality_float(da, 100*100*100*1.0f, 1e-10)); + assert(approx_fp_equality_float(db, 100*100*100*1.0f, 1e-10)); printf("hello! %f, res2 %f, da: %f, db: %f\n", ret, ret, da,db); return 0; diff --git a/enzyme/functional_tests_c/readwriteread.c b/enzyme/functional_tests_c/readwriteread.c index 355c632190a2..a7d0c42f216b 100644 --- a/enzyme/functional_tests_c/readwriteread.c +++ b/enzyme/functional_tests_c/readwriteread.c @@ -2,6 +2,9 @@ #include #include #include + +#include "test_utils.h" + #define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); @@ -38,9 +41,8 @@ int main(int argc, char** argv) { *dx = 0.0; __builtin_autodiff(readwriteread, x, dx, &ret, &dret); - printf("dx is %f ret is %f\n", *dx, ret); - assert(*dx == 3*2.0*2.0); + assert(approx_fp_equality_double(*dx, 3*2.0*2.0, 1e-10)); return 0; } diff --git a/enzyme/functional_tests_c/recurse.c b/enzyme/functional_tests_c/recurse.c index fe02b21bbb58..f846b64d7ca3 100644 --- a/enzyme/functional_tests_c/recurse.c +++ b/enzyme/functional_tests_c/recurse.c @@ -1,9 +1,12 @@ #include #include #include + +#include "test_utils.h" + #define __builtin_autodiff __enzyme_autodiff double __enzyme_autodiff(void*, ...); -int counter = 0; + double recurse_max_helper(float* a, float* b, int N) { if (N <= 0) { return *a + *b; @@ -14,36 +17,25 @@ void recurse_max(float* a, float* b, float* ret, int N) { *ret = recurse_max_helper(a,b,N); } - - int main(int argc, char** argv) { - - - float a = 2.0; float b = 3.0; - - - float da = 0;//(float*) malloc(sizeof(float)); - float db = 0;//(float*) malloc(sizeof(float)); - + float da = 0; + float db = 0; float ret = 0; float dret = 2.0; - recurse_max(&a, &b, &ret, 20); + //recurse_max(&a, &b, &ret, 20); int N = 20; int dN = 0; __builtin_autodiff(recurse_max, &a, &da, &b, &db, &ret, &dret, 20); - - assert(da == 17711.0*2); - assert(db == 17711.0*2); - - + assert(approx_fp_equality_float(da, 17711.0*2, 1e-10)); + assert(approx_fp_equality_float(db, 17711.0*2, 1e-10)); printf("hello! %f, res2 %f, da: %f, db: %f\n", ret, ret, da,db); return 0; diff --git a/enzyme/functional_tests_c/test_utils.h b/enzyme/functional_tests_c/test_utils.h new file mode 100644 index 000000000000..07f8a61619dc --- /dev/null +++ b/enzyme/functional_tests_c/test_utils.h @@ -0,0 +1,13 @@ +#include +#include +#include + +static bool approx_fp_equality_float(float f1, float f2, double threshold) { + if (fabs(f1-f2) > threshold) return false; + return true; +} + +static bool approx_fp_equality_double(double f1, double f2, double threshold) { + if (fabs(f1-f2) > threshold) return false; + return true; +} diff --git a/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme0.test b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme0.test new file mode 100644 index 000000000000..5b6d1b9c0f86 --- /dev/null +++ b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme0.test @@ -0,0 +1,6 @@ +; RUN: cd %desired_wd +; RUN: make clean-differential_pointer_return-enzyme0 ENZYME_PLUGIN=%loadEnzyme +; RUN: make build/differential_pointer_return-enzyme0 ENZYME_PLUGIN=%loadEnzyme CLANG_BIN_PATH=%clangBinPath +; RUN: build/differential_pointer_return-enzyme0 +; RUN: make clean-differential_pointer_return-enzyme0 ENZYME_PLUGIN=%loadEnzyme + diff --git a/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme1.test b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme1.test new file mode 100644 index 000000000000..8b2543788ec7 --- /dev/null +++ b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme1.test @@ -0,0 +1,6 @@ +; RUN: cd %desired_wd +; RUN: make clean-differential_pointer_return-enzyme1 ENZYME_PLUGIN=%loadEnzyme +; RUN: make build/differential_pointer_return-enzyme1 ENZYME_PLUGIN=%loadEnzyme CLANG_BIN_PATH=%clangBinPath +; RUN: build/differential_pointer_return-enzyme1 +; RUN: make clean-differential_pointer_return-enzyme1 ENZYME_PLUGIN=%loadEnzyme + diff --git a/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme2.test b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme2.test new file mode 100644 index 000000000000..602ca72052f6 --- /dev/null +++ b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme2.test @@ -0,0 +1,6 @@ +; RUN: cd %desired_wd +; RUN: make clean-differential_pointer_return-enzyme2 ENZYME_PLUGIN=%loadEnzyme +; RUN: make build/differential_pointer_return-enzyme2 ENZYME_PLUGIN=%loadEnzyme CLANG_BIN_PATH=%clangBinPath +; RUN: build/differential_pointer_return-enzyme2 +; RUN: make clean-differential_pointer_return-enzyme2 ENZYME_PLUGIN=%loadEnzyme + diff --git a/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme3.test b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme3.test new file mode 100644 index 000000000000..5f917a9424da --- /dev/null +++ b/enzyme/functional_tests_c/testfiles/differential_pointer_return-enzyme3.test @@ -0,0 +1,6 @@ +; RUN: cd %desired_wd +; RUN: make clean-differential_pointer_return-enzyme3 ENZYME_PLUGIN=%loadEnzyme +; RUN: make build/differential_pointer_return-enzyme3 ENZYME_PLUGIN=%loadEnzyme CLANG_BIN_PATH=%clangBinPath +; RUN: build/differential_pointer_return-enzyme3 +; RUN: make clean-differential_pointer_return-enzyme3 ENZYME_PLUGIN=%loadEnzyme +