Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "stream_active_manager.h"
12 : #include "adapter_rts_common.h"
13 :
14 : using namespace hccl;
15 : std::atomic<bool> StreamActiveManager::initFlag_ = {false};
16 650 : StreamActiveManager::StreamActiveManager()
17 : {
18 650 : }
19 :
20 650 : StreamActiveManager::~StreamActiveManager()
21 : {
22 650 : std::unique_lock<std::mutex> lock(streamActiveManagerMutex_);
23 650 : initFlag_ = false;
24 650 : streamActiveManager_.clear();
25 650 : lock.unlock();
26 650 : }
27 :
28 533 : StreamActiveManager &StreamActiveManager::GetInstance(s32 deviceLogicID)
29 : {
30 1183 : static StreamActiveManager streamActiveManager[MAX_MODULE_DEVICE_NUM];
31 536 : if (static_cast<u32>(deviceLogicID) >= MAX_MODULE_DEVICE_NUM) {
32 8 : HCCL_WARNING("[HcomGetCtxHomInfo][GetInstance] deviceLogicID[%d] is invalid", deviceLogicID);
33 8 : return streamActiveManager[0];
34 : }
35 528 : return streamActiveManager[deviceLogicID];
36 : }
37 :
38 521 : HcclResult StreamActiveManager::Init()
39 : {
40 521 : initFlag_ = true;
41 521 : return HCCL_SUCCESS;
42 : }
43 :
44 2 : HcclResult StreamActiveManager::StreamActive(HcclRtStream activeStream, HcclRtStream stream)
45 : {
46 2 : std::unique_lock<std::mutex> lock(streamActiveManagerMutex_);
47 2 : if (initFlag_ && streamActiveManager_.count(activeStream) == 0) {
48 0 : CHK_RET(hrtStreamActive(activeStream, stream));
49 0 : streamActiveManager_.insert(activeStream);
50 0 : lock.unlock();
51 0 : s32 activeStreamId = 0;
52 0 : CHK_RET(hrtGetStreamId(activeStream, activeStreamId));
53 0 : s32 streamId = 0;
54 0 : CHK_RET(hrtGetStreamId(stream, streamId));
55 0 : HCCL_INFO("StreamActive: activeStream[%d] stream[%d]", activeStreamId, streamId);
56 : }
57 2 : return HCCL_SUCCESS;
58 2 : }
59 :
60 : // ge在model析构时,先销毁流、在unload task,此时hccl获取不到流id
61 13 : HcclResult StreamActiveManager::StreamsUnactive(const std::vector<Stream> &streams)
62 : {
63 13 : if (initFlag_) {
64 20 : for (auto &curStream : streams) {
65 8 : std::unique_lock<std::mutex> lock(streamActiveManagerMutex_);
66 8 : if (streamActiveManager_.count(curStream.ptr()) == 1) {
67 0 : streamActiveManager_.erase(curStream.ptr());
68 : }
69 8 : lock.unlock();
70 8 : }
71 : }
72 13 : return HCCL_SUCCESS;
73 : }
|