Unified Memory Group Allocator
InPageAllocator.h
Go to the documentation of this file.
1 
4 #include <cmath>
5 #include <functional>
6 #include <mutex>
7 #include <vector>
8 #include <list>
9 #include <utility>
10 #include "ListAllocator.h"
11 #include "Helper.h"
12 #include <cuda_runtime.h>
13 
14 #pragma once
15 
16 namespace groupallocator {
17 
22  public:
23  InPageAllocator() = delete;
24 
28  InPageAllocator(std::size_t page_size)
29  : PAGE_SIZE(page_size) {
30  gpuAssert(cudaMallocManaged((void **) &mem, PAGE_SIZE), __FILE__, __LINE__);
31  l = ListAllocator(mem, PAGE_SIZE);
32  }
33 
37  ~InPageAllocator() { gpuAssert(cudaFree(mem), __FILE__, __LINE__); }
38 
44  template<class T>
45  void allocate(T **ptr, size_t s, bool forceAligned128) {
46 #ifdef DEBUGALLOC
47  std::clog << "Allocating in IPA " << __FILE__ << ":" << __LINE__ << std::endl;
48 #endif
49  m.lock();
50  l.alloc(ptr, s, forceAligned128);
51  m.unlock();
52  }
53 
54  template<class T>
55  void free(T *ptr) {
56  m.lock();
57  l.free(ptr);
58  m.unlock();
59  }
60 
61  bool contains(size_t ptr) {
62  return ptr >= (size_t) mem && ptr < (size_t) mem + PAGE_SIZE;
63  }
64 
65  void moveToDevice(int device, cudaStream_t stream) {
66  #ifndef DISABLE_PREFETCH
67  gpuAssert(cudaMemPrefetchAsync(mem, PAGE_SIZE, device, stream), __FILE__, __LINE__);
68  #endif
69  }
70 
71  size_t getPages() { return 1; }
72 
73  size_t getPageSize() { return PAGE_SIZE; }
74 
75  private:
76  char *mem;
77  ListAllocator l;
78  std::mutex m;
79  const size_t PAGE_SIZE;
80  };
81 
82 } // namespace groupallocator
groupallocator::gpuAssert
void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
Definition: Helper.h:18
groupallocator::ListAllocator::alloc
void alloc(T **p, size_t s, bool forceAligned128)
allocates data in a free area or sets p to nullptr
Definition: ListAllocator.h:40
groupallocator::InPageAllocator::allocate
void allocate(T **ptr, size_t s, bool forceAligned128)
Definition: InPageAllocator.h:45
groupallocator::InPageAllocator::~InPageAllocator
~InPageAllocator()
Definition: InPageAllocator.h:37
groupallocator::ListAllocator::free
void free(T *p)
right now there is no compaction
Definition: ListAllocator.h:67
Helper.h
groupallocator::InPageAllocator::InPageAllocator
InPageAllocator(std::size_t page_size)
Definition: InPageAllocator.h:28
groupallocator::ListAllocator
not thread safe and no compaction
Definition: ListAllocator.h:21
groupallocator::InPageAllocator
Definition: InPageAllocator.h:21
ListAllocator.h