Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contrib separation & ftl::Trampoline #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ project(FiberTaskingLib)

# Options
option(FTL_BUILD_TESTS "Build FiberTaskingLib tests" ON)
option(FTL_BUILD_CONTRIB_TESTS "Should FiberTaskingLib contrib tests be built?" ON)
option(FTL_BUILD_BENCHMARKS "Build FiberTaskingLib benchmarks" ON)
option(FTL_VALGRIND "Link and test with Valgrind" OFF)
option(FTL_FIBER_STACK_GUARD_PAGES "Add guard pages around the fiber stacks" OFF)
Expand Down
128 changes: 128 additions & 0 deletions include/ftl/contrib/trampoline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* FiberTaskingLib - A tasking library that uses fibers for efficient task switching
*
* This library was created as a proof of concept of the ideas presented by
* Christian Gyrling in his 2015 GDC Talk 'Parallelizing the Naughty Dog Engine Using Fibers'
*
* http://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine
*
* FiberTaskingLib is the legal property of Adrian Astley
* Copyright Adrian Astley 2015 - 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "ftl/task.h"

#include <type_traits>
#include <tuple>
#include <functional>

namespace ftl {
class TaskScheduler;

template<class F, class... Args> struct BoundTrampoline;

template<class F>
struct Trampoline {
Trampoline(F&& f) : handler(std::move(f)) {}
F handler;

template<class... Args>
BoundTrampoline<F, Args...>* bind(Args&&... args) {
return new BoundTrampoline<F, Args...>(*this, std::forward_as_tuple(args...));
}
};

template<class F>
Trampoline<F> make_trampoline(F&& f) {
Trampoline<F> t(std::forward<F>(f));
return t;
}

/*
C++11 machinery to power std::apply
*/
namespace detail {
// based on http://stackoverflow.com/a/17426611/410767 by Xeo
template <size_t... Ints>
struct index_sequence {
using type = index_sequence;
using value_type = size_t;
static constexpr std::size_t size() noexcept { return sizeof...(Ints); }
};

// --------------------------------------------------------------

template <class Sequence1, class Sequence2>
struct _merge_and_renumber;

template <size_t... I1, size_t... I2>
struct _merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>>
: index_sequence<I1..., (sizeof...(I1) + I2)...> {};

// --------------------------------------------------------------

template <size_t N>
struct make_index_sequence
: _merge_and_renumber<typename make_index_sequence<N / 2>::type,
typename make_index_sequence<N - N / 2>::type> {};

template<> struct make_index_sequence<0> : index_sequence<> {};
template<> struct make_index_sequence<1> : index_sequence<0> {};


template<typename F, typename... Args>
auto invoke(F f, Args&&... args) -> decltype(std::ref(f)(std::forward<Args>(args)...)) {
return std::ref(f)(std::forward<Args>(args)...);
}

template <class F, class Tuple, std::size_t... I>
auto apply_impl(F&& f, Tuple&& t, detail::index_sequence<I...>) -> decltype(std::ref(f)(std::get<I>(std::forward<Tuple>(t))...)) {
return invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...);
}

} // namespace detail

template<class F, class... Args>
struct BoundTrampoline {
Trampoline<F> tramp;
std::tuple<Args...> args;

friend struct Trampoline<F>;
private:
BoundTrampoline(Trampoline<F> func, std::tuple<Args...>&& args) : tramp(func), args(std::move(args)) {}
public:
BoundTrampoline(const BoundTrampoline&) = delete;
BoundTrampoline& operator=(const BoundTrampoline&) = delete;

BoundTrampoline(BoundTrampoline&&) = default;
BoundTrampoline& operator=(BoundTrampoline&&) = default;

// call task
static void gencall(ftl::TaskScheduler* ts, void * arg) {
auto t = static_cast<BoundTrampoline<F, Args...>*>(arg);
detail::apply_impl(t->tramp.handler, t->args,detail::make_index_sequence<std::tuple_size<typename std::remove_reference<decltype(args)>::type>::value>{});
delete t;
}

operator Task() {
return Task{ &gencall, static_cast<void*>(this) };
}

~BoundTrampoline() = default;
};

} // End of namespace ftl
13 changes: 11 additions & 2 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,24 @@ SetSourceGroup(NAME Util
../include/ftl/ftl_valgrind.h
)

SetSourceGroup(NAME Contrib
PREFIX FTL
SOURCE_FILES ../include/ftl/contrib/trampoline.h
)

# Link all the sources into one
set(FIBER_TASKING_LIB_SRC
${FTL_CORE}
${FTL_UTIL}
)

add_library(ftl STATIC ${FIBER_TASKING_LIB_SRC})
set(FIBER_TASKING_LIB_CONTRIB_SRC
${FTL_CONTRIB}
)

add_library(ftl STATIC ${FIBER_TASKING_LIB_SRC} ${FIBER_TASKING_LIB_CONTRIB_SRC})
target_link_libraries(ftl boost_context ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(ftl PUBLIC ../include)

# Remove the prefix
set_target_properties(ftl PROPERTIES PREFIX "")
set_target_properties(ftl PROPERTIES PREFIX "")
1 change: 0 additions & 1 deletion source/atomic_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,4 @@ void AtomicCounter::CheckWaitingFibers(uint value) {
}
}


} // End of namespace ftl
18 changes: 17 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,24 @@ set(FIBER_TASKING_LIB_TESTS_SRC
${FTL_TEST_TRIANGLE_NUMBER}
)

if(FTL_BUILD_CONTRIB_TESTS)
SetSourceGroup(NAME "Trampoline"
PREFIX FTL_TEST_CONTRIB
SOURCE_FILES contrib/trampoline.cpp
)

# Link all the sources into one
set(FIBER_TASKING_LIB_TESTS_CONTRIB_SRC
${FTL_TEST_CONTRIB_TRAMPOLINE}
)

set(FIBER_TASKING_LIB_TESTS_SRC
${FIBER_TASKING_LIB_TESTS_SRC}
${FIBER_TASKING_LIB_TESTS_CONTRIB_SRC}
)
endif()

add_executable(ftl-test ${FIBER_TASKING_LIB_TESTS_SRC})
target_link_libraries(ftl-test gtest gtest_main ftl)

GTEST_ADD_TESTS(ftl-test "" ${FIBER_TASKING_LIB_TESTS_SRC})
GTEST_ADD_TESTS(ftl-test "" ${FIBER_TASKING_LIB_TESTS_SRC})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline

61 changes: 61 additions & 0 deletions tests/contrib/trampoline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* FiberTaskingLib - A tasking library that uses fibers for efficient task switching
*
* This library was created as a proof of concept of the ideas presented by
* Christian Gyrling in his 2015 GDC Talk 'Parallelizing the Naughty Dog Engine Using Fibers'
*
* http://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine
*
* FiberTaskingLib is the legal property of Adrian Astley
* Copyright Adrian Astley 2015 - 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ftl/atomic_counter.h"
#include "ftl/task_scheduler.h"

#include <gtest/gtest.h>

#include "ftl/contrib/trampoline.h"

struct Foo {};

/*
Showcases Trampoline functionality

Features:
- automatic type safe interface and lambdas-as-tasks
- error if mismatched type/count of arguments (if a bit cryptic)
- no need to repeat types

Glossary:
- Trampoline: wrapper around a lambda / function
- BoundTrampoline: Trampoline + arguments
*/

void TrampolineMainTask(ftl::TaskScheduler *taskScheduler, void *arg) {
Foo f;
int a = 3, b = 4, d = 6;

ftl::AtomicCounter counter(taskScheduler);
taskScheduler->AddTask(*ftl::make_trampoline([](int& a, const int& b, const Foo& f, const int d) { a++; }).bind(a, b, f, d), &counter);
taskScheduler->WaitForCounter(&counter, 0);
// values are correctly captured and const is respected
std::cout << a << '\n';
Copy link
Owner

@RichieSams RichieSams Oct 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the cout, can you do a GTEST_ASSERT_*() ?

}

TEST(ContribTests, Trampoline) {
ftl::TaskScheduler taskScheduler;
taskScheduler.Run(400, TrampolineMainTask);
}