18 #if !defined( BOLT_CL_CONSTANT_ITERATOR_H )
19 #define BOLT_CL_CONSTANT_ITERATOR_H
22 #include <boost/iterator/iterator_facade.hpp>
39 template<
typename value_type >
40 class constant_iterator:
public boost::iterator_facade< constant_iterator< value_type >, value_type,
41 constant_iterator_tag, value_type, int >
44 typedef typename boost::iterator_facade< constant_iterator< value_type >, value_type,
55 m_constValue( init ), m_Index( 0 )
57 const ::cl::CommandQueue& m_commQueue = ctl.getCommandQueue( );
60 cl_int l_Error = CL_SUCCESS;
61 ::cl::Context l_Context = m_commQueue.getInfo< CL_QUEUE_CONTEXT >( &l_Error );
62 V_OPENCL( l_Error,
"device_vector failed to query for the context of the ::cl::CommandQueue object" );
64 m_devMemory = ::cl::Buffer( l_Context, CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
65 1 *
sizeof( value_type ),const_cast<value_type *>(&m_constValue) );
69 template<
typename OtherType >
71 m_Index( rhs.m_Index ), m_constValue( rhs.m_constValue )
75 constant_iterator< value_type >& operator= (
const constant_iterator< value_type >& rhs )
80 m_devMemory = rhs.m_devMemory;
81 m_constValue = rhs.m_constValue;
82 m_Index = rhs.m_Index;
86 constant_iterator< value_type >& operator+= (
const difference_type & n )
92 const constant_iterator< value_type > operator+ (
const difference_type & n )
const
94 constant_iterator< value_type > result( *
this );
99 const ::cl::Buffer& getBuffer( )
const
104 const constant_iterator< value_type > & getContainer( )
const
109 Payload gpuPayload( )
const
111 Payload payload = { m_constValue };
115 const difference_type gpuPayloadSize( )
const
117 return sizeof( Payload );
120 difference_type distance_to(
const constant_iterator< value_type >& rhs )
const
123 return rhs.m_Index - m_Index;
127 difference_type m_Index;
131 friend class boost::iterator_core_access;
134 template <
typename >
friend class constant_iterator;
137 void advance( difference_type n )
152 template<
typename OtherType >
153 bool equal(
const constant_iterator< OtherType >& rhs )
const
155 bool sameIndex = (rhs.m_constValue == m_constValue) && (rhs.m_Index == m_Index);
160 typename boost::iterator_facade< constant_iterator< value_type >, value_type,
161 constant_iterator_tag, value_type,
int >::reference dereference( )
const
166 ::cl::Buffer m_devMemory;
167 value_type m_constValue;
172 static std::string deviceConstantIterator = STRINGIFY_CODE(
173 namespace bolt {
namespace cl { \n
174 template<
typename T > \n
175 class constant_iterator \n
178 typedef int iterator_category;
179 typedef T value_type; \n
180 typedef size_t difference_type; \n
181 typedef size_t size_type; \n
182 typedef T* pointer; \n
183 typedef T& reference; \n
185 constant_iterator( value_type init ): m_constValue( init ), m_Ptr( 0 ) \n
188 void init( global value_type* ptr ) \n
191 value_type operator[]( size_type threadID ) const \n
193 return m_constValue; \n
196 value_type operator*( ) const \n
198 return m_constValue; \n
201 value_type m_constValue; \n
206 template<
typename Type >
207 constant_iterator< Type > make_constant_iterator( Type constValue )
209 constant_iterator< Type > tmp( constValue );