Skip to content

Commit

Permalink
non-parallel, world run system func directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 28, 2020
1 parent 377f57e commit 83932d8
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 28 deletions.
2 changes: 2 additions & 0 deletions doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
- [x] exception: invalid `Entity`
- [x] lock `FilterChange`
- [ ] `EntityMngr` `Query`-driven API
- [x] non-parallel job
- [x] world run system func directly
- [x] `CmptsView` = `const EntityLocator* locator + void** cmpts`
- [ ] pmr
- [ ] batch create/instantiate (need benchmark)
Expand Down
1 change: 1 addition & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace Ubpa::UECS {
void GenEntityJob(World*, Job*, SystemFunc*) const;
void GenChunkJob(World*, Job*, SystemFunc*) const;
void GenJob(World*, Job*, SystemFunc*) const;
void AutoGen(World*, Job*, SystemFunc*) const;

struct EntityInfo {
Archetype* archetype{ nullptr };
Expand Down
8 changes: 4 additions & 4 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ namespace Ubpa::UECS {
const SystemFunc* RegisterEntityJob(
Func&&,
std::string name,
bool isParallel = true,
ArchetypeFilter = {},
CmptLocator = {},
SingletonLocator = {},
bool isParallel = true
SingletonLocator = {}
);

// Func's argument list:
Expand All @@ -53,8 +53,8 @@ namespace Ubpa::UECS {
Func&&,
std::string name,
ArchetypeFilter = {},
SingletonLocator = {},
bool isParallel = true
bool isParallel = true,
SingletonLocator = {}
);

// Func's argument list:
Expand Down
44 changes: 44 additions & 0 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@ namespace Ubpa::UECS {

void AddCommand(std::function<void(World*)> command);

// Func's argument list:
// World*
// {LastFrame|Latest}<Singleton<Cmpt>>
// SingletonsView
// Entity
// size_t indexInQuery
// <tagged-components>: [const] <Cmpt>*...
// CmptsView
template<typename Func>
void RunEntityJob(
Func&&,
bool isParallel = true,
ArchetypeFilter = {},
CmptLocator = {},
SingletonLocator = {}
);

// Func's argument list:
// World*
// {LastFrame|Latest}<Singleton<Cmpt>>
// SingletonsView
// ChunkView (necessary)
template<typename Func>
void RunChunkJob(
Func&&,
ArchetypeFilter = {},
bool isParallel = true,
SingletonLocator = {}
);

// Func's argument list:
// World*
// {LastFrame|Write|Latest}<Singleton<Cmpt>>
// SingletonsView
template<typename Func>
void RunJob(
Func&&,
SingletonLocator = {}
);

private:
mutable JobExecutor executor;
Schedule schedule;
Expand All @@ -50,10 +90,14 @@ namespace Ubpa::UECS {
std::mutex commandBufferMutex;
void RunCommands();

void Run(SystemFunc*);

// ==================================================
World(const World& world) = delete;
World(World&& world) = delete;
World& operator==(World&& world) = delete;
World& operator=(const World& world) = delete;
};
}

#include "detail/World.inl"
8 changes: 4 additions & 4 deletions include/UECS/detail/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Ubpa::UECS {
const SystemFunc* Schedule::RegisterEntityJob(
Func&& func,
std::string name,
bool isParallel,
ArchetypeFilter filter,
CmptLocator cmptLocator,
SingletonLocator singletonLocator,
bool isParallel
SingletonLocator singletonLocator
) {
return Request(
std::forward<Func>(func),
Expand All @@ -26,8 +26,8 @@ namespace Ubpa::UECS {
Func&& func,
std::string name,
ArchetypeFilter filter,
SingletonLocator singletonLocator,
bool isParallel
bool isParallel,
SingletonLocator singletonLocator
) {
return Request(
std::forward<Func>(func),
Expand Down
52 changes: 52 additions & 0 deletions include/UECS/detail/World.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

namespace Ubpa::UECS {
template<typename Func>
void World::RunEntityJob(
Func&& func,
bool isParallel,
ArchetypeFilter filter,
CmptLocator cmptLocator,
SingletonLocator singletonLocator
) {
SystemFunc sys{
std::forward<Func>(func),
"",
std::move(filter),
std::move(cmptLocator),
std::move(singletonLocator),
std::move(isParallel)
};
Run(&sys);
}

template<typename Func>
void World::RunChunkJob(
Func&& func,
ArchetypeFilter filter,
bool isParallel,
SingletonLocator singletonLocator
) {
SystemFunc sys{
std::forward<Func>(func),
"",
std::move(filter),
std::move(singletonLocator),
std::move(isParallel)
};
Run(&sys);
}

template<typename Func>
void World::RunJob(
Func&& func,
SingletonLocator singletonLocator
) {
SystemFunc sys{
std::forward<Func>(func),
"",
std::move(singletonLocator)
};
Run(&sys);
}
}
18 changes: 18 additions & 0 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ void EntityMngr::GenJob(World* w, Job* job, SystemFunc* sys) const {
});
}

void EntityMngr::AutoGen(World* w, Job* job, SystemFunc* sys) const {
switch (sys->GetMode())
{
case SystemFunc::Mode::Entity:
GenEntityJob(w, job, sys);
break;
case SystemFunc::Mode::Chunk:
GenChunkJob(w, job, sys);
break;
case SystemFunc::Mode::Job:
GenJob(w, job, sys);
break;
default:
assert("not support" && false);
break;
}
}

void EntityMngr::Accept(IListener* listener) const {
listener->EnterEntityMngr(this);
for (const auto& [ts, a] : ts2a) {
Expand Down
22 changes: 8 additions & 14 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,7 @@ void World::Update() {
for (const auto& [func, adjVs] : graph.GetAdjList()) {
auto job = jobPool.Request(func->Name());
jobs.push_back(job);
switch (func->GetMode())
{
case Ubpa::UECS::SystemFunc::Mode::Entity:
entityMngr.GenEntityJob(this, job, func);
break;
case Ubpa::UECS::SystemFunc::Mode::Chunk:
entityMngr.GenChunkJob(this, job, func);
break;
case Ubpa::UECS::SystemFunc::Mode::Job:
entityMngr.GenJob(this, job, func);
break;
default:
break;
}
entityMngr.AutoGen(this, job, func);
table[func] = jobGraph.composed_of(*job);
}

Expand All @@ -57,6 +44,13 @@ string World::DumpUpdateJobGraph() const {
return jobGraph.dump();
}

void World::Run(SystemFunc* sys) {
Job job;
JobExecutor executor;
entityMngr.AutoGen(this, &job, sys);
executor.run(job).wait();
}

// after running Update
UGraphviz::Graph World::GenUpdateFrameGraph() const {
UGraphviz::Graph graph("Update Frame Graph", true);
Expand Down
1 change: 1 addition & 0 deletions src/test/04_filter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MySystem : public System {
cout << e->val << endl;
},
"test filter",
true,
filter
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/06_none_parallel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class MySystem : public System {
ArchetypeFilter filter_w0, filter_w1;
filter_w0.none = { CmptType::Of<A> };
filter_w1.all = { CmptAccessType::Of<A> };
schedule.RegisterEntityJob([](B*) {}, "need B, none A", filter_w0);
schedule.RegisterEntityJob([](B*) {}, "need A, B", filter_w1);;
schedule.RegisterEntityJob([](B*) {}, "need B, none A", true, filter_w0);
schedule.RegisterEntityJob([](B*) {}, "need A, B", true, filter_w1);;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/test/11_runtime_cmpt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class RTDSystem: public System{
auto luaCmpt = cmpts.GetCmpt(CmptAccessType{ "LuaCmpt", AccessMode::WRITE });
double& val = *reinterpret_cast<double*>(luaCmpt.Ptr());
val = 520.;
}, "write", ArchetypeFilter{}, locator_write);
}, "write", true, ArchetypeFilter{}, locator_write);
schedule.RegisterEntityJob(
[](CmptsView cmpts) {
auto luaCmpt = cmpts.GetCmpt(CmptAccessType{ "LuaCmpt", AccessMode::LATEST });
const double& val = *reinterpret_cast<const double*>(luaCmpt.Ptr());
cout << "value : " << val << endl;
}, "read", ArchetypeFilter{}, locator_read);
}, "read", true, ArchetypeFilter{}, locator_read);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/test/12_framegraph/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MySystem : public System {
.RegisterEntityJob(
[](LastFrame<A> a, Write<B> b, Latest<C> c) {},
"System Func",
true,
filter
);
}
Expand Down
11 changes: 9 additions & 2 deletions src/test/17_serial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PrintASystem : public System {
}, "Spilt");
schedule.RegisterEntityJob([](const A*) {
std::cout << "A" << std::endl;
}, "Serial Print A", {}, {}, {}, false);
}, "Serial Print A", false);
schedule.Order("Parallel Print A", "Spilt");
schedule.Order("Spilt", "Serial Print A");
}
Expand All @@ -37,11 +37,18 @@ int main() {
w.entityMngr.cmptTraits.Register
<A, B, C>();

for (size_t i = 0; i < 100; i++) {
for (size_t i = 0; i < 5; i++) {
w.Update();
std::cout << "^^^^^^^^^^" << std::endl;
}

for (size_t i = 0; i < 100; i++)
w.entityMngr.Create();

w.RunEntityJob([](Entity e) {
std::cout << e.Idx() << std::endl;
}, false);

std::cout << w.DumpUpdateJobGraph() << std::endl;
std::cout << w.GenUpdateFrameGraph().Dump() << std::endl;
}

0 comments on commit 83932d8

Please sign in to comment.