forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenmp_test.cc
55 lines (45 loc) · 1.56 KB
/
openmp_test.cc
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
#include <numeric>
#include <unordered_set>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#if defined(_OPENMP)
#include <omp.h>
#endif
#include "drake/common/text_logging.h"
namespace drake {
namespace {
#if defined(_OPENMP)
constexpr bool kHasOpenmp = true;
#else
constexpr bool kHasOpenmp = false;
// TODO(jwnimmer-tri) This should be a Drake helper function wrapper that
// abstracts away this difference. The openmp_helpers.hpp wrapper from
// common_robotics_utilities is a likely candidate to delegate to, or at
// least take as inspiration.
int omp_get_thread_num() { return 0; }
#endif
// Mostly, this just checks for compilation failures.
GTEST_TEST(OpenmpTest, ParallelFor) {
drake::log()->info("Using kHasOpenmp = {}", kHasOpenmp);
// Allocate storage for one integer result per loop.
constexpr int num_outputs = 100;
std::vector<int> outputs(static_cast<size_t>(num_outputs), 0);
// Populate the storage, in parallel.
#if defined(_OPENMP)
#pragma omp parallel for
#endif
for (int i = 0; i < num_outputs; ++i) {
outputs[i] = omp_get_thread_num();
}
// Our BUILD rule will run this program with a maximum of two threads.
// Confirm how many threads were used and that their thread numbers were the
// expected set of either {0, 1} (with OpenMP) or {0} (without OpenMP).
std::unordered_set<int> thread_nums(outputs.begin(), outputs.end());
if (kHasOpenmp) {
EXPECT_THAT(thread_nums, testing::UnorderedElementsAre(0, 1));
} else {
EXPECT_THAT(thread_nums, testing::UnorderedElementsAre(0));
}
}
} // namespace
} // namespace drake