Skip to content

Commit

Permalink
int --> double in Enzyme examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkoukpaizan committed Dec 19, 2024
1 parent d5e410c commit 0b10c47
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Examples/Enzyme/Library/library.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
int square(int x) {
double square(double x) {
return x * x;
}
2 changes: 1 addition & 1 deletion Examples/Enzyme/Library/library.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#pragma once
int square(int x);
double square(double x);
21 changes: 9 additions & 12 deletions Examples/Enzyme/Library/main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
#include <iostream>
#include <limits>
#include "library.hpp"

int __enzyme_autodiff(int (*)(int), ...);
double __enzyme_autodiff(double (*)(double), ...);

int main() {
int fail = 0;
int sq = square(5);
int dersq = __enzyme_autodiff(square, 5);
double sq = square(5.0);
double dsq = __enzyme_autodiff(square, 5.0);

std::cout << "x = 5, x^2 = " << sq << ", d(x^2)/dx = " << dersq << "\n";
if (__enzyme_autodiff(square, 5) != 10)
std::cout << "x = 5, x^2 = " << sq << ", d(x^2)/dx = " << dsq << "\n";
if (std::abs(dsq - 10.0) > std::numeric_limits<double>::epsilon())
{
--fail;
// std::cout << "Incorrect result\n";
fail++;
std::cout << "Result incorrect\n";
}
if(fail < 0)
std::cout << "Result incorrect\n\n";
else
std::cout << "Bingo!!\n\n";
std:: cout << fail << "\n";
std::cout << "Status: " << fail << "\n";
return fail;
}
19 changes: 12 additions & 7 deletions Examples/Enzyme/Standalone/main.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#include <iostream>
#include <limits>

int square(int x) {
double square(double x) {
return x * x;
}

int __enzyme_autodiff(int(*)(int), ...);
int dsquare(int x) {
double __enzyme_autodiff(double(*)(double), ...);
double dsquare(double x) {
return __enzyme_autodiff(square, x);
}

int sq = square(5);
int dsq = dsquare(5);
double sq = square(5.0);
double dsq = dsquare(5.0);

int main()
{
int fail = 0;
std::cout << "x = 5, x^2 = " << sq << ", d(x^2)/dx = " << __enzyme_autodiff(square, 5) << "\n";
if (dsq != 10)
std::cout << "x = 5, x^2 = " << sq << ", d(x^2)/dx = " << dsq << "\n";
if (std::abs(dsq - 10.0) > std::numeric_limits<double>::epsilon())
{
fail++;
std::cout << "Result incorrect\n";
}
std::cout << "Status: " << fail << "\n";
return fail;
}

0 comments on commit 0b10c47

Please sign in to comment.