* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef GE_GRAPH_MANAGER_SESSION_SCOPE_MEM_ALLOCATOR_H_
#define GE_GRAPH_MANAGER_SESSION_SCOPE_MEM_ALLOCATOR_H_
#include <iostream>
#include <memory>
#include <mutex>
#include <vector>
#include <unordered_map>
#include "graph/node.h"
#include "runtime/mem.h"
#include "graph/manager/graph_mem_allocator.h"
namespace ge {
class SessionScopeMemoryInfo {
public:
SessionScopeMemoryInfo(const size_t size, const std::shared_ptr<uint8_t> &ptr)
: size_(size), ptr_(ptr) {}
SessionScopeMemoryInfo() = delete;
virtual ~SessionScopeMemoryInfo() = default;
SessionScopeMemoryInfo(const SessionScopeMemoryInfo &) = default;
SessionScopeMemoryInfo &operator=(const SessionScopeMemoryInfo &) & = default;
size_t GetSize() const {
return size_;
}
private:
size_t size_;
std::shared_ptr<uint8_t> ptr_;
};
class SessionScopeMemAllocator {
public:
explicit SessionScopeMemAllocator(const rtMemType_t memory_type, MemoryAllocator *const memory_allocator = nullptr);
SessionScopeMemAllocator(const SessionScopeMemAllocator &) = delete;
SessionScopeMemAllocator &operator=(const SessionScopeMemAllocator &) & = delete;
virtual ~SessionScopeMemAllocator() = default;
Status Initialize(const uint32_t device_id = 0U);
void Finalize(const uint32_t device_id = 0U);
uint8_t *Malloc(size_t size, const uint64_t session_id, const uint32_t device_id = 0U);
Status Free(const uint64_t session_id, const uint32_t device_id = 0U);
private:
void FreeAllMemory();
private:
rtMemType_t memory_type_;
MemoryAllocator *memory_allocator_;
mutable std::recursive_mutex mutex_;
std::unordered_map<uint64_t, std::vector<SessionScopeMemoryInfo>> allocated_memory_;
};
}
#endif