Skip to content

Commit 0749d0c

Browse files
author
Christoph Hansknecht
committed
Added parallel test case
1 parent 0dfb6e7 commit 0749d0c

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

ADOL-C/test/Makefile.am

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
##
1111
##############################################################################
1212

13-
noinst_PROGRAMS = powexam speelpenning fminmax
13+
noinst_PROGRAMS = powexam speelpenning fminmax parallel
1414

1515
speelpenning_SOURCES = speelpenning.cpp
1616

1717
powexam_SOURCES = powexam.cpp
1818

19+
parallel_SOURCES = parallel.cpp
20+
1921
fminmax_SOURCES = fminmax.cpp
2022

2123
powexam_LDADD = ../lib${adolclib}.la
2224
speelpenning_LDADD = ../lib${adolclib}.la
2325
fminmax_LDADD = ../lib${adolclib}.la
26+
parallel_LDADD = ../lib${adolclib}.la
27+
28+
parallel_CXXFLAGS = -pthread
2429

2530
AM_CFLAGS = @ac_adolc_cflags@
2631
AM_CXXFLAGS = @ac_adolc_cxxflags@
@@ -35,7 +40,7 @@ EXTRA_DIST = run_tests
3540
# on Cygwin
3641
DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)/inc
3742

38-
test: powexam speelpenning
43+
test: powexam speelpenning parallel
3944
chmod u+x $(srcdir)/run_tests
4045
$(srcdir)/run_tests
4146

ADOL-C/test/parallel.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <adolc/adouble.h> // use of active doubles
2+
#include <adolc/drivers/drivers.h> // use of "Easy to Use" drivers
3+
// gradient(.) and hessian(.)
4+
#include <adolc/taping.h> // use of taping
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
#include <cstdlib>
10+
#include <thread>
11+
12+
void derive(const short my_tape, double init)
13+
{
14+
const int n = 100;
15+
16+
double *xp = new double[n];
17+
double yp = 0.0;
18+
adouble *x = new adouble[n];
19+
adouble y = 1;
20+
21+
for(int i = 0; i < n; i++)
22+
{
23+
xp[i] = (i + init)/(2. + i); // some initialization
24+
}
25+
26+
trace_on(my_tape);
27+
28+
for(int i = 0; i < n; i++) {
29+
x[i] <<= xp[i];
30+
y *= x[i];
31+
}
32+
33+
y >>= yp;
34+
35+
delete[] x;
36+
37+
trace_off();
38+
39+
double* g = new double[n];
40+
41+
gradient(my_tape, n, xp, g);
42+
43+
double** H = new double*[n];
44+
45+
for(int i = 0; i < n; i++)
46+
{
47+
H[i] = (double*) new double[i + 1];
48+
}
49+
50+
hessian(my_tape, n, xp, H);
51+
52+
double errh = 0.;
53+
54+
for(int i = 0; i < n; i++)
55+
{
56+
for(int j = 0; j < n; j++)
57+
{
58+
if (i>j)
59+
{
60+
errh += fabs(H[i][j] - g[i]/xp[j]);
61+
}
62+
} // end for
63+
} // end for
64+
65+
std::cout << "Computed Hessian in tape " << my_tape
66+
<< ", error = "
67+
<< errh
68+
<< std::endl;
69+
70+
for(int i = 0; i < n; ++i)
71+
{
72+
delete[] H[i];
73+
}
74+
75+
delete[] H;
76+
delete[] g;
77+
}
78+
79+
80+
int main()
81+
{
82+
std::vector<std::thread> threads;
83+
84+
for(int i = 1; i <= 10; ++i)
85+
{
86+
threads.push_back(std::thread(derive, i, (double) i));
87+
}
88+
89+
for(auto& thread: threads)
90+
{
91+
thread.join();
92+
}
93+
94+
return 0;
95+
}
96+

0 commit comments

Comments
 (0)