forked from mindspore-Ecosystem/mindspore
!15776 support CPU actor runtime
From: @limingqi107 Reviewed-by: @cristoval,@wilfchen Signed-off-by: @wilfchen
This commit is contained in:
commit
5c4729744c
|
@ -85,7 +85,14 @@ bool CPUDeviceAddress::SyncHostToDevice(const ShapeVector & /*shape*/, size_t si
|
|||
MS_LOG(DEBUG) << "host_ptr is equal to ptr_, request ignored.";
|
||||
return true;
|
||||
}
|
||||
if (type_id_ == kNumberTypeFloat32 && type == kNumberTypeFloat16) {
|
||||
|
||||
if (type == type_id_) {
|
||||
auto ret_code = memcpy_s(ptr_, size_, host_ptr, size);
|
||||
if (ret_code != EOK) {
|
||||
MS_LOG(ERROR) << "Failed to copy tensor!";
|
||||
return false;
|
||||
}
|
||||
} else if (type_id_ == kNumberTypeFloat32 && type == kNumberTypeFloat16) {
|
||||
HalfToFloat(ptr_, host_ptr, size / 2);
|
||||
} else if (type_id_ == kNumberTypeFloat32 && type == kNumberTypeFloat64) {
|
||||
DoubleToFloat(ptr_, host_ptr, size / sizeof(double));
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#include "runtime/device/device_address.h"
|
||||
#include "runtime/device/memory_manager.h"
|
||||
#include "runtime/device/cpu/cpu_simple_mem_plan.h"
|
||||
#include "runtime/hardware/cpu/cpu_memory_pool.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace device {
|
||||
namespace cpu {
|
||||
|
@ -33,7 +35,7 @@ class CPUMemoryManager : public MemoryManager {
|
|||
virtual ~CPUMemoryManager();
|
||||
|
||||
void MallocDeviceMemory() override {}
|
||||
void FreeDeviceMemory() override {}
|
||||
void FreeDeviceMemory() override { CPUMemoryPool::GetInstance().ReleaseDeviceRes(); }
|
||||
void ResetDynamicMemory() override;
|
||||
|
||||
void AssignMemory(const session::KernelGraph *graph);
|
||||
|
@ -44,6 +46,12 @@ class CPUMemoryManager : public MemoryManager {
|
|||
void IncreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs);
|
||||
void DecreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs);
|
||||
|
||||
void *MallocMemFromMemPool(size_t size) override { return CPUMemoryPool::GetInstance().AllocTensorMem(size); }
|
||||
void FreeMemFromMemPool(void *device_ptr) override { CPUMemoryPool::GetInstance().FreeTensorMem(device_ptr); }
|
||||
std::vector<void *> MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) override {
|
||||
return CPUMemoryPool::GetInstance().AllocContinuousTensorMem(total_size, size_list);
|
||||
}
|
||||
|
||||
protected:
|
||||
uint8_t *MallocStaticMem(size_t size, bool communication_mem, uint32_t graph_id = kInvalidGraphId) override;
|
||||
uint8_t *MallocDynamicMem(size_t size, bool communication_mem) override;
|
||||
|
|
|
@ -2,13 +2,11 @@ file(GLOB_RECURSE HARDWARE_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
|||
"device_context_manager.cc")
|
||||
|
||||
if(ENABLE_GPU)
|
||||
file(GLOB_RECURSE HARDWARE_GPU_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
"gpu/gpu_device_context.cc")
|
||||
file(GLOB_RECURSE HARDWARE_GPU_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "gpu/*.cc")
|
||||
endif()
|
||||
|
||||
if(ENABLE_CPU)
|
||||
file(GLOB_RECURSE HARDWARE_CPU_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
"cpu/cpu_device_context.cc")
|
||||
file(GLOB_RECURSE HARDWARE_CPU_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "cpu/*.cc")
|
||||
endif()
|
||||
|
||||
set_property(SOURCE ${HARDWARE_SRC_LIST} ${HARDWARE_D_SRC_LIST} ${HARDWARE_GPU_SRC_LIST} ${HARDWARE_CPU_SRC_LIST}
|
||||
|
|
|
@ -41,12 +41,23 @@ bool CPUDeviceContext::Initialize() {
|
|||
}
|
||||
|
||||
bool CPUDeviceContext::AllocateMemory(DeviceAddress *const &address, size_t size) const {
|
||||
address->ptr_ = static_cast<CPUMemoryManager *>(mem_manager_.get())->StaticMemMalloc(size);
|
||||
MS_EXCEPTION_IF_NULL(address);
|
||||
MS_EXCEPTION_IF_NULL(mem_manager_);
|
||||
auto device_ptr = mem_manager_->MallocMemFromMemPool(size);
|
||||
if (!device_ptr) {
|
||||
return false;
|
||||
}
|
||||
address->ptr_ = device_ptr;
|
||||
address->size_ = size;
|
||||
address->from_mem_pool_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPUDeviceContext::FreeMemory(DeviceAddress *const &address) const {
|
||||
static_cast<CPUMemoryManager *>(mem_manager_.get())->MemFree(address->ptr_);
|
||||
MS_EXCEPTION_IF_NULL(address);
|
||||
MS_EXCEPTION_IF_NULL(address->ptr_);
|
||||
MS_EXCEPTION_IF_NULL(mem_manager_);
|
||||
mem_manager_->FreeMemFromMemPool(address->ptr_);
|
||||
address->ptr_ = nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "runtime/hardware/cpu/cpu_memory_pool.h"
|
||||
#include <string>
|
||||
#include "utils/log_adapter.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace device {
|
||||
namespace cpu {
|
||||
namespace {
|
||||
const size_t kKBToByte = 1024;
|
||||
const size_t kLineMaxSize = 1024;
|
||||
|
||||
size_t GetSystemMemorySize(const std::string &key) {
|
||||
FILE *file = fopen("/proc/meminfo", "r");
|
||||
if (file == nullptr) {
|
||||
MS_LOG(EXCEPTION) << "Get system meminfo failed.";
|
||||
}
|
||||
|
||||
size_t mem_size = 0;
|
||||
std::string format = key + ": %zu kB\n";
|
||||
while (true) {
|
||||
auto ret = fscanf(file, format.c_str(), &mem_size);
|
||||
if (feof(file)) {
|
||||
MS_LOG(ERROR) << "Get system memory failed.";
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret == 1) {
|
||||
MS_LOG(INFO) << "Get system memory(" << key << "): " << mem_size << " kB";
|
||||
break;
|
||||
} else {
|
||||
// Need skip current line if fscanf does not capture the result.
|
||||
char temp[kLineMaxSize];
|
||||
auto temp_ret = fgets(temp, kLineMaxSize, file);
|
||||
(void)temp_ret;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return mem_size * kKBToByte;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
size_t CPUMemoryPool::AllocDeviceMem(size_t alloc_size, DeviceMemPtr *addr) {
|
||||
if (alloc_size == 0) {
|
||||
MS_LOG(EXCEPTION) << "The memory alloc size is 0.";
|
||||
}
|
||||
|
||||
*addr = malloc(alloc_size);
|
||||
if (*addr == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc memory failed.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
total_used_memory_ += alloc_size;
|
||||
MS_LOG(INFO) << "Current alloc size[" << alloc_size << "], total used size[" << total_used_memory_ << "].";
|
||||
|
||||
return alloc_size;
|
||||
}
|
||||
|
||||
bool CPUMemoryPool::FreeDeviceMem(const DeviceMemPtr &addr) {
|
||||
free(addr);
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t CPUMemoryPool::free_mem_size() { return GetSystemMemorySize("MemAvailable"); }
|
||||
|
||||
size_t CPUMemoryPool::total_mem_size() { return GetSystemMemorySize("MemTotal"); }
|
||||
} // namespace cpu
|
||||
} // namespace device
|
||||
} // namespace mindspore
|
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CCSRC_RUNTIME_HARDWARE_CPU_CPU_MEMORY_POOL_H_
|
||||
#define MINDSPORE_CCSRC_RUNTIME_HARDWARE_CPU_CPU_MEMORY_POOL_H_
|
||||
|
||||
#include <memory>
|
||||
#include "utils/ms_utils.h"
|
||||
#include "backend/optimizer/mem_reuse/mem_dynamic_allocator.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace device {
|
||||
namespace cpu {
|
||||
class CPUMemoryPool : public DynamicMemPoolBestFit {
|
||||
public:
|
||||
~CPUMemoryPool() override = default;
|
||||
|
||||
static CPUMemoryPool &GetInstance() {
|
||||
static CPUMemoryPool instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr) override;
|
||||
bool FreeDeviceMem(const DeviceMemPtr &addr) override;
|
||||
size_t free_mem_size() override;
|
||||
size_t total_mem_size() override;
|
||||
|
||||
private:
|
||||
CPUMemoryPool() = default;
|
||||
DISABLE_COPY_AND_ASSIGN(CPUMemoryPool);
|
||||
|
||||
size_t total_used_memory_{0};
|
||||
};
|
||||
} // namespace cpu
|
||||
} // namespace device
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_CPU_CPU_MEMORY_POOL_H_
|
Loading…
Reference in New Issue