HCC
HCC is a single-source, C/C++ compiler for heterogeneous computing. It's optimized with HSA (http://www.hsafoundation.com/).
kalmar_serialize.h
1 #pragma once
2 
3 #include <set>
4 #include "kalmar_runtime.h"
5 #include "kalmar_exception.h"
6 
8 namespace Kalmar
9 {
10 
12 class FunctorBufferWalker {
13 public:
14  virtual void Append(size_t sz, const void* s) {}
15  virtual void AppendPtr(size_t sz, const void* s) {}
16  virtual void visit_buffer(struct rw_info* rw, bool modify, bool isArray) = 0;
17 };
18 
20 class Serialize {
21  FunctorBufferWalker* vis;
22 public:
23  Serialize(FunctorBufferWalker* vis) : vis(vis) {}
24  void Append(size_t sz, const void* s) { vis->Append(sz, s); }
25  void AppendPtr(size_t sz, const void* s) { vis->AppendPtr(sz, s); }
26  void visit_buffer(struct rw_info* rw, bool modify, bool isArray) {
27  vis->visit_buffer(rw, modify, isArray);
28  }
29 };
30 
33 class CPUVisitor : public FunctorBufferWalker
34 {
35  std::shared_ptr<KalmarQueue> pQueue;
36  std::set<struct rw_info*> bufs;
37 public:
38  CPUVisitor(std::shared_ptr<KalmarQueue> pQueue) : pQueue(pQueue) {}
39  void visit_buffer(struct rw_info* rw, bool modify, bool isArray) override {
40  if (isArray) {
41  auto curr = pQueue->getDev()->get_path();
42  auto path = rw->master->getDev()->get_path();
43  if (path == L"cpu") {
44  auto asoc = rw->stage->getDev()->get_path();
45  if (asoc == L"cpu" || path != curr)
46  throw runtime_exception(__errorMsg_UnsupportedAccelerator, E_FAIL);
47  }
48  }
49  rw->sync(pQueue, modify, false);
50  if (bufs.find(rw) == std::end(bufs)) {
51  void*& device = rw->devs[pQueue->getDev()].data;
52  void*& data = rw->data;
53  bufs.insert(rw);
54  std::swap(device, data);
55  }
56  }
57 };
58 
60 class BufferArgumentsAppender : public FunctorBufferWalker
61 {
62  std::shared_ptr<KalmarQueue> pQueue;
63  void* k_;
64  int current_idx_;
65 public:
66  BufferArgumentsAppender(std::shared_ptr<KalmarQueue> pQueue, void* k)
67  : pQueue(pQueue), k_(k), current_idx_(0) {}
68  void Append(size_t sz, const void *s) override {
69  CLAMP::PushArg(k_, current_idx_++, sz, s);
70  }
71  void AppendPtr(size_t sz, const void *s) override {
72  CLAMP::PushArgPtr(k_, current_idx_++, sz, s);
73  }
74  void visit_buffer(struct rw_info* rw, bool modify, bool isArray) override {
75  if (isArray) {
76  auto curr = pQueue->getDev()->get_path();
77  auto path = rw->master->getDev()->get_path();
78  if (path == L"cpu") {
79  auto asoc = rw->stage->getDev()->get_path();
80  if (asoc == L"cpu" || path != curr)
81  throw runtime_exception(__errorMsg_UnsupportedAccelerator, E_FAIL);
82  }
83  }
84  rw->sync(pQueue, modify, false);
85  pQueue->Push(k_, current_idx_++, rw->devs[pQueue->getDev()].data, modify);
86  }
87 };
88 
95 class QueueSearcher : public FunctorBufferWalker
96 {
97  std::shared_ptr<KalmarQueue> pQueue;
98 public:
99  QueueSearcher() = default;
100  void visit_buffer(struct rw_info* rw, bool modify, bool isArray) override {
101  if (isArray && !pQueue) {
102  if (rw->master->getDev()->get_path() != L"cpu")
103  pQueue = rw->master;
104  else if (rw->stage->getDev()->get_path() != L"cpu")
105  pQueue = rw->stage;
106  }
107  }
108  std::shared_ptr<KalmarQueue> get_que() const { return pQueue; }
109 };
110 
111 } // namespace Kalmar
namespace for internal classes of Kalmar compiler / runtime
Definition: hc.hpp:42