Skip to content

Commit

Permalink
add differential pointer return c test (#29)
Browse files Browse the repository at this point in the history
* add differential pointer return c test

* change all tests to do approx fp comparisons, will be needed for more complex tests
  • Loading branch information
timkaler authored and wsmoses committed May 17, 2021
1 parent 176de49 commit 40ba5e7
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 105 deletions.
50 changes: 50 additions & 0 deletions enzyme/functional_tests_c/differential_pointer_return.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>

#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;
}
6 changes: 4 additions & 2 deletions enzyme/functional_tests_c/insertsort_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <math.h>
#include <assert.h>

#include "test_utils.h"

#define __builtin_autodiff __enzyme_autodiff

double __enzyme_autodiff(void*, ...);
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 4 additions & 31 deletions enzyme/functional_tests_c/insertsort_sum_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
#include <math.h>
#include <assert.h>

#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) {
Expand All @@ -37,36 +31,19 @@ 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];
}
*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;

Expand All @@ -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;
}
17 changes: 4 additions & 13 deletions enzyme/functional_tests_c/loops.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#include <math.h>
#include <assert.h>

#define __builtin_autodiff __enzyme_autodiff
#include "test_utils.h"

#define __builtin_autodiff __enzyme_autodiff

double __enzyme_autodiff(void*, ...);

Expand All @@ -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;
Expand All @@ -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;
Expand Down
34 changes: 8 additions & 26 deletions enzyme/functional_tests_c/loopsdouble.c
Original file line number Diff line number Diff line change
@@ -1,56 +1,38 @@
#include <stdio.h>
#include <math.h>
#include <assert.h>

#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;
}
*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;
Expand Down
20 changes: 6 additions & 14 deletions enzyme/functional_tests_c/loopstriple.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <math.h>
#include <assert.h>

#include "test_utils.h"

#define __builtin_autodiff __enzyme_autodiff
double __enzyme_autodiff(void*, ...);
void compute_loops(float* a, float* b, float* ret) {
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions enzyme/functional_tests_c/readwriteread.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <stdlib.h>
#include <math.h>
#include <assert.h>

#include "test_utils.h"

#define __builtin_autodiff __enzyme_autodiff
double __enzyme_autodiff(void*, ...);

Expand Down Expand Up @@ -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;
}
26 changes: 9 additions & 17 deletions enzyme/functional_tests_c/recurse.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <stdio.h>
#include <math.h>
#include <assert.h>

#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;
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions enzyme/functional_tests_c/test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

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;
}
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit 40ba5e7

Please sign in to comment.