目录

Function Flow Runtime Task Graph (C)

Overview

The 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).

You can use either of the following ways to build a task graph:

[h2]Task Dependency

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.

Task dependency applies to scenarios where tasks have specific sequence or logical process requirements. For example:

[h2]Data Dependency

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.

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.

Data dependency applies to scenarios where tasks are triggered by data production and consumption relationships.

A 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.

When 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.

FFRT can dynamically build producer/consumer-based data dependencies between tasks at runtime and perform scheduling based on the task data dependency status, including:

For example, the relationship between a group of tasks and data A is expressed as follows:

task1(OUT A);
task2(IN A);
task3(IN A);
task4(OUT A);
task5(OUT A);

For ease of description, circles are used to represent tasks and squares are used to represent data.

The following conclusions can be drawn:

Example: Streaming Video Processing

A user uploads a video to the platform. The processing steps include: parsing, transcoding, generating a thumbnail, adding a watermark, and releasing the video. Transcoding and thumbnail generation can occur simultaneously. The following figure shows the task process.

The FFRT provides task graph that can describe the task dependency and parallelize the preceding video processing process. The code is as follows:

#include <cstdio>
#include "hilog/log.h"
#include "ffrt/ffrt.h" // From the HarmonyOS third-party library "@ppd/ffrt".

#undef LOG_TAG
#define LOG_TAG "ParallelTag"
void FuncTaskA(void* arg)
{
    OH_LOG_INFO(LOG_APP, "Video parsing");
    printf("Parse\n");
}

void FuncTaskB(void* arg)
{
    OH_LOG_INFO(LOG_APP, "Video transcoding");
    printf("Transcode\n");
}

void FuncTaskC(void* arg)
{
    OH_LOG_INFO(LOG_APP, "Video thumbnail generation");
    printf("Generate a thumbnail\n");
}

void FuncTaskD(void* arg)
{
    OH_LOG_INFO(LOG_APP, "Video watermarking");
    printf("Add watermark\n");
}

void FuncTaskE(void* arg)
{
    OH_LOG_INFO(LOG_APP, "Video publishing");
    printf("Release\n");
}

int DependenceCExec()
{
    // Submit task A.
    ffrt_task_handle_t hTaskA = ffrt_submit_h_f(FuncTaskA, NULL, NULL, NULL, NULL);

    // Submit tasks B and C.
    ffrt_dependence_t taskA_deps[] = {{ffrt_dependence_task, hTaskA}};
    ffrt_deps_t dTaskA = {1, taskA_deps};
    ffrt_task_handle_t hTaskB = ffrt_submit_h_f(FuncTaskB, NULL, &dTaskA, NULL, NULL);
    ffrt_task_handle_t hTaskC = ffrt_submit_h_f(FuncTaskC, NULL, &dTaskA, NULL, NULL);

    // Submit task D.
    ffrt_dependence_t taskBC_deps[] = {{ffrt_dependence_task, hTaskB}, {ffrt_dependence_task, hTaskC}};
    ffrt_deps_t dTaskBC = {2, taskBC_deps};
    ffrt_task_handle_t hTaskD = ffrt_submit_h_f(FuncTaskD, NULL, &dTaskBC, NULL, NULL);

    // Submit task E.
    ffrt_dependence_t taskD_deps[] = {{ffrt_dependence_task, hTaskD}};
    ffrt_deps_t dTaskD = {1, taskD_deps};
    ffrt_submit_f(FuncTaskE, NULL, &dTaskD, NULL, NULL);

    // Wait until all tasks are complete.
    ffrt_wait();

    ffrt_task_handle_destroy(hTaskA);
    ffrt_task_handle_destroy(hTaskB);
    ffrt_task_handle_destroy(hTaskC);
    ffrt_task_handle_destroy(hTaskD);
    return 0;
}

The expected output may be as follows:

Video parsing
Video transcoding
Thumbnails generation
Watermark adding
Video release

The ffrt_submit_h_f and ffrt_submit_f APIs can receive raw function pointer tasks as parameters. If there are pre-processing and post-processing operations on the task, refer to the ffrt_alloc_auto_managed_function_storage_base function to construct the task structure.

Example: Fibonacci Sequence

Each number in the Fibonacci sequence is the sum of the two preceding 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:

#include <cstdio>
#include "hilog/log.h"
#include "ffrt/ffrt.h" // From the HarmonyOS third-party library "@ppd/ffrt".

#undef LOG_TAG
#define LOG_TAG "ParallelTag"
const int FIB_NUM = 5;
typedef struct {
    int x;
    int* y;
} FibFfrtS;

void FibFfrt(void* arg)
{
    FibFfrtS* p = (FibFfrtS*)arg;
    int x = p->x;
    int* y = p->y;

    if (x <= 1) {
        *y = x;
    } else {
        int y1;
        int y2;
        FibFfrtS s1 = {x - 1, &y1};
        FibFfrtS s2 = {x - 2, &y2};

        // Build data dependencies.
        ffrt_dependence_t dx_deps[] = {{ffrt_dependence_data, &x}};
        ffrt_deps_t dx = {1, dx_deps};
        ffrt_dependence_t dy1_deps[] = {{ffrt_dependence_data, &y1}};
        ffrt_deps_t dy1 = {1, dy1_deps};
        ffrt_dependence_t dy2_deps[] = {{ffrt_dependence_data, &y2}};
        ffrt_deps_t dy2 = {1, dy2_deps};
        ffrt_dependence_t dy12_deps[] = {{ffrt_dependence_data, &y1}, {ffrt_dependence_data, &y2}};
        ffrt_deps_t dy12 = {2, dy12_deps};

        // Submit tasks separately.
        ffrt_submit_f(FibFfrt, &s1, &dx, &dy1, NULL);
        ffrt_submit_f(FibFfrt, &s2, &dx, &dy2, NULL);

        // Wait until the task is complete.
        ffrt_wait_deps(&dy12);
        *y = y1 + y2;
    }
}

int FibCExec()
{
    int r;
    FibFfrtS s = {FIB_NUM, &r};
    ffrt_dependence_t dr_deps[] = {{ffrt_dependence_data, &r}};
    ffrt_deps_t dr = {1, dr_deps};
    ffrt_submit_f(FibFfrt, &s, NULL, &dr, NULL);

    // Wait until the task is complete.
    ffrt_wait_deps(&dr);
    OH_LOG_INFO(LOG_APP, "Fibonacci result: %{public}d", r);
    printf("Fibonacci(5) is %d\n", r);
    return r;
}

Expected output:

Fibonacci(5) is 5

In 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.

The ffrt_submit_f API can receive a naked function pointer task as a parameter. If there are pre-processing and post-processing operations on the task, refer to the ffrt_alloc_auto_managed_function_storage_base function to construct the task structure.

The tasks form a call tree within FFRT.

Available APIs

The main FFRT APIs involved in the preceding example are as follows:

Name Description
ffrt_submit_f

Submits a task.

Note: This API is supported since API version 20.

ffrt_submit_h_f

Submits a task, and obtains the task handle.

Note: This API is supported since API version 20.

ffrt_wait_deps Waits until the dependent tasks are complete.
  • For details about how to use FFRT C++ APIs, see Using FFRT C++ APIs.
  • When using FFRT C or C++ APIs, you can simplify header file inclusion by using the FFRT C++ API third-party library, that is, use the #include "ffrt/ffrt.h" statement.

Constraints