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 #ifndef TENSORFLOW_STREAM_EXECUTOR_SCRATCH_ALLOCATOR_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_SCRATCH_ALLOCATOR_H_ 18 19 #include <memory> 20 21 #include "tensorflow/stream_executor/device_memory.h" 22 #include "tensorflow/stream_executor/lib/statusor.h" 23 #include "tensorflow/stream_executor/platform/port.h" 24 #include "tensorflow/stream_executor/temporary_device_memory.h" 25 26 namespace stream_executor { 27 28 class Stream; 29 30 // Interface that allows stream operations (e.g. 31 // Stream::ThenConvolveWithScratch) to optionally request scratch space be 32 // allocated in order to speed up the operation being enqueued. 33 // 34 // Note that the caller is responsible for deallocating the scratch space at a 35 // known-safe point, when all scratch-memory-consuming kernels are known for 36 // sure to have finished; e.g. at stream synchronization time. This is different 37 // from a traditional C++ object allocator, where the client is responsible for 38 // releasing. (Conceptually, scratch memory is a form of "temporary" device 39 // memory allocation.) 40 class ScratchAllocator { 41 public: 42 virtual ~ScratchAllocator(); 43 44 // Returns a limit of memory this scratch allocator wants to produce, in 45 // bytes. This information may be used to help select an algorithm. 46 // 47 // Returns values < 0 to indicate that there is no recommended limit. 48 virtual int64 GetMemoryLimitInBytes(Stream* stream) = 0; 49 50 // Returns an allocation on byte_size bytes for use in an operation on stream. 51 // 52 // This is a temporary allocation, and the caller is responsible for 53 // deallocating at some known-safe point. See the class comment above. 54 virtual port::StatusOr<DeviceMemory<uint8>> AllocateBytes( 55 Stream* stream, int64 byte_size) = 0; 56 }; 57 58 // Allocates a single temporary memory allocation -- this memory is deallocated 59 // at the next stream synchronization point after this object has gone out of 60 // scope. This satisfies the lifetime and deallocation properties given in the 61 // class comment above. 62 // 63 // Thread-compatible, but not thread-safe (use in scenarios where only one 64 // thread will request the scratch allocation). 65 class OneTimeScratchAllocator : public ScratchAllocator { 66 public: 67 OneTimeScratchAllocator(); 68 ~OneTimeScratchAllocator() override; 69 int64 GetMemoryLimitInBytes(Stream* stream) override; 70 port::StatusOr<DeviceMemory<uint8>> AllocateBytes(Stream* stream, 71 int64 byte_size) override; 72 73 private: 74 std::unique_ptr<TemporaryDeviceMemory<uint8>> temporary_; 75 76 SE_DISALLOW_COPY_AND_ASSIGN(OneTimeScratchAllocator); 77 }; 78 79 } // namespace stream_executor 80 81 #endif // TENSORFLOW_STREAM_EXECUTOR_SCRATCH_ALLOCATOR_H_ 82