Bolt  1.3
C++ template library with support for OpenCL
addressof.h
1 /***************************************************************************
2 * © 2012,2014 Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 
16 ***************************************************************************/
17 #ifndef BOLT_ADDRESSOF_H
18 #define BOLT_ADDRESSOF_H
19 #include <bolt/cl/device_vector.h>
20 #include <bolt/cl/iterator/permutation_iterator.h>
21 #include <bolt/cl/iterator/transform_iterator.h>
24 
25 namespace bolt{
26 namespace cl{
27 
28  template <typename Iterator>
29  typename Iterator::value_type * addressof(Iterator itr)
30  {
31  return std::addressof(*itr);
32  }
33 
34  template <typename T>
35  T * addressof(T* itr)
36  {
37  return itr;
38  }
39 
40  template <typename UnaryFunction, typename Iterator>
41  typename bolt::cl::transform_iterator<UnaryFunction, Iterator>::pointer
42  addressof(const typename bolt::cl::transform_iterator<UnaryFunction, Iterator> itr)
43  {
44  typedef typename bolt::cl::transform_iterator<UnaryFunction, Iterator>::pointer pointer;
45  pointer ptr = const_cast<pointer>(itr.getPointer());
46  return ptr;
47  }
48 
49  template <typename value_type>
50  typename bolt::cl::counting_iterator<value_type>::pointer
51  addressof(typename bolt::cl::counting_iterator<value_type> itr)
52  {
53  typedef typename bolt::cl::counting_iterator<value_type>::pointer pointer;
54  pointer ptr = const_cast<pointer>(itr.getPointer() );
55  return ptr;
56  }
57 
58  template <typename value_type>
59  typename bolt::cl::constant_iterator<value_type>::pointer
60  addressof(typename bolt::cl::constant_iterator<value_type> itr)
61  {
62  typedef typename bolt::cl::constant_iterator<value_type>::pointer pointer;
63  pointer ptr = const_cast<pointer>(itr.getPointer() );
64  return ptr;
65  }
66 
67  template <typename Iterator, typename DeviceIterator>
69  create_device_itr(bolt::cl::transform_iterator_tag, Iterator itr, DeviceIterator dev_itr_1)
70  {
71  typedef typename Iterator::unary_func unary_func;
72  return transform_iterator<unary_func, DeviceIterator> (dev_itr_1, itr.functor());
73  }
74 
75  template <typename Iterator, typename DeviceIterator>
77  create_device_itr(std::random_access_iterator_tag, Iterator itr, DeviceIterator dev_itr_1)
78  {
79  return dev_itr_1;
80  }
81 
82  template <typename T, typename DeviceIterator>
84  create_device_itr(std::random_access_iterator_tag, T* ptr, DeviceIterator dev_itr_1)
85  {
86  return dev_itr_1;
87  }
88 
89  template <typename Iterator, typename DeviceIterator>
90  const constant_iterator<typename Iterator::value_type>
91  create_device_itr(bolt::cl::constant_iterator_tag, Iterator itr, DeviceIterator dev_itr_1)
92  {
93  return itr;
94  }
95 
96  template <typename Iterator, typename DeviceIterator>
97  const counting_iterator<typename Iterator::value_type>
98  create_device_itr(bolt::cl::counting_iterator_tag, Iterator itr, DeviceIterator dev_itr_1)
99  {
100  return itr;
101  }
102 
103  /******************* Create Mapped Iterators **********************/
104 
105  template <typename Iterator, typename T>
107  create_mapped_iterator(bolt::cl::transform_iterator_tag, bolt::cl::control &ctl, Iterator &itr, T* ptr)
108  {
109  typedef typename Iterator::unary_func unary_func;
110  return transform_iterator<unary_func, T*> (ptr, itr.functor());
111  }
112 
113  template <typename Iterator, typename T>
114  T*
115  create_mapped_iterator(bolt::cl::device_vector_tag, bolt::cl::control &ctl, Iterator &itr, T* ptr)
116  {
117  return ptr + itr.m_Index;
118  }
119 
120  /*TODO - The current constant and counting iterator implementations are buggy.
121  They create an OpenCL device buffer even if the iterator is to be used on host only.
122  */
123  template <typename Iterator, typename T>
124  const constant_iterator<typename Iterator::value_type> &
125  create_mapped_iterator(bolt::cl::constant_iterator_tag, bolt::cl::control &ctl, Iterator &itr, T* ptr)
126  {
127  return itr;
128  }
129 
130  template <typename Iterator, typename T>
131  const counting_iterator<typename Iterator::value_type> &
132  create_mapped_iterator(bolt::cl::counting_iterator_tag, bolt::cl::control &ctl, Iterator &itr, T* ptr)
133  {
134  return itr;
135  }
136 
137  template <typename Iterator, typename T>
138  const permutation_iterator<typename Iterator::value_type*, typename Iterator::index_type*>
139  create_mapped_iterator(bolt::cl::permutation_iterator_tag, bolt::cl::control &ctl, Iterator &itr, T* ptr)
140  {
141  typedef typename Iterator::value_type value_type;
142  typedef typename Iterator::index_type index_type;
143  index_type *i_ptr = ptr;
144  ::cl::Buffer elementBuffer = itr.m_elt_iter.getContainer( ).getBuffer( );
145  size_t elementBuffer_sz = elementBuffer.getInfo<CL_MEM_SIZE>();
146  cl_int map_err;
147  value_type *mapped_elementPtr = (value_type*)ctl.getCommandQueue().enqueueMapBuffer(elementBuffer, true,
148  CL_MAP_WRITE, 0,
149  elementBuffer_sz,
150  NULL, NULL, &map_err);
151 
152  return bolt::cl::make_permutation_iterator (mapped_elementPtr, i_ptr);
153  }
154 
155  /*template <typename Iterator, typename T>
156  void release_mapped_iterator(bolt::cl::permutation_iterator_tag, bolt::cl::control &ctl, Iterator &itr)
157  {
158  typedef typename Iterator::value_type value_type;
159  ::cl::Buffer elementBuffer = itr.m_elt_iter.getContainer( ).getBuffer( );
160  value_type * element_ptr = elementBuffer.getInfo<CL_MEM_HOST_PTR>();
161  ctl.getCommandQueue().enqueueUnmapMemObject(elementBuffer, element_ptr, NULL, );
162  return;
163  }*/
164 
165 }} //namespace bolt::cl
166 
167 #endif