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/Support/ThreadPool.h"
16 #include "gtest/gtest.h"
20 uint32_t array
[1024 * 1024];
24 // Tests below are hanging up on mingw. Investigating.
25 #if !defined(__MINGW32__)
27 TEST(Parallel
, sort
) {
28 std::mt19937 randEngine
;
29 std::uniform_int_distribution
<uint32_t> dist
;
34 parallelSort(std::begin(array
), std::end(array
));
35 ASSERT_TRUE(llvm::is_sorted(array
));
38 TEST(Parallel
, parallel_for
) {
39 // We need to test the case with a TaskSize > 1. We are white-box testing
40 // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
43 std::fill(range
, range
+ 2050, 1);
44 parallelFor(0, 2049, [&range
](size_t I
) { ++range
[I
]; });
46 uint32_t expected
[2049];
47 std::fill(expected
, expected
+ 2049, 2);
48 ASSERT_TRUE(std::equal(range
, range
+ 2049, expected
));
49 // Check that we don't write past the end of the requested range.
50 ASSERT_EQ(range
[2049], 1u);
53 TEST(Parallel
, TransformReduce
) {
54 // Sum an empty list, check that it works.
55 auto identity
= [](uint32_t v
) { return v
; };
56 uint32_t sum
= parallelTransformReduce(ArrayRef
<uint32_t>(), 0U,
57 std::plus
<uint32_t>(), identity
);
60 // Sum the lengths of these strings in parallel.
61 const char *strs
[] = {"a", "ab", "abc", "abcd", "abcde", "abcdef"};
63 parallelTransformReduce(strs
, static_cast<size_t>(0), std::plus
<size_t>(),
64 [](const char *s
) { return strlen(s
); });
65 EXPECT_EQ(lenSum
, static_cast<size_t>(21));
67 // Check that we handle non-divisible task sizes as above.
69 std::fill(std::begin(range
), std::end(range
), 1);
70 sum
= parallelTransformReduce(range
, 0U, std::plus
<uint32_t>(), identity
);
71 EXPECT_EQ(sum
, 2050U);
73 std::fill(std::begin(range
), std::end(range
), 2);
74 sum
= parallelTransformReduce(range
, 0U, std::plus
<uint32_t>(), identity
);
75 EXPECT_EQ(sum
, 4100U);
77 // Avoid one large task.
78 uint32_t range2
[3060];
79 std::fill(std::begin(range2
), std::end(range2
), 1);
80 sum
= parallelTransformReduce(range2
, 0U, std::plus
<uint32_t>(), identity
);
81 EXPECT_EQ(sum
, 3060U);
84 TEST(Parallel
, ForEachError
) {
85 int nums
[] = {1, 2, 3, 4, 5, 6};
86 Error e
= parallelForEachError(nums
, [](int v
) -> Error
{
88 return createStringError(std::errc::invalid_argument
, "asdf");
89 return Error::success();
91 EXPECT_TRUE(e
.isA
<ErrorList
>());
92 std::string errText
= toString(std::move(e
));
93 EXPECT_EQ(errText
, std::string("asdf\nasdf\nasdf"));
96 TEST(Parallel
, TaskGroupSequentialFor
) {
99 parallel::TaskGroup tg
;
100 for (size_t Idx
= 0; Idx
< 500; Idx
++)
101 tg
.spawn([&Count
, Idx
]() { EXPECT_EQ(Count
++, Idx
); }, true);
103 EXPECT_EQ(Count
, 500ul);
106 #if LLVM_ENABLE_THREADS
107 TEST(Parallel
, NestedTaskGroup
) {
109 // 1. Root TaskGroup is in Parallel mode.
110 // 2. Nested TaskGroup is not in Parallel mode.
111 parallel::TaskGroup tg
;
114 EXPECT_TRUE(tg
.isParallel() || (parallel::strategy
.ThreadsRequested
== 1));
118 parallel::TaskGroup nestedTG
;
119 EXPECT_FALSE(nestedTG
.isParallel());
121 nestedTG
.spawn([&]() {
122 // Check that root TaskGroup is in Parallel mode.
123 EXPECT_TRUE(tg
.isParallel() ||
124 (parallel::strategy
.ThreadsRequested
== 1));
126 // Check that nested TaskGroup is not in Parallel mode.
127 EXPECT_FALSE(nestedTG
.isParallel());
132 TEST(Parallel
, ParallelNestedTaskGroup
) {
133 // This test checks that it is possible to have several TaskGroups
134 // run from different threads in Parallel mode.
135 std::atomic
<size_t> Count
{0};
138 std::function
<void()> Fn
= [&]() {
139 parallel::TaskGroup tg
;
142 // Check that root TaskGroup is in Parallel mode.
143 EXPECT_TRUE(tg
.isParallel() ||
144 (parallel::strategy
.ThreadsRequested
== 1));
146 // Check that nested TaskGroup is not in Parallel mode.
147 parallel::TaskGroup nestedTG
;
148 EXPECT_FALSE(nestedTG
.isParallel());
151 nestedTG
.spawn([&]() {
152 // Check that root TaskGroup is in Parallel mode.
153 EXPECT_TRUE(tg
.isParallel() ||
154 (parallel::strategy
.ThreadsRequested
== 1));
156 // Check that nested TaskGroup is not in Parallel mode.
157 EXPECT_FALSE(nestedTG
.isParallel());
163 DefaultThreadPool Pool
;
174 EXPECT_EQ(Count
, 12ul);