Virtual Testbed
Ship dynamics simulator for extreme conditions
pipeline.hh
1 #ifndef VTESTBED_OPENCL_PIPELINE_HH
2 #define VTESTBED_OPENCL_PIPELINE_HH
3 
4 #include <vector>
5 
6 #include <blitz/array.h>
7 
8 #include <openclx/array_view>
9 #include <openclx/buffer>
10 #include <openclx/command_queue_flags>
11 #include <openclx/command_stack>
12 #include <openclx/kernel>
13 #include <openclx/memory_flags>
14 
15 namespace vtb {
16 
17  namespace opencl {
18 
19  void
20  resize(
21  const clx::context& context,
22  clx::buffer& buffer,
23  size_t size_in_bytes,
24  clx::memory_flags flags=clx::memory_flags::read_write
25  );
26 
27  template <class T>
28  inline void
29  resize(
30  const clx::context& context,
31  clx::buffer& buffer,
32  size_t size,
33  clx::memory_flags flags=clx::memory_flags::read_write
34  ) { ::vtb::opencl::resize(context, buffer, size*sizeof(T), flags); }
35 
36  template <class T>
37  class Buffer: public clx::buffer {
38 
39  public:
40  using clx::buffer::buffer;
41 
42  inline void resize(clx::context ctx, size_t size,
43  clx::memory_flags flags=clx::memory_flags::read_write) {
44  ::vtb::opencl::resize(ctx, *this, size*sizeof(T), flags);
45  }
46  };
47 
48  class NDRange: public clx::range {
49 
50  public:
51 
52  inline explicit NDRange(): clx::range{} {}
53  inline explicit NDRange(size_t rhs): clx::range{rhs} {}
54 
55  inline explicit
56  NDRange(const blitz::TinyVector<size_t,1>& rhs):
57  clx::range{rhs(0)} {}
58 
59  inline explicit
60  NDRange(const blitz::TinyVector<size_t,2>& rhs):
61  clx::range{rhs(0),rhs(1)} {}
62 
63  inline explicit
64  NDRange(const blitz::TinyVector<size_t,3>& rhs):
65  clx::range{rhs(0),rhs(1),rhs(2)} {}
66 
67  };
68 
69  class Pipeline: public clx::command_stack {
70 
71  private:
72  clx::context _context;
73 
74  public:
75  using clx::command_stack::command_stack;
76  using clx::command_stack::copy;
77  using clx::command_stack::kernel;
78 
79  inline void context(clx::context rhs) { this->_context = rhs; }
80  inline const clx::context& context() const { return this->_context; }
81 
82  template <class T, int N>
83  inline void allocate(const blitz::Array<T,N>& src, Buffer<T>& dst) {
84  const auto& tmp = src.isStorageContiguous() ? src : src.copy();
85  if (!tmp.isStorageContiguous()) {
86  throw std::logic_error("array is not contiguous");
87  }
88  dst.resize(context(), src.size());
89  }
90 
91  template <class T, int N>
92  inline void copy(const blitz::Array<T,N>& src, Buffer<T>& dst) {
93  this->allocate<T,N>(src, dst);
94  this->copy(src.data(), dst.slice(0, src.size()*sizeof(T)));
95  }
96 
97  template <class T, int N>
98  inline void copy(const Buffer<T>& src, blitz::Array<T,N>& dst) {
99  if (!dst.isStorageContiguous()) {
100  throw std::logic_error("array is not contiguous");
101  }
102  this->copy(src.slice(0,dst.size()*sizeof(T)), dst.data());
103  }
104 
105  template <class T>
106  inline void allocate(const std::vector<T>& src, Buffer<T>& dst) {
107  dst.resize(context(), src.size());
108  }
109 
110  template <class T>
111  inline void copy(const std::vector<T>& src, Buffer<T>& dst) {
112  const auto size = src.size();
113  dst.resize(context(), size);
114  this->copy(src.data(), dst.slice(0, size*sizeof(T)));
115  }
116 
117  template <class T>
118  inline void copy(const Buffer<T>& src, std::vector<T>& dst) {
119  this->copy(src.slice(0,dst.size()*sizeof(T)), dst.data());
120  }
121 
122  inline void copy(const void* src, size_t size, clx::buffer& dst) {
123  ::vtb::opencl::resize(context(), dst, size);
124  this->copy(src, dst.slice(0, size));
125  }
126 
127  template <class T>
128  inline void allocate(size_t size, Buffer<T>& dst) {
129  dst.resize(context(), size);
130  }
131 
132  template <class T, int N>
133  inline void allocate(const blitz::TinyVector<int,N>& shape, Buffer<T>& dst) {
134  dst.resize(context(), product(shape));
135  }
136 
137  template <int N>
138  inline void
139  kernel(const clx::kernel& k, const blitz::TinyVector<int,N>& global) {
140  this->kernel(k, NDRange{global});
141  }
142 
143  template <int N>
144  inline void
145  kernel(
146  const clx::kernel& k,
147  const blitz::TinyVector<int,N>& global,
148  const blitz::TinyVector<int,N>& local
149  ) {
150  this->kernel(k, NDRange{global}, NDRange{local});
151  }
152 
153  template <int N>
154  inline void
155  kernel(
156  const clx::kernel& k,
157  const blitz::TinyVector<int,N>& offset,
158  const blitz::TinyVector<int,N>& global,
159  const blitz::TinyVector<int,N>& local
160  ) {
161  this->kernel(k, NDRange{offset}, NDRange{global}, NDRange{local});
162  }
163 
164  };
165 
166  }
167 
168 }
169 
170 #endif // vim:filetype=cpp
Main namespace.
Definition: convert.hh:9