ParallelTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <array>
  17. #include <iostream>
  18. #include <memory>
  19. #include <vector>
  20. #include <glog/logging.h>
  21. #include <folly/gen/Base.h>
  22. #include <folly/gen/Parallel.h>
  23. #include <folly/portability/GTest.h>
  24. using namespace folly;
  25. using namespace folly::gen;
  26. using std::vector;
  27. const auto square = [](int i) { return i * i; };
  28. const auto even = [](int i) { return 0 == i % 2; };
  29. static auto sleepyWork = [](int i) {
  30. const auto sleepyTime = std::chrono::microseconds(100);
  31. std::this_thread::sleep_for(sleepyTime);
  32. return i;
  33. };
  34. static auto isPrime = [](int n) {
  35. if (n < 2) {
  36. return false;
  37. } else if (n > 2) {
  38. for (int d = 3; d * d <= n; d += 2) {
  39. if (0 == n % d) {
  40. return false;
  41. }
  42. }
  43. }
  44. return true;
  45. };
  46. struct {
  47. template <class T>
  48. std::unique_ptr<T> operator()(T t) const {
  49. return std::make_unique<T>(std::move(t));
  50. }
  51. } makeUnique;
  52. static auto primes = seq(1, 1 << 14) | filter(isPrime) | as<vector<size_t>>();
  53. static auto primeFactors = [](int n) {
  54. return from(primes) | filter([&](int d) { return 0 == n % d; }) | count;
  55. };
  56. TEST(ParallelTest, Serial) {
  57. EXPECT_EQ(
  58. seq(1, 10) | map(square) | filter(even) | sum,
  59. seq(1, 10) | parallel(map(square) | filter(even)) | sum);
  60. }
  61. auto heavyWork = map(primeFactors);
  62. TEST(ParallelTest, ComputeBound64) {
  63. int length = 1 << 10;
  64. EXPECT_EQ(
  65. seq<size_t>(1, length) | heavyWork | sum,
  66. seq<size_t>(1, length) | parallel(heavyWork) | sum);
  67. }
  68. TEST(ParallelTest, Take) {
  69. int length = 1 << 18;
  70. int limit = 1 << 14;
  71. EXPECT_EQ(
  72. seq(1, length) | take(limit) | count,
  73. seq(1, length) | parallel(heavyWork) | take(limit) | count);
  74. }
  75. TEST(ParallelTest, Unique) {
  76. auto uniqued = from(primes) | map(makeUnique) | as<vector>();
  77. EXPECT_EQ(
  78. primes.size(),
  79. from(primes) | parallel(map(makeUnique)) |
  80. parallel(dereference | map(makeUnique)) | dereference | count);
  81. EXPECT_EQ(
  82. 2,
  83. from(primes) | parallel(map(makeUnique)) |
  84. parallel(dereference | map(makeUnique)) | dereference | take(2) |
  85. count);
  86. }
  87. TEST(ParallelTest, PSum) {
  88. EXPECT_EQ(
  89. from(primes) | map(sleepyWork) | sum,
  90. from(primes) | parallel(map(sleepyWork) | sub(sum)) | sum);
  91. }
  92. int main(int argc, char* argv[]) {
  93. testing::InitGoogleTest(&argc, argv);
  94. gflags::ParseCommandLineFlags(&argc, &argv, true);
  95. return RUN_ALL_TESTS();
  96. }