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 : #ifndef HCCLV2_HIERARCHICAL_QUEUE_H
12 : #define HCCLV2_HIERARCHICAL_QUEUE_H
13 :
14 : #include "exception_util.h"
15 : #include "null_ptr_exception.h"
16 : #include <string_util.h>
17 : #include "iterator.h"
18 : #include "iterator_unconst.h"
19 : #include "types.h"
20 :
21 : #include <memory>
22 : #include <vector>
23 :
24 : namespace Hccl {
25 : using namespace std;
26 :
27 : template <typename E, typename SubQueue> class HierarchicalQueue {
28 : public:
29 : using SlaveIterator = BaseConstIterator<vector, shared_ptr<SubQueue>>;
30 :
31 : using Iterator = BaseConstIterator<vector, unique_ptr<E>>;
32 :
33 : using UnConstIterator = BaseIterator<vector, unique_ptr<E>>;
34 :
35 : using UnConstSlaveIterator = BaseIterator<vector, shared_ptr<SubQueue>>;
36 :
37 42 : inline u32 GetId() const
38 : {
39 42 : return id;
40 : }
41 :
42 1 : inline bool IsMaster() const
43 : {
44 1 : return masterFlag;
45 : }
46 :
47 34 : virtual shared_ptr<SubQueue> Fork()
48 : {
49 : // 待修改 Fork() can only be called by master queue!;
50 34 : auto slave = make_shared<SubQueue>();
51 34 : slave->masterFlag = false;
52 34 : slave->id = slaves.size() + 1;
53 34 : slaves.push_back(slave);
54 34 : slave->master = static_cast<SubQueue *>(this)->shared_from_this();
55 34 : return slave;
56 0 : }
57 :
58 70 : virtual void Append(unique_ptr<E> elem)
59 : {
60 70 : if (elem == nullptr) {
61 1 : std::string msg = StringFormat("[%s] elem Get nullptr", __func__);
62 1 : THROW<NullPtrException>(msg);
63 1 : }
64 69 : elements.push_back(std::move(elem));
65 69 : }
66 :
67 : weak_ptr<HierarchicalQueue> GetMaster()
68 : {
69 : return master;
70 : }
71 :
72 41 : SlaveIterator IterSlaves() const
73 : {
74 41 : return SlaveIterator(slaves);
75 : };
76 :
77 57 : Iterator Iter() const
78 : {
79 57 : return Iterator(elements);
80 : };
81 :
82 7 : UnConstSlaveIterator UnConstIterSlaves()
83 : {
84 7 : return UnConstSlaveIterator(slaves);
85 : };
86 :
87 10 : UnConstIterator UnConstIter()
88 : {
89 10 : return UnConstIterator(elements);
90 : };
91 :
92 9 : inline u32 Size() const
93 : {
94 9 : return elements.size();
95 : };
96 :
97 2 : const E *First() const
98 : {
99 2 : if (elements.empty()) {
100 0 : return nullptr;
101 : }
102 2 : return elements.front().get();
103 : };
104 :
105 : const E *Last() const
106 : {
107 : if (elements.empty()) {
108 : return nullptr;
109 : }
110 : return elements.back().get();
111 : };
112 :
113 24 : inline u32 SizeOfSlaves() const
114 : {
115 24 : return slaves.size();
116 : };
117 :
118 : protected:
119 119 : HierarchicalQueue() : id(0), masterFlag(true){};
120 :
121 : QId id;
122 : bool masterFlag;
123 : weak_ptr<SubQueue> master;
124 : vector<shared_ptr<SubQueue>> slaves;
125 : // 使用指针保证E的多态
126 : vector<unique_ptr<E>> elements;
127 : };
128 : } // namespace Hccl
129 :
130 : #endif // HCCLV2_HIERARCHICAL_QUEUE_H
|