Blender V5.3
task_graph.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_task_c.hh"
14
15#include <memory>
16#include <vector>
17
18#ifdef WITH_TBB
19# include <tbb/flow_graph.h>
20#endif
21
22namespace blender {
23
24/* Task Graph */
25struct TaskGraph {
26#ifdef WITH_TBB
27 tbb::flow::graph tbb_graph;
28#endif
29 std::vector<std::unique_ptr<TaskNode>> nodes;
30
31 MEM_CXX_CLASS_ALLOC_FUNCS("task_graph:TaskGraph")
32};
33
34/* TaskNode - a node in the task graph. */
35struct TaskNode {
36 /* TBB Node. */
37#ifdef WITH_TBB
38 tbb::flow::continue_node<tbb::flow::continue_msg> tbb_node;
39#endif
40 /* Successors to execute after this task, for serial execution fallback. */
41 std::vector<TaskNode *> successors;
42
43 /* User function to be executed with given task data. */
45 void *task_data;
46 /* Optional callback to free task data along with the graph. If task data
47 * is shared between nodes, only a single task node should free the data. */
49
50 TaskNode(TaskGraph *task_graph,
52 void *task_data,
54 :
55#ifdef WITH_TBB
56 tbb_node(task_graph->tbb_graph,
57 tbb::flow::unlimited,
58 [&](const tbb::flow::continue_msg input) { run(input); }),
59#endif
63 {
64#ifndef WITH_TBB
65 UNUSED_VARS(task_graph);
66#endif
67 }
68
69 TaskNode(const TaskNode &other) = delete;
70 TaskNode &operator=(const TaskNode &other) = delete;
71
73 {
74 if (task_data && free_func) {
76 }
77 }
78
79#ifdef WITH_TBB
80 tbb::flow::continue_msg run(const tbb::flow::continue_msg /*input*/)
81 {
83 return tbb::flow::continue_msg();
84 }
85#endif
86
88 {
90 for (TaskNode *successor : successors) {
91 successor->run_serial();
92 }
93 }
94
95 MEM_CXX_CLASS_ALLOC_FUNCS("task_graph:TaskNode")
96};
97
99{
100 return new TaskGraph();
101}
102
104{
105 delete task_graph;
106}
107
109{
110#ifdef WITH_TBB
111 task_graph->tbb_graph.wait_for_all();
112#else
113 UNUSED_VARS(task_graph);
114#endif
115}
116
119 void *user_data,
121{
122 TaskNode *task_node = new TaskNode(task_graph, run, user_data, free_func);
123 task_graph->nodes.push_back(std::unique_ptr<TaskNode>(task_node));
124 return task_node;
125}
126
128{
129#ifdef WITH_TBB
131 return task_node->tbb_node.try_put(tbb::flow::continue_msg());
132 }
133#endif
134
135 task_node->run_serial();
136 return true;
137}
138
140{
141#ifdef WITH_TBB
143 tbb::flow::make_edge(from_node->tbb_node, to_node->tbb_node);
144 return;
145 }
146#endif
147
148 from_node->successors.push_back(to_node);
149}
150
151} // namespace blender
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
BMesh Iterators.
struct TaskGraph * BLI_task_graph_create()
Definition task_graph.cc:98
bool BLI_task_graph_node_push_work(struct TaskNode *task_node)
int BLI_task_scheduler_num_threads()
void BLI_task_graph_free(struct TaskGraph *task_graph)
struct TaskNode * BLI_task_graph_node_create(struct TaskGraph *task_graph, TaskGraphNodeRunFunction run, void *user_data, TaskGraphNodeFreeFunction free_func)
void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph)
void(*) TaskGraphNodeRunFunction(void *__restrict task_data)
void(*) TaskGraphNodeFreeFunction(void *task_data)
void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node)
std::vector< std::unique_ptr< TaskNode > > nodes
Definition task_graph.cc:29
TaskNode(const TaskNode &other)=delete
std::vector< TaskNode * > successors
Definition task_graph.cc:41
TaskNode(TaskGraph *task_graph, TaskGraphNodeRunFunction run_func, void *task_data, TaskGraphNodeFreeFunction free_func)
Definition task_graph.cc:50
TaskNode & operator=(const TaskNode &other)=delete
TaskGraphNodeFreeFunction free_func
Definition task_graph.cc:48
TaskGraphNodeRunFunction run_func
Definition task_graph.cc:44