OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DeviceMemoryAllocationMap.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 HEAVY.AI, Inc.
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 <utility>
18 
20 #include "Logger/Logger.h"
21 
22 namespace CudaMgr_Namespace {
23 
25 
27  return map_;
28 }
29 
31  return map_.empty();
32 }
33 
35  const uint64_t size,
36  const uint64_t handle,
37  const heavyai::UUID uuid,
38  const int device_num,
39  const bool is_slab) {
40  Allocation allocation{size, handle, uuid, device_num, is_slab};
41  CHECK(map_.try_emplace(device_ptr, std::move(allocation)).second);
42 }
43 
45  const DevicePtr device_ptr) {
46  // find the exact match
47  auto const itr = map_.find(device_ptr);
48  CHECK(itr != map_.end());
49  // copy out the allocation
50  Allocation allocation = itr->second;
51  // remove from map
52  map_.erase(itr);
53  // return the allocation that was
54  return allocation;
55 }
56 
57 std::pair<DeviceMemoryAllocationMap::DevicePtr, DeviceMemoryAllocationMap::Allocation>
59  // find the map entry above this address
60  auto itr = map_.upper_bound(device_ptr);
61  CHECK(itr != map_.begin());
62  // return the previous entry
63  --itr;
64  return std::make_pair(itr->first, itr->second);
65 }
66 
69  auto const cbid = ++last_map_changed_cbid_;
70  CHECK(map_changed_cbs_.emplace(cbid, cb).second) << "Repeat registration";
71  return cbid;
72 }
73 
75  auto itr = map_changed_cbs_.find(cbid);
76  CHECK(itr != map_changed_cbs_.end()) << "Failed to unregister";
77  map_changed_cbs_.erase(itr);
78 }
79 
81  const bool is_slab) const {
82  for (auto const& cb : map_changed_cbs_) {
83  cb.second(device_uuid, is_slab);
84  }
85 }
86 
87 } // namespace CudaMgr_Namespace
std::unordered_map< MapChangedCBID, MapChangedCB > map_changed_cbs_
void notifyMapChanged(const heavyai::UUID device_uuid, const bool is_slab) const
Allocation removeAllocation(const DevicePtr device_ptr)
const MapChangedCBID registerMapChangedCB(MapChangedCB cb)
std::function< void(const heavyai::UUID, const bool)> MapChangedCB
void addAllocation(const DevicePtr device_ptr, const uint64_t size, const uint64_t handle, const heavyai::UUID uuid, const int device_num, const bool is_slab)
std::pair< DevicePtr, Allocation > getAllocation(const DevicePtr device_ptr)
#define CHECK(condition)
Definition: Logger.h:291