1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <cuda_runtime.h>
#include <string>
namespace faiss { namespace gpu {
class DeviceMemory;
class DeviceMemoryReservation {
public:
DeviceMemoryReservation();
DeviceMemoryReservation(DeviceMemory* state,
int device, void* p, size_t size,
cudaStream_t stream);
DeviceMemoryReservation(DeviceMemoryReservation&& m) noexcept;
~DeviceMemoryReservation();
DeviceMemoryReservation& operator=(DeviceMemoryReservation&& m);
int device() { return device_; }
void* get() { return data_; }
size_t size() { return size_; }
cudaStream_t stream() { return stream_; }
private:
DeviceMemory* state_;
int device_;
void* data_;
size_t size_;
cudaStream_t stream_;
};
/// Manages temporary memory allocations on a GPU device
class DeviceMemory {
public:
virtual ~DeviceMemory();
/// Returns the device we are managing memory for
virtual int getDevice() const = 0;
/// Obtains a temporary memory allocation for our device,
/// whose usage is ordered with respect to the given stream.
virtual DeviceMemoryReservation getMemory(cudaStream_t stream,
size_t size) = 0;
/// Returns the current size available without calling cudaMalloc
virtual size_t getSizeAvailable() const = 0;
/// Returns a string containing our current memory manager state
virtual std::string toString() const = 0;
/// Returns the high-water mark of cudaMalloc allocations for our
/// device
virtual size_t getHighWaterCudaMalloc() const = 0;
protected:
friend class DeviceMemoryReservation;
virtual void returnAllocation(DeviceMemoryReservation& m) = 0;
};
} } // namespace