HCC
HCC is a single-source, C/C++ compiler for heterogeneous computing. It's optimized with HSA (http://www.hsafoundation.com/).
kalmar_buffer.h
1 //===----------------------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #pragma once
9 
10 #include "kalmar_runtime.h"
11 #include "kalmar_serialize.h"
12 
14 namespace Kalmar {
15 
16 // Dummy interface that looks somewhat like std::shared_ptr<T>
17 template <typename T>
18 class _data {
19 public:
20  _data() = delete;
21  _data(int count) : p_(nullptr) {}
22  _data(const _data& d) restrict(cpu, amp)
23  : p_(d.p_) {}
24  _data(int count, void* d) restrict(cpu, amp)
25  : p_(static_cast<T*>(d)) {}
26  template <typename U>
27  _data(const _data<U>& d) restrict(cpu, amp)
28  : p_(reinterpret_cast<T *>(d.get())) {}
29  __attribute__((annotate("user_deserialize")))
30  explicit _data(T* t) restrict(cpu, amp) { p_ = t; }
31  T* get(void) const restrict(cpu, amp) { return p_; }
32  T* get_device_pointer() const restrict(cpu, amp) { return p_; }
33  std::shared_ptr<KalmarQueue> get_av() const { return nullptr; }
34  void reset() const {}
35 
36  T* map_ptr(bool modify, size_t count, size_t offset) const { return nullptr; }
37  void unmap_ptr(const void* addr, bool modify, size_t count, size_t offset) const {}
38  void synchronize(bool modify = false) const {}
39  void get_cpu_access(bool modify = false) const {}
40  void copy(_data<T> other, int, int, int) const {}
41  void write(const T*, int , int offset = 0, bool blocking = false) const {}
42  void read(T*, int , int offset = 0) const {}
43  void refresh() const {}
44  void set_const() const {}
45  access_type get_access() const { return access_type_auto; }
46  std::shared_ptr<KalmarQueue> get_stage() const { return nullptr; }
47 
48 private:
49  T* p_;
50 };
51 
52 template <typename T>
53 class _data_host {
54  mutable std::shared_ptr<rw_info> mm;
55  bool isArray;
56  template <typename U> friend class _data_host;
57 public:
58  _data_host(size_t count, const void* src = nullptr)
59  : mm(std::make_shared<rw_info>(count*sizeof(T), const_cast<void*>(src))),
60  isArray(false) {}
61 
62  _data_host(std::shared_ptr<KalmarQueue> av, std::shared_ptr<KalmarQueue> stage, int count,
63  access_type mode)
64  : mm(std::make_shared<rw_info>(av, stage, count*sizeof(T), mode)), isArray(true) {}
65 
66  _data_host(std::shared_ptr<KalmarQueue> av, std::shared_ptr<KalmarQueue> stage, int count,
67  void* device_pointer, access_type mode)
68  : mm(std::make_shared<rw_info>(av, stage, count*sizeof(T), device_pointer, mode)), isArray(true) {}
69 
70  _data_host(const _data_host& other) : mm(other.mm), isArray(false) {}
71 
72  template <typename U>
73  _data_host(const _data_host<U>& other) : mm(other.mm), isArray(false) {}
74 
75  T *get() const { return static_cast<T*>(mm->data); }
76  T* get_device_pointer() const { return static_cast<T*>(mm->get_device_pointer()); }
77  void synchronize(bool modify = false) const { mm->synchronize(modify); }
78  void discard() const { mm->disc(); }
79  void refresh() const {}
80  size_t size() const { return mm->count; }
81  void reset() const { mm.reset(); }
82  void get_cpu_access(bool modify = false) const { mm->get_cpu_access(modify); }
83  std::shared_ptr<KalmarQueue> get_av() const { return mm->master; }
84  std::shared_ptr<KalmarQueue> get_stage() const { return mm->stage; }
85  access_type get_access() const { return mm->mode; }
86  void copy(_data_host<T> other, int src_offset, int dst_offset, int size) const {
87  mm->copy(other.mm.get(), src_offset * sizeof(T), dst_offset * sizeof(T), size * sizeof(T));
88  }
89  void write(const T* src, int size, int offset = 0, bool blocking = false) const {
90  mm->write(src, size * sizeof(T), offset * sizeof(T), blocking);
91  }
92  void read(T* dst, int size, int offset = 0) const {
93  mm->read(dst, size * sizeof(T), offset * sizeof(T));
94  }
95  T* map_ptr(bool modify, size_t count, size_t offset) const {
96  return (T*)mm->map(count * sizeof(T), offset * sizeof(T), modify);
97  }
98  void unmap_ptr(const void* addr, bool modify, size_t count, size_t offset) const { return mm->unmap(const_cast<void*>(addr), count * sizeof(T), offset * sizeof(T), modify); }
99  void sync_to(std::shared_ptr<KalmarQueue> pQueue) const { mm->sync(pQueue, false); }
100 
101  __attribute__((annotate("serialize")))
102  void __cxxamp_serialize(Serialize& s) const {
103  s.visit_buffer(mm.get(), !std::is_const<T>::value, isArray);
104  }
105  __attribute__((annotate("user_deserialize")))
106  explicit _data_host(typename std::remove_const<T>::type* t) {}
107 };
108 
109 } // namespace Kalmar
void copy(const array_view< const T, N > &src, const array_view< T, N > &dest)
The contents of "src" are copied into "dest".
Definition: amp.h:4979
STL namespace.
namespace for internal classes of Kalmar compiler / runtime
Definition: hc.hpp:42