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 "phase_driver.h"
17 #include "mpl_timer.h"
18
19 namespace maple {
20 constexpr long kAlternateUnits = 1000;
21 thread_local PhaseDriverImpl *PhaseDriver::phaseImplLocal = nullptr;
PhaseDriver(const std::string & phaseName)22 PhaseDriver::PhaseDriver(const std::string &phaseName)
23 : MplScheduler(phaseName), module(nullptr), phaseImpl(nullptr), phaseName(phaseName)
24 {
25 }
26
RunAll(MIRModule * currModule,int thread,bool bSeq)27 void PhaseDriver::RunAll(MIRModule *currModule, int thread, bool bSeq)
28 {
29 module = currModule;
30 phaseImpl = NewPhase();
31 CHECK_FATAL(phaseImpl != nullptr, "null ptr check");
32 phaseImpl->GlobalInit();
33 if (thread == 1) {
34 RunSerial();
35 } else {
36 RunParallel(thread, bSeq);
37 }
38 delete phaseImpl;
39 phaseImpl = nullptr;
40 }
41
RunSerial()42 void PhaseDriver::RunSerial()
43 {
44 phaseImplLocal = NewPhase();
45 CHECK_FATAL(phaseImplLocal != nullptr, "null ptr check");
46 phaseImplLocal->LocalInit();
47 MPLTimer timer;
48 if (dumpTime) {
49 timer.Start();
50 }
51 RegisterTasks();
52 if (dumpTime) {
53 timer.Stop();
54 INFO(kLncInfo, "PhaseDriver::RegisterTasks (%s): %lf ms", phaseName.c_str(),
55 timer.ElapsedMicroseconds() / kAlternateUnits);
56 timer.Start();
57 }
58 MplTask *task = GetTaskToRun();
59 while (task != nullptr) {
60 MplTaskParam *paramRun = CallbackGetTaskRunParam();
61 (void)task->Run(paramRun);
62 MplTaskParam *paramFinish = CallbackGetTaskFinishParam();
63 (void)task->Run(paramFinish);
64 }
65 if (dumpTime) {
66 timer.Stop();
67 INFO(kLncInfo, "PhaseDriver::RunTask (%s): %lf ms", phaseName.c_str(),
68 timer.ElapsedMicroseconds() / kAlternateUnits);
69 }
70 }
71
RunParallel(int thread,bool bSeq)72 void PhaseDriver::RunParallel(int thread, bool bSeq)
73 {
74 MPLTimer timer;
75 if (dumpTime) {
76 timer.Start();
77 }
78 RegisterTasks();
79 if (dumpTime) {
80 timer.Stop();
81 INFO(kLncInfo, "PhaseDriver::RegisterTasks (%s): %lf ms", phaseName.c_str(),
82 timer.ElapsedMicroseconds() / kAlternateUnits);
83 }
84 if (dumpTime) {
85 timer.Start();
86 }
87 int ret = RunTask(static_cast<uint32>(thread), bSeq);
88 CHECK_FATAL(ret == 0, "RunTask failed");
89 if (dumpTime) {
90 timer.Stop();
91 INFO(kLncInfo, "PhaseDriver::RunTask (%s): %lf ms", phaseName.c_str(),
92 timer.ElapsedMicroseconds() / kAlternateUnits);
93 }
94 }
95
CallbackThreadMainStart()96 void PhaseDriver::CallbackThreadMainStart()
97 {
98 phaseImplLocal = NewPhase();
99 CHECK_FATAL(phaseImplLocal != nullptr, "null ptr check");
100 phaseImplLocal->LocalInit();
101 }
102
CallbackThreadMainEnd()103 void PhaseDriver::CallbackThreadMainEnd()
104 {
105 delete phaseImplLocal;
106 phaseImplLocal = nullptr;
107 }
108 } // namespace maple
109