1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/stream_executor/temporary_memory_manager.h"
17 
18 #include "absl/strings/str_cat.h"
19 #include "absl/strings/str_format.h"
20 #include "tensorflow/stream_executor/platform/logging.h"
21 #include "tensorflow/stream_executor/stream.h"
22 #include "tensorflow/stream_executor/stream_executor_pimpl.h"
23 
24 namespace stream_executor {
25 namespace internal {
26 
ForceDeallocateAll()27 void TemporaryMemoryManager::ForceDeallocateAll() {
28   absl::MutexLock lock(&mutex_);
29   VLOG(1) << "force-deallocating " << records_.size() << " remaining records";
30   for (auto it = records_.begin(); it != records_.end(); ++it) {
31     DeviceMemoryBase device_memory = it->first;
32     stream_->parent()->Deallocate(&device_memory);
33   }
34 }
35 
MarkFinalized(const DeviceMemoryBase & device_memory,uint64 generation,bool must_exist)36 void TemporaryMemoryManager::MarkFinalized(
37     const DeviceMemoryBase& device_memory, uint64 generation, bool must_exist) {
38   absl::MutexLock lock(&mutex_);
39   auto it = records_.find(device_memory);
40   if (it == records_.end()) {
41     if (must_exist) {
42       LOG(FATAL) << "attempted to mark finalization for temporary "
43                     "memory that does not exist";
44     }
45     return;
46   }
47   it->second.finalized = true;
48 }
49 
DeallocateFinalizedTemporaries()50 void TemporaryMemoryManager::DeallocateFinalizedTemporaries() {
51   absl::MutexLock lock(&mutex_);
52   int deallocated_count = 0;
53   for (auto it = records_.begin(); it != records_.end();) {
54     if (it->second.finalized) {
55       DeviceMemoryBase device_memory = it->first;
56       stream_->parent()->Deallocate(&device_memory);
57       ++deallocated_count;
58       it = records_.erase(it);
59     } else {
60       ++it;
61     }
62   }
63   VLOG(1) << "deallocated " << deallocated_count << " finalized temporaries";
64 }
65 
IsFinalized(const DeviceMemoryBase & device_memory,uint64 allocation_generation) const66 bool TemporaryMemoryManager::IsFinalized(const DeviceMemoryBase& device_memory,
67                                          uint64 allocation_generation) const {
68   absl::MutexLock lock(&mutex_);
69   auto it = records_.find(device_memory);
70   if (it == records_.end()) {
71     return true;  // If there's no record present it's vacuously finalized.
72   }
73 
74   if (it->second.allocation_generation == allocation_generation) {
75     return it->second.finalized;
76   }
77 
78   // If the allocation generation did not match, it's vacuously true.
79   return true;
80 }
81 
HasAllocated(const DeviceMemoryBase & device_memory,uint64 generation) const82 bool TemporaryMemoryManager::HasAllocated(const DeviceMemoryBase& device_memory,
83                                           uint64 generation) const {
84   absl::MutexLock lock(&mutex_);
85   auto it = records_.find(device_memory);
86   if (it == records_.end()) {
87     return false;
88   }
89   return it->second.allocation_generation == generation;
90 }
91 
92 port::StatusOr<std::unique_ptr<TemporaryDeviceMemoryBase>>
AllocateArrayBase(uint64 element_count,uint64 element_size)93 TemporaryMemoryManager::AllocateArrayBase(uint64 element_count,
94                                           uint64 element_size) {
95   uint64 byte_size = element_count * element_size;
96   DeviceMemoryBase device_memory =
97       stream_->parent()->AllocateArray<uint8>(byte_size);
98   if (device_memory == nullptr) {
99     return port::Status(port::error::RESOURCE_EXHAUSTED,
100                         absl::StrCat("could not allocate temporary memory of ",
101                                      byte_size, " bytes"));
102   }
103 
104   uint64 generation;
105 
106   // Add the record before instantiating the device memory instance so we can
107   // check the allocation invariant at TemporaryDeviceMemory construction time.
108   {
109     absl::MutexLock lock(&mutex_);
110     generation = ++generation_;
111     DCHECK(records_.find(device_memory) == records_.end());
112     records_[device_memory] = {generation,
113                                /*finalized=*/false};
114   }
115 
116   VLOG(1) << absl::StreamFormat(
117       "stream %p allocated temporary device memory at %p (size %u) in "
118       "generation %u",
119       stream_, device_memory.opaque(), byte_size, generation);
120   std::unique_ptr<TemporaryDeviceMemoryBase> result(
121       new TemporaryDeviceMemoryBase(stream_, device_memory, generation));
122   return std::move(result);
123 }
124 
125 }  // namespace internal
126 }  // namespace stream_executor
127