You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory_manager.cpp 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * \file dnn/test/common/memory_manager.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "./memory_manager.h"
  12. #include "src/common/utils.h"
  13. #include "test/common/utils.h"
  14. namespace {
  15. using namespace megdnn;
  16. using namespace test;
  17. std::unique_ptr<MemoryManager> create_memory_manager_from_handle(Handle* handle) {
  18. return make_unique<HandleMemoryManager>(handle);
  19. }
  20. } // anonymous namespace
  21. megdnn::test::MemoryManagerHolder megdnn::test::MemoryManagerHolder::m_instance;
  22. megdnn::test::HandleMemoryManager::HandleMemoryManager(Handle* handle)
  23. : MemoryManager(), m_handle(handle) {}
  24. void* megdnn::test::HandleMemoryManager::malloc(size_t size) {
  25. auto comp_handle = m_handle->megcore_computing_handle();
  26. megcoreDeviceHandle_t dev_handle;
  27. megcore_check(megcoreGetDeviceHandle(comp_handle, &dev_handle));
  28. void* ptr;
  29. megcore_check(megcoreMalloc(dev_handle, &ptr, size));
  30. return ptr;
  31. }
  32. void megdnn::test::HandleMemoryManager::free(void* ptr) {
  33. auto comp_handle = m_handle->megcore_computing_handle();
  34. megcoreDeviceHandle_t dev_handle;
  35. megcore_check(megcoreGetDeviceHandle(comp_handle, &dev_handle));
  36. megcore_check(megcoreFree(dev_handle, ptr));
  37. }
  38. megdnn::test::MemoryManager* megdnn::test::MemoryManagerHolder::get(Handle* handle) {
  39. std::lock_guard<std::mutex> lock(m_map_mutex);
  40. auto i = m_map.find(handle);
  41. if (i != m_map.end()) {
  42. // found
  43. return i->second.get();
  44. } else {
  45. // not found. create it
  46. auto mm = create_memory_manager_from_handle(handle);
  47. auto res = mm.get();
  48. m_map.emplace(std::make_pair(handle, std::move(mm)));
  49. return res;
  50. }
  51. }
  52. void MemoryManagerHolder::update(
  53. Handle* handle, std::unique_ptr<MemoryManager> memory_manager) {
  54. std::lock_guard<std::mutex> lock(m_map_mutex);
  55. m_map[handle] = std::move(memory_manager);
  56. }
  57. void MemoryManagerHolder::clear() {
  58. std::lock_guard<std::mutex> lock(m_map_mutex);
  59. m_map.clear();
  60. }
  61. // vim: syntax=cpp.doxygen