• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Function Flow Runtime Task Graph (C++)
2
3<!--Kit: Function Flow Runtime Kit-->
4<!--Subsystem: Resourceschedule-->
5<!--Owner: @chuchihtung; @yanleo-->
6<!--Designer: @geoffrey_guo; @huangyouzhong-->
7<!--Tester: @lotsof; @sunxuhao-->
8<!--Adviser: @foryourself-->
9
10## Overview
11
12The FFRT task graph supports task dependency and data dependency. Each node in the task graph indicates a task, and each edge indicates the dependency between tasks. Task dependency is classified into input dependency (`in_deps`) and output dependency (`out_deps`).
13
14You can use either of the following ways to build a task graph:
15
16- Use the task dependency to build a task graph. The task `handle` is used to indicate a task object.
17- Use the data dependency to build a task graph. The data object is abstracted as a data signature, and each data signature uniquely indicates a data object.
18
19### Task dependency
20
21> **NOTE**
22>
23> When a task handle appears in `in_deps`, the corresponding task is the previous task. When a task handle appears in `out_deps`, the corresponding task is the subsequent task.
24
25Task dependency applies to scenarios where tasks have specific sequence or logical process requirements. For example:
26
27- Tasks with sequence. For example, a data preprocessing task is executed before a model training task.
28- Logic process control. For example, in a typical commodity transaction process, orders are placed, followed by production and then logistics transportation.
29- Multi-level chain: For example, during video processing, you can perform tasks such as transcoding, generating thumbnails, adding watermarks, and releasing the final video.
30
31### Data Dependency
32
33> **NOTE**
34>
35> When the signature of a data object appears in `in_deps` of a task, the task is referred to as a consumer task that executes without modifying the original input data object.
36> When the signature of a data object appears in `out_deps` of a task, the task is referred to as a producer task that updates the output data object's content to create a new version.
37
38Data dependency applies to scenarios where tasks are triggered by data production and consumption relationships.
39
40A data object may have multiple versions. Each version corresponds to one producer task and zero, one, or more consumer tasks. A sequence of the data object versions and the version-specific producer task and consumer tasks are defined according to the delivery sequence of the producer task and consumer tasks.
41
42When all producer tasks and consumer tasks of the data object of all the available versions are executed, the data dependency is removed. In this case, the task enters the ready state and can be scheduled for execution.
43
44FFRT can dynamically build producer/consumer-based data dependencies between tasks at runtime and perform scheduling based on the task data dependency status, including:
45
46- Producer-Consumer dependency
47
48  A dependency formed between the producer task of a data object of a specific version and a consumer task of the data object of the same version. It is also referred to as a read-after-write dependency.
49
50- Consumer-Producer dependency
51
52  A dependency formed between a consumer task of a data object of a specific version and the producer task of the data object of the next version. It is also referred to as a write-after-read dependency.
53
54- Producer-Producer dependency
55
56  A dependency formed between the producer task of a data object of a specific version and a producer task of the data object of the next version. It is also referred to as a write-after-write dependency.
57
58For example, the relationship between a group of tasks and data A is expressed as follows:
59
60```cpp
61task1(OUT A);
62task2(IN A);
63task3(IN A);
64task4(OUT A);
65task5(OUT A);
66```
67
68![image](figures/ffrt_figure3.png)
69
70For ease of description, circles are used to represent tasks and squares are used to represent data.
71
72The following conclusions can be drawn:
73
74- task1 and task2/task3 form a producer-consumer dependency. This means that task2/task3 can read data A only after task1 writes data A.
75- task2/task3 and task4 form a consumer-producer dependency. This means that task4 can write data A only after task2/task3 reads data A.
76- task 4 and task 5 form a producer-producer dependency. This means that task 5 can write data A only after task 4 writes data A.
77
78## Example: Streaming Video Processing
79
80A user uploads a video to the platform. The processing steps include: parsing, transcoding, generating a thumbnail, adding watermark, and releasing the video. Transcoding and thumbnail generation can occur simultaneously. The following figure shows the task process.
81
82![image](figures/ffrt_figure1.png)
83
84The FFRT provides task graph that can describe the task dependency and parallelize the preceding video processing process. The code is as follows:
85
86```cpp
87#include <iostream>
88#include "ffrt/ffrt.h" // From the OpenHarmony third-party library "@ppd/ffrt"
89
90int main()
91{
92    // Submit a task.
93    auto handle_A = ffrt::submit_h([] () { std::cout << "Parse" << std::endl; });
94    auto handle_B = ffrt::submit_h([] () { std::cout << "Transcode" << std::endl; }, {handle_A});
95    auto handle_C = ffrt::submit_h([] () { std::cout << "Generate a thumbnail" << std::endl; }, {handle_A});
96    auto handle_D = ffrt::submit_h([] () { std::cout << "Add watermark" << std::endl; }, {handle_B, handle_C});
97    ffrt::submit([] () { std::cout << "Release" << std::endl; }, {handle_D});
98
99    // Wait until all tasks are complete.
100    ffrt::wait();
101    return 0;
102}
103```
104
105The expected output may be as follows:
106
107```plain
108Video parsing
109Video transcoding
110Thumbnails generation
111Watermark adding
112Video release
113```
114
115## Example: Fibonacci Sequence
116
117Each number in the Fibonacci sequence is the sum of the first two numbers. The process of calculating the Fibonacci number can well express the task dependency through the data object. The code for calculating the Fibonacci number using the FFRT framework is as follows:
118
119```cpp
120#include <iostream>
121#include "ffrt/ffrt.h" // From the OpenHarmony third-party library "@ppd/ffrt"
122
123void Fib(int x, int& y)
124{
125    if (x <= 1) {
126        y = x;
127    } else {
128        int y1, y2;
129
130        // Submit the task and build data dependencies.
131        ffrt::submit([&]() { Fib(x - 1, y1); }, {&x}, {&y1});
132        ffrt::submit([&]() { Fib(x - 2, y2); }, {&x}, {&y2});
133
134        // Wait until the task is complete.
135        ffrt::wait({&y1, &y2});
136        y = y1 + y2;
137    }
138}
139
140int main()
141{
142    int y;
143    Fib(5, y);
144    std::cout << "Fibonacci(5) is " << y << std::endl;
145}
146```
147
148Expected output:
149
150```plain
151Fibonacci(5) is 5
152```
153
154In the example, `fibonacci(x-1)` and `fibonacci(x-2)` are submitted to FFRT as two tasks. After the two tasks are complete, the results are accumulated. Although a single task is split into two subtasks, the subtasks can be further split. Therefore, the concurrency of the entire computational graph is very high.
155
156Each task forms a call tree in the FFRT.
157
158![image](figures/ffrt_figure2.png)
159
160## Available APIs
161
162The main FFRT APIs involved in the preceding example are as follows:
163
164| Name                                                                                                               | Description                            |
165| ------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
166| [submit](https://gitee.com/openharmony/resourceschedule_ffrt/blob/master/docs/ffrt-api-guideline-cpp.md#submit)     | Submits a task.              |
167| [submit_h](https://gitee.com/openharmony/resourceschedule_ffrt/blob/master/docs/ffrt-api-guideline-cpp.md#submit_h) | Submits a task, and obtains the task handle.|
168| [wait](https://gitee.com/openharmony/resourceschedule_ffrt/blob/master/docs/ffrt-api-guideline-cpp.md#wait)         | Waits until all tasks in the context are complete.        |
169
170> **NOTE**
171>
172> - For details about how to use FFRT C++ APIs, see [Using FFRT C++ APIs](ffrt-development-guideline.md#using-ffrt-c-api-1).
173> - When using FFRT C or C++ APIs, you can use the FFRT C++ API third-party library to simplify the header file inclusion, that is, use the `#include "ffrt/ffrt.h"` header file to include statements.
174
175## Constraints
176
177- For `submit`, the total number of input dependencies and output dependencies of each task cannot exceed 8.
178- For `submit_h`, the total number of input dependencies and output dependencies of each task cannot exceed 7.
179- When a parameter is used as both an input dependency and an output dependency, it is counted as one dependency. For example, if the input dependency is `{&x}` and the output dependency is also `{&x}`, then the number of dependencies is 1.
180