proxygen
ParallelMapBenchmark.cpp
Go to the documentation of this file.
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 
17 #include <algorithm>
18 #include <atomic>
19 #include <thread>
20 #include <vector>
21 
22 #include <folly/Benchmark.h>
23 #include <folly/gen/Base.h>
24 #include <folly/gen/ParallelMap.h>
26 
27 using namespace folly::gen;
28 
30  threads,
31  std::max(1, (int32_t)sysconf(_SC_NPROCESSORS_CONF) / 2),
32  "Num threads.");
33 
34 constexpr int kFib = 35; // unit of work
35 size_t fib(int n) {
36  return n <= 1 ? 1 : fib(n - 1) * fib(n - 2);
37 }
38 
39 BENCHMARK(FibSumMap, n) {
40  auto result = seq(1, (int)n) | map([](int) { return fib(kFib); }) | sum;
42 }
43 
44 BENCHMARK_RELATIVE(FibSumPmap, n) {
45  // Schedule more work: enough so that each worker thread does the
46  // same amount as one FibSumMap.
47  const size_t kNumThreads = FLAGS_threads;
48  // clang-format off
49  auto result =
50  seq(1, (int)(n * kNumThreads))
51  | pmap([](int) { return fib(kFib); }, kNumThreads)
52  | sum;
53  // clang-format on
55 }
56 
57 BENCHMARK_RELATIVE(FibSumThreads, n) {
58  // Schedule kNumThreads to execute the same code as FibSumMap.
59  const size_t kNumThreads = FLAGS_threads;
60  std::vector<std::thread> workers;
61  workers.reserve(kNumThreads);
62  auto fn = [n] {
63  auto result = seq(1, (int)n) | map([](int) { return fib(kFib); }) | sum;
65  };
66  for (size_t i = 0; i < kNumThreads; i++) {
67  workers.push_back(std::thread(fn));
68  }
69  for (auto& w : workers) {
70  w.join();
71  }
72 }
73 
74 /*
75  ============================================================================
76  folly/gen/test/ParallelMapBenchmark.cpp relative time/iter iters/s
77  ============================================================================
78  FibSumMap 41.64ms 24.02
79  FibSumPmap 98.38% 42.32ms 23.63
80  FibSumThreads 94.48% 44.07ms 22.69
81  ============================================================================
82 
83  real0m15.595s
84  user2m47.100s
85  sys0m0.016s
86 */
87 
88 int main(int argc, char* argv[]) {
89  gflags::ParseCommandLineFlags(&argc, &argv, true);
91  return 0;
92 }
constexpr int kFib
LogLevel max
Definition: LogLevel.cpp:31
Gen seq(Value first, Value last)
Definition: Base.h:484
static size_t const kNumThreads
void runBenchmarks()
Definition: Benchmark.cpp:456
size_t fib(int n)
constexpr detail::Sum sum
Definition: Base-inl.h:2549
std::vector< std::thread::id > threads
char ** argv
BENCHMARK_RELATIVE(FibSumPmap, n)
Map map(Predicate pred=Predicate())
Definition: Base.h:545
int main(int argc, char *argv[])
DEFINE_int32(threads, std::max(1,(int32_t) sysconf(_SC_NPROCESSORS_CONF)/2),"Num threads.")
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
PMap pmap(Predicate pred=Predicate(), size_t nThreads=0)
Definition: ParallelMap.h:42