forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpenMP_test.cpp
More file actions
74 lines (54 loc) · 1.97 KB
/
Copy pathOpenMP_test.cpp
File metadata and controls
74 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @file OpenMP_test.cpp
*
* @date March 17th 2020
* @author Hubert Degaudenzi
*
* @copyright 2012-2020 Euclid Science Ground Segment
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <omp.h>
#include <chrono>
#include <cstdint>
#include <vector>
#include <boost/test/unit_test.hpp>
using std::uint64_t;
constexpr int target_thread_num = 4;
uint64_t getMilliSecondsSinceEpoch() {
namespace chrono = std::chrono;
auto now = chrono::system_clock::now();
auto now_ms = chrono::time_point_cast<chrono::milliseconds>(now);
auto value = now_ms.time_since_epoch();
uint64_t duration = value.count();
return duration;
}
BOOST_AUTO_TEST_SUITE(OpenMP_test)
BOOST_AUTO_TEST_CASE(HasOpenMP_test) {
omp_set_num_threads(target_thread_num);
std::vector<uint64_t> times(target_thread_num);
int is_in_parallel = 0;
// Initialize all the times
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
times[thread_id] = getMilliSecondsSinceEpoch();
is_in_parallel = omp_in_parallel();
times[thread_id] = getMilliSecondsSinceEpoch();
}
BOOST_CHECK_NE(is_in_parallel, 0);
BOOST_CHECK_EQUAL(omp_in_parallel(), 0);
}
//-----------------------------------------------------------------------------
// End of the Boost tests
BOOST_AUTO_TEST_SUITE_END()