3
3
4
4
#include < vector>
5
5
#include " caffe/common.hpp"
6
+ #ifndef CPU_ONLY
7
+
8
+ namespace cub {
9
+ class CachingDeviceAllocator ;
10
+ }
6
11
7
12
namespace caffe {
8
13
9
- class GPUMemoryManager {
10
- public:
11
- enum PoolMode {
12
- NO_POOL, // Straight CUDA malloc/free (may be expensive)
13
- CUB_POOL, // CUB caching allocator
14
- #ifdef CPU_ONLY
15
- DEFAULT_POOL = NO_POOL
16
- #else
17
- DEFAULT_POOL = CUB_POOL // CUB pool is able to use unified memory properly
18
- #endif
19
- };
14
+ struct GPUMemory {
15
+ static void GetInfo (size_t * free_mem, size_t * used_mem) {
16
+ return mgr_.GetInfo (free_mem, used_mem);
17
+ }
20
18
21
- static const char * pool_name ();
22
- static bool using_pool () {
23
- return mode_ != NO_POOL;
19
+ template <class Any >
20
+ static void allocate (Any** ptr, size_t size,
21
+ cudaStream_t stream = cudaStreamDefault) {
22
+ CHECK (try_allocate (reinterpret_cast <void **>(ptr), size, stream));
24
23
}
25
24
26
- class Arena {
27
- public:
28
- Arena (const std::vector<int >& gpus, PoolMode m = DEFAULT_POOL, bool debug =
29
- false ) {
30
- init (gpus, m, debug);
31
- }
32
- ~Arena () {
33
- destroy ();
34
- }
25
+ static void deallocate (void * ptr,
26
+ cudaStream_t stream = cudaStreamDefault) {
27
+ mgr_.deallocate (ptr, stream);
28
+ }
29
+
30
+ static bool try_allocate (void ** ptr, size_t size,
31
+ cudaStream_t stream = cudaStreamDefault) {
32
+ return mgr_.try_allocate (ptr, size, stream);
33
+ }
34
+
35
+ enum Mode {
36
+ CUDA_MALLOC, // Straight CUDA malloc/free (may be expensive)
37
+ CUB_ALLOCATOR // CUB caching allocator
35
38
};
36
39
37
- #ifndef CPU_ONLY
38
- class Buffer {
39
- public:
40
- // Construction/destruction
41
- Buffer () :
42
- ptr_ (NULL ), stream_(), size_(0 ) {
40
+ // Scope initializes global Memory Manager for a given scope.
41
+ // It's instantiated in test(), train() and time() Caffe brewing functions
42
+ // as well as in unit tests main().
43
+ struct Scope {
44
+ Scope (const std::vector<int >& gpus, Mode m = CUB_ALLOCATOR,
45
+ bool debug = false ) {
46
+ mgr_.init (gpus, m, debug);
43
47
}
44
- Buffer (size_t size, cudaStream_t s = cudaStreamDefault) :
45
- stream_ (s) {
48
+ };
49
+
50
+ // Workspace's release() functionality depends on global pool availability
51
+ // If pool is available, it returns memory to the pool and sets ptr to NULL
52
+ // If pool is not available, it retains memory.
53
+ struct Workspace {
54
+ Workspace () : ptr_(NULL ), stream_(), size_(0 ) {}
55
+ Workspace (size_t size, cudaStream_t s = cudaStreamDefault) : stream_(s) {
46
56
reserve (size);
47
57
}
48
- ~Buffer () {
49
- GPUMemoryManager::deallocate (ptr_, stream_);
50
- }
58
+ ~Workspace () { mgr_.deallocate (ptr_, stream_); }
51
59
52
- // Accessors
53
- void * data () const {
54
- return ptr_;
55
- }
56
- size_t size () const {
57
- return size_;
58
- }
60
+ void * data () const { return ptr_; }
61
+ size_t size () const { return size_; }
59
62
60
- // Memory allocation/release
61
63
bool try_reserve (size_t size) {
62
64
bool status = true ;
63
65
if (size > size_) {
64
66
if (ptr_) {
65
- GPUMemoryManager:: deallocate (ptr_, stream_);
67
+ mgr_. deallocate (ptr_, stream_);
66
68
}
67
- status = GPUMemoryManager:: try_allocate (&ptr_, size, stream_);
69
+ status = mgr_. try_allocate (&ptr_, size, stream_);
68
70
if (status) {
69
71
size_ = size;
70
72
}
71
73
}
72
74
return status;
73
75
}
74
76
75
- void reserve (size_t size) {
76
- CHECK (try_reserve (size));
77
- }
77
+ void reserve (size_t size) { CHECK (try_reserve (size)); }
78
78
79
- /*
80
- * This method behaves differently depending on pool availability:
81
- * If pool is available, it returns memory to the pool and sets ptr to NULL
82
- * If pool is not available, it does nothing (retaining memory)
83
- */
84
79
void release () {
85
- if (GPUMemoryManager:: using_pool ()) {
86
- GPUMemoryManager:: deallocate (ptr_, stream_);
80
+ if (mgr_. using_pool ()) {
81
+ mgr_. deallocate (ptr_, stream_);
87
82
ptr_ = NULL ;
88
83
size_ = 0 ;
89
84
}
@@ -95,44 +90,46 @@ class GPUMemoryManager {
95
90
cudaStream_t stream_;
96
91
size_t size_;
97
92
};
98
- static void update_dev_info (int device);
99
- #endif // CPU_ONLY
100
93
101
94
private:
102
- static void init (const std::vector<int >&, PoolMode, bool );
103
- static void destroy ();
95
+ struct Manager {
96
+ Manager ();
97
+ ~Manager ();
98
+ void GetInfo (size_t * free_mem, size_t * used_mem);
99
+ void deallocate (void * ptr, cudaStream_t stream);
100
+ bool try_allocate (void ** ptr, size_t size, cudaStream_t);
101
+ const char * pool_name () const ;
102
+ bool using_pool () const { return mode_ != CUDA_MALLOC; }
103
+ void init (const std::vector<int >&, Mode, bool );
104
+
105
+ Mode mode_;
106
+ bool debug_;
104
107
105
- static bool initialized_;
106
- static PoolMode mode_;
107
- static bool debug_;
108
-
109
- #ifndef CPU_ONLY
110
- struct MemInfo {
111
- MemInfo () {
112
- free_ = total_ = flush_count_ = 0 ;
113
- }
114
- size_t free_;
115
- size_t total_;
116
- unsigned flush_count_;
108
+ private:
109
+ struct DevInfo {
110
+ DevInfo () {
111
+ free_ = total_ = flush_count_ = 0 ;
112
+ }
113
+ size_t free_;
114
+ size_t total_;
115
+ unsigned flush_count_;
116
+ };
117
+ void update_dev_info (int device);
118
+ vector<DevInfo> dev_info_;
119
+ bool initialized_;
120
+ cub::CachingDeviceAllocator* cub_allocator_;
121
+
122
+ static unsigned int BIN_GROWTH; // /< Geometric growth factor for bin-sizes
123
+ static unsigned int MIN_BIN; // /< Minimum bin
124
+ static unsigned int MAX_BIN; // /< Maximum bin
125
+ static size_t MAX_CACHED_BYTES; // /< Maximum aggregate cached bytes
117
126
};
118
- static vector<MemInfo> dev_info_;
119
-
120
- public:
121
- typedef void * pointer;
122
- static bool try_allocate (pointer* ptr, size_t size, cudaStream_t stream =
123
- cudaStreamDefault);
124
- static void allocate (pointer* ptr, size_t size, cudaStream_t stream =
125
- cudaStreamDefault) {
126
- CHECK (try_allocate (ptr, size, stream));
127
- }
128
- static void deallocate (pointer ptr, cudaStream_t = cudaStreamDefault);
129
- static void GetInfo (size_t * free_mem, size_t * used_mem);
130
127
131
- private:
132
- static void InitMemory (const std::vector<int >& gpus, PoolMode m);
133
- #endif
128
+ static Manager mgr_;
134
129
};
135
130
136
131
} // namespace caffe
137
132
138
133
#endif
134
+
135
+ #endif
0 commit comments