proxygen
ParallelMapBenchmark.cpp File Reference
#include <algorithm>
#include <atomic>
#include <thread>
#include <vector>
#include <folly/Benchmark.h>
#include <folly/gen/Base.h>
#include <folly/gen/ParallelMap.h>
#include <folly/portability/Unistd.h>

Go to the source code of this file.

Functions

 DEFINE_int32 (threads, std::max(1,(int32_t) sysconf(_SC_NPROCESSORS_CONF)/2),"Num threads.")
 
size_t fib (int n)
 
 BENCHMARK (FibSumMap, n)
 
 BENCHMARK_RELATIVE (FibSumPmap, n)
 
 BENCHMARK_RELATIVE (FibSumThreads, n)
 
int main (int argc, char *argv[])
 

Variables

constexpr int kFib = 35
 

Function Documentation

BENCHMARK ( FibSumMap  ,
 
)

Definition at line 39 of file ParallelMapBenchmark.cpp.

References folly::doNotOptimizeAway(), fib(), folly::gen::map(), folly::gen::seq(), and folly::gen::sum.

39  {
40  auto result = seq(1, (int)n) | map([](int) { return fib(kFib); }) | sum;
42 }
std::atomic< int64_t > sum(0)
constexpr int kFib
Gen seq(Value first, Value last)
Definition: Base.h:484
size_t fib(int n)
static Map map(mapCap)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( FibSumPmap  ,
 
)

Definition at line 44 of file ParallelMapBenchmark.cpp.

References folly::doNotOptimizeAway(), fib(), kNumThreads, folly::gen::pmap(), folly::gen::seq(), and folly::gen::sum.

44  {
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 }
std::atomic< int64_t > sum(0)
constexpr int kFib
Gen seq(Value first, Value last)
Definition: Base.h:484
static size_t const kNumThreads
size_t fib(int n)
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
BENCHMARK_RELATIVE ( FibSumThreads  ,
 
)

Definition at line 57 of file ParallelMapBenchmark.cpp.

References folly::doNotOptimizeAway(), fib(), i, kNumThreads, folly::gen::map(), folly::gen::seq(), and folly::gen::sum.

57  {
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 }
std::atomic< int64_t > sum(0)
constexpr int kFib
Gen seq(Value first, Value last)
Definition: Base.h:484
static size_t const kNumThreads
size_t fib(int n)
static Map map(mapCap)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
DEFINE_int32 ( threads  ,
std::max(1,(int32_t) sysconf(_SC_NPROCESSORS_CONF)/2)  ,
"Num threads."   
)
size_t fib ( int  n)

Definition at line 35 of file ParallelMapBenchmark.cpp.

Referenced by BENCHMARK(), and BENCHMARK_RELATIVE().

35  {
36  return n <= 1 ? 1 : fib(n - 1) * fib(n - 2);
37 }
size_t fib(int n)
int main ( int  argc,
char *  argv[] 
)

Definition at line 88 of file ParallelMapBenchmark.cpp.

References folly::runBenchmarks().

88  {
89  gflags::ParseCommandLineFlags(&argc, &argv, true);
91  return 0;
92 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv

Variable Documentation

constexpr int kFib = 35

Definition at line 34 of file ParallelMapBenchmark.cpp.