Skip to content

Commit dd8125c

Browse files
committed
Add support for custom mutex-like synchronization.
1 parent 2aa30b0 commit dd8125c

9 files changed

Lines changed: 300 additions & 2 deletions

include/opdi/logic/logicInterface.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ namespace opdi {
3434
public:
3535

3636
enum MutexKind : std::size_t {
37-
Critical = 0, Lock, NestedLock, Ordered, Reduction
37+
Critical = 0, Lock, NestedLock, Ordered, Reduction, Custom
3838
};
3939

40-
static constexpr std::size_t nMutexKind = 5;
40+
static constexpr std::size_t nMutexKind = 6;
4141

4242
enum ScopeEndpoint : std::size_t {
4343
Begin = 1, End, BeginEnd
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-723.925
3+
-5515.29
4+
95.0474
5+
-6729
6+
-2140.22
7+
Point 1 :
8+
-325.634
9+
3921.84
10+
7865.3
11+
4847.89
12+
-3985.11
13+
Point 2 :
14+
237.233
15+
4779.5
16+
-3268.62
17+
-5359.29
18+
-10427.2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-723.925
3+
-5515.29
4+
95.0474
5+
-6729
6+
-2140.22
7+
Point 1 :
8+
-325.634
9+
3921.84
10+
7865.3
11+
4847.89
12+
-3985.11
13+
Point 2 :
14+
237.233
15+
4779.5
16+
-3268.62
17+
-5359.29
18+
-10427.2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-723.925
3+
-5515.29
4+
95.0474
5+
-6729
6+
-2140.22
7+
Point 1 :
8+
-325.634
9+
3921.84
10+
7865.3
11+
4847.89
12+
-3985.11
13+
Point 2 :
14+
237.233
15+
4779.5
16+
-3268.62
17+
-5359.29
18+
-10427.2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-723.925
3+
-5515.29
4+
95.0474
5+
-6729
6+
-2140.22
7+
Point 1 :
8+
-325.634
9+
3921.84
10+
7865.3
11+
4847.89
12+
-3985.11
13+
Point 2 :
14+
237.233
15+
4779.5
16+
-3268.62
17+
-5359.29
18+
-10427.2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-723.925
3+
0
4+
0
5+
0
6+
0
7+
Point 1 :
8+
-325.634
9+
0
10+
0
11+
0
12+
0
13+
Point 2 :
14+
237.233
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+
-723.925
3+
-5515.29
4+
95.0474
5+
-6729
6+
-2140.22
7+
Point 1 :
8+
-325.634
9+
3921.84
10+
7865.3
11+
4847.89
12+
-3985.11
13+
Point 2 :
14+
237.233
15+
4779.5
16+
-3268.62
17+
-5359.29
18+
-10427.2
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Point 0 :
2+
-723.925
3+
-5515.29
4+
95.0474
5+
-6729
6+
-2140.22
7+
2.88369e+07
8+
1.52287e+06
9+
3519.89
10+
3.96565e+06
11+
1.52287e+06
12+
8.3703e+06
13+
-1.30966e+07
14+
6068.5
15+
3519.89
16+
-1.30966e+07
17+
2.94023e+07
18+
2.47353e+06
19+
3.96565e+06
20+
6068.5
21+
2.47353e+06
22+
1.08257e+06
23+
Point 1 :
24+
-325.634
25+
3921.84
26+
7865.3
27+
4847.89
28+
-3985.11
29+
2.45363e+07
30+
2.04504e+07
31+
11528.2
32+
-1.59193e+07
33+
2.04504e+07
34+
4.35072e+07
35+
3.71884e+07
36+
57805.4
37+
11528.2
38+
3.71884e+07
39+
4.81492e+07
40+
4.52196e+06
41+
-1.59193e+07
42+
57805.4
43+
4.52196e+06
44+
-3.22903e+07
45+
Point 2 :
46+
237.233
47+
4779.5
48+
-3268.62
49+
-5359.29
50+
-10427.2
51+
-8.25368e+07
52+
-6.60466e+07
53+
6916.15
54+
531171
55+
-6.60466e+07
56+
5.86882e+07
57+
8.23864e+07
58+
276447
59+
6916.15
60+
8.23864e+07
61+
6.14194e+07
62+
1.67793e+07
63+
531171
64+
276447
65+
1.67793e+07
66+
4.97229e+08

tests/tests/TestCustomMutex.hpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 TestCustomMutex : public TestBase<4, 1, 3, TestCustomMutex<_Case>> {
32+
public:
33+
using Case = _Case;
34+
using Base = TestBase<4, 1, 3, TestCustomMutex<Case>>;
35+
36+
struct CustomMutex {
37+
private:
38+
int status;
39+
40+
public:
41+
CustomMutex() : status(0) {}
42+
43+
void lock() {
44+
int localStatus;
45+
46+
while (true) {
47+
// wait until unlocked
48+
do {
49+
#ifdef _OPENMP
50+
#pragma omp atomic read
51+
#endif
52+
localStatus = status;
53+
} while (localStatus != 0);
54+
55+
// try to lock
56+
#ifdef _OPENMP
57+
#pragma omp atomic capture
58+
#endif
59+
localStatus = ++status;
60+
61+
// success
62+
if (localStatus == 1) {
63+
break;
64+
}
65+
66+
// already acquired, try again
67+
#ifdef _OPENMP
68+
#pragma omp atomic update
69+
#endif
70+
--status;
71+
}
72+
#ifdef _OPENMP
73+
opdi::logic->onMutexAcquired(opdi::LogicInterface::MutexKind::Custom, reinterpret_cast<opdi::LogicInterface::WaitId>(&status));
74+
#endif
75+
}
76+
77+
void unlock() {
78+
#ifdef _OPENMP
79+
#pragma omp atomic update
80+
#endif
81+
--status;
82+
#ifdef _OPENMP
83+
opdi::logic->onMutexReleased(opdi::LogicInterface::MutexKind::Custom, reinterpret_cast<opdi::LogicInterface::WaitId>(&status));
84+
#endif
85+
}
86+
};
87+
88+
template<typename T>
89+
static void test(std::array<T, Base::nIn> const& in, std::array<T, Base::nOut>& out) {
90+
91+
int const N = 1000;
92+
T* jobResults = new T[N];
93+
T out1 = 0.0;
94+
T out2 = 0.0;
95+
96+
CustomMutex mutex1, mutex2;
97+
98+
OPDI_PARALLEL()
99+
{
100+
int nThreads = omp_get_num_threads();
101+
int start = ((N - 1) / nThreads + 1) * omp_get_thread_num();
102+
int end = std::min(N, ((N - 1) / nThreads + 1) * (omp_get_thread_num() + 1));
103+
104+
for (int i = start; i < end; ++i) {
105+
Base::job1(i, in, jobResults[i]);
106+
107+
mutex1.lock();
108+
out1 += jobResults[i];
109+
mutex1.unlock();
110+
111+
Base::job2(i, in, jobResults[i]);
112+
113+
mutex2.lock();
114+
out2 += jobResults[i];
115+
mutex2.unlock();
116+
}
117+
}
118+
OPDI_END_PARALLEL
119+
120+
out[0] = out1 + out2;
121+
122+
delete [] jobResults;
123+
}
124+
};

0 commit comments

Comments
 (0)