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_TOKENINFO_MANAGER_H
12 : #define HCCLV2_TOKENINFO_MANAGER_H
13 :
14 : #include <mutex>
15 : #include <vector>
16 : #include <unordered_map>
17 : #include <hccl/hccl_types.h>
18 : #include "buffer_key.h"
19 : #include "orion_adapter_hccp.h"
20 :
21 : namespace Hccl {
22 :
23 : using TokenInfo = std::pair<TokenIdHandle, uint32_t>;
24 : using BufKeyVecIndex = u32;
25 : constexpr BufKeyVecIndex INVALID_BUF_KEY_VEC_INDEX = UINT32_MAX;
26 :
27 : class TokenRefMap {
28 : public:
29 : using Iterator = std::unordered_map<BufKeyVecIndex, TokenInfo>::iterator;
30 :
31 30 : Iterator begin() { return data_.begin(); }
32 :
33 34 : Iterator end() { return data_.end(); }
34 :
35 153 : u32 insert(BufKeyVecIndex key, const TokenInfo& value)
36 : {
37 153 : if (has(key)) {
38 0 : ref_[key]++;
39 : } else {
40 153 : data_.insert(std::make_pair(key, value));
41 153 : ref_[key] = 1;
42 : }
43 153 : return count(key);
44 : }
45 :
46 2 : u32 eraseAndGet(BufKeyVecIndex key, TokenInfo& erasedValue)
47 : {
48 2 : u32 refCount = count(key);
49 2 : if (refCount > 1) {
50 0 : ref_[key]--;
51 2 : } else if (refCount == 1) {
52 2 : erasedValue = std::move(data_[key]);
53 2 : data_.erase(key);
54 2 : ref_.erase(key);
55 : }
56 2 : return count(key);
57 : }
58 :
59 30 : void clear()
60 : {
61 30 : data_.clear();
62 30 : ref_.clear();
63 30 : }
64 :
65 618 : bool has(BufKeyVecIndex key) { return data_.find(key) != data_.end(); }
66 :
67 310 : u32 count(BufKeyVecIndex key) { return has(key) ? ref_[key] : 0; }
68 :
69 153 : TokenInfo& operator[](BufKeyVecIndex key) { return data_[key]; }
70 :
71 : private:
72 : std::unordered_map<BufKeyVecIndex, TokenInfo> data_;
73 : std::unordered_map<BufKeyVecIndex, u32> ref_;
74 : };
75 :
76 : class TokenInfoManager {
77 : public:
78 49 : TokenInfoManager(u32 devId, RdmaHandle rdmahandle) : devId_(devId), rdmahandle_(rdmahandle) {}
79 :
80 : TokenInfo GetTokenInfo(const BufferKey<uintptr_t, u64>& bufKey);
81 : void PutTokenInfo(const BufferKey<uintptr_t, u64>& bufKey, TokenIdHandle tokenIdHandle);
82 :
83 : void Destroy();
84 :
85 : private:
86 : u32 devId_;
87 : RdmaHandle rdmahandle_;
88 : std::mutex tokenInfoMgrMutex_;
89 :
90 : TokenRefMap tokenRefMap_;
91 : std::unordered_map<u32, vector<vector<BufferKey<uintptr_t, u64>>>>
92 : bufferKeysMap_; // <devId, BufKeyVecIndex, vector<BufferKey>>
93 : std::unordered_map<TokenIdHandle, BufKeyVecIndex> tokenIdToIndex_;
94 :
95 : BufKeyVecIndex GetBufferVecIndex(const BufferKey<uintptr_t, u64>& inputBufKey);
96 : };
97 :
98 : bool HasIntersect(const vector<BufferKey<uintptr_t, u64>>& bufKeys, const BufferKey<uintptr_t, u64>& inputBufKey);
99 :
100 : } // namespace Hccl
101 :
102 : #endif // HCCLV2_TOKENINFO_MANAGER_H
|