SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
malloc_allocator.h
1 
17 #ifndef MALLOC_ALLOCATOR_H
18 #define MALLOC_ALLOCATOR_H
19 
20 #include "debug.h"
21 
22 #ifdef USE_PINNED_MEMORY
23 #include <cuda.h>
24 #endif
25 
26 namespace skepu
27 {
28 
29 template <class T>
30 class malloc_allocator
31 {
32 public:
33  typedef T value_type;
34  typedef value_type* pointer;
35  typedef const value_type* const_pointer;
36  typedef value_type& reference;
37  typedef const value_type& const_reference;
38  typedef std::size_t size_type;
39  typedef std::ptrdiff_t difference_type;
40 
41 private:
42  void operator=(const malloc_allocator&);
43  static int count;
44 
45 public:
46  malloc_allocator() {}
48  ~malloc_allocator() {}
49 
50 
51 public:
52  template <class U>
54 
55  template <class U>
56  struct rebind
57  {
58  typedef malloc_allocator<U> other;
59  };
60 
61  pointer address(reference x) const
62  {
63  return &x;
64  }
65 
66  void construct(pointer p, const value_type& x)
67  {
68  new(p) value_type(x);
69  }
70 
71  void destroy(pointer p)
72  {
73  p->~value_type();
74  }
75 
76  const_pointer address(const_reference x) const
77  {
78  return &x;
79  }
80 
81  pointer allocate(size_type n, const_pointer = 0)
82  {
83  void* p=0;
84 
85 #ifdef USE_PINNED_MEMORY
86  cudaError_t err;
87  DEBUG_TEXT_LEVEL1("Pinned memory allocation ******\n")
88 // err = cudaMallocHost((void **)&p, n * sizeof(T)); // Works before CUDA 4.0
89  err = cudaHostAlloc((void **)&p, n * sizeof(T), cudaHostAllocPortable); // For CUDA 4.0
90 // err = cudaHostAlloc((void **)&p, n * sizeof(T)); // For CUDA 4.0
91 
92  if(err)
93  {
94  std::cerr<<"*** Error Malloc: "<< cudaGetErrorString(err)<<"\n";
95  throw std::bad_alloc();
96  }
97 #else
98  p = std::malloc(n * sizeof(T));
99 #endif
100 
101  if (!p)
102  {
103  std::cerr<<"*** Error Malloc **************\n";
104  throw std::bad_alloc();
105  }
106  return static_cast<pointer>(p);
107  }
108 
109  void deallocate(pointer p, size_type)
110  {
111 #ifdef USE_PINNED_MEMORY
112  cudaError_t err;
113  err = cudaFreeHost(p);
114  if(err)
115  {
116  std::cerr<<"*** Error De-Alloc: "<< cudaGetErrorString(err) <<"\n";
117  throw std::bad_alloc();
118  }
119  else
120  {
121  DEBUG_TEXT_LEVEL1("Pinned memory de-allocation Successful ******\n")
122  }
123 #else
124  std::free(p);
125 #endif
126  }
127 
128  size_type max_size() const
129  {
130  return static_cast<size_type>(-1) / sizeof(value_type);
131  }
132 };
133 
134 template <class T>
136 
137 template <class T>
138 inline bool operator==(const malloc_allocator<T>&, const malloc_allocator<T>&)
139 {
140  return true;
141 }
142 
143 template <class T>
144 inline bool operator!=(const malloc_allocator<T>&, const malloc_allocator<T>&)
145 {
146  return false;
147 }
148 
149 
150 template<>
151 class malloc_allocator<void>
152 {
153  typedef void value_type;
154  typedef void* pointer;
155  typedef const void* const_pointer;
156 
157  template <class U>
158  struct rebind
159  {
160  typedef malloc_allocator<U> other;
161  };
162 };
163 
164 }
165 
166 #endif
167 
168 
Defines a few macros that includes macros to output text when debugging. The macros use std::cerr...
A custom memory allocator used with std::vector.