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

Implement custom thread scheduling #161

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
5 changes: 4 additions & 1 deletion .github/workflows/clang_libstdc++.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ jobs:
run: |
set(ENV{NINJA_STATUS} "[%f/%t %e sec] ")

include(ProcessorCount)
ProcessorCount(N)

execute_process(
COMMAND cmake --build build
COMMAND cmake --build build -j ${N}
RESULT_VARIABLE result
)
if (NOT result EQUAL 0)
Expand Down
17 changes: 10 additions & 7 deletions .github/workflows/gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Update to gcc 11
run: |
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt install gcc-11 g++-11
/usr/bin/g++-11 --version
shell: bash
# - name: Update to gcc 11
# run: |
# sudo add-apt-repository ppa:ubuntu-toolchain-r/test
# sudo apt install gcc-11 g++-11
# /usr/bin/g++-11 --version
# shell: bash

- uses: actions/checkout@v1

Expand Down Expand Up @@ -57,8 +57,11 @@ jobs:
run: |
set(ENV{NINJA_STATUS} "[%f/%t %e sec] ")

include(ProcessorCount)
ProcessorCount(N)

execute_process(
COMMAND cmake --build build
COMMAND cmake --build build -j ${N}
RESULT_VARIABLE result
)
if (NOT result EQUAL 0)
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/msvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ jobs:
endif()
endforeach()

include(ProcessorCount)
ProcessorCount(N)

execute_process(
COMMAND cmake --build build
COMMAND cmake --build build -j ${N}
RESULT_VARIABLE result
)
if (NOT result EQUAL 0)
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ if (ECS_STANDALONE_PROJECT)
add_subdirectory ("examples/parallelism")
add_subdirectory ("examples/plague_spell")
add_subdirectory ("examples/scheduler")
add_subdirectory ("examples/scheduler_layout_demo")
add_subdirectory ("examples/sorting")
add_subdirectory ("examples/tagged_components")

Expand Down
156 changes: 0 additions & 156 deletions CMakePresets.json

This file was deleted.

104 changes: 64 additions & 40 deletions examples/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//#define ECS_SCHEDULER_LAYOUT_DEMO
#include <ecs/ecs.h>
#include <iostream>
#include <thread>

using namespace std::chrono_literals;

// A small example that creates 6 systems with dependencies on 3 components.
//
Expand All @@ -13,75 +11,91 @@ using namespace std::chrono_literals;
// while systems with dependencies will only be executed
// after other systems are done with them.

// |--------------|
// |---------| |
// 1----2 3 4 5----6
// |--------------| |
// |-------------------|
//
// sys1 (write type<0>, read type<1>)
//
// sys2 (write type<1>)
// depends on 1? true
//
// sys3 (write type<2>)
// depends on 1? false
// depends on 2? false
//
// sys4 (read type<0>)
// depends on 1? true
// depends on 2? false
// depends on 3? false
//
// sys5 (write type<2>, read type<0>)
// depends on 1? true
// depends on 2? false
// depends on 3? true
// depends on 4? false
//
// sys6 (read type<2>)
// depends on 1? false
// depends on 2? false
// depends on 3? true
// depends on 4? false
// depends on 5? true

template <size_t I>
struct type {};

int main() {
ecs::runtime ecs;

std::cout << std::boolalpha;
std::cout << "creating systems:\n";

//
// Assumes that type 0 is writte to, and type 1 is only read from.
auto &sys1 = ecs.make_system([](type<0> &, type<1> const &) {
std::cout << "1 ";
std::this_thread::sleep_for(20ms); // simulate work
});
std::cout << "\nsys1 (type<0>&, type<1> const&)\n";
auto const& sys1 = ecs.make_system([](type<0>&, type<1> const&) mutable { std::cout << "1 "; });
std::cout << "\nsys1 (write type<0>, read type<1>)\n";
std::cout << " depends on no systems\n";

//
// Writes to type 1. This system must not execute until after sys1 is done,
// in order to avoid race conditions.
auto &sys2 = ecs.make_system([](type<1> &) {
std::cout << "2 ";
std::this_thread::sleep_for(20ms);
});
std::cout << "\nsys2 (type<1>&)\n";
// Writes to type 1. This system must not execute until after sys1 is done.
// None of the following system use type 1, so sys2 can run parallel with
// all of them.
auto const& sys2 = ecs.make_system([](type<1>&) mutable { std::cout << "2 "; });
std::cout << "\nsys2 (write type<1>)\n";
std::cout << " depends on 1? " << sys2.depends_on(&sys1) << '\n';

//
// Writes to type 2. This has no dependencies on type 0 or 1, so it can be run
// concurrently with sys1 and sys2.
auto &sys3 = ecs.make_system([](type<2> &) {
std::cout << "3 ";
std::this_thread::sleep_for(20ms);
});
std::cout << "\nsys3 (type<2>&)\n";
auto const& sys3 = ecs.make_system([](type<2>&) mutable { std::cout << "3 "; });
std::cout << "\nsys3 (write type<2>)\n";
std::cout << " depends on 1? " << sys3.depends_on(&sys1) << '\n';
std::cout << " depends on 2? " << sys3.depends_on(&sys2) << '\n';

//
// Reads from type 0. Must not execute until sys1 is done.
auto &sys4 = ecs.make_system([](type<0> const &) {
std::cout << "4 ";
std::this_thread::sleep_for(20ms);
});
std::cout << "\nsys4 (type<0> const&)\n";
auto const& sys4 = ecs.make_system([](type<0> const&) mutable { std::cout << "4 "; });
std::cout << "\nsys4 (read type<0>)\n";
std::cout << " depends on 1? " << sys4.depends_on(&sys1) << '\n';
std::cout << " depends on 2? " << sys4.depends_on(&sys2) << '\n';
std::cout << " depends on 3? " << sys4.depends_on(&sys3) << '\n';

//
// Writes to type 2 and reads from type 0. Must not execute until after
// sys3 and sys1 us done.
auto &sys5 = ecs.make_system([](type<2> &, type<0> const &) {
std::cout << "5 ";
std::this_thread::sleep_for(20ms);
});
std::cout << "\nsys5 (type<2>&, type<0> const&)\n";
// Writes to type 2 and reads from type 0.
// Must not execute until after sys3 and sys1 is done.
auto const& sys5 = ecs.make_system([](type<2>&, type<0> const&) mutable { std::cout << "5 "; });
std::cout << "\nsys5 (write type<2>, read type<0>)\n";
std::cout << " depends on 1? " << sys5.depends_on(&sys1) << '\n';
std::cout << " depends on 2? " << sys5.depends_on(&sys2) << '\n';
std::cout << " depends on 3? " << sys5.depends_on(&sys3) << '\n';
std::cout << " depends on 4? " << sys5.depends_on(&sys4) << '\n';

//
// Reads from type 2. Must not execute until sys5 is done.
auto &sys6 = ecs.make_system([](type<2> const &) {
std::cout << "6 ";
std::this_thread::sleep_for(20ms);
});
std::cout << "\nsys6 (type<2> const&)\n";
auto const& sys6 = ecs.make_system([](type<2> const&) { std::cout << "6 "; });
std::cout << "\nsys6 (read type<2>)\n";
std::cout << " depends on 1? " << sys6.depends_on(&sys1) << '\n';
std::cout << " depends on 2? " << sys6.depends_on(&sys2) << '\n';
std::cout << " depends on 3? " << sys6.depends_on(&sys3) << '\n';
Expand All @@ -90,8 +104,18 @@ int main() {

//
// Add the components to an entitiy and run the systems.
std::cout << "\nrunning systems on 5 entities:\n";
ecs.add_component({0, 4}, type<0>{}, type<1>{}, type<2>{});
std::cout << "\nrunning systems on entities:\n";
std::cout << " dependcy chains: 21, 41, 531, 653\n\n";
ecs.add_component({0, 9}, type<0>{});
ecs.add_component({4, 9}, type<1>{});
ecs.add_component({7, 9}, type<2>{});
ecs.update();
std::cout << '\n';

// System 1 should run 6 times
// System 2 should run 6 times
// System 3 should run 3 times
// System 4 should run 6+4 times
// System 5 should run 3 times
// System 6 should run 3 times
}
5 changes: 5 additions & 0 deletions examples/scheduler_layout_demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required (VERSION 3.15)

add_executable (scheduler_layout_demo "scheduler_layout_demo.cpp")
target_compile_options(scheduler_layout_demo PUBLIC /bigobj)
target_link_libraries(scheduler_layout_demo ecs)
Loading