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