8000 Autodiff by shawn-mcadam · Pull Request #9 · uofs-simlab/func · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Autodiff #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions examples/chaste_log_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,9 @@
#define FUNC(x) (7.7-13.0287*log(x))
#define FUNCNAME "(7.7-13.0287*log(x))"

class MyFunction final : public EvaluationFunctor<double,double>
template <typename T>
class MyFunction final : public EvaluationFunctor<T,T>
{
public:
double operator()(double x) override { return FUNC(x); }
double deriv(double x) override
{
return -13.0287/x;
}
double deriv2(double x) override
{
return 13.0287/x/x;
}
double deriv3(double x) override
{
return -26.0574/x/x/x;
}
double deriv4(double x) override
{
return 78.1722/x/x/x/x;
}
double deriv5(double x) override
{
return -312.6888/x/x/x/x/x;
}
double deriv6(double x) override
{
return 1563.444/x/x/x/x/x/x;
}
double deriv7(double x) override
{
return -9380.664/x/x/x/x/x/x/x;
}
T operator()(T x) override { return FUNC(x); }
};
8 changes: 6 additions & 2 deletions examples/compute_max_err_of_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ int main()
{
using namespace std;

MyFunction func;
FunctionContainer func_container;
func_container.double_func = new MyFunction<double>;
func_container.fvar1_func = new MyFunction<fvar1>;
func_container.fvar2_func = new MyFunction<fvar2>;
func_container.fvar3_func = new MyFunction<fvar3>;

/* Which implementations to use */
std::vector<std::string> implNames {"UniformLinearInterpolationTable",
Expand All @@ -24,7 +28,7 @@ int main()
"UniformQuadraticTaylorTable",
"UniformCubicTaylorTable"};

UniformLookupTableGenerator gen(&func, MIN_ARG, MAX_ARG);
UniformLookupTableGenerator gen(&func_container, MIN_ARG, MAX_ARG);

cout << "# Function: " << FUNCNAME << endl;
cout << "# h ";
Expand Down
39 changes: 26 additions & 13 deletions examples/experiment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ int main(int argc, char* argv[])
int nEvals = std::stoi(argv[5]);
unsigned int seed = std::stoi(argv[6]);

MyFunction func;
FunctionContainer func_container{new MyFunction<double>, new MyFunction<fvar1>,
new MyFunction<fvar2>, new MyFunction<fvar3>, new MyFunction<fvar4>,
new MyFunction<fvar5>, new MyFunction<fvar6>, new MyFunction<fvar7>};
double stepSize;

/* Check which implementations are available */
Expand All @@ -58,30 +60,41 @@ int main(int argc, char* argv[])
std::vector<unique_ptr<EvaluationImplementation>> impls;

/* Which LUT implementations to use */
std::vector<std::string> implNames {"UniformLinearInterpolationTable",
"UniformLinearPrecomputedInterpolationTable",
"UniformQuadraticPrecomputedInterpolationTable",
"UniformCubicPrecomputedInterpolationTable",
"UniformArmadilloPrecomputedInterpolationTable<4>",
"UniformArmadilloPrecomputedInterpolationTable<5>",
"UniformArmadilloPrecomputedInterpolationTable<6>",
"UniformArmadilloPrecomputedInterpolationTable<7>",
std::vector<std::string> implNames {
//"UniformLinearInterpolationTable",
//"UniformLinearPrecomputedInterpolationTable",
//"UniformQuadraticPrecomputedInterpolationTable",
//"UniformCubicPrecomputedInterpolationTable",
//"UniformArmadilloPrecomputedInterpolationTable<4>",
//"UniformArmadilloPrecomputedInterpolationTable<5>",
//"UniformArmadilloPrecomputedInterpolationTable<6>",
//"UniformArmadilloPrecomputedInterpolationTable<7>",
"UniformPadeTable<1,1>",
"UniformPadeTable<2,1>",
"UniformPadeTable<3,1>",
"UniformPadeTable<4,1>",
"UniformPadeTable<5,1>",
"UniformPadeTable<6,1>",
"UniformPadeTable<3,2>",
"UniformPadeTable<4,2>",
"UniformPadeTable<5,2>",
"UniformPadeTable<2,2>",
"UniformPadeTable<3,3>",
"UniformPadeTable<4,3>",
"UniformLinearTaylorTable",
"UniformQuadraticTaylorTable",
"UniformCubicTaylorTable"};
"UniformCubicTaylorTable",
"UniformCubicHermiteTable"
};

UniformLookupTableGenerator gen(&func, tableMin, tableMax);
UniformLookupTableGenerator gen(&func_container, tableMin, tableMax);

/* add implementations to vector */
// unique_ptr<EvaluationImplementation> test = make_unique<DirectEvaluation>(&func,tableMin,tableMax);

impls.emplace_back(unique_ptr<EvaluationImplementation>(new DirectEvaluation(&func,tableMin,tableMax)));
impls.emplace_back(unique_ptr<EvaluationImplementation>(new DirectEvaluation(&func_container,tableMin,tableMax)));
for (auto itName : implNames) {
impls.emplace_back(gen.generate_by_tol(itName,tableTol));
impls.emplace_back(new UniformFailureProofTable(gen.generate_by_tol(itName,tableTol)));
}

/* Run comparator */
Expand Down
9 changes: 7 additions & 2 deletions examples/experiment_best_worst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ int main(int argc, char* argv[])
int nEvals = std::stoi(argv[3]);
int seed = std::stoi(argv[4]);

ZeroFunction func;
FunctionContainer func_container;
func_container.double_func = new ZeroFunction<double>;
func_container.fvar1_func = new ZeroFunction<fvar1>;
func_container.fvar2_func = new ZeroFunction<fvar2>;
func_container.fvar3_func = new ZeroFunction<fvar3>;

double stepSize;

/* Which LUT implementations to use */
Expand All @@ -68,7 +73,7 @@ int main(int argc, char* argv[])
std::cout << "\n# impls using ~ " << percentRam <<"% of RAM\n";
cout << "# Function: " << FUNCNAME << endl << endl;

UniformLookupTableGenerator gen(&func, 0, 1);
UniformLookupTableGenerator gen(&func_container, 0, 1);

/* Fill in the implementations */
std::vector<unique_ptr<EvaluationImplementation>> impls;
Expand Down
8 changes: 6 additions & 2 deletions examples/generate_tables_at_tol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ int main()
{
using namespace std;

MyFunction func;
FunctionContainer func_container;
func_container.double_func = new MyFunction<double>;
func_container.fvar1_func = new MyFunction<fvar1>;
func_container.fvar2_func = new MyFunction<fvar2>;
func_container.fvar3_func = new MyFunction<fvar3>;

cout << "# Function: " << FUNCNAME << "\n";
cout << "# Tol: " << TOL << "\n";
Expand All @@ -34,7 +38,7 @@ int main()
"UniformCubicTaylorTable"};


UniformLookupTableGenerator gen(&func, MIN_ARG, MAX_ARG);
UniformLookupTableGenerator gen(&func_container, MIN_ARG, MAX_ARG);

for (auto itName : implNames) {
std::cout << "\nGenerating " << itName << ":" << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions examples/plot_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ int main()
{
using namespace std;

MyFunction func;
FunctionContainer func_container;
func_container.double_func = new MyFunction<double>;

cout << "# Function: " << FUNCNAME << endl;
cout << "# h";
Expand All @@ -25,7 +26,7 @@ int main()
// UniformLookupTableGenerator<UniformLinearInterpolationTable>
// gen(&func,MIN_ARG,MAX_ARG,0.0);

UniformLookupTableGenerator gen(&func,MIN_ARG,MAX_ARG);
UniformLookupTableGenerator gen(&func_container,MIN_ARG,MAX_ARG);

gen.plot_implementation_at_step_size("UniformLinearInterpolationTable",STEP);

Expand Down
17 changes: 3 additions & 14 deletions examples/test_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@
#define FUNC(x) (7.7*x*exp(x)-13.0287*x*x*log(x)-x +13.0*x*x*x)
#define FUNCNAME "(7.7*x*exp(x)-13.0287*x*x*log(x)-x +13.0*x*x*x)"

class MyFunction final : public EvaluationFunctor<double,double>
template <typename T>
class MyFunction final : public EvaluationFunctor<T,T>
{
public:
double operator()(double x) override { return FUNC(x); }
double deriv(double x) override
{
return 7.7*exp(x)*(1.0+x) - 13.0287*(2.0*x*log(x)+x) - 1.0 + 39.0*x*x;
}
double deriv2(double x) override
{
return 7.7*exp(x)*(2.0+x) - 13.0287*(2.0*log(x)+3.0) + 78.0*x;
}
double deriv3(double x) override
{
return 7.7*exp(x)*(3.0+x) - 13.0287*(2.0/x) + 78.0;
}
T operator()(T x) override { return FUNC(x); }
};
9 changes: 7 additions & 2 deletions src/ArgumentRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ ArgumentRecord::ArgumentRecord(unsigned int histSize, double min, double max) :
{
/* variables needed for recording function arguments */
this->mp_histogram=new unsigned int[histSize];
// init the array to zeros so we can easily add 1 to each entry
this->mp_histogram_mutex.reset(new std::mutex[histSize]);

// init the array to zeros so we can easily increment each entry
std::fill(mp_histogram, mp_histogram+histSize, 0);
this->m_peak=0;
}

void ArgumentRecord::record_arg(double x)
{
unsigned int index = CALC_INDEX(x);
// lock only exists for the scope of this function
std::lock_guard<std::mutex> lock1(mp_histogram_mutex[index]);
mp_histogram[index]++;

std::lock_guard<std::mutex> lock2(m_peak_mutex);
unsigned int peak_index = CALC_INDEX(m_peak);
if(mp_histogram[index]>mp_histogram[peak_index])
m_peak=x;
Expand Down Expand Up @@ -68,4 +74,3 @@ ArgumentRecord::~ArgumentRecord()
{
delete mp_histogram;
}

6 changes: 6 additions & 0 deletions src/ArgumentRecord.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#pragma once
#include <string> // to_string()
#include <memory>
#include <mutex>

// Store a histogram of each location a function has been sampled at
class ArgumentRecord
{
unsigned int *mp_histogram;
std::unique_ptr<std::mutex[]> mp_histogram_mutex;

unsigned int m_histSize;
// min and max should always be the same as the table that contains the ArgumentRecord
double m_min;
double m_max;

// statistic plus a mutex for locking
double m_peak;
std::mutex m_peak_mutex;

public:
ArgumentRecord(unsigned int histSize, double min, double max);
Expand Down
9 changes: 4 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ list(APPEND func_impls_src
table_types/UniformCubicHermiteTable.cpp
table_types/UniformCubicPrecomputedInterpolationTable.cpp
table_types/UniformCubicTaylorTable.cpp
table_types/UniformFailureProofCubicPITable.cpp
table_types/UniformFailureProofLinearPITable.cpp
table_types/UniformFailureProofTable.cpp
table_types/UniformLinearInterpolationTable.cpp
table_types/UniformLinearPrecomputedInterpolationTable.cpp
table_types/UniformLinearTaylorTable.cpp
Expand All @@ -27,8 +26,8 @@ if(USE_ARMADILLO AND ARMADILLO_FOUND)
list(APPEND func_impls_link_libs ${ARMADILLO_LIBRARIES})

list(APPEND func_impls_src
table_types/UniformArmadilloPrecomputedInterpolationTable.cpp
table_types/UniformPadeTable.cpp
table_types/UniformArmadilloPrecomputedInterpolationTable.cpp
table_types/UniformPadeTable.cpp
)
endif()

Expand All @@ -55,7 +54,7 @@ if(FUNC_RECORD)
endif()

# TODO: Create a simplified public header? BTW every header is always being made
set_target_properties(func PROPERTIES PUBLIC_HEADER "func.hpp;func_impls.hpp;EvaluationFunctor.hpp;EvaluationImplementation.hpp;DirectEvaluation.hpp;ImplementationComparator.hpp;RngInterface.hpp;StdRng.hpp;Timer.hpp;UniformLookupTableGenerator.hpp;table_types/UniformArmadilloPrecomputedInterpolationTable.hpp;table_types/UniformConstantTaylorTable.hpp;table_types/UniformCubicHermiteTable.hpp;table_types/UniformCubicPrecomputedInterpolationTable.hpp;table_types/UniformCubicTaylorTable.hpp;table_types/UniformFailureProofCubicPITable.hpp;table_types/UniformFailureProofLinearPITable.hpp;table_types/UniformLinearInterpolationTable.hpp;table_types/UniformLinearPrecomputedInterpolationTable.hpp;table_types/UniformLinearTaylorTable.hpp;table_types/UniformLookupTable.hpp;table_types/UniformPadeTable.hpp;table_types/UniformQuadraticPrecomputedInterpolationTable.hpp;table_types/UniformQuadraticTaylorTable.hpp;table_types/UniformTables.hpp")
set_target_properties(func PROPERTIES PUBLIC_HEADER "func.hpp;func_impls.hpp;EvaluationFunctor.hpp;EvaluationImplementation.hpp;DirectEvaluation.hpp;ImplementationComparator.hpp;RngInterface.hpp;StdRng.hpp;Timer.hpp;UniformLookupTableGenerator.hpp;table_types/UniformArmadilloPrecomputedInterpolationTable.hpp;table_types/UniformConstantTaylorTable.hpp;table_types/UniformCubicHermiteTable.hpp;table_types/UniformCubicPrecomputedInterpolationTable.hpp;table_types/UniformCubicTaylorTable.hpp;table_types/UniformFailureProofTable.hpp;table_types/UniformLinearInterpolationTable.hpp;table_types/UniformLinearPrecomputedInterpolationTable.hpp;table_types/UniformLinearTaylorTable.hpp;table_types/UniformLookupTable.hpp;table_types/UniformPadeTable.hpp;table_types/UniformQuadraticPrecomputedInterpolationTable.hpp;table_types/UniformQuadraticTaylorTable.hpp;table_types/UniformTables.hpp")

install(TARGETS func func_impls
LIBRARY DESTINATION lib
Expand Down
6 changes: 3 additions & 3 deletions src/DirectEvaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "ArgumentRecord.hpp"
#endif

DirectEvaluation::DirectEvaluation(EvaluationFunctor<double,double> *func, double min, double max, unsigned int histSize) :
EvaluationImplementation(func, "DirectEvaluation")
DirectEvaluation::DirectEvaluation(FunctionContainer *func_container, double min, double max, unsigned int histSize) :
EvaluationImplementation(func_container->double_func, "DirectEvaluation")
{
/* Base class default variables */
this->m_minArg = min; this->m_maxArg = max;
Expand Down Expand Up @@ -45,7 +45,7 @@ void DirectEvaluation::print_details(std::ostream& out)
{
out<< m_name << " " << m_minArg << " " << m_maxArg;
#ifdef FUNC_RECORD
out << endl;
out << std::endl;
mp_recorder->print_details(out);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/DirectEvaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DirectEvaluation final : public EvaluationImplementation
std::unique_ptr<ArgumentRecord> mp_recorder;
#endif
public:
DirectEvaluation(EvaluationFunctor<double,double> *func, double min = 0, double max = 1, unsigned int histSize = 10);
DirectEvaluation(FunctionContainer *func_container, double min = 0, double max = 1, unsigned int histSize = 10);
double operator()(double x) override;
void print_details(std::ostream& out) override;
void print_details_json(std::ostream& out);
Expand Down
Loading
0