28 #ifndef REMORA_GPU_VECTOR_HPP 29 #define REMORA_GPU_VECTOR_HPP 31 #include "../detail/traits.hpp" 32 #include "../detail/vector_proxy_classes.hpp" 33 #include "../assignment.hpp" 34 #include <boost/compute/container/vector.hpp> 35 #include <boost/compute/algorithm/fill.hpp> 40 template<
class Arg,
class T>
41 struct induced_vector_element{
42 typedef T result_type;
45 boost::compute::buffer
const& buffer;
48 template<
class Arg,
class T>
49 boost::compute::detail::meta_kernel& operator<< (
50 boost::compute::detail::meta_kernel& k,
51 induced_vector_element<Arg, T>
const& e
53 return k << k.get_buffer_identifier<T>(e.buffer, boost::compute::memory_object::global_memory)
54 <<
'['<<e.arg <<
'*'<<e.stride<<
']';
65 class vector<T, gpu_tag>:
public vector_container<vector<T, gpu_tag>, gpu_tag > {
68 typedef value_type const_reference;
69 typedef value_type reference;
70 typedef std::size_t size_type;
72 typedef vector_reference<vector const> const_closure_type;
73 typedef vector_reference<vector> closure_type;
74 typedef gpu::dense_vector_storage<T,continuous_dense_tag> storage_type;
75 typedef gpu::dense_vector_storage<T,continuous_dense_tag> const_storage_type;
76 typedef elementwise<dense_tag> evaluation_category;
84 vector(boost::compute::command_queue& queue = boost::compute::system::default_queue())
85 :m_storage(queue.get_context()), m_queue(&queue){}
91 explicit vector(size_type size, boost::compute::command_queue& queue = boost::compute::system::default_queue())
92 : m_storage(size,queue.get_context()), m_queue(&queue){}
98 vector(size_type size, value_type
const& init, boost::compute::command_queue& queue = boost::compute::system::default_queue())
99 : m_storage(size, init, queue), m_queue(&queue){}
104 : m_storage(
std::move(v.m_storage))
105 , m_queue(&v.queue()){}
109 vector(vector
const& v) =
default;
114 vector(vector_expression<E, gpu_tag>
const& e)
115 : m_storage(e().size(), e().queue().get_context())
116 , m_queue(&e().queue()){
124 vector(vector_expression<E, gpu_tag>
const& e, boost::compute::command_queue& queue)
125 : m_storage(e().size(), queue.get_context())
131 template <
class IndexExpr>
132 detail::induced_vector_element<IndexExpr,T> operator()(IndexExpr
const& i)
const {
133 return {i,1,m_storage.get_buffer()};
144 vector& operator = (vector
const& v){
152 vector& operator = (vector && v){
153 m_storage = std::move(v.m_storage);
163 vector& operator = (vector_container<C, gpu_tag>
const& v) {
165 return assign(*
this, v);
173 vector& operator = (vector_expression<E, gpu_tag>
const& e) {
174 vector temporary(e,queue());
175 swap(*
this,temporary);
184 size_type size()
const {
185 return m_storage.size();
188 boost::compute::command_queue& queue()
const{
192 const_storage_type raw_storage()
const{
193 return {m_storage.get_buffer(),0,1};
197 storage_type raw_storage(){
198 return {m_storage.get_buffer(),0,1};
206 void resize(size_type size) {
207 if(size < m_storage.size())
208 m_storage.resize(size);
210 m_storage = boost::compute::vector<T>(size, queue().get_context());
219 void resize(size_type size, value_type init) {
221 boost::compute::fill(m_storage.begin(),m_storage.end(), init);
225 boost::compute::fill(m_storage.begin(),m_storage.end(), value_type());
229 return m_storage.empty();
235 friend void swap(vector& v1, vector& v2) {
237 swap(v1.m_storage,v2.m_storage);
242 typedef typename boost::compute::vector<T>::iterator iterator;
243 typedef typename boost::compute::vector<T>::const_iterator const_iterator;
246 const_iterator cbegin()
const {
247 return m_storage.begin();
251 const_iterator cend()
const {
252 return m_storage.end();
256 const_iterator begin()
const {
261 const_iterator end()
const {
267 return m_storage.begin();
272 return m_storage.end();
276 boost::compute::vector<T> m_storage;
277 boost::compute::command_queue* m_queue;