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 : #include "stream.h"
11 : #include "runtime_api_exception.h"
12 : #include "log.h"
13 : #include "binary_stream.h"
14 : namespace Hccl {
15 :
16 202 : Stream::Stream(aclrtStream ptr, bool isMaster) : ptr(ptr), selfOwned(false), isMaster_(isMaster)
17 : {
18 202 : id = static_cast<u32>(HrtGetStreamId(ptr));
19 202 : InitDevPhyId();
20 202 : }
21 :
22 123 : Stream::Stream(bool deviceUsed, bool isMaster) : selfOwned(true), devUsed(deviceUsed), isMaster_(isMaster)
23 : {
24 : try {
25 123 : if (deviceUsed) {
26 11 : ptr = HrtStreamCreateWithFlags(HCCL_STREAM_PRIORITY_HIGH, ACL_STREAM_DEVICE_USE_ONLY);
27 11 : sqId = HrtStreamGetSqId(ptr);
28 11 : cqId = HrtStreamGetCqId(ptr);
29 : } else {
30 112 : ptr = HrtStreamCreateWithFlags(HCCL_STREAM_PRIORITY_HIGH, ACL_STREAM_FAST_LAUNCH | ACL_STREAM_FAST_SYNC);
31 112 : HrtStreamSetMode(ptr, STREAM_MODE_STOP_ON_FAILURE);
32 : }
33 123 : id = static_cast<u32>(HrtGetStreamId(ptr));
34 123 : InitDevPhyId();
35 0 : } catch (RuntimeApiException& e) {
36 0 : HCCL_ERROR("HrtGetStreamId failed: %s", e.what());
37 0 : HrtStreamDestroy(ptr);
38 0 : throw;
39 0 : }
40 123 : }
41 :
42 325 : Stream::~Stream()
43 : {
44 : try {
45 325 : if (selfOwned) {
46 123 : HrtStreamDestroy(ptr);
47 : }
48 0 : } catch (HcclException& e) {
49 0 : HCCL_ERROR("%s", e.what());
50 0 : } catch (std::exception& e) {
51 0 : HCCL_ERROR("%s", e.what());
52 0 : } catch (...) {
53 0 : HCCL_ERROR("Unknown Error occurs when destruct stream %d", id);
54 0 : }
55 325 : }
56 :
57 36 : void Stream::SetStmMode(u64 stmMode) { mode = stmMode; }
58 :
59 150 : aclrtStream Stream::GetPtr() const { return ptr; }
60 :
61 137 : u32 Stream::GetId() const { return id; }
62 :
63 7 : u32 Stream::GetSqId() const { return sqId; }
64 :
65 25 : bool Stream::IsMaster() const { return isMaster_; }
66 :
67 3 : bool Stream::IsSelfOwned() const { return selfOwned; }
68 :
69 16 : u64 Stream::GetMode() const { return mode; }
70 :
71 3 : std::vector<char> Stream::GetUniqueId() const
72 : {
73 3 : std::vector<char> result;
74 :
75 3 : BinaryStream binaryStream;
76 3 : binaryStream << id;
77 3 : binaryStream << sqId;
78 3 : binaryStream << devPhyId;
79 3 : binaryStream << cqId;
80 3 : binaryStream.Dump(result);
81 9 : HCCL_INFO("Stream::GetUniqueId:%s:data=%s", Describe().c_str(), Bytes2hex(result.data(), result.size()).c_str());
82 3 : return result;
83 3 : }
84 :
85 26 : std::string Stream::Describe() const
86 : {
87 : return StringFormat(
88 26 : "Stream[ptr=%p, id=%u, sqId=%u, selfOwned=%u, devUsed=%d, devPhyId=%u]", ptr, id, sqId, selfOwned, devUsed,
89 26 : devPhyId);
90 : }
91 :
92 303 : void Stream::InitDevPhyId() { devPhyId = HrtGetDevicePhyIdByIndex(HrtGetDevice()); }
93 :
94 : } // namespace Hccl
|