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 HETEROG_MR_MANAGER_PUB_H
12 : #define HETEROG_MR_MANAGER_PUB_H
13 :
14 : #include <vector>
15 : #include <list>
16 : #include <mutex>
17 : #include <atomic>
18 : #include <hccl/hccl_types.h>
19 :
20 : #include "hccl/base.h"
21 : #include "log.h"
22 :
23 : namespace hccl {
24 : constexpr u32 MEM_BLOCK_SIZE = 128;
25 : constexpr u32 MEM_BLOCK_CAP = 64 * 1024;
26 : constexpr u32 MEM_BLOCK_NUM = MEM_BLOCK_CAP - 1;
27 : constexpr u32 MEM_BLOCK_CAP_BIGER = 2048 * 1024;
28 : constexpr u32 MEM_BLOCK_NUM_BIGER = MEM_BLOCK_CAP_BIGER - 1;
29 : constexpr u32 MEM_BLOCK_DOUBLE = 2;
30 : constexpr u32 MEM_BLOCK_RECV_WQE_BATCH_NUM = 192;
31 :
32 : struct MemBlockQueue {
33 : std::atomic<u32> headPos{0};
34 : std::atomic<u32> tailPos{0};
35 : std::vector<void*> entry;
36 : u32 blockCap = MEM_BLOCK_CAP;
37 0 : MemBlockQueue() : entry{} {};
38 0 : ~MemBlockQueue() {};
39 0 : void Init(u32 capNum)
40 : {
41 0 : blockCap = capNum;
42 0 : entry.resize(blockCap);
43 0 : }
44 0 : HcclResult Push(void* item)
45 : {
46 0 : entry[headPos] = item;
47 0 : headPos = (headPos + 1) & (blockCap - 1);
48 0 : return ((headPos == tailPos) ? HCCL_E_INTERNAL : HCCL_SUCCESS);
49 : };
50 0 : HcclResult Pop(void*& item)
51 : {
52 0 : item = entry[tailPos];
53 0 : if (headPos == tailPos) {
54 0 : return HCCL_E_INTERNAL;
55 : }
56 0 : tailPos = (tailPos + 1) & (blockCap - 1);
57 0 : return HCCL_SUCCESS;
58 : };
59 0 : inline u32 Size() const { return (headPos + blockCap - tailPos) & (blockCap - 1); };
60 : };
61 :
62 : class HeterogMemBlocksManager {
63 : public:
64 : explicit HeterogMemBlocksManager();
65 : virtual ~HeterogMemBlocksManager();
66 :
67 : HcclResult Init(u32 memBlockNum);
68 :
69 : HcclResult Alloc(std::list<void*>& blockList);
70 :
71 : HcclResult Alloc(void** block)
72 : {
73 : if (usableBlockQue_.Size() < 1) {
74 : HCCL_ERROR("[HeterogMemBlocksManager][Alloc]lack of resources");
75 : return HCCL_E_PARA;
76 : }
77 : std::unique_lock<std::mutex> lock(usableBlockQueMutex_);
78 : CHK_RET(usableBlockQue_.Pop(*block));
79 : return HCCL_SUCCESS;
80 : };
81 :
82 0 : HcclResult Free(void* block)
83 : {
84 0 : std::unique_lock<std::mutex> lock(usableBlockQueMutex_);
85 0 : CHK_RET(usableBlockQue_.Push(block));
86 0 : return HCCL_SUCCESS;
87 0 : };
88 :
89 0 : inline void* GetMemAddr() const { return beginAddr_; }
90 :
91 0 : inline u64 GetMemSize() const { return memSize_; }
92 :
93 : private:
94 : bool isinited_;
95 : MemBlockQueue usableBlockQue_;
96 : std::mutex usableBlockQueMutex_;
97 : void* beginAddr_;
98 : u64 memSize_;
99 : s8* memStartAddr_;
100 : };
101 : } // namespace hccl
102 : #endif
|