/home/runner/work/HiCR/HiCR/include/hicr/frontends/tasking/task.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/frontends/tasking/task.hpp Source File
HiCR
task.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <atomic>
27#include <memory>
28#include <queue>
29#include <utility>
30#include <vector>
31#include <hicr/core/definitions.hpp>
37#include "callbackMap.hpp"
38#include "common.hpp"
39
40#ifdef ENABLE_INSTRUMENTATION
41 #include <tracr.hpp>
42#endif
43
44namespace HiCR::tasking
45{
46
56class Task
57{
58 public:
59
85
90
91 Task() = delete;
92 ~Task() = default;
93
101 __INLINE__ Task(std::shared_ptr<HiCR::ExecutionUnit> executionUnit, taskCallbackMap_t *callbackMap = nullptr)
102 : _executionUnit(std::move(executionUnit)),
103 _callbackMap(callbackMap) {};
104
110 __INLINE__ void setCallbackMap(taskCallbackMap_t *callbackMap) { _callbackMap = callbackMap; }
111
117 __INLINE__ taskCallbackMap_t *getCallbackMap() { return _callbackMap; }
118
122 __INLINE__ void sendSyncSignal() { _callbackMap->trigger(this, HiCR::tasking::Task::callback_t::onTaskSync); }
123
132 {
133 // If the execution state has not been initialized then return the value expliclitly
134 if (_executionState == nullptr) return HiCR::ExecutionState::state_t::uninitialized;
135
136 // Otherwise just query the initial execution state
137 return _executionState->getState();
138 }
139
145 __INLINE__ void setExecutionUnit(std::shared_ptr<HiCR::ExecutionUnit> executionUnit) { _executionUnit = std::move(executionUnit); }
146
152 [[nodiscard]] __INLINE__ std::shared_ptr<HiCR::ExecutionUnit> getExecutionUnit() const { return _executionUnit; }
153
159 __INLINE__ void initialize(std::unique_ptr<HiCR::ExecutionState> executionState)
160 {
162 HICR_THROW_LOGIC("Attempting to initialize a task that has already been initialized (State: %d).\n", getState());
163
164 // Getting execution state as a unique pointer (to prcallback sharing the same state among different tasks)
165 _executionState = std::move(executionState);
166 }
167
173 __INLINE__ void run()
174 {
176 HICR_THROW_RUNTIME("Attempting to run a task that is not in a initialized or suspended state (State: %d).\n", getState());
177
178 // Triggering execution callback, if defined
179 if (_callbackMap != nullptr) _callbackMap->trigger(this, callback_t::onTaskExecute);
180
181// TraCR set trace of thread executing a task
182#ifdef ENABLE_INSTRUMENTATION
183 INSTRUMENTATION_THREAD_MARK_SET((long)0);
184#endif
185
186 // Now resuming the task's execution
187 _executionState->resume();
188
189// TraCR set trace of thread polling again
190#ifdef ENABLE_INSTRUMENTATION
191 INSTRUMENTATION_THREAD_MARK_SET((long)2);
192#endif
193
194 // Checking execution state finalization
195 bool isFinished = _executionState->checkFinalization();
196
197 // Getting state after execution
198 const auto state = getState();
199
201 HICR_THROW_RUNTIME("Task has to be either in suspended or in finished state but I got State: %d. IsFinished: %b\n", state, isFinished);
202
203 // If the task is suspended and callback map is defined, trigger the corresponding callback.
205 if (_callbackMap != nullptr) _callbackMap->trigger(this, callback_t::onTaskSuspend);
206
207 // If the task is still running (no suspension), then the task has fully finished executing. If so,
208 // trigger the corresponding callback, if the callback map is defined. It is important that this function
209 // is called from outside the context of a task to allow the upper layer to free its memory upon finishing
211 if (_callbackMap != nullptr) _callbackMap->trigger(this, callback_t::onTaskFinish);
212 }
213
217 __INLINE__ void suspend()
218 {
219 if (getState() != HiCR::ExecutionState::state_t::running) HICR_THROW_RUNTIME("Attempting to yield a task that is not in a running state (State: %d).\n", getState());
220
221 // Yielding execution back to worker
222 _executionState->suspend();
223 }
224
225 private:
226
230 std::shared_ptr<HiCR::ExecutionUnit> _executionUnit;
231
235 taskCallbackMap_t *_callbackMap = nullptr;
236
240 std::unique_ptr<HiCR::ExecutionState> _executionState = nullptr;
241
242}; // class Task
243
244} // namespace HiCR::tasking
Provides a definition for the HiCR CallbackMap class.
state_t
Definition executionState.hpp:45
@ initialized
Definition executionState.hpp:54
@ finished
Definition executionState.hpp:69
@ uninitialized
Definition executionState.hpp:49
@ suspended
Definition executionState.hpp:64
@ running
Definition executionState.hpp:59
Definition callbackMap.hpp:40
__INLINE__ void trigger(T arg, const E callback) const
Definition callbackMap.hpp:69
Definition task.hpp:57
__INLINE__ std::shared_ptr< HiCR::ExecutionUnit > getExecutionUnit() const
Definition task.hpp:152
__INLINE__ void suspend()
Definition task.hpp:217
__INLINE__ void sendSyncSignal()
Definition task.hpp:122
__INLINE__ taskCallbackMap_t * getCallbackMap()
Definition task.hpp:117
__INLINE__ void run()
Definition task.hpp:173
callback_t
Definition task.hpp:64
@ onTaskFinish
Definition task.hpp:78
@ onTaskSuspend
Definition task.hpp:73
@ onTaskExecute
Definition task.hpp:68
@ onTaskSync
Definition task.hpp:83
__INLINE__ void setCallbackMap(taskCallbackMap_t *callbackMap)
Definition task.hpp:110
__INLINE__ Task(std::shared_ptr< HiCR::ExecutionUnit > executionUnit, taskCallbackMap_t *callbackMap=nullptr)
Definition task.hpp:101
__INLINE__ void setExecutionUnit(std::shared_ptr< HiCR::ExecutionUnit > executionUnit)
Definition task.hpp:145
HiCR::tasking::CallbackMap< Task *, callback_t > taskCallbackMap_t
Definition task.hpp:89
__INLINE__ void initialize(std::unique_ptr< HiCR::ExecutionState > executionState)
Definition task.hpp:159
__INLINE__ const HiCR::ExecutionState::state_t getState()
Definition task.hpp:131
Provides a definition for the abstract compute manager class.
Provides a base definition for a HiCR Execution State class.
Provides a base definition for a HiCR Execution Unit class.
Provides a definition for a HiCR ProcessingUnit class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67
This file implements variables shared among mutiple code files.