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.h"
17 #include "common.h"
18
19 using namespace ffrt;
20
Fib(int x,int & y)21 void Fib(int x, int& y)
22 {
23 if (x <= 1) {
24 y = x;
25 } else {
26 int y1, 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, y2;
40 ffrt::submit([&]() { FibFFRTChildWait(x - 1, y1); }, {}, {});
41 ffrt::submit([&]() { FibFFRTChildWait(x - 2, y2); }, {}, {});
42 ffrt::wait();
43 y = y1 + y2;
44 }
45 simulate_task_compute_time(COMPUTE_TIME_US);
46 }
47
FibFFRTDataWait(int x,int & y)48 void FibFFRTDataWait(int x, int& y)
49 {
50 if (x <= 1) {
51 y = x;
52 } else {
53 int y1, y2;
54 ffrt::submit([&]() { FibFFRTDataWait(x - 1, y1); }, {}, {&y1});
55 ffrt::submit([&]() { FibFFRTDataWait(x - 2, y2); }, {}, {&y2});
56 ffrt::wait({&y1, &y2});
57 y = y1 + y2;
58 }
59 simulate_task_compute_time(COMPUTE_TIME_US);
60 }
61
FibDataWait()62 void FibDataWait()
63 {
64 PreHotFFRT();
65
66 int output;
67
68 {
69 int expect;
70 Fib(FIB_NUM, expect);
71 }
72
73 TIME_BEGIN(t);
74 for (uint64_t i = 0; i < REPEAT; ++i) {
75 ffrt::submit([&]() { FibFFRTDataWait(FIB_NUM, output); }, {}, {&output});
76 ffrt::wait({&output});
77 }
78 TIME_END_INFO(t, "fib_data_wait");
79 }
80
FibFFRTNoWait(int x,int * y)81 void FibFFRTNoWait(int x, int* y)
82 {
83 if (x <= 1) {
84 *y = x;
85 } else {
86 int *y1, *y2;
87 y1 = reinterpret_cast<int *>(malloc(sizeof(int)));
88 y2 = reinterpret_cast<int *>(malloc(sizeof(int)));
89 ffrt::submit([=]() { FibFFRTNoWait(x - 1, y1); }, {}, {y1});
90 ffrt::submit([=]() { FibFFRTNoWait(x - 2, y2); }, {}, {y2});
91 ffrt::submit(
92 [=]() {
93 *y = *y1 + *y2;
94 free(y1);
95 free(y2);
96 },
97 {y1, y2}, {y});
98 }
99 simulate_task_compute_time(COMPUTE_TIME_US);
100 }
101
FibNoWait()102 void FibNoWait()
103 {
104 PreHotFFRT();
105
106 int output;
107
108 {
109 int expect;
110 Fib(FIB_NUM, expect);
111 }
112
113 TIME_BEGIN(t);
114 for (uint64_t i = 0; i < REPEAT; ++i) {
115 ffrt::submit([&]() { FibFFRTNoWait(FIB_NUM, &output); }, {}, {&output});
116 ffrt::wait({&output});
117 }
118 TIME_END_INFO(t, "fib_no_wait");
119 }
120
FibChildWait()121 void FibChildWait()
122 {
123 PreHotFFRT();
124
125 int output;
126 {
127 int expect;
128 Fib(FIB_NUM, expect);
129 }
130
131 TIME_BEGIN(t);
132 for (uint64_t i = 0; i < REPEAT; ++i) {
133 ffrt::submit([&]() { FibFFRTChildWait(FIB_NUM, output); }, {}, {&output});
134 ffrt::wait({&output});
135 }
136 TIME_END_INFO(t, "fib_child_wait");
137 }
138
main()139 int main()
140 {
141 GetEnvs();
142 FibDataWait();
143 FibChildWait();
144 FibNoWait();
145 }