• 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_CORE_COMMON_RUNTIME_GPU_GPU_DEBUG_ALLOCATOR_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_DEBUG_ALLOCATOR_H_
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "tensorflow/core/common_runtime/gpu/gpu_id.h"
24 #include "tensorflow/core/framework/allocator.h"
25 #include "tensorflow/core/platform/macros.h"
26 #include "tensorflow/core/platform/stream_executor.h"
27 #include "tensorflow/core/platform/types.h"
28 
29 namespace tensorflow {
30 
31 // An allocator that wraps a GPU allocator and adds debugging
32 // functionality that verifies that users do not write outside their
33 // allocated memory.
34 class GPUDebugAllocator : public Allocator {
35  public:
36   explicit GPUDebugAllocator(Allocator* allocator,
37                              PlatformGpuId platform_gpu_id);
38   ~GPUDebugAllocator() override;
Name()39   string Name() override { return "gpu_debug"; }
40   void* AllocateRaw(size_t alignment, size_t num_bytes) override;
41   void DeallocateRaw(void* ptr) override;
42   bool TracksAllocationSizes() override;
43   size_t RequestedSize(const void* ptr) override;
44   size_t AllocatedSize(const void* ptr) override;
45   int64 AllocationId(const void* ptr) override;
46   absl::optional<AllocatorStats> GetStats() override;
47   void ClearStats() override;
48 
49   // For testing.
50   bool CheckHeader(void* ptr);
51   bool CheckFooter(void* ptr);
52 
53  private:
54   Allocator* base_allocator_ = nullptr;  // owned
55 
56   se::StreamExecutor* stream_exec_;  // Not owned.
57 
58   TF_DISALLOW_COPY_AND_ASSIGN(GPUDebugAllocator);
59 };
60 
61 // An allocator that wraps a GPU allocator and resets the memory on
62 // allocation and free to 'NaN', helping to identify cases where the
63 // user forgets to initialize the memory.
64 class GPUNanResetAllocator : public Allocator {
65  public:
66   explicit GPUNanResetAllocator(Allocator* allocator,
67                                 PlatformGpuId platform_gpu_id);
68   ~GPUNanResetAllocator() override;
Name()69   string Name() override { return "gpu_nan_reset"; }
70   void* AllocateRaw(size_t alignment, size_t num_bytes) override;
71   void DeallocateRaw(void* ptr) override;
72   size_t RequestedSize(const void* ptr) override;
73   size_t AllocatedSize(const void* ptr) override;
74   absl::optional<AllocatorStats> GetStats() override;
75   void ClearStats() override;
76 
77  private:
78   Allocator* base_allocator_ = nullptr;  // owned
79 
80   se::StreamExecutor* stream_exec_;  // Not owned.
81 
82   TF_DISALLOW_COPY_AND_ASSIGN(GPUNanResetAllocator);
83 };
84 
85 }  // namespace tensorflow
86 
87 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_DEBUG_ALLOCATOR_H_
88