1# Function Flow Runtime Task Graph (C++) 2 3## Overview 4 5The 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`). 6 7You can use either of the following ways to build a task graph: 8 9- Use the task dependency to build a task graph. The task `handle` is used to indicate a task object. 10- 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. 11 12### Task dependency 13 14> **NOTE** 15> 16> 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. 17 18Task dependency applies to scenarios where tasks have specific sequence or logical process requirements. For example: 19 20- Tasks with sequence. For example, a data preprocessing task is executed before a model training task. 21- Logic process control. For example, in a typical commodity transaction process, orders are placed, followed by production and then logistics transportation. 22- Multi-level chain: For example, during video processing, you can perform tasks such as transcoding, generating thumbnails, adding watermarks, and releasing the final video. 23 24### Data Dependency 25 26> **NOTE** 27> 28> 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. 29> 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. 30 31Data dependency applies to scenarios where tasks are triggered by data production and consumption relationships. 32 33A 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. 34 35When 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. 36 37FFRT can dynamically build producer/consumer-based data dependencies between tasks at runtime and perform scheduling based on the task data dependency status, including: 38 39- Producer-Consumer dependency 40 41 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. 42 43- Consumer-Producer dependency 44 45 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. 46 47- Producer-Producer dependency 48 49 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. 50 51For example, the relationship between a group of tasks and data A is expressed as follows: 52 53```cpp 54task1(OUT A); 55task2(IN A); 56task3(IN A); 57task4(OUT A); 58task5(OUT A); 59``` 60 61 62 63For ease of description, circles are used to represent tasks and squares are used to represent data. 64 65The following conclusions can be drawn: 66 67- task1 and task2/task3 form a producer-consumer dependency. This means that task2/task3 can read data A only after task1 writes data A. 68- task2/task3 and task4 form a consumer-producer dependency. This means that task4 can write data A only after task2/task3 reads data A. 69- 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. 70 71## Example: Streaming Video Processing 72 73A 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. 74 75 76 77The FFRT provides task graph that can describe the task dependency and parallelize the preceding video processing process. The code is as follows: 78 79```cpp 80#include <iostream> 81#include "ffrt/cpp/task.h" 82 83int main() 84{ 85 // Submit a task. 86 auto handle_A = ffrt::submit_h([] () { std::cout << "Parse" << std::endl; }); 87 auto handle_B = ffrt::submit_h([] () { std::cout << "Transcode" << std::endl; }, {handle_A}); 88 auto handle_C = ffrt::submit_h([] () { std::cout << "Generate a thumbnail" << std::endl; }, {handle_A}); 89 auto handle_D = ffrt::submit_h([] () { std::cout << "Add watermark" << std::endl; }, {handle_B, handle_C}); 90 ffrt::submit([] () { std::cout << "Release" << std::endl; }, {handle_D}); 91 92 // Wait until all tasks are complete. 93 ffrt::wait(); 94 return 0; 95} 96``` 97 98The expected output may be as follows: 99 100```plain 101Video parsing 102Video transcoding 103Thumbnails generation 104Watermark adding 105Video release 106``` 107 108## Example: Fibonacci Sequence 109 110Each 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: 111 112```cpp 113#include <iostream> 114#include "ffrt/cpp/task.h" 115 116void Fib(int x, int& y) 117{ 118 if (x <= 1) { 119 y = x; 120 } else { 121 int y1, y2; 122 123 // Submit the task and build data dependencies. 124 ffrt::submit([&]() { Fib(x - 1, y1); }, {&x}, {&y1}); 125 ffrt::submit([&]() { Fib(x - 2, y2); }, {&x}, {&y2}); 126 127 // Wait until the task is complete. 128 ffrt::wait({&y1, &y2}); 129 y = y1 + y2; 130 } 131} 132 133int main() 134{ 135 int y; 136 Fib(5, y); 137 std::cout << "Fibonacci(5) is " << y << std::endl; 138} 139``` 140 141Expected output: 142 143```plain 144Fibonacci(5) is 5 145``` 146 147In 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. 148 149Each task forms a call tree in the FFRT. 150 151 152 153## Available APIs 154 155The main FFRT APIs involved in the preceding example are as follows: 156 157| Name | Description | 158| ------------------------------------------------------------------------------------------------------------------- | -------------------------------- | 159| [submit](https://gitee.com/openharmony/resourceschedule_ffrt/blob/master/docs/ffrt-api-guideline-cpp.md#submit) | Submits a task. | 160| [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.| 161| [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. | 162 163> **NOTE** 164> 165> For details about how to use FFRT C++ APIs, see [Using FFRT C++ APIs](ffrt-development-guideline.md#using-ffrt-c-api-1). 166 167## Constraints 168 169- For `submit`, the total number of input dependencies and output dependencies of each task cannot exceed 8. 170- For `submit_h`, the total number of input dependencies and output dependencies of each task cannot exceed 7. 171- 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. 172