18 #if !defined( BOLT_CL_COUNTING_ITERATOR_H )
19 #define BOLT_CL_COUNTING_ITERATOR_H
22 #include <boost/iterator/iterator_facade.hpp>
38 template<
typename value_type >
39 class counting_iterator:
public boost::iterator_facade< counting_iterator< value_type >, value_type,
40 counting_iterator_tag, value_type, int >
44 typedef typename boost::iterator_facade< counting_iterator< value_type >, value_type,
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 ), &m_initValue );
69 template<
typename OtherType >
71 m_Index( rhs.m_Index ), m_initValue( rhs.m_initValue )
75 counting_iterator< value_type >& operator= (
const counting_iterator< value_type >& rhs )
80 m_devMemory = rhs.m_devMemory;
81 m_initValue = rhs.m_initValue;
82 m_Index = rhs.m_Index;
86 counting_iterator< value_type >& operator+= (
const difference_type & n )
92 const counting_iterator< value_type > operator+ (
const difference_type & n )
const
94 counting_iterator< value_type > result( *
this );
99 const ::cl::Buffer& getBuffer( )
const
104 const counting_iterator< value_type > & getContainer( )
const
109 Payload gpuPayload( )
const
111 Payload payload = { m_initValue };
115 const difference_type gpuPayloadSize( )
const
117 return sizeof( Payload );
121 difference_type distance_to(
const counting_iterator< value_type >& rhs )
const
124 return rhs.m_Index - m_Index;
128 difference_type m_Index;
132 friend class boost::iterator_core_access;
135 template <
typename >
friend class counting_iterator;
138 void advance(difference_type n )
153 template<
typename OtherType >
154 bool equal(
const counting_iterator< OtherType >& rhs )
const
156 bool sameIndex = (rhs.m_initValue == m_initValue) && (rhs.m_Index == m_Index);
161 typename boost::iterator_facade< counting_iterator< value_type >, value_type,
162 counting_iterator_tag, value_type,
int >::reference dereference( )
const
164 return m_initValue + m_Index;
167 ::cl::Buffer m_devMemory;
168 value_type m_initValue;
173 static std::string deviceCountingIterator = STRINGIFY_CODE(
175 namespace bolt {
namespace cl { \n
176 template<
typename T > \n
177 class counting_iterator \n
180 typedef int iterator_category;
181 typedef T value_type; \n
182 typedef size_t difference_type; \n
183 typedef size_t size_type; \n
184 typedef T* pointer; \n
185 typedef T& reference; \n
187 counting_iterator( value_type init ): m_StartIndex( init ), m_Ptr( 0 ) \n
190 void init( global value_type* ptr ) \n
196 value_type operator[]( size_type threadID ) const \n
198 return m_StartIndex + threadID; \n
201 value_type operator*( ) const \n
203 return m_StartIndex + threadID; \n
206 value_type m_StartIndex; \n
212 template<
typename Type >
213 counting_iterator< Type > make_counting_iterator( Type constValue )
215 counting_iterator< Type > tmp( constValue );