1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ffrt_inner.h"
17 #include "common.h"
18 
19 using namespace ffrt;
Fib(int x,int & y)20 void Fib(int x, int& y)
21 {
22     if (x <= 1) {
23         y = x;
24     } else {
25         int y1;
26         int y2;
27         Fib(x - 1, y1);
28         Fib(x - 2, y2);
29         y = y1 + y2;
30     }
31     simulate_task_compute_time(COMPUTE_TIME_US);
32 }
33 
FibFFRTChildWait(int x,int & y)34 void FibFFRTChildWait(int x, int& y)
35 {
36     if (x <= 1) {
37         y = x;
38     } else {
39         int y1;
40         int y2;
41         ffrt::submit([&]() { FibFFRTChildWait(x - 1, y1); }, {}, {});
42         ffrt::submit([&]() { FibFFRTChildWait(x - 2, y2); }, {}, {});
43         ffrt::wait();
44         y = y1 + y2;
45     }
46     simulate_task_compute_time(COMPUTE_TIME_US);
47 }
48 
FibFFRTDataWait(int x,int & y)49 void FibFFRTDataWait(int x, int& y)
50 {
51     if (x <= 1) {
52         y = x;
53     } else {
54         int y1;
55         int y2;
56         ffrt::submit([&]() { FibFFRTDataWait(x - 1, y1); }, {}, {&y1});
57         ffrt::submit([&]() { FibFFRTDataWait(x - 2, y2); }, {}, {&y2});
58         ffrt::wait({&y1, &y2});
59         y = y1 + y2;
60     }
61     simulate_task_compute_time(COMPUTE_TIME_US);
62 }
63 
FibDataWait()64 void FibDataWait()
65 {
66     PreHotFFRT();
67 
68     int output;
69 
70     {
71         int expect;
72         Fib(FIB_NUM, expect);
73     }
74 
75     TIME_BEGIN(t);
76     for (uint64_t i = 0; i < REPEAT; ++i) {
77         ffrt::submit([&]() { FibFFRTDataWait(FIB_NUM, output); }, {}, {&output});
78         ffrt::wait({&output});
79     }
80     TIME_END_INFO(t, "fib_data_wait");
81 }
82 
FibFFRTNoWait(int x,int * y)83 void FibFFRTNoWait(int x, int* y)
84 {
85     if (x <= 1) {
86         *y = x;
87     } else {
88         int *y1, *y2;
89         y1 = reinterpret_cast<int *>(malloc(sizeof(int)));
90         y2 = reinterpret_cast<int *>(malloc(sizeof(int)));
91         ffrt::submit([=]() { FibFFRTNoWait(x - 1, y1); }, {}, {y1});
92         ffrt::submit([=]() { FibFFRTNoWait(x - 2, y2); }, {}, {y2});
93         ffrt::submit(
94             [=]() {
95                 *y = *y1 + *y2;
96                 free(y1);
97                 free(y2);
98             },
99             {y1, y2}, {y});
100     }
101     simulate_task_compute_time(COMPUTE_TIME_US);
102 }
103 
FibNoWait()104 void FibNoWait()
105 {
106     PreHotFFRT();
107 
108     int output;
109 
110     {
111         int expect;
112         Fib(FIB_NUM, expect);
113     }
114 
115     TIME_BEGIN(t);
116     for (uint64_t i = 0; i < REPEAT; ++i) {
117         ffrt::submit([&]() { FibFFRTNoWait(FIB_NUM, &output); }, {}, {&output});
118         ffrt::wait({&output});
119     }
120     TIME_END_INFO(t, "fib_no_wait");
121 }
122 
FibChildWait()123 void FibChildWait()
124 {
125     PreHotFFRT();
126 
127     int output;
128     {
129         int expect;
130         Fib(FIB_NUM, expect);
131     }
132 
133     TIME_BEGIN(t);
134     for (uint64_t i = 0; i < REPEAT; ++i) {
135         ffrt::submit([&]() { FibFFRTChildWait(FIB_NUM, output); }, {}, {&output});
136         ffrt::wait({&output});
137     }
138     TIME_END_INFO(t, "fib_child_wait");
139 }
140 
main()141 int main()
142 {
143     GetEnvs();
144     FibDataWait();
145     FibChildWait();
146     FibNoWait();
147 }