Skip to content

Commit

Permalink
Merge pull request #61 from TimSiebert1/master
Browse files Browse the repository at this point in the history
Adding C Interface + some repo updates
  • Loading branch information
awalther1 authored Oct 23, 2024
2 parents 61c6c44 + a20d760 commit 5ab0657
Show file tree
Hide file tree
Showing 15 changed files with 1,504 additions and 146 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: build-multi-platform
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang]
cpp_compiler: [g++, clang++]

steps:
- uses: actions/checkout@v4

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ config.h.in
config.h
config.log
config.guess

# Ignore file generated by cmake

CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
cmake_install.cmake
install_manifest.txt
*.dylib
*.so
*.dll
311 changes: 311 additions & 0 deletions ADOL-C/c_interface/ADOLC_TB_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
#include <adolc/adolc.h>
#include "ADOLC_TB_interface.h"

/*
Constructor & Destructor for class tape-based adouble
*/
extern "C"
{
TBAdoubleHandle create_tb_adouble(const double x)
{
return new adouble(x);
}
TBAdoubleHandle create_tb_adouble_empty()
{
return new adouble();
}

void free_tb_adouble(TBAdoubleHandle a)
{
return delete static_cast<adouble *>(a);
}
}

/*
Utilities for adouble
*/
extern "C"
{
double get_tb_value(TBAdoubleHandle a)
{
return static_cast<adouble *>(a)->getValue();
}
}

/*
Arithmetics for class adouble
*/
extern "C"
{
TBAdoubleHandle add_tb_adouble(const TBAdoubleHandle a, const TBAdoubleHandle b)
{
return new adouble(*static_cast<adouble *>(a) + *static_cast<adouble *>(b));
}
TBAdoubleHandle add_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return new adouble(x + *static_cast<adouble *>(b));
}
TBAdoubleHandle add_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(*static_cast<adouble *>(a) + x);
}
TBAdoubleHandle mult_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return new adouble(*static_cast<adouble *>(a) * *static_cast<adouble *>(b));
}
TBAdoubleHandle mult_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return new adouble(x * *static_cast<adouble *>(b));
}
TBAdoubleHandle mult_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(*static_cast<adouble *>(a) * x);
}
TBAdoubleHandle subtr_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return new adouble(*static_cast<adouble *>(a) - *static_cast<adouble *>(b));
}
TBAdoubleHandle subtr_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return new adouble(x - *static_cast<adouble *>(b));
}
TBAdoubleHandle subtr_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(*static_cast<adouble *>(a) - x);
}
TBAdoubleHandle div_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return new adouble(*static_cast<adouble *>(a) / *static_cast<adouble *>(b));
}
TBAdoubleHandle div_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return new adouble(x / *static_cast<adouble *>(b));
}
TBAdoubleHandle div_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(*static_cast<adouble *>(a) / x);
}
TBAdoubleHandle max_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return new adouble(fmax(*static_cast<adouble *>(a), *static_cast<adouble *>(b)));
}
TBAdoubleHandle max_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return new adouble(fmax(x, *static_cast<adouble *>(b)));
}
TBAdoubleHandle max_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(fmax(*static_cast<adouble *>(a), x));
}
TBAdoubleHandle min_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return new adouble(fmin(*static_cast<adouble *>(a), *static_cast<adouble *>(b)));
}
TBAdoubleHandle min_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return new adouble(fmin(x, *static_cast<adouble *>(b)));
}
TBAdoubleHandle min_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(fmin(*static_cast<adouble *>(a), x));
}
TBAdoubleHandle pow_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return new adouble(pow(*static_cast<adouble *>(a), *static_cast<adouble *>(b)));
}
TBAdoubleHandle pow_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return new adouble(pow(*static_cast<adouble *>(a), x));
}
bool ge_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return *static_cast<adouble *>(a) >= *static_cast<adouble *>(b);
}
bool ge_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return x >= *static_cast<adouble *>(b);
}
bool ge_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return *static_cast<adouble *>(a) >= x;
}
bool g_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return *static_cast<adouble *>(a) > *static_cast<adouble *>(b);
}
bool g_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return x > *static_cast<adouble *>(b);
}
bool g_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return *static_cast<adouble *>(a) > x;
}
bool le_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return *static_cast<adouble *>(a) <= *static_cast<adouble *>(b);
}
bool le_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return x <= *static_cast<adouble *>(b);
}
bool le_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return *static_cast<adouble *>(a) <= x;
}
bool l_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return *static_cast<adouble *>(a) < *static_cast<adouble *>(b);
}
bool l_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return x < *static_cast<adouble *>(b);
}
bool l_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return *static_cast<adouble *>(a) < x;
}
bool eq_tb_adouble(TBAdoubleHandle a, TBAdoubleHandle b)
{
return *static_cast<adouble *>(a) == *static_cast<adouble *>(b);
}
bool eq_double_tb_adouble(const double x, TBAdoubleHandle b)
{
return x == *static_cast<adouble *>(b);
}
bool eq_tb_adouble_double(TBAdoubleHandle a, const double x)
{
return *static_cast<adouble *>(a) == x;
}
TBAdoubleHandle tb_abs(TBAdoubleHandle a)
{
return new adouble(fabs(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_sqrt(TBAdoubleHandle a)
{
return new adouble(sqrt(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_log(TBAdoubleHandle a)
{
return new adouble(log(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_log10(TBAdoubleHandle a)
{
return new adouble(log10(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_sin(TBAdoubleHandle a)
{
return new adouble(sin(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_cos(TBAdoubleHandle a)
{
return new adouble(cos(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_tan(TBAdoubleHandle a)
{
return new adouble(tan(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_exp(TBAdoubleHandle a)
{
return new adouble(exp(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_asin(TBAdoubleHandle a)
{
return new adouble(asin(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_acos(TBAdoubleHandle a)
{
return new adouble(acos(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_atan(TBAdoubleHandle a)
{
return new adouble(atan(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_sinh(TBAdoubleHandle a)
{
return new adouble(sinh(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_cosh(TBAdoubleHandle a)
{
return new adouble(cosh(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_tanh(TBAdoubleHandle a)
{
return new adouble(tanh(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_asinh(TBAdoubleHandle a)
{
return new adouble(asinh(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_acosh(TBAdoubleHandle a)
{
return new adouble(acosh(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_atanh(TBAdoubleHandle a)
{
return new adouble(atanh(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_ceil(TBAdoubleHandle a)
{
return new adouble(ceil(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_floor(TBAdoubleHandle a)
{
return new adouble(floor(*static_cast<adouble *>(a)));
}
TBAdoubleHandle tb_ldexp(TBAdoubleHandle a, const int n)
{
return new adouble(ldexp(*static_cast<adouble *>(a), n));
}
TBAdoubleHandle tb_erf(TBAdoubleHandle a)
{
return new adouble(erf(*static_cast<adouble *>(a)));
}
}

/*
Tape utilities
*/
extern "C"
{
int c_trace_on(short int tag, int keep)
{
return trace_on(tag, keep);
}
void c_trace_off(int flag)
{
return trace_off(flag);
}
void create_independent(TBAdoubleHandle a, const double x)
{
*static_cast<adouble *>(a) <<= x;
}
void create_dependent(TBAdoubleHandle a, double *y)
{
*static_cast<adouble *>(a) >>= *y;
}
size_t num_independent(short tape_id)
{
size_t y[STAT_SIZE];
tapestats(tape_id, y);
return y[NUM_INDEPENDENTS];
}
size_t num_dependent(short tape_id)
{
size_t y[STAT_SIZE];
tapestats(tape_id, y);
return y[NUM_DEPENDENTS];
}
void enable_min_max_using_abs()
{
return enableMinMaxUsingAbs();
}
void disable_min_max_using_abs()
{
return disableMinMaxUsingAbs();
}
TBAdoubleHandle mkparam_(const double val)
{
return new adouble(mkparam(val));
}
}
Loading

0 comments on commit 5ab0657

Please sign in to comment.