NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mgpualloc.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "util/util.h"
5 #include <cuda.h>
6 
7 namespace mgpu {
8 
9 class CudaDevice;
10 
13 
15 // Customizable allocator.
16 
17 // CudaAlloc is the interface class all allocator accesses. Users may derive
18 // this, implement custom allocators, and set it to the device with
19 // CudaDevice::SetAllocator.
20 
21 class CudaAlloc : public CudaBase {
22 public:
23  virtual cudaError_t Malloc(size_t size, void** p) = 0;
24  virtual bool Free(void* p) = 0;
25  virtual void Clear() = 0;
26 
27  virtual ~CudaAlloc() { }
28 
29  CudaDevice& Device() { return _device; }
30 
31 protected:
32  CudaAlloc(CudaDevice& device) : _device(device) { }
34 };
35 
36 // A concrete class allocator that simply calls cudaMalloc and cudaFree.
37 class CudaAllocSimple : public CudaAlloc {
38 public:
39  CudaAllocSimple(CudaDevice& device) : CudaAlloc(device) { }
40 
41  virtual cudaError_t Malloc(size_t size, void** p);
42  virtual bool Free(void* p);
43  virtual void Clear() { }
44  virtual ~CudaAllocSimple() { }
45 };
46 
47 // A concrete class allocator that uses exponentially-spaced buckets and an LRU
48 // to reuse allocations. This is the default allocator. It is shared between
49 // all contexts on the device.
50 class CudaAllocBuckets : public CudaAlloc {
51 public:
53  virtual ~CudaAllocBuckets();
54 
55  virtual cudaError_t Malloc(size_t size, void** p);
56  virtual bool Free(void* p);
57  virtual void Clear();
58 
59  size_t Allocated() const { return _allocated; }
60  size_t Committed() const { return _committed; }
61  size_t Capacity() const { return _capacity; }
62 
63  bool SanityCheck() const;
64 
65  void SetCapacity(size_t capacity, size_t maxObjectSize) {
66  _capacity = capacity;
67  _maxObjectSize = maxObjectSize;
68  Clear();
69  }
70 
71 private:
72  static const int NumBuckets = 84;
73  static const size_t BucketSizes[NumBuckets];
74 
75  struct MemNode;
76  typedef std::list<MemNode> MemList;
77  typedef std::map<void*, MemList::iterator> AddressMap;
78  typedef std::multimap<int, MemList::iterator> PriorityMap;
79 
80  struct MemNode {
81  AddressMap::iterator address;
82  PriorityMap::iterator priority;
83  int bucket;
84  };
85 
86  void Compact(size_t extra);
87  void FreeNode(MemList::iterator memIt);
88  int LocateBucket(size_t size) const;
89 
90  AddressMap _addressMap;
91  PriorityMap _priorityMap;
92  MemList _memLists[NumBuckets + 1];
93 
94  size_t _maxObjectSize, _capacity, _allocated, _committed;
95  int _counter;
96 };
97 
98 } // namespace mgpu