Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
StackDeviceMemory.h
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD+Patents license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 
10 #pragma once
11 
12 #include "DeviceMemory.h"
13 #include <list>
14 #include <memory>
15 #include <unordered_map>
16 
17 namespace faiss { namespace gpu {
18 
19 /// Device memory manager that provides temporary memory allocations
20 /// out of a region of memory
22  public:
23  /// Allocate a new region of memory that we manage
24  explicit StackDeviceMemory(int device, size_t allocPerDevice);
25 
26  /// Manage a region of memory for a particular device, with or
27  /// without ownership
28  StackDeviceMemory(int device, void* p, size_t size, bool isOwner);
29 
30  ~StackDeviceMemory() override;
31 
32  /// Enable or disable the warning about not having enough temporary memory
33  /// when cudaMalloc gets called
34  void setCudaMallocWarning(bool b);
35 
36  int getDevice() const override;
37 
38  DeviceMemoryReservation getMemory(cudaStream_t stream,
39  size_t size) override;
40 
41  size_t getSizeAvailable() const override;
42  std::string toString() const override;
43  size_t getHighWaterCudaMalloc() const override;
44 
45  protected:
46  void returnAllocation(DeviceMemoryReservation& m) override;
47 
48  protected:
49  /// Previous allocation ranges and the streams for which
50  /// synchronization is required
51  struct Range {
52  inline Range(char* s, char* e, cudaStream_t str) :
53  start_(s), end_(e), stream_(str) {
54  }
55 
56  // References a memory range [start, end)
57  char* start_;
58  char* end_;
59  cudaStream_t stream_;
60  };
61 
62  struct Stack {
63  /// Constructor that allocates memory via cudaMalloc
64  Stack(int device, size_t size);
65 
66  /// Constructor that references a pre-allocated region of memory
67  Stack(int device, void* p, size_t size, bool isOwner);
68  ~Stack();
69 
70  /// Returns how much size is available for an allocation without
71  /// calling cudaMalloc
72  size_t getSizeAvailable() const;
73 
74  /// Obtains an allocation; all allocations are guaranteed to be 16
75  /// byte aligned
76  char* getAlloc(size_t size, cudaStream_t stream);
77 
78  /// Returns an allocation
79  void returnAlloc(char* p, size_t size, cudaStream_t stream);
80 
81  /// Returns the stack state
82  std::string toString() const;
83 
84  /// Returns the high-water mark of cudaMalloc activity
85  size_t getHighWaterCudaMalloc() const;
86 
87  /// Device this allocation is on
88  int device_;
89 
90  /// Do we own our region of memory?
91  bool isOwner_;
92 
93  /// Where our allocation begins and ends
94  /// [start_, end_) is valid
95  char* start_;
96  char* end_;
97 
98  /// Total size end_ - start_
99  size_t size_;
100 
101  /// Stack head within [start, end)
102  char* head_;
103 
104  /// List of previous last users of allocations on our stack, for
105  /// possible synchronization purposes
106  std::list<Range> lastUsers_;
107 
108  /// How much cudaMalloc memory is currently outstanding?
110 
111  /// What's the high water mark in terms of memory used from the
112  /// temporary buffer?
114 
115  /// What's the high water mark in terms of memory allocated via
116  /// cudaMalloc?
118 
119  /// Whether or not a warning upon cudaMalloc is generated
121  };
122 
123  /// Our device
124  int device_;
125 
126  /// Memory stack
128 };
129 
130 } } // namespace
bool isOwner_
Do we own our region of memory?
DeviceMemoryReservation getMemory(cudaStream_t stream, size_t size) override
size_t getHighWaterCudaMalloc() const
Returns the high-water mark of cudaMalloc activity.
bool cudaMallocWarning_
Whether or not a warning upon cudaMalloc is generated.
size_t size_
Total size end_ - start_.
Stack(int device, size_t size)
Constructor that allocates memory via cudaMalloc.
size_t mallocCurrent_
How much cudaMalloc memory is currently outstanding?
void returnAlloc(char *p, size_t size, cudaStream_t stream)
Returns an allocation.
char * head_
Stack head within [start, end)
size_t getSizeAvailable() const override
Returns the current size available without calling cudaMalloc.
int device_
Device this allocation is on.
std::string toString() const override
Returns a string containing our current memory manager state.
Manages temporary memory allocations on a GPU device.
Definition: DeviceMemory.h:45
std::string toString() const
Returns the stack state.
size_t getHighWaterCudaMalloc() const override
char * getAlloc(size_t size, cudaStream_t stream)
int getDevice() const override
Returns the device we are managing memory for.
StackDeviceMemory(int device, size_t allocPerDevice)
Allocate a new region of memory that we manage.