main.cpp 1011 Bytes
Newer Older
1
#include <pls/pls.h>
2
#include <pls/internal/helpers/profiler.h>
3

4
#include <iostream>
5

6 7 8
static pls::static_scheduler_memory<8, 2 << 14> my_scheduler_memory;

static constexpr int CUTOFF = 10;
9 10

long fib_serial(long n) {
11 12 13 14 15 16 17 18
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }

  return fib_serial(n - 1) + fib_serial(n - 2);
19 20 21
}

long fib(long n) {
22 23 24 25 26 27 28 29 30 31 32
  if (n <= CUTOFF) {
    return fib_serial(n);
  }

  // Actual 'invoke_parallel' logic/code
  int left, right;
  pls::invoke_parallel(
      [&] { left = fib(n - 1); },
      [&] { right = fib(n - 2); }
  );
  return left + right;
33 34 35
}

int main() {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  PROFILE_ENABLE
  pls::scheduler scheduler{&my_scheduler_memory, 8};

  long result;
  scheduler.perform_work([&] {
    PROFILE_MAIN_THREAD
    // Call looks just the same, only requirement is
    // the enclosure in the perform_work lambda.
    for (int i = 0; i < 10; i++) {
      result = fib(30);
      std::cout << "Fib(30)=" << result << std::endl;
    }
  });

  PROFILE_SAVE("test_profile.prof")
51
}