HCC
HCC is a single-source, C/C++ compiler for heterogeneous computing. It's optimized with HSA (http://www.hsafoundation.com/).
pinned_vector.hpp
1 #pragma once
2 
3 #ifndef _PINNED_VECTOR_H
4 #define _PINNED_VECTOR_H
5 
6 #include <new>
7 #include "hc.hpp"
8 #include "hc_am.hpp"
9 
10 namespace hc
11 {
12 
13 // minimal allocator that uses am_alloc to allocate pinned memory on the host,
14 // with comparison functions used by the C++ standard library
15 
16 template <class T>
17 struct am_allocator {
18  typedef T value_type;
19 
20  am_allocator() = default;
21 
22  template <class U> am_allocator(const am_allocator<U>&) {}
23 
24  T* allocate(std::size_t n) {
25  hc::accelerator acc;
26  auto p = static_cast<T*>(hc::am_alloc(n*sizeof(T), acc, amHostPinned));
27  if(p == nullptr){ throw std::bad_alloc(); }
28  return p;
29  }
30 
31  void deallocate(T* p, std::size_t) {
32  // am_free returns an am_status_t; we can't return that, since
33  // allocate is a void function, and we can't throw an exception either,
34  // since deallocate is used in destructors. Hmmm.
35  hc::am_free(p);
36  }
37 };
38 
39 template <class T, class U>
40 bool operator==(const am_allocator<T>&, const am_allocator<U>&) { return true; }
41 
42 template <class T, class U>
43 bool operator!=(const am_allocator<T>&, const am_allocator<U>&) { return false; }
44 
45 
46 // convenience alias
47 template<typename T>
48 using pinned_vector = std::vector<T, am_allocator<T>>;
49 
50 } // namespace hc
51 
52 #endif // _PINNED_VECTOR_H
Heterogeneous C++ (HC) API.
auto_voidp am_alloc(std::size_t size, hc::accelerator &acc, unsigned flags)
Allocate a block of size bytes of memory on the specified acc.
Definition: pinned_vector.hpp:17
am_status_t am_free(void *ptr)
Free a block of memory previously allocated with am_alloc.
Heterogeneous C++ (HC) namespace.
Definition: grid_launch.h:10
Represents a physical accelerated computing device.
Definition: hc.hpp:700