• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 for "scratch" allocator for device memory, which deallocates all
31 // buffers it has allocated at destruction. Returned memory pointers are not
32 // owning.
33 //
34 // Used by stream operations (e.g. Stream::ThenConvolveWithScratch) to
35 // optionally request scratch space to speed up the operation.
36 class ScratchAllocator {
37  public:
38   virtual ~ScratchAllocator();
39 
40   // Returns a limit of memory this scratch allocator wants to produce, in
41   // bytes. This information may be used to help select an algorithm.
42   //
43   // Returns values < 0 to indicate that there is no recommended limit.
44   virtual int64 GetMemoryLimitInBytes() = 0;
45 
46   // Returns an allocation on byte_size bytes for use in an operation on stream.
47   //
48   // This is a temporary allocation, and the caller is responsible for
49   // deallocating at some known-safe point. See the class comment above.
50   virtual port::StatusOr<DeviceMemory<uint8>> AllocateBytes(
51       int64 byte_size) = 0;
52 };
53 
54 // Allocates a single temporary memory allocation -- this memory is deallocated
55 // at the next stream synchronization point after this object has gone out of
56 // scope. This satisfies the lifetime and deallocation properties given in the
57 // class comment above.
58 //
59 // Thread-compatible, but not thread-safe (use in scenarios where only one
60 // thread will request the scratch allocation).
61 class OneTimeScratchAllocator : public ScratchAllocator {
62  public:
63   explicit OneTimeScratchAllocator(Stream* stream);
64   ~OneTimeScratchAllocator() override;
65   int64 GetMemoryLimitInBytes() override;
66   port::StatusOr<DeviceMemory<uint8>> AllocateBytes(int64 byte_size) override;
67 
68  private:
69   std::unique_ptr<TemporaryDeviceMemory<uint8>> temporary_;
70   Stream* stream_;
71 
72   SE_DISALLOW_COPY_AND_ASSIGN(OneTimeScratchAllocator);
73 };
74 
75 }  // namespace stream_executor
76 
77 #endif  // TENSORFLOW_STREAM_EXECUTOR_SCRATCH_ALLOCATOR_H_
78