1 /**
2 * Copyright 2019-2020 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
17 #include "runtime/device/ascend/ge_runtime/task/stream_switch_task.h"
18 #include "runtime/kernel.h"
19 #include "runtime/device/ascend/ge_runtime/task/task_factory.h"
20 #include "mindspore/core/utils/convert_utils_base.h"
21
22 namespace mindspore::ge::model_runner {
StreamSwitchTask(const ModelContext & model_context,const std::shared_ptr<StreamSwitchTaskInfo> & task_info)23 StreamSwitchTask::StreamSwitchTask(const ModelContext &model_context,
24 const std::shared_ptr<StreamSwitchTaskInfo> &task_info)
25 : TaskRepeater<StreamSwitchTaskInfo>(model_context, task_info),
26 task_info_(task_info),
27 stream_(nullptr),
28 stream_list_() {
29 MS_EXCEPTION_IF_NULL(task_info);
30 stream_list_ = model_context.stream_list();
31 if (stream_list_.size() == 1) {
32 stream_ = stream_list_[0];
33 } else if (stream_list_.size() > task_info->stream_id()) {
34 stream_ = stream_list_[task_info->stream_id()];
35 } else {
36 MS_LOG(EXCEPTION) << "Index: " << task_info->stream_id() << " >= stream_list.size(): " << stream_list_.size();
37 }
38 }
39
~StreamSwitchTask()40 StreamSwitchTask::~StreamSwitchTask() {}
41
Distribute()42 void StreamSwitchTask::Distribute() {
43 MS_LOG(INFO) << "Init StreamSwitchTask start.";
44 MS_LOG(INFO) << "Stream " << task_info_->stream_id() << " active " << task_info_->true_stream_id();
45 MS_EXCEPTION_IF_NULL(stream_);
46
47 if (static_cast<uint64_t>(task_info_->true_stream_id()) >= stream_list_.size()) {
48 MS_LOG(EXCEPTION) << "true_stream_id " << task_info_->true_stream_id() << " must be less than stream_list_ size "
49 << stream_list_.size();
50 }
51
52 void *input = reinterpret_cast<void *>(task_info_->input_addr());
53 rtCondition_t cond = static_cast<rtCondition_t>(task_info_->cond());
54 void *value = reinterpret_cast<void *>(task_info_->value_addr());
55 rtStream_t true_stream = stream_list_[LongToSize(task_info_->true_stream_id())];
56 rtSwitchDataType_t data_type = static_cast<rtSwitchDataType_t>(task_info_->data_type());
57
58 MS_LOG(INFO) << "InitStreamSwitchTask, cond: " << cond << ", trueStream: " << true_stream
59 << ", trueStreamID: " << task_info_->true_stream_id() << ", datatype: " << task_info_->data_type();
60
61 MS_LOG(INFO) << "StreamSwitchTask Distribute Start.";
62 rtError_t rt_ret = rtStreamSwitchEx(input, cond, value, true_stream, stream_, data_type);
63 if (rt_ret != RT_ERROR_NONE) {
64 MS_LOG(EXCEPTION) << "Call rt api rtStreamSwitchEx failed, ret: " << rt_ret;
65 }
66
67 MS_LOG(INFO) << "Distribute StreamSwitch success";
68 }
69
70 REGISTER_TASK(TaskInfoType::STREAM_SWITCH, StreamSwitchTask, StreamSwitchTaskInfo);
71 } // namespace mindspore::ge::model_runner
72