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