1 //===- llvm/unittest/Support/ParallelTest.cpp -----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Parallel.h unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Parallel.h"
15 #include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS
16 #include "llvm/Support/ThreadPool.h"
17 #include "gtest/gtest.h"
21 uint32_t array
[1024 * 1024];
25 // Tests below are hanging up on mingw. Investigating.
26 #if !defined(__MINGW32__)
28 TEST(Parallel
, sort
) {
29 std::mt19937 randEngine
;
30 std::uniform_int_distribution
<uint32_t> dist
;
35 parallelSort(std::begin(array
), std::end(array
));
36 ASSERT_TRUE(llvm::is_sorted(array
));
39 TEST(Parallel
, parallel_for
) {
40 // We need to test the case with a TaskSize > 1. We are white-box testing
41 // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
44 std::fill(range
, range
+ 2050, 1);
45 parallelFor(0, 2049, [&range
](size_t I
) { ++range
[I
]; });
47 uint32_t expected
[2049];
48 std::fill(expected
, expected
+ 2049, 2);
49 ASSERT_TRUE(std::equal(range
, range
+ 2049, expected
));
50 // Check that we don't write past the end of the requested range.
51 ASSERT_EQ(range
[2049], 1u);
54 TEST(Parallel
, TransformReduce
) {
55 // Sum an empty list, check that it works.
56 auto identity
= [](uint32_t v
) { return v
; };
57 uint32_t sum
= parallelTransformReduce(ArrayRef
<uint32_t>(), 0U,
58 std::plus
<uint32_t>(), identity
);
61 // Sum the lengths of these strings in parallel.
62 const char *strs
[] = {"a", "ab", "abc", "abcd", "abcde", "abcdef"};
64 parallelTransformReduce(strs
, static_cast<size_t>(0), std::plus
<size_t>(),
65 [](const char *s
) { return strlen(s
); });
66 EXPECT_EQ(lenSum
, static_cast<size_t>(21));
68 // Check that we handle non-divisible task sizes as above.
70 std::fill(std::begin(range
), std::end(range
), 1);
71 sum
= parallelTransformReduce(range
, 0U, std::plus
<uint32_t>(), identity
);
72 EXPECT_EQ(sum
, 2050U);
74 std::fill(std::begin(range
), std::end(range
), 2);
75 sum
= parallelTransformReduce(range
, 0U, std::plus
<uint32_t>(), identity
);
76 EXPECT_EQ(sum
, 4100U);
78 // Avoid one large task.
79 uint32_t range2
[3060];
80 std::fill(std::begin(range2
), std::end(range2
), 1);
81 sum
= parallelTransformReduce(range2
, 0U, std::plus
<uint32_t>(), identity
);
82 EXPECT_EQ(sum
, 3060U);
85 TEST(Parallel
, ForEachError
) {
86 int nums
[] = {1, 2, 3, 4, 5, 6};
87 Error e
= parallelForEachError(nums
, [](int v
) -> Error
{
89 return createStringError(std::errc::invalid_argument
, "asdf");
90 return Error::success();
92 EXPECT_TRUE(e
.isA
<ErrorList
>());
93 std::string errText
= toString(std::move(e
));
94 EXPECT_EQ(errText
, std::string("asdf\nasdf\nasdf"));
97 #if LLVM_ENABLE_THREADS
98 TEST(Parallel
, NestedTaskGroup
) {
100 // 1. Root TaskGroup is in Parallel mode.
101 // 2. Nested TaskGroup is not in Parallel mode.
102 parallel::TaskGroup tg
;
105 EXPECT_TRUE(tg
.isParallel() || (parallel::strategy
.ThreadsRequested
== 1));
109 parallel::TaskGroup nestedTG
;
110 EXPECT_FALSE(nestedTG
.isParallel());
112 nestedTG
.spawn([&]() {
113 // Check that root TaskGroup is in Parallel mode.
114 EXPECT_TRUE(tg
.isParallel() ||
115 (parallel::strategy
.ThreadsRequested
== 1));
117 // Check that nested TaskGroup is not in Parallel mode.
118 EXPECT_FALSE(nestedTG
.isParallel());
123 TEST(Parallel
, ParallelNestedTaskGroup
) {
124 // This test checks that it is possible to have several TaskGroups
125 // run from different threads in Parallel mode.
126 std::atomic
<size_t> Count
{0};
129 std::function
<void()> Fn
= [&]() {
130 parallel::TaskGroup tg
;
133 // Check that root TaskGroup is in Parallel mode.
134 EXPECT_TRUE(tg
.isParallel() ||
135 (parallel::strategy
.ThreadsRequested
== 1));
137 // Check that nested TaskGroup is not in Parallel mode.
138 parallel::TaskGroup nestedTG
;
139 EXPECT_FALSE(nestedTG
.isParallel());
142 nestedTG
.spawn([&]() {
143 // Check that root TaskGroup is in Parallel mode.
144 EXPECT_TRUE(tg
.isParallel() ||
145 (parallel::strategy
.ThreadsRequested
== 1));
147 // Check that nested TaskGroup is not in Parallel mode.
148 EXPECT_FALSE(nestedTG
.isParallel());
154 DefaultThreadPool Pool
;
165 EXPECT_EQ(Count
, 12ul);