Commit e44c76df by FritzFlorian

Add high level header with all relevant APIs included.

parent 67553581
// Headers are available because we added the pls target
#include <pls/library.h>
#include <iostream>
#include <functional>
#include <array>
#include <atomic>
#include <pls/pls.h>
using namespace pls;
// Example for static memory allocation (no malloc or free required)
static static_scheduler_memory<8, 2 << 12> my_scheduler_memory;
class fib: public tbb_sub_task {
static constexpr int CUTOFF = 20;
int num_;
int* result_;
public:
fib(int num, int* result): num_{num}, result_{result} {}
private:
static int fib_serial(int num) {
if (num == 0) {
return 0;
}
if (num == 1) {
return 1;
}
return fib_serial(num - 1) + fib_serial(num - 2);
}
protected:
void execute_internal() override {
if (num_ <= CUTOFF) {
*result_ = fib_serial(num_);
return;
}
int left_result;
int right_result;
fib left_child{num_ - 1, &left_result};
spawn_child(left_child);
fib right_child{num_ - 2, &right_result};
spawn_child(right_child);
wait_for_all();
*result_ = left_result + right_result;
}
public:
void test() override {
std::cout << "Test Override" << std::endl;
}
};
int main() {
// All interfaces are scoped in the pls namespace...
// ...explicitly name it...
pls::hello();
// ...or use the namespace...
using namespace pls;
hello();
scheduler my_scheduler{&my_scheduler_memory, 1};
auto start = std::chrono::high_resolution_clock::now();
my_scheduler.perform_work([] (){
int result;
fib fib_sub_task{45, &result};
tbb_task tbb_task{&fib_sub_task};
scheduler::execute_task(tbb_task);
std::cout << "Result: " << result << std::endl;
});
auto end = std::chrono::high_resolution_clock::now();
long time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Startup time in us: " << time << std::endl;
}
#ifndef PLS_LIBRARY_H
#define PLS_LIBRARY_H
#include <pls/internal/scheduling/scheduler.h>
#include <pls/internal/scheduling/tbb_task.h>
namespace pls {
void hello();
int test_adder(int a, int b);
using internal::scheduling::scheduler;
using internal::scheduling::static_scheduler_memory;
using internal::scheduling::tbb_sub_task;
using internal::scheduling::tbb_task;
}
#endif
#include "pls/library.h"
#include <iostream>
namespace pls {
void hello() {
std::cout << "Hello from PLS!" << std::endl;
}
int test_adder(int a, int b) {
return a + b;
}
}
\ No newline at end of file
#include "pls/pls.h"
namespace pls {
}
add_executable(tests
main.cpp
example_tests.cpp
base_tests.cpp)
target_link_libraries(tests catch2 pls)
#include <catch.hpp>
#include <pls/library.h>
// BDD style
SCENARIO( "the test_adder works", "[library]" ) {
GIVEN ("two numbers") {
WHEN ( "they are positive") {
THEN ( "the result is positive") {
REQUIRE(pls::test_adder(1, 2) >= 0);
}
THEN ( "the result is the sum of both") {
REQUIRE(pls::test_adder(1, 4) == 5);
}
}
}
}
// Normal Style
TEST_CASE( "the test_adder can sumup", "[libray]") {
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment