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>
28 : class HierarchicalQueue {
29 : public:
30 : using SlaveIterator = BaseConstIterator<vector, shared_ptr<SubQueue>>;
31 :
32 : using Iterator = BaseConstIterator<vector, unique_ptr<E>>;
33 :
34 : using UnConstIterator = BaseIterator<vector, unique_ptr<E>>;
35 :
36 : using UnConstSlaveIterator = BaseIterator<vector, shared_ptr<SubQueue>>;
37 :
38 42 : inline u32 GetId() const { return id; }
39 :
40 1 : inline bool IsMaster() const { return masterFlag; }
41 :
42 34 : virtual shared_ptr<SubQueue> Fork()
43 : {
44 : // 待修改 Fork() can only be called by master queue!;
45 34 : auto slave = make_shared<SubQueue>();
46 34 : slave->masterFlag = false;
47 34 : slave->id = slaves.size() + 1;
48 34 : slaves.push_back(slave);
49 34 : slave->master = static_cast<SubQueue*>(this)->shared_from_this();
50 34 : return slave;
51 0 : }
52 :
53 70 : virtual void Append(unique_ptr<E> elem)
54 : {
55 70 : if (elem == nullptr) {
56 1 : std::string msg = StringFormat("[%s] elem Get nullptr", __func__);
57 1 : THROW<NullPtrException>(msg);
58 1 : }
59 69 : elements.push_back(std::move(elem));
60 69 : }
61 :
62 : weak_ptr<HierarchicalQueue> GetMaster() { return master; }
63 :
64 41 : SlaveIterator IterSlaves() const { return SlaveIterator(slaves); };
65 :
66 57 : Iterator Iter() const { return Iterator(elements); };
67 :
68 7 : UnConstSlaveIterator UnConstIterSlaves() { return UnConstSlaveIterator(slaves); };
69 :
70 10 : UnConstIterator UnConstIter() { return UnConstIterator(elements); };
71 :
72 9 : inline u32 Size() const { return elements.size(); };
73 :
74 2 : const E* First() const
75 : {
76 2 : if (elements.empty()) {
77 0 : return nullptr;
78 : }
79 2 : return elements.front().get();
80 : };
81 :
82 : const E* Last() const
83 : {
84 : if (elements.empty()) {
85 : return nullptr;
86 : }
87 : return elements.back().get();
88 : };
89 :
90 24 : inline u32 SizeOfSlaves() const { return slaves.size(); };
91 :
92 : protected:
93 119 : HierarchicalQueue() : id(0), masterFlag(true) {};
94 :
95 : QId id;
96 : bool masterFlag;
97 : weak_ptr<SubQueue> master;
98 : vector<shared_ptr<SubQueue>> slaves;
99 : // 使用指针保证E的多态
100 : vector<unique_ptr<E>> elements;
101 : };
102 : } // namespace Hccl
103 :
104 : #endif // HCCLV2_HIERARCHICAL_QUEUE_H
|