Skip to content

Commit

Permalink
Added parallel test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Hansknecht committed Aug 7, 2023
1 parent 0dfb6e7 commit 0749d0c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ADOL-C/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
##
##############################################################################

noinst_PROGRAMS = powexam speelpenning fminmax
noinst_PROGRAMS = powexam speelpenning fminmax parallel

speelpenning_SOURCES = speelpenning.cpp

powexam_SOURCES = powexam.cpp

parallel_SOURCES = parallel.cpp

fminmax_SOURCES = fminmax.cpp

powexam_LDADD = ../lib${adolclib}.la
speelpenning_LDADD = ../lib${adolclib}.la
fminmax_LDADD = ../lib${adolclib}.la
parallel_LDADD = ../lib${adolclib}.la

parallel_CXXFLAGS = -pthread

AM_CFLAGS = @ac_adolc_cflags@
AM_CXXFLAGS = @ac_adolc_cxxflags@
Expand All @@ -35,7 +40,7 @@ EXTRA_DIST = run_tests
# on Cygwin
DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)/inc

test: powexam speelpenning
test: powexam speelpenning parallel
chmod u+x $(srcdir)/run_tests
$(srcdir)/run_tests

96 changes: 96 additions & 0 deletions ADOL-C/test/parallel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <adolc/adouble.h> // use of active doubles
#include <adolc/drivers/drivers.h> // use of "Easy to Use" drivers
// gradient(.) and hessian(.)
#include <adolc/taping.h> // use of taping

#include <iostream>
using namespace std;

#include <cstdlib>
#include <thread>

void derive(const short my_tape, double init)
{
const int n = 100;

double *xp = new double[n];
double yp = 0.0;
adouble *x = new adouble[n];
adouble y = 1;

for(int i = 0; i < n; i++)
{
xp[i] = (i + init)/(2. + i); // some initialization
}

trace_on(my_tape);

for(int i = 0; i < n; i++) {
x[i] <<= xp[i];
y *= x[i];
}

y >>= yp;

delete[] x;

trace_off();

double* g = new double[n];

gradient(my_tape, n, xp, g);

double** H = new double*[n];

for(int i = 0; i < n; i++)
{
H[i] = (double*) new double[i + 1];
}

hessian(my_tape, n, xp, H);

double errh = 0.;

for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if (i>j)
{
errh += fabs(H[i][j] - g[i]/xp[j]);
}
} // end for
} // end for

std::cout << "Computed Hessian in tape " << my_tape
<< ", error = "
<< errh
<< std::endl;

for(int i = 0; i < n; ++i)
{
delete[] H[i];
}

delete[] H;
delete[] g;
}


int main()
{
std::vector<std::thread> threads;

for(int i = 1; i <= 10; ++i)
{
threads.push_back(std::thread(derive, i, (double) i));
}

for(auto& thread: threads)
{
thread.join();
}

return 0;
}

0 comments on commit 0749d0c

Please sign in to comment.