• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "runtime/hardware/device_context.h"
18 #include "backend/common/optimizer/common_backend_optimization.h"
19 
20 namespace mindspore {
21 namespace device {
AllocateOffloadMemory(size_t size) const22 void *DeviceResManager::AllocateOffloadMemory(size_t size) const { return offloaded_mem_pool_->MallocHost(size); }
23 
FreeOffloadMemory(void * ptr) const24 void DeviceResManager::FreeOffloadMemory(void *ptr) const { offloaded_mem_pool_->FreeHost(ptr); }
25 
AllocateMemory(DeviceAddress * const & address,uint32_t stream_id) const26 bool DeviceResManager::AllocateMemory(DeviceAddress *const &address, uint32_t stream_id) const {
27   MS_EXCEPTION_IF_NULL(address);
28   if (address->GetPtr() != nullptr) {
29     MS_LOG(ERROR) << "Memory leak detected!";
30     return false;
31   }
32 
33   if (stream_id == UINT32_MAX) {
34     stream_id = address->stream_id();
35   }
36   auto device_ptr = AllocateMemory(address->GetSize(), stream_id);
37   if (!device_ptr) {
38     MS_LOG(WARNING) << "Allocate memory failed for size: " << address->GetSize();
39     return false;
40   }
41   address->set_ptr(device_ptr);
42   address->set_from_mem_pool(true);
43   return true;
44 }
45 
FreeMemory(DeviceAddress * const & address) const46 void DeviceResManager::FreeMemory(DeviceAddress *const &address) const {
47   MS_EXCEPTION_IF_NULL(address);
48   if (address->GetPtr() == nullptr) {
49     MS_LOG(EXCEPTION) << "Device ptr is null in device address:" << address << " to release!";
50   }
51 
52   if (!address->from_mem_pool()) {
53     MS_LOG(DEBUG) << "device address:" << address << " ptr:" << address->GetMutablePtr() << " not from pool";
54     return;
55   }
56   MS_LOG(DEBUG) << "Free memory from device address:" << address << " ptr:" << address->GetMutablePtr();
57   FreeMemory(address->GetMutablePtr());
58   address->set_ptr(nullptr);
59 }
60 
DestroyEvent(const DeviceEventPtr & event)61 bool DeviceResManager::DestroyEvent(const DeviceEventPtr &event) {
62   MS_EXCEPTION_IF_NULL(event);
63   if (!event->DestroyEvent()) {
64     MS_LOG(ERROR) << "DestroyEvent failed.";
65     return false;
66   }
67 
68   std::lock_guard<std::mutex> lock(device_events_mutex_);
69   const auto &iter = std::find(device_events_.begin(), device_events_.end(), event);
70   if (iter == device_events_.end()) {
71     MS_LOG(ERROR) << "Can't find specified device event.";
72     return false;
73   }
74   (void)device_events_.erase(iter);
75   return true;
76 }
77 
DestroyAllEvents()78 bool DeviceResManager::DestroyAllEvents() {
79   DeviceEventPtrList device_events_inner;
80   {
81     // Reduce the scopt to prevent deadlock.
82     std::lock_guard<std::mutex> lock(device_events_mutex_);
83     device_events_inner = device_events_;
84     device_events_.clear();
85   }
86   (void)std::for_each(device_events_inner.begin(), device_events_inner.end(), [this](const auto &event) {
87     MS_EXCEPTION_IF_NULL(event);
88     if (!event->DestroyEvent()) {
89       MS_LOG(ERROR) << "DestroyEvent failed.";
90     }
91   });
92   device_events_.clear();
93   return true;
94 }
95 
UnifyMindIR(const KernelGraphPtr & graph) const96 void KernelExecutor::UnifyMindIR(const KernelGraphPtr &graph) const { opt::CommonUnifyMindIR(graph); }
97 }  // namespace device
98 }  // namespace mindspore
99