Blender  V3.3
BLI_task_graph_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "testing/testing.h"
4 
5 #include "MEM_guardedalloc.h"
6 
7 #include "BLI_task.h"
8 
9 struct TaskData {
10  int value;
11  int store;
12 };
13 
14 static void TaskData_increase_value(void *taskdata)
15 {
16  TaskData *data = (TaskData *)taskdata;
17  data->value += 1;
18 }
19 static void TaskData_decrease_value(void *taskdata)
20 {
21  TaskData *data = (TaskData *)taskdata;
22  data->value -= 1;
23 }
24 static void TaskData_multiply_by_two_value(void *taskdata)
25 {
26  TaskData *data = (TaskData *)taskdata;
27  data->value *= 2;
28 }
29 
30 static void TaskData_multiply_by_two_store(void *taskdata)
31 {
32  TaskData *data = (TaskData *)taskdata;
33  data->store *= 2;
34 }
35 
36 static void TaskData_store_value(void *taskdata)
37 {
38  TaskData *data = (TaskData *)taskdata;
39  data->store = data->value;
40 }
41 
42 static void TaskData_square_value(void *taskdata)
43 {
44  TaskData *data = (TaskData *)taskdata;
45  data->value *= data->value;
46 }
47 
48 /* Sequential Test for using `BLI_task_graph` */
49 TEST(task, GraphSequential)
50 {
51  TaskData data = {0};
53 
54  /* 0 => 1 */
56  /* 1 => 2 */
59  /* 2 => 1 */
61  /* 2 => 1 */
63  /* 1 => 1 */
65  /* 1 => 2 */
66  const int expected_value = 2;
67 
68  BLI_task_graph_edge_create(node_a, node_b);
69  BLI_task_graph_edge_create(node_b, node_c);
70  BLI_task_graph_edge_create(node_c, node_d);
71  BLI_task_graph_edge_create(node_d, node_e);
72 
73  EXPECT_TRUE(BLI_task_graph_node_push_work(node_a));
75 
76  EXPECT_EQ(expected_value, data.value);
78 }
79 
80 TEST(task, GraphStartAtAnyNode)
81 {
82  TaskData data = {4};
84 
91 
92  // ((4 - 1) * (4 - 1)) + 1
93  const int expected_value = 10;
94 
95  BLI_task_graph_edge_create(node_a, node_b);
96  BLI_task_graph_edge_create(node_b, node_c);
97  BLI_task_graph_edge_create(node_c, node_d);
98  BLI_task_graph_edge_create(node_d, node_e);
99 
100  EXPECT_TRUE(BLI_task_graph_node_push_work(node_c));
102 
103  EXPECT_EQ(expected_value, data.value);
105 }
106 
107 TEST(task, GraphSplit)
108 {
109  TaskData data = {1};
110 
117  BLI_task_graph_edge_create(node_a, node_b);
118  BLI_task_graph_edge_create(node_b, node_c);
119  BLI_task_graph_edge_create(node_b, node_d);
120  EXPECT_TRUE(BLI_task_graph_node_push_work(node_a));
122 
123  EXPECT_EQ(3, data.value);
124  EXPECT_EQ(4, data.store);
126 }
127 
128 TEST(task, GraphForest)
129 {
130  TaskData data1 = {1};
131  TaskData data2 = {3};
132 
134 
135  {
136  TaskNode *tree1_node_a = BLI_task_graph_node_create(
137  graph, TaskData_increase_value, &data1, nullptr);
138  TaskNode *tree1_node_b = BLI_task_graph_node_create(
139  graph, TaskData_store_value, &data1, nullptr);
140  TaskNode *tree1_node_c = BLI_task_graph_node_create(
141  graph, TaskData_increase_value, &data1, nullptr);
142  TaskNode *tree1_node_d = BLI_task_graph_node_create(
144  BLI_task_graph_edge_create(tree1_node_a, tree1_node_b);
145  BLI_task_graph_edge_create(tree1_node_b, tree1_node_c);
146  BLI_task_graph_edge_create(tree1_node_b, tree1_node_d);
147  EXPECT_TRUE(BLI_task_graph_node_push_work(tree1_node_a));
148  }
149 
150  {
151  TaskNode *tree2_node_a = BLI_task_graph_node_create(
152  graph, TaskData_increase_value, &data2, nullptr);
153  TaskNode *tree2_node_b = BLI_task_graph_node_create(
154  graph, TaskData_store_value, &data2, nullptr);
155  TaskNode *tree2_node_c = BLI_task_graph_node_create(
156  graph, TaskData_increase_value, &data2, nullptr);
157  TaskNode *tree2_node_d = BLI_task_graph_node_create(
159  BLI_task_graph_edge_create(tree2_node_a, tree2_node_b);
160  BLI_task_graph_edge_create(tree2_node_b, tree2_node_c);
161  BLI_task_graph_edge_create(tree2_node_b, tree2_node_d);
162  EXPECT_TRUE(BLI_task_graph_node_push_work(tree2_node_a));
163  }
164 
166 
167  EXPECT_EQ(3, data1.value);
168  EXPECT_EQ(4, data1.store);
169  EXPECT_EQ(5, data2.value);
170  EXPECT_EQ(8, data2.store);
172 }
173 
174 TEST(task, GraphTaskData)
175 {
176  TaskData data = {0};
181  BLI_task_graph_edge_create(node_a, node_b);
182  EXPECT_TRUE(BLI_task_graph_node_push_work(node_a));
184  EXPECT_EQ(0, data.value);
185  EXPECT_EQ(0, data.store);
187  /* data should be freed once */
188  EXPECT_EQ(1, data.value);
189  EXPECT_EQ(0, data.store);
190 }
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node)
Definition: task_graph.cc:139
struct TaskGraph * BLI_task_graph_create(void)
Definition: task_graph.cc:98
bool BLI_task_graph_node_push_work(struct TaskNode *task_node)
Definition: task_graph.cc:127
struct TaskNode * BLI_task_graph_node_create(struct TaskGraph *task_graph, TaskGraphNodeRunFunction run, void *user_data, TaskGraphNodeFreeFunction free_func)
Definition: task_graph.cc:117
void BLI_task_graph_free(struct TaskGraph *task_graph)
Definition: task_graph.cc:103
void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph)
Definition: task_graph.cc:108
TEST(task, GraphSequential)
static void TaskData_decrease_value(void *taskdata)
static void TaskData_square_value(void *taskdata)
static void TaskData_increase_value(void *taskdata)
static void TaskData_multiply_by_two_value(void *taskdata)
static void TaskData_store_value(void *taskdata)
static void TaskData_multiply_by_two_store(void *taskdata)
Read Guarded memory(de)allocation.
Depsgraph * graph
static const float data2[18 *GP_PRIM_DATABUF_SIZE]
static const float data1[33 *GP_PRIM_DATABUF_SIZE]
struct blender::compositor::@179::@181 task