Skip to content

Commit 3c8e7af

Browse files
committed
Add test for resetting tasks.
1 parent d301a8e commit 3c8e7af

8 files changed

Lines changed: 319 additions & 0 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-329.171
3+
53035.8
4+
-5048.17
5+
7413.25
6+
5865.32
7+
Point 1 :
8+
-233.929
9+
48661.8
10+
-50152.2
11+
-80857.1
12+
79140.6
13+
Point 2 :
14+
258.77
15+
71664.1
16+
62269.5
17+
10990.8
18+
244818
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-329.171
3+
53035.8
4+
-5048.17
5+
7413.25
6+
5865.32
7+
Point 1 :
8+
-233.929
9+
48661.8
10+
-50152.2
11+
-80857.1
12+
79140.6
13+
Point 2 :
14+
258.77
15+
71664.1
16+
62269.5
17+
10990.8
18+
244818
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-329.171
3+
53035.8
4+
-5048.17
5+
7413.25
6+
5865.32
7+
Point 1 :
8+
-233.929
9+
48661.8
10+
-50152.2
11+
-80857.1
12+
79140.6
13+
Point 2 :
14+
258.77
15+
71664.1
16+
62269.5
17+
10990.8
18+
244818
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-329.171
3+
0
4+
0
5+
0
6+
0
7+
Point 1 :
8+
-233.929
9+
0
10+
0
11+
0
12+
0
13+
Point 2 :
14+
258.77
15+
0
16+
0
17+
0
18+
0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-329.171
3+
53035.8
4+
-5048.17
5+
7413.25
6+
5865.32
7+
Point 1 :
8+
-233.929
9+
48661.8
10+
-50152.2
11+
-80857.1
12+
79140.6
13+
Point 2 :
14+
258.77
15+
71664.1
16+
62269.5
17+
10990.8
18+
244818
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-329.171
3+
53035.8
4+
-5048.17
5+
7413.25
6+
5865.32
7+
Point 1 :
8+
-233.929
9+
48661.8
10+
-50152.2
11+
-80857.1
12+
79140.6
13+
Point 2 :
14+
258.77
15+
71664.1
16+
62269.5
17+
10990.8
18+
244818
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Point 0 :
2+
-329.171
3+
53035.8
4+
-5048.17
5+
7413.25
6+
5865.32
7+
6.65216e+08
8+
5.72577e+07
9+
3.53375e+08
10+
1.87396e+08
11+
5.72577e+07
12+
3.13447e+07
13+
1.80746e+08
14+
6.28514e+07
15+
3.53375e+08
16+
1.80746e+08
17+
2.16494e+09
18+
6.43048e+08
19+
1.87396e+08
20+
6.28514e+07
21+
6.43048e+08
22+
2.04131e+08
23+
Point 1 :
24+
-233.929
25+
48661.8
26+
-50152.2
27+
-80857.1
28+
79140.6
29+
-6.42261e+08
30+
-1.12272e+09
31+
-7.32461e+08
32+
4.47931e+08
33+
-1.12272e+09
34+
-7.42347e+08
35+
-3.85575e+07
36+
-2.51188e+08
37+
-7.32461e+08
38+
-3.85575e+07
39+
5.6739e+08
40+
-5.9756e+07
41+
4.47931e+08
42+
-2.51188e+08
43+
-5.9756e+07
44+
2.23628e+09
45+
Point 2 :
46+
258.77
47+
71664.1
48+
62269.5
49+
10990.8
50+
244818
51+
2.61445e+08
52+
2.46283e+08
53+
2.32523e+07
54+
-1.28228e+08
55+
2.46283e+08
56+
-1.76676e+08
57+
-2.76886e+08
58+
-4.0755e+07
59+
2.32523e+07
60+
-2.76886e+08
61+
-2.16983e+08
62+
2.86957e+07
63+
-1.28228e+08
64+
-4.0755e+07
65+
2.86957e+07
66+
-5.2885e+08

tests/tests/TestTaskReset.hpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* OpDiLib, an Open Multiprocessing Differentiation Library
3+
*
4+
* Copyright (C) 2020-2022 Chair for Scientific Computing (SciComp), TU Kaiserslautern
5+
* Copyright (C) 2023-2025 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
6+
* Homepage: https://scicomp.rptu.de
7+
* Contact: Prof. Nicolas R. Gauger (opdi@scicomp.uni-kl.de)
8+
*
9+
* Lead developer: Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
10+
*
11+
* This file is part of OpDiLib (https://scicomp.rptu.de/software/opdi).
12+
*
13+
* OpDiLib is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
14+
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
15+
* version.
16+
*
17+
* OpDiLib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
18+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19+
* details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License along with OpDiLib. If not, see
22+
* <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
#pragma once
27+
28+
#include "testBase.hpp"
29+
30+
template<typename _Case>
31+
struct TestTaskReset : public TestBase<4, 1, 3, TestTaskReset<_Case>> {
32+
public:
33+
using Case = _Case;
34+
using Base = TestBase<4, 1, 3, TestTaskReset<Case>>;
35+
36+
template<typename T>
37+
static void test(std::array<T, Base::nIn> const& in, std::array<T, Base::nOut>& out) {
38+
39+
int const N = 1000;
40+
T* a = new T[N];
41+
T* b = new T[N];
42+
T* c = new T[N];
43+
44+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
45+
46+
OPDI_PARALLEL()
47+
{
48+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
49+
50+
int outerNThreads = omp_get_num_threads();
51+
int outerStart = ((N - 1) / outerNThreads + 1) * omp_get_thread_num();
52+
int outerEnd = std::min(N, ((N - 1) / outerNThreads + 1) * (omp_get_thread_num() + 1));
53+
54+
// shared reading of in
55+
for (int i = outerStart; i < outerEnd; ++i) {
56+
Base::job1(i, in, a[i]);
57+
}
58+
59+
#if _OPENMP
60+
opdi::logic->setAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical);
61+
#endif
62+
63+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical);
64+
65+
// no shared reading
66+
for (int i = outerStart; i < outerEnd; ++i) {
67+
b[i] = sin(exp(a[i]));
68+
}
69+
70+
auto position = T::getTape().getPosition();
71+
#if _OPENMP
72+
auto mode = opdi::logic->getAdjointAccessMode();
73+
#endif
74+
75+
OPDI_BARRIER()
76+
77+
OPDI_PARALLEL()
78+
{
79+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical);
80+
81+
int innerNThreads = omp_get_num_threads();
82+
int innerStart = outerStart + (((outerEnd - outerStart) - 1) / innerNThreads + 1) * omp_get_thread_num();
83+
int innerEnd = std::min(outerEnd, outerStart + (((outerEnd - outerStart) - 1) / innerNThreads + 1)
84+
* (omp_get_thread_num() + 1));
85+
86+
#if _OPENMP
87+
opdi::logic->setAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
88+
#endif
89+
90+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
91+
92+
// shared reading on a
93+
for (int j = innerStart; j < innerEnd; ++j) {
94+
T arg = b[j];
95+
for (int k = 0; k < 10; ++k) {
96+
arg += a[(j + k) % N];
97+
}
98+
c[j] = cos(arg);
99+
}
100+
101+
// atomic adjoint access mode to be transported out of the nested parallel region
102+
}
103+
OPDI_END_PARALLEL
104+
105+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
106+
107+
// remove the inner parallel region by positional reset
108+
T::getTape().resetTo(position);
109+
110+
#if _OPENMP
111+
opdi::logic->resetTask(&position, mode);
112+
#endif
113+
114+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical);
115+
116+
#if _OPENMP
117+
opdi::logic->setAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
118+
opdi::logic->addReverseBarrier();
119+
#endif
120+
121+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
122+
123+
// shared reading on a
124+
for (int i = outerStart; i < outerEnd; ++i) {
125+
T arg = c[i] + b[i];
126+
for (int j = 0; j < 10; ++j) {
127+
arg += a[(i + j) % N];
128+
}
129+
c[i] = cos(arg);
130+
}
131+
132+
}
133+
OPDI_END_PARALLEL
134+
135+
assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic);
136+
137+
for (int i = 0; i < N; ++i) {
138+
out[0] += c[i];
139+
}
140+
141+
delete [] a;
142+
delete [] b;
143+
delete [] c;
144+
}
145+
};

0 commit comments

Comments
 (0)