• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_MAYBE_OWNING_DEVICE_MEMORY_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_MAYBE_OWNING_DEVICE_MEMORY_H_
18 
19 #include "absl/types/optional.h"
20 #include "absl/types/variant.h"
21 #include "tensorflow/compiler/xla/service/device_memory_allocator.h"
22 #include "tensorflow/compiler/xla/service/owning_device_memory.h"
23 
24 namespace xla {
25 
26 // MaybeOwningDeviceMemory represents either an owned or unowned device memory.
27 // Like std::variant<OwningDeviceMemory, DeviceMemory>. When the object goes
28 // output of scope, it will free the underlying memory if it owns it.
29 class MaybeOwningDeviceMemory {
30  public:
31   MaybeOwningDeviceMemory() = default;
MaybeOwningDeviceMemory(OwningDeviceMemory owned)32   explicit MaybeOwningDeviceMemory(OwningDeviceMemory owned)
33       : mem_(std::move(owned)) {}
MaybeOwningDeviceMemory(se::DeviceMemoryBase unowned)34   explicit MaybeOwningDeviceMemory(se::DeviceMemoryBase unowned)
35       : mem_(unowned) {}
36   MaybeOwningDeviceMemory(MaybeOwningDeviceMemory&&) = default;
37   ~MaybeOwningDeviceMemory() = default;
38 
39   MaybeOwningDeviceMemory& operator=(se::DeviceMemoryBase unowned) {
40     mem_ = unowned;
41     return *this;
42   }
43 
44   MaybeOwningDeviceMemory& operator=(OwningDeviceMemory owned) {
45     mem_ = std::move(owned);
46     return *this;
47   }
48 
49   MaybeOwningDeviceMemory& operator=(MaybeOwningDeviceMemory&&) = default;
50 
51   // Fetches the underlying DeviceMemoryBase from a MaybeOwningDeviceMemory. The
52   // caller of this function is *not* responsible for freeing the memory.
53   se::DeviceMemoryBase AsDeviceMemoryBase();
54 
55   // Release the OwningDeviceMemory without freeing it, and moves the ownership
56   // of the memory buffer from the object to the caller.
57   //
58   // A nullopt is returned if the HasOwnership() == false;
59   absl::optional<OwningDeviceMemory> Release();
60 
61   // Returns true if the device_memory has ownership over underlying memory.
62   bool HasOwnership() const;
63 
64  private:
65   absl::variant<OwningDeviceMemory, se::DeviceMemoryBase> mem_;
66 };
67 
68 }  // namespace xla
69 
70 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_MAYBE_OWNING_DEVICE_MEMORY_H_
71