brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.9 KiB · c7ecc4e Raw
169 lines · cpp
1//===- llvm/unittest/Support/ParallelTest.cpp -----------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// Parallel.h unit tests.11///12//===----------------------------------------------------------------------===//13 14#include "llvm/Support/Parallel.h"15#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS16#include "llvm/Support/ThreadPool.h"17#include "gtest/gtest.h"18#include <random>19 20uint32_t array[1024 * 1024];21 22using namespace llvm;23 24// Tests below are hanging up on mingw. Investigating.25#if !defined(__MINGW32__)26 27TEST(Parallel, sort) {28  std::mt19937 randEngine;29  std::uniform_int_distribution<uint32_t> dist;30 31  for (auto &i : array)32    i = dist(randEngine);33 34  parallelSort(std::begin(array), std::end(array));35  ASSERT_TRUE(llvm::is_sorted(array));36}37 38TEST(Parallel, parallel_for) {39  // We need to test the case with a TaskSize > 1. We are white-box testing40  // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of41  // writing.42  uint32_t range[2050];43  std::fill(range, range + 2050, 1);44  parallelFor(0, 2049, [&range](size_t I) { ++range[I]; });45 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);51}52 53TEST(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);58  EXPECT_EQ(sum, 0U);59 60  // Sum the lengths of these strings in parallel.61  const char *strs[] = {"a", "ab", "abc", "abcd", "abcde", "abcdef"};62  size_t lenSum =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));66 67  // Check that we handle non-divisible task sizes as above.68  uint32_t range[2050];69  llvm::fill(range, 1);70  sum = parallelTransformReduce(range, 0U, std::plus<uint32_t>(), identity);71  EXPECT_EQ(sum, 2050U);72 73  llvm::fill(range, 2);74  sum = parallelTransformReduce(range, 0U, std::plus<uint32_t>(), identity);75  EXPECT_EQ(sum, 4100U);76 77  // Avoid one large task.78  uint32_t range2[3060];79  llvm::fill(range2, 1);80  sum = parallelTransformReduce(range2, 0U, std::plus<uint32_t>(), identity);81  EXPECT_EQ(sum, 3060U);82}83 84TEST(Parallel, ForEachError) {85  int nums[] = {1, 2, 3, 4, 5, 6};86  Error e = parallelForEachError(nums, [](int v) -> Error {87    if ((v & 1) == 0)88      return createStringError(std::errc::invalid_argument, "asdf");89    return Error::success();90  });91  EXPECT_TRUE(e.isA<ErrorList>());92  std::string errText = toString(std::move(e));93  EXPECT_EQ(errText, std::string("asdf\nasdf\nasdf"));94}95 96#if LLVM_ENABLE_THREADS97TEST(Parallel, NestedTaskGroup) {98  // This test checks:99  // 1. Root TaskGroup is in Parallel mode.100  // 2. Nested TaskGroup is not in Parallel mode.101  parallel::TaskGroup tg;102 103  tg.spawn([&]() {104    EXPECT_TRUE(tg.isParallel() || (parallel::strategy.ThreadsRequested == 1));105  });106 107  tg.spawn([&]() {108    parallel::TaskGroup nestedTG;109    EXPECT_FALSE(nestedTG.isParallel());110 111    nestedTG.spawn([&]() {112      // Check that root TaskGroup is in Parallel mode.113      EXPECT_TRUE(tg.isParallel() ||114                  (parallel::strategy.ThreadsRequested == 1));115 116      // Check that nested TaskGroup is not in Parallel mode.117      EXPECT_FALSE(nestedTG.isParallel());118    });119  });120}121 122TEST(Parallel, ParallelNestedTaskGroup) {123  // This test checks that it is possible to have several TaskGroups124  // run from different threads in Parallel mode.125  std::atomic<size_t> Count{0};126 127  {128    std::function<void()> Fn = [&]() {129      parallel::TaskGroup tg;130 131      tg.spawn([&]() {132        // Check that root TaskGroup is in Parallel mode.133        EXPECT_TRUE(tg.isParallel() ||134                    (parallel::strategy.ThreadsRequested == 1));135 136        // Check that nested TaskGroup is not in Parallel mode.137        parallel::TaskGroup nestedTG;138        EXPECT_FALSE(nestedTG.isParallel());139        ++Count;140 141        nestedTG.spawn([&]() {142          // Check that root TaskGroup is in Parallel mode.143          EXPECT_TRUE(tg.isParallel() ||144                      (parallel::strategy.ThreadsRequested == 1));145 146          // Check that nested TaskGroup is not in Parallel mode.147          EXPECT_FALSE(nestedTG.isParallel());148          ++Count;149        });150      });151    };152 153    DefaultThreadPool Pool;154 155    Pool.async(Fn);156    Pool.async(Fn);157    Pool.async(Fn);158    Pool.async(Fn);159    Pool.async(Fn);160    Pool.async(Fn);161 162    Pool.wait();163  }164  EXPECT_EQ(Count, 12ul);165}166#endif167 168#endif169