18 #if !defined( BOLT_CL_COUNTING_ITERATOR_H )
19 #define BOLT_CL_COUNTING_ITERATOR_H
23 #include <boost/iterator/iterator_facade.hpp>
74 template<
typename value_type >
75 class counting_iterator:
public boost::iterator_facade< counting_iterator< value_type >, value_type,
76 counting_iterator_tag, value_type, int >
80 typedef typename boost::iterator_facade< counting_iterator< value_type >, value_type,
83 typedef std::random_access_iterator_tag memory_system;
84 typedef value_type * pointer;
96 const ::cl::CommandQueue& m_commQueue = ctl.getCommandQueue( );
99 cl_int l_Error = CL_SUCCESS;
100 ::cl::Context l_Context = m_commQueue.getInfo< CL_QUEUE_CONTEXT >( &l_Error );
101 V_OPENCL( l_Error,
"device_vector failed to query for the context of the ::cl::CommandQueue object" );
103 m_devMemory = ::cl::Buffer( l_Context, CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
104 1 *
sizeof( value_type ), &m_initValue );
108 template<
typename OtherType >
110 m_Index( rhs.m_Index ), m_initValue( rhs.m_initValue )
114 value_type* getPointer()
119 const value_type* getPointer()
const
124 counting_iterator< value_type >& operator= (
const counting_iterator< value_type >& rhs )
129 m_devMemory = rhs.m_devMemory;
130 m_initValue = rhs.m_initValue;
131 m_Index = rhs.m_Index;
135 counting_iterator< value_type >& operator+= (
const difference_type & n )
141 const counting_iterator< value_type > operator+ (
const difference_type & n )
const
143 counting_iterator< value_type > result( *
this );
148 const ::cl::Buffer& getBuffer( )
const
153 const counting_iterator< value_type > & getContainer( )
const
158 const counting_iterator< value_type > & base( )
const
163 Payload gpuPayload( )
const
165 Payload payload = { m_initValue };
169 const difference_type gpuPayloadSize( )
const
171 return sizeof( Payload );
174 int setKernelBuffers(
int arg_num, ::cl::Kernel &kernel)
const
176 const ::cl::Buffer &buffer = getContainer().getBuffer();
177 kernel.setArg(arg_num, buffer );
182 difference_type distance_to(
const counting_iterator< value_type >& rhs )
const
185 return rhs.m_Index - m_Index;
189 difference_type m_Index;
193 friend class boost::iterator_core_access;
196 template <
typename >
friend class counting_iterator;
199 void advance(difference_type n )
214 template<
typename OtherType >
215 bool equal(
const counting_iterator< OtherType >& rhs )
const
217 bool sameIndex = (rhs.m_initValue == m_initValue) && (rhs.m_Index == m_Index);
222 typename boost::iterator_facade< counting_iterator< value_type >, value_type,
223 counting_iterator_tag, value_type,
int >::reference dereference( )
const
225 return m_initValue + m_Index;
228 ::cl::Buffer m_devMemory;
229 value_type m_initValue;
234 static std::string deviceCountingIterator =
235 std::string(
"#if !defined(BOLT_CL_COUNTING_ITERATOR) \n#define BOLT_CL_COUNTING_ITERATOR \n") +
237 namespace bolt {
namespace cl { \n
238 template<
typename T > \n
239 class counting_iterator \n
242 typedef int iterator_category;
243 typedef T value_type; \n
244 typedef T base_type; \n
245 typedef size_t difference_type; \n
246 typedef size_t size_type; \n
247 typedef T* pointer; \n
248 typedef T& reference; \n
250 counting_iterator( value_type init ): m_StartIndex( init ), m_Ptr( 0 ) \n
253 void init( global value_type* ptr ) \n
259 value_type operator[]( size_type threadID ) const \n
261 return m_StartIndex + threadID; \n
264 value_type operator*( ) const \n
266 return m_StartIndex + threadID; \n
269 value_type m_StartIndex; \n
273 + std::string(
"#endif \n");
276 template<
typename Type >
277 counting_iterator< Type > make_counting_iterator( Type constValue )
279 counting_iterator< Type > tmp( constValue );