18 #if !defined( BOLT_CL_CONSTANT_ITERATOR_H )
19 #define BOLT_CL_CONSTANT_ITERATOR_H
22 #include <boost/iterator/iterator_facade.hpp>
74 template<
typename value_type >
75 class constant_iterator:
public boost::iterator_facade< constant_iterator< value_type >, value_type,
76 constant_iterator_tag, value_type, int >
79 typedef typename boost::iterator_facade< constant_iterator< value_type >, value_type,
constant_iterator_tag,
80 value_type,
int >::difference_type difference_type;
82 typedef std::random_access_iterator_tag memory_system;
83 typedef value_type * pointer;
92 m_constValue( init ), m_Index( 0 )
94 const ::cl::CommandQueue& m_commQueue = ctl.getCommandQueue( );
97 cl_int l_Error = CL_SUCCESS;
98 ::cl::Context l_Context = m_commQueue.getInfo< CL_QUEUE_CONTEXT >( &l_Error );
99 V_OPENCL( l_Error,
"device_vector failed to query for the context of the ::cl::CommandQueue object" );
101 m_devMemory = ::cl::Buffer( l_Context, CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
102 1 *
sizeof( value_type ),const_cast<value_type *>(&m_constValue) );
106 template<
typename OtherType >
108 m_Index( rhs.m_Index ), m_constValue( rhs.m_constValue )
112 constant_iterator< value_type >& operator= (
const constant_iterator< value_type >& rhs )
117 m_devMemory = rhs.m_devMemory;
118 m_constValue = rhs.m_constValue;
119 m_Index = rhs.m_Index;
123 value_type* getPointer()
125 return &m_constValue;
128 const value_type* getPointer()
const
130 return &m_constValue;
133 constant_iterator< value_type >& operator+= (
const difference_type & n )
139 const constant_iterator< value_type > operator+ (
const difference_type & n )
const
141 constant_iterator< value_type > result( *
this );
146 const ::cl::Buffer& getBuffer( )
const
151 const constant_iterator< value_type > & getContainer( )
const
156 const constant_iterator< value_type > & base( )
const
161 Payload gpuPayload( )
const
163 Payload payload = { m_constValue };
167 const difference_type gpuPayloadSize( )
const
169 return sizeof( Payload );
172 int setKernelBuffers(
int arg_num, ::cl::Kernel &kernel)
const
174 const ::cl::Buffer &buffer = getContainer().getBuffer();
175 kernel.setArg(arg_num, buffer );
180 difference_type distance_to(
const constant_iterator< value_type >& rhs )
const
183 return rhs.m_Index - m_Index;
187 difference_type m_Index;
191 friend class boost::iterator_core_access;
194 template <
typename >
friend class constant_iterator;
197 void advance( difference_type n )
212 template<
typename OtherType >
213 bool equal(
const constant_iterator< OtherType >& rhs )
const
215 bool sameIndex = (rhs.m_constValue == m_constValue) && (rhs.m_Index == m_Index);
219 typename boost::iterator_facade< constant_iterator< value_type >, value_type,
220 constant_iterator_tag, value_type,
int >::reference dereference( )
const
225 ::cl::Buffer m_devMemory;
226 value_type m_constValue;
231 static std::string deviceConstantIterator =
232 std::string(
"#if !defined(BOLT_CL_CONSTANT_ITERATOR) \n#define BOLT_CL_CONSTANT_ITERATOR \n") +
234 namespace bolt {
namespace cl { \n
235 template<
typename T > \n
236 class constant_iterator \n
239 typedef int iterator_category;
240 typedef T value_type; \n
241 typedef T base_type; \n
242 typedef size_t difference_type; \n
243 typedef size_t size_type; \n
244 typedef T* pointer; \n
245 typedef T& reference; \n
247 constant_iterator( value_type init ): m_constValue( init ), m_Ptr( 0 ) \n
250 void init( global value_type* ptr ) \n
253 value_type operator[]( size_type threadID ) const \n
255 return m_constValue; \n
258 value_type operator*( ) const \n
260 return m_constValue; \n
263 value_type m_constValue; \n
267 + std::string(
"#endif \n");
269 template<
typename Type >
270 constant_iterator< Type > make_constant_iterator( Type constValue )
272 constant_iterator< Type > tmp( constValue );