no message
This commit is contained in:
51
cocos/base/job-system/JobSystem.h
Normal file
51
cocos/base/job-system/JobSystem.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if CC_USE_JOB_SYSTEM_TASKFLOW
|
||||
#include "job-system-taskflow/TFJobGraph.h"
|
||||
#include "job-system-taskflow/TFJobSystem.h"
|
||||
namespace cc {
|
||||
using JobToken = TFJobToken;
|
||||
using JobGraph = TFJobGraph;
|
||||
using JobSystem = TFJobSystem;
|
||||
} // namespace cc
|
||||
#elif CC_USE_JOB_SYSTEM_TBB
|
||||
#include "job-system-tbb/TBBJobGraph.h"
|
||||
#include "job-system-tbb/TBBJobSystem.h"
|
||||
namespace cc {
|
||||
using JobToken = TBBJobToken;
|
||||
using JobGraph = TBBJobGraph;
|
||||
using JobSystem = TBBJobSystem;
|
||||
} // namespace cc
|
||||
#else
|
||||
#include "job-system-dummy/DummyJobGraph.h"
|
||||
#include "job-system-dummy/DummyJobSystem.h"
|
||||
namespace cc {
|
||||
using JobToken = DummyJobToken;
|
||||
using JobGraph = DummyJobGraph;
|
||||
using JobSystem = DummyJobSystem;
|
||||
} // namespace cc
|
||||
#endif
|
||||
147
cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp
Normal file
147
cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "DummyJobGraph.h"
|
||||
#include "base/Macros.h"
|
||||
|
||||
#define DUMMY_GRAPH_NODE_CHUNK_SIZE 64
|
||||
|
||||
namespace cc {
|
||||
|
||||
namespace {
|
||||
DummyGraphNode *freeList{nullptr};
|
||||
ccstd::vector<DummyGraphNode *> allocatedChunks;
|
||||
} // namespace
|
||||
|
||||
DummyGraphNode::~DummyGraphNode() {
|
||||
delete _callback;
|
||||
}
|
||||
|
||||
void DummyGraphNode::reset() {
|
||||
_successors.clear();
|
||||
_predecessors.clear();
|
||||
delete _callback;
|
||||
_callback = nullptr;
|
||||
}
|
||||
|
||||
void DummyGraphNode::succeed(DummyGraphNode *other) {
|
||||
CC_ASSERT_NE(this, other);
|
||||
// Run after other
|
||||
this->_predecessors.emplace(other);
|
||||
other->_successors.emplace(this);
|
||||
}
|
||||
|
||||
void DummyGraphNode::precede(DummyGraphNode *other) {
|
||||
// Run before other
|
||||
other->succeed(this);
|
||||
}
|
||||
|
||||
void DummyGraphNode::allocChunk() {
|
||||
CC_ASSERT_NULL(freeList);
|
||||
freeList = ccnew DummyGraphNode[DUMMY_GRAPH_NODE_CHUNK_SIZE]();
|
||||
allocatedChunks.emplace_back(freeList);
|
||||
for (auto i = 0; i < DUMMY_GRAPH_NODE_CHUNK_SIZE - 1; i++) {
|
||||
freeList[i]._next = &freeList[i + 1];
|
||||
}
|
||||
freeList[DUMMY_GRAPH_NODE_CHUNK_SIZE - 1]._next = nullptr;
|
||||
}
|
||||
|
||||
DummyGraphNode *DummyGraphNode::alloc() {
|
||||
if (freeList == nullptr) {
|
||||
DummyGraphNode::allocChunk();
|
||||
}
|
||||
auto *p = freeList;
|
||||
freeList = freeList->_next;
|
||||
p->reset();
|
||||
return p;
|
||||
}
|
||||
|
||||
void DummyGraphNode::free(DummyGraphNode *node) {
|
||||
node->_next = freeList;
|
||||
freeList = node;
|
||||
}
|
||||
|
||||
void DummyGraphNode::freeAll() {
|
||||
for (auto *chunk : allocatedChunks) {
|
||||
delete[] chunk;
|
||||
}
|
||||
allocatedChunks.clear();
|
||||
}
|
||||
|
||||
DummyGraph::~DummyGraph() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void DummyGraph::clear() {
|
||||
for (auto *node : _nodes) {
|
||||
DummyGraphNode::free(node);
|
||||
}
|
||||
_nodes.clear();
|
||||
}
|
||||
|
||||
void DummyGraph::link(size_t precede, size_t after) {
|
||||
_nodes[precede]->precede(_nodes[after]);
|
||||
}
|
||||
|
||||
void DummyGraph::run() {
|
||||
for (auto *node : _nodes) {
|
||||
if (!excuted(node)) {
|
||||
walk(node);
|
||||
}
|
||||
}
|
||||
_generation++;
|
||||
}
|
||||
|
||||
void DummyGraph::walk(DummyGraphNode *node) { //NOLINT(misc-no-recursion)
|
||||
for (DummyGraphNode *n : node->_predecessors) {
|
||||
if (!excuted(n)) {
|
||||
walk(n);
|
||||
}
|
||||
}
|
||||
if (!excuted(node)) {
|
||||
node->_callback->execute();
|
||||
node->_generation++;
|
||||
}
|
||||
|
||||
for (DummyGraphNode *n : node->_successors) {
|
||||
if (!excuted(n)) {
|
||||
walk(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DummyGraph::excuted(DummyGraphNode *n) const {
|
||||
return n->_generation != _generation;
|
||||
}
|
||||
|
||||
void DummyJobGraph::makeEdge(uint32_t j1, uint32_t j2) {
|
||||
_dummyGraph.link(j1, j2);
|
||||
}
|
||||
|
||||
void DummyJobGraph::run() noexcept {
|
||||
_dummyGraph.run();
|
||||
_dummyGraph.clear();
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
162
cocos/base/job-system/job-system-dummy/DummyJobGraph.h
Normal file
162
cocos/base/job-system/job-system-dummy/DummyJobGraph.h
Normal file
@@ -0,0 +1,162 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/Macros.h"
|
||||
#include "base/memory/Memory.h"
|
||||
#include "base/std/container/unordered_set.h"
|
||||
#include "base/std/container/vector.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class DummyJobSystem;
|
||||
|
||||
class DummyGraphNode;
|
||||
class DummyGraph final {
|
||||
public:
|
||||
DummyGraph() = default;
|
||||
DummyGraph(const DummyGraph &) = delete;
|
||||
DummyGraph(DummyGraph &&) = delete;
|
||||
DummyGraph &operator=(const DummyGraph &) = delete;
|
||||
DummyGraph &operator=(DummyGraph &&) = delete;
|
||||
~DummyGraph();
|
||||
|
||||
template <class Fn>
|
||||
size_t addNode(Fn &&fn);
|
||||
|
||||
void run();
|
||||
void link(size_t precede, size_t after);
|
||||
void walk(DummyGraphNode *node);
|
||||
void clear();
|
||||
|
||||
private:
|
||||
bool excuted(DummyGraphNode *n) const;
|
||||
|
||||
int _generation{0};
|
||||
ccstd::vector<DummyGraphNode *> _nodes;
|
||||
};
|
||||
|
||||
class DummyGraphNodeTaskItf {
|
||||
public:
|
||||
virtual ~DummyGraphNodeTaskItf() = default;
|
||||
virtual void execute() = 0;
|
||||
};
|
||||
|
||||
template <class Fn>
|
||||
class DummyGraphNodeTaskImpl final : public DummyGraphNodeTaskItf {
|
||||
public:
|
||||
explicit DummyGraphNodeTaskImpl(Fn &&t) noexcept;
|
||||
DummyGraphNodeTaskImpl(const DummyGraphNodeTaskImpl &) = delete;
|
||||
DummyGraphNodeTaskImpl(DummyGraphNodeTaskImpl &&) = delete;
|
||||
DummyGraphNodeTaskImpl &operator=(const DummyGraphNodeTaskImpl &) = delete;
|
||||
DummyGraphNodeTaskImpl &operator=(DummyGraphNodeTaskImpl &&) = delete;
|
||||
~DummyGraphNodeTaskImpl() override = default;
|
||||
inline void execute() override { _task(); }
|
||||
|
||||
private:
|
||||
Fn _task;
|
||||
};
|
||||
|
||||
class DummyGraphNode final {
|
||||
public:
|
||||
DummyGraphNode() = default;
|
||||
DummyGraphNode(const DummyGraphNode &) = delete;
|
||||
DummyGraphNode(DummyGraphNode &&) = delete;
|
||||
DummyGraphNode &operator=(const DummyGraphNode &) = delete;
|
||||
DummyGraphNode &operator=(DummyGraphNode &&) = delete;
|
||||
~DummyGraphNode();
|
||||
|
||||
private:
|
||||
static void allocChunk();
|
||||
static DummyGraphNode *alloc();
|
||||
static void free(DummyGraphNode *n);
|
||||
static void freeAll();
|
||||
|
||||
void succeed(DummyGraphNode *other);
|
||||
void precede(DummyGraphNode *other);
|
||||
void reset();
|
||||
|
||||
DummyGraphNodeTaskItf *_callback{nullptr};
|
||||
ccstd::unordered_set<DummyGraphNode *> _successors{};
|
||||
ccstd::unordered_set<DummyGraphNode *> _predecessors{};
|
||||
DummyGraphNode *_next{nullptr};
|
||||
int _generation{0};
|
||||
friend class DummyGraph;
|
||||
};
|
||||
|
||||
template <class Fn>
|
||||
DummyGraphNodeTaskImpl<Fn>::DummyGraphNodeTaskImpl(Fn &&t) noexcept : _task(t) {}
|
||||
|
||||
template <class Fn>
|
||||
size_t DummyGraph::addNode(Fn &&fn) {
|
||||
DummyGraphNode *n = DummyGraphNode::alloc();
|
||||
n->_callback = ccnew DummyGraphNodeTaskImpl<Fn>(std::forward<Fn>(fn));
|
||||
n->_generation = _generation;
|
||||
_nodes.emplace_back(n);
|
||||
return _nodes.size() - 1;
|
||||
}
|
||||
|
||||
// exported declarations
|
||||
|
||||
class DummyJobGraph final {
|
||||
public:
|
||||
explicit DummyJobGraph(DummyJobSystem * /*system*/) noexcept {}
|
||||
DummyJobGraph(const DummyJobGraph &) = delete;
|
||||
DummyJobGraph(DummyJobGraph &&) = delete;
|
||||
DummyJobGraph &operator=(const DummyJobGraph &) = delete;
|
||||
DummyJobGraph &operator=(DummyJobGraph &&) = delete;
|
||||
~DummyJobGraph() noexcept = default;
|
||||
|
||||
template <typename Function>
|
||||
uint32_t createJob(Function &&func) noexcept;
|
||||
|
||||
template <typename Function>
|
||||
uint32_t createForEachIndexJob(uint32_t begin, uint32_t end, uint32_t step, Function &&func) noexcept;
|
||||
|
||||
void makeEdge(uint32_t j1, uint32_t j2);
|
||||
|
||||
void run() noexcept;
|
||||
|
||||
inline void waitForAll() { run(); }
|
||||
|
||||
private:
|
||||
DummyGraph _dummyGraph{};
|
||||
};
|
||||
|
||||
template <typename Function>
|
||||
uint32_t DummyJobGraph::createJob(Function &&func) noexcept {
|
||||
return static_cast<uint32_t>(_dummyGraph.addNode(std::forward<Function>(func)));
|
||||
}
|
||||
|
||||
template <typename Function>
|
||||
uint32_t DummyJobGraph::createForEachIndexJob(uint32_t begin, uint32_t end, uint32_t step, Function &&func) noexcept {
|
||||
return static_cast<uint32_t>(_dummyGraph.addNode([callable = std::forward<Function>(func), first = begin, last = end, step = step]() {
|
||||
for (auto i = first; i < last; i += step) {
|
||||
callable(i);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
32
cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp
Normal file
32
cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "DummyJobSystem.h"
|
||||
#include "DummyJobGraph.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
DummyJobSystem *DummyJobSystem::instance = nullptr;
|
||||
|
||||
} // namespace cc
|
||||
62
cocos/base/job-system/job-system-dummy/DummyJobSystem.h
Normal file
62
cocos/base/job-system/job-system-dummy/DummyJobSystem.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/Macros.h"
|
||||
#include "base/memory/Memory.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
using DummyJobToken = size_t;
|
||||
|
||||
class DummyJobGraph;
|
||||
|
||||
class DummyJobSystem final {
|
||||
private:
|
||||
static DummyJobSystem *instance;
|
||||
|
||||
public:
|
||||
static DummyJobSystem *getInstance() {
|
||||
if (!instance) {
|
||||
instance = ccnew DummyJobSystem;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void destroyInstance() {
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
DummyJobSystem() noexcept = default;
|
||||
explicit DummyJobSystem(uint32_t /*threadCount*/) noexcept {}
|
||||
|
||||
inline uint32_t threadCount() const { return THREAD_COUNT; } //NOLINT
|
||||
|
||||
private:
|
||||
static constexpr uint32_t THREAD_COUNT = 1U; //always one
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
40
cocos/base/job-system/job-system-taskflow/TFJobGraph.cpp
Normal file
40
cocos/base/job-system/job-system-taskflow/TFJobGraph.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "TFJobGraph.h"
|
||||
#include "TFJobSystem.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
void TFJobGraph::makeEdge(uint32_t j1, uint32_t j2) noexcept {
|
||||
_tasks[j1].precede(_tasks[j2]);
|
||||
}
|
||||
|
||||
void TFJobGraph::run() noexcept {
|
||||
if (_pending) return;
|
||||
_future = _executor->run(_flow);
|
||||
_pending = true;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
78
cocos/base/job-system/job-system-taskflow/TFJobGraph.h
Normal file
78
cocos/base/job-system/job-system-taskflow/TFJobGraph.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "TFJobSystem.h"
|
||||
#include "base/std/container/deque.h"
|
||||
#include "taskflow/taskflow.hpp"
|
||||
|
||||
namespace cc {
|
||||
|
||||
using TFJobToken = void;
|
||||
|
||||
class TFJobGraph final {
|
||||
public:
|
||||
explicit TFJobGraph(TFJobSystem *system) noexcept : _executor(&system->_executor) {}
|
||||
|
||||
template <typename Function>
|
||||
uint32_t createJob(Function &&func) noexcept;
|
||||
|
||||
template <typename Function>
|
||||
uint32_t createForEachIndexJob(uint32_t begin, uint32_t end, uint32_t step, Function &&func) noexcept;
|
||||
|
||||
void makeEdge(uint32_t j1, uint32_t j2) noexcept;
|
||||
|
||||
void run() noexcept;
|
||||
|
||||
inline void waitForAll() {
|
||||
if (_pending) {
|
||||
_future.wait();
|
||||
_pending = false;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
tf::Executor *_executor = nullptr;
|
||||
|
||||
tf::Taskflow _flow;
|
||||
ccstd::deque<tf::Task> _tasks; // existing tasks cannot be invalidated
|
||||
|
||||
std::future<void> _future;
|
||||
bool _pending = false;
|
||||
};
|
||||
|
||||
template <typename Function>
|
||||
uint32_t TFJobGraph::createJob(Function &&func) noexcept {
|
||||
_tasks.emplace_back(_flow.emplace(func));
|
||||
return static_cast<uint32_t>(_tasks.size() - 1u);
|
||||
}
|
||||
|
||||
template <typename Function>
|
||||
uint32_t TFJobGraph::createForEachIndexJob(uint32_t begin, uint32_t end, uint32_t step, Function &&func) noexcept {
|
||||
_tasks.emplace_back(_flow.for_each_index(begin, end, step, func));
|
||||
return static_cast<uint32_t>(_tasks.size() - 1u);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
38
cocos/base/job-system/job-system-taskflow/TFJobSystem.cpp
Normal file
38
cocos/base/job-system/job-system-taskflow/TFJobSystem.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "TFJobSystem.h"
|
||||
#include "TFJobGraph.h"
|
||||
#include "base/Log.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
TFJobSystem *TFJobSystem::_instance = nullptr;
|
||||
|
||||
TFJobSystem::TFJobSystem(uint32_t threadCount) noexcept
|
||||
: _executor(threadCount) {
|
||||
CC_LOG_INFO("Taskflow Job system initialized: %d worker threads", threadCount);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
60
cocos/base/job-system/job-system-taskflow/TFJobSystem.h
Normal file
60
cocos/base/job-system/job-system-taskflow/TFJobSystem.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <thread>
|
||||
#include "base/memory/Memory.h"
|
||||
#include "taskflow/taskflow.hpp"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class TFJobSystem final {
|
||||
public:
|
||||
static TFJobSystem *getInstance() {
|
||||
if (!_instance) {
|
||||
_instance = ccnew TFJobSystem;
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
static void destroyInstance() {
|
||||
CC_SAFE_DELETE(_instance);
|
||||
}
|
||||
|
||||
TFJobSystem() noexcept : TFJobSystem(std::max(2u, std::thread::hardware_concurrency() - 2u)) {}
|
||||
explicit TFJobSystem(uint32_t threadCount) noexcept;
|
||||
|
||||
inline uint32_t threadCount() { return static_cast<uint32_t>(_executor.num_workers()); }
|
||||
|
||||
private:
|
||||
friend class TFJobGraph;
|
||||
|
||||
static TFJobSystem *_instance;
|
||||
|
||||
tf::Executor _executor;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
46
cocos/base/job-system/job-system-tbb/TBBJobGraph.cpp
Normal file
46
cocos/base/job-system/job-system-tbb/TBBJobGraph.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "TBBJobGraph.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
void TBBJobGraph::makeEdge(uint32_t j1, uint32_t j2) noexcept {
|
||||
if (j1 & PARALLEL_JOB_FLAG) {
|
||||
j1 = _parallelJobs[j1 & PARALLEL_JOB_MASK].successor;
|
||||
}
|
||||
|
||||
if (j2 & PARALLEL_JOB_FLAG) {
|
||||
j2 = _parallelJobs[j2 & PARALLEL_JOB_MASK].predecessor;
|
||||
}
|
||||
|
||||
tbb::flow::make_edge(_nodes[j1], _nodes[j2]);
|
||||
}
|
||||
|
||||
void TBBJobGraph::run() noexcept {
|
||||
_nodes.front().try_put(tbb::flow::continue_msg());
|
||||
_pending = true;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
107
cocos/base/job-system/job-system-tbb/TBBJobGraph.h
Normal file
107
cocos/base/job-system/job-system-tbb/TBBJobGraph.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tbb/flow_graph.h>
|
||||
#include "base/std/container/deque.h"
|
||||
#include "base/std/container/vector.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
using TBBJobToken = tbb::flow::continue_msg;
|
||||
|
||||
class TBBJobSystem;
|
||||
|
||||
class TBBJobGraph final {
|
||||
public:
|
||||
explicit TBBJobGraph(TBBJobSystem *system) noexcept {
|
||||
_nodes.emplace_back(_graph, [](TBBJobToken t) {});
|
||||
}
|
||||
|
||||
template <typename Function>
|
||||
uint32_t createJob(Function &&func) noexcept;
|
||||
|
||||
template <typename Function>
|
||||
uint32_t createForEachIndexJob(uint32_t begin, uint32_t end, uint32_t step, Function &&func) noexcept;
|
||||
|
||||
void makeEdge(uint32_t j1, uint32_t j2) noexcept;
|
||||
|
||||
void run() noexcept;
|
||||
|
||||
inline void waitForAll() {
|
||||
if (_pending) {
|
||||
_graph.wait_for_all();
|
||||
_pending = false;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr uint32_t PARALLEL_JOB_FLAG = 1u << 20;
|
||||
static constexpr uint32_t PARALLEL_JOB_MASK = ~PARALLEL_JOB_FLAG;
|
||||
|
||||
tbb::flow::graph _graph;
|
||||
|
||||
using TBBJobNode = tbb::flow::continue_node<tbb::flow::continue_msg>;
|
||||
ccstd::deque<TBBJobNode> _nodes; // existing nodes cannot be invalidated
|
||||
|
||||
struct TBBParallelJob {
|
||||
uint32_t predecessor = 0u;
|
||||
uint32_t successor = 0u;
|
||||
};
|
||||
ccstd::vector<TBBParallelJob> _parallelJobs;
|
||||
|
||||
bool _pending = false;
|
||||
};
|
||||
|
||||
template <typename Function>
|
||||
uint32_t TBBJobGraph::createJob(Function &&func) noexcept {
|
||||
_nodes.emplace_back(_graph, func);
|
||||
tbb::flow::make_edge(_nodes.front(), _nodes.back());
|
||||
return static_cast<uint32_t>(_nodes.size() - 1u);
|
||||
}
|
||||
|
||||
template <typename Function>
|
||||
uint32_t TBBJobGraph::createForEachIndexJob(uint32_t begin, uint32_t end, uint32_t step, Function &&func) noexcept {
|
||||
_nodes.emplace_back(_graph, [](TBBJobToken t) {});
|
||||
auto predecessorIdx = static_cast<uint32_t>(_nodes.size() - 1u);
|
||||
TBBJobNode &predecessor = _nodes.back();
|
||||
|
||||
tbb::flow::make_edge(_nodes.front(), predecessor);
|
||||
|
||||
_nodes.emplace_back(_graph, [](TBBJobToken t) {});
|
||||
auto successorIdx = static_cast<uint32_t>(_nodes.size() - 1u);
|
||||
TBBJobNode &successor = _nodes.back();
|
||||
|
||||
for (uint32_t i = begin; i < end; i += step) {
|
||||
_nodes.emplace_back(_graph, [i, &func](TBBJobToken t) { func(i); });
|
||||
tbb::flow::make_edge(predecessor, _nodes.back());
|
||||
tbb::flow::make_edge(_nodes.back(), successor);
|
||||
}
|
||||
|
||||
_parallelJobs.push_back({predecessorIdx, successorIdx});
|
||||
return static_cast<uint32_t>((_parallelJobs.size() - 1u)) | PARALLEL_JOB_FLAG;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
40
cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp
Normal file
40
cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "base/Log.h"
|
||||
|
||||
#include "TBBJobGraph.h"
|
||||
#include "TBBJobSystem.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
TBBJobSystem *TBBJobSystem::_instance = nullptr;
|
||||
|
||||
TBBJobSystem::TBBJobSystem(uint32_t threadCount) noexcept
|
||||
: _control(tbb::global_control::max_allowed_parallelism, threadCount),
|
||||
_threadCount(threadCount) {
|
||||
CC_LOG_INFO("TBB Job system initialized: %d worker threads", threadCount);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
61
cocos/base/job-system/job-system-tbb/TBBJobSystem.h
Normal file
61
cocos/base/job-system/job-system-tbb/TBBJobSystem.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <thread>
|
||||
#include "base/memory/Memory.h"
|
||||
#include "tbb/global_control.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class TBBJobGraph;
|
||||
|
||||
class TBBJobSystem final {
|
||||
public:
|
||||
static TBBJobSystem *getInstance() {
|
||||
if (!_instance) {
|
||||
_instance = ccnew TBBJobSystem;
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
static void destroyInstance() {
|
||||
CC_SAFE_DELETE(_instance);
|
||||
}
|
||||
|
||||
TBBJobSystem() noexcept : TBBJobSystem(std::max(2u, std::thread::hardware_concurrency() - 2u)) {}
|
||||
explicit TBBJobSystem(uint32_t threadCount) noexcept;
|
||||
|
||||
inline uint32_t threadCount() { return _threadCount; }
|
||||
|
||||
private:
|
||||
static TBBJobSystem *_instance;
|
||||
|
||||
tbb::global_control _control;
|
||||
uint32_t _threadCount{0u};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user