/[escript]/trunk/cusplibrary/cusp/array1d.h
ViewVC logotype

Contents of /trunk/cusplibrary/cusp/array1d.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5148 - (show annotations)
Mon Sep 15 01:25:23 2014 UTC (5 years, 8 months ago) by caltinay
File MIME type: text/plain
File size: 7497 byte(s)
Merging ripley diagonal storage + CUDA support into trunk.
Options file version has been incremented due to new options
'cuda' and 'nvccflags'.

1 /*
2 * Copyright 2008-2009 NVIDIA Corporation
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 /*! \file array1d.h
18 * \brief One-dimensional array
19 */
20
21 #pragma once
22
23 #include <cusp/detail/config.h>
24
25 #include <cusp/memory.h>
26 #include <cusp/format.h>
27 #include <cusp/exception.h>
28
29 #include <thrust/host_vector.h>
30 #include <thrust/device_vector.h>
31 #include <thrust/detail/vector_base.h>
32
33 namespace cusp
34 {
35 // forward definitions
36 template <typename RandomAccessIterator> class array1d_view;
37
38 /*! \addtogroup arrays Arrays
39 */
40
41 /*! \addtogroup array_containers Array Containers
42 * \ingroup arrays
43 * \{
44 */
45
46 /*! \p array1d : One-dimensional array container
47 *
48 * \tparam T value_type of the array
49 * \tparam MemorySpace memory space of the array (cusp::host_memory or cusp::device_memory)
50 *
51 * \TODO example
52 */
53 template <typename T, typename MemorySpace>
54 class array1d : public thrust::detail::vector_base<T, typename cusp::default_memory_allocator<T, MemorySpace>::type>
55 {
56 private:
57 typedef typename cusp::default_memory_allocator<T, MemorySpace>::type Alloc;
58 typedef typename thrust::detail::vector_base<T,Alloc> Parent;
59
60 public:
61 typedef MemorySpace memory_space;
62 typedef cusp::array1d_format format;
63
64 template<typename MemorySpace2>
65 struct rebind { typedef cusp::array1d<T, MemorySpace2> type; };
66
67 /*! equivalent container type
68 */
69 typedef typename cusp::array1d<T,MemorySpace> container;
70
71 /*! equivalent view type
72 */
73 typedef typename cusp::array1d_view<typename Parent::iterator> view;
74
75 /*! equivalent const_view type
76 */
77 typedef typename cusp::array1d_view<typename Parent::const_iterator> const_view;
78
79 typedef typename Parent::size_type size_type;
80 typedef typename Parent::value_type value_type;
81
82 array1d(void) : Parent() {}
83
84 explicit array1d(size_type n)
85 : Parent()
86 {
87 if(n > 0)
88 {
89 Parent::m_storage.allocate(n);
90 Parent::m_size = n;
91 }
92 }
93
94 array1d(size_type n, const value_type &value)
95 : Parent(n, value) {}
96
97 template<typename Array>
98 array1d(const Array& a, typename thrust::detail::enable_if<!thrust::detail::is_convertible<Array,size_type>::value>::type * = 0)
99 : Parent(a.begin(), a.end()) {}
100
101 template<typename InputIterator>
102 array1d(InputIterator first, InputIterator last)
103 : Parent(first, last) {}
104
105 template<typename Array>
106 array1d &operator=(const Array& a)
107 { Parent::assign(a.begin(), a.end()); return *this; }
108
109 // TODO specialize resize()
110 }; // class array1d
111 /*! \}
112 */
113
114 /*! \addtogroup array_views Array Views
115 * \ingroup arrays
116 * \{
117 */
118
119 /*! \p array1d_view : One-dimensional array view
120 *
121 * \tparam RandomAccessIterator Underlying iterator type
122 *
123 * \TODO example
124 */
125 template <typename RandomAccessIterator>
126 class array1d_view
127 {
128 public:
129 // what about const_iterator and const_reference?
130 typedef RandomAccessIterator iterator;
131 typedef cusp::array1d_format format;
132 typedef typename thrust::iterator_reference<RandomAccessIterator>::type reference;
133 typedef typename thrust::iterator_difference<RandomAccessIterator>::type difference_type;
134 typedef typename thrust::iterator_value<RandomAccessIterator>::type value_type;
135 #if THRUST_VERSION >= 100600
136 typedef typename thrust::iterator_system<RandomAccessIterator>::type memory_space;
137 #else
138 typedef typename thrust::iterator_space<RandomAccessIterator>::type memory_space;
139 #endif
140 typedef typename thrust::iterator_pointer<RandomAccessIterator>::type pointer;
141
142 /*! equivalent container type
143 */
144 typedef typename cusp::array1d<value_type,memory_space> container;
145
146 /*! equivalent view type
147 */
148 typedef typename cusp::array1d_view<RandomAccessIterator> view;
149
150 // is this right?
151 typedef size_t size_type;
152
153 array1d_view(void)
154 : m_begin(), m_size(0), m_capacity(0) {}
155
156 template <typename Array>
157 explicit array1d_view(Array& a)
158 : m_begin(a.begin()), m_size(a.size()), m_capacity(a.capacity()) {}
159
160 template <typename Array>
161 explicit array1d_view(const Array& a)
162 : m_begin(a.begin()), m_size(a.size()), m_capacity(a.capacity()) {}
163
164 // should these be templated?
165 array1d_view(RandomAccessIterator first, RandomAccessIterator last)
166 : m_begin(first), m_size(last - first), m_capacity(last - first) {}
167
168 array1d_view& operator=(const array1d_view& a)
169 {
170 m_begin = a.begin();
171 m_size = a.size();
172 m_capacity = a.capacity();
173 return *this;
174 }
175
176 //template <typename Array>
177 //array1d_view &operator=(Array &a)
178 //{
179 // m_begin = a.begin();
180 // m_size = a.size();
181 // m_capacity = a.capacity();
182 // return *this;
183 //}
184
185 reference front(void) const
186 {
187 return m_begin[0];
188 }
189
190 reference back(void) const
191 {
192 return m_begin[size() - 1];
193 }
194
195 reference operator[](difference_type n) const
196 {
197 return m_begin[n];
198 }
199
200 iterator begin(void) const
201 {
202 return m_begin;
203 }
204
205 iterator end(void) const
206 {
207 return m_begin + m_size;
208 }
209
210 size_type size(void) const
211 {
212 return m_size;
213 }
214
215 size_type capacity(void) const
216 {
217 return m_capacity;
218 }
219
220
221 pointer data(void)
222 {
223 return &front();
224 }
225 // TODO is there any value in supporting the two-argument form?
226 // i.e. void resize(size_type new_size, value_type x = value_type())
227 void resize(size_type new_size)
228 {
229 if (new_size <= m_capacity)
230 m_size = new_size;
231 else
232 // XXX is not_implemented_exception the right choice?
233 throw cusp::not_implemented_exception("array1d_view cannot resize() larger than capacity()");
234 }
235
236 protected:
237 iterator m_begin;
238 size_type m_size;
239 size_type m_capacity;
240 };
241
242 /* Convenience functions */
243
244 template <typename Iterator>
245 array1d_view<Iterator> make_array1d_view(Iterator first, Iterator last)
246 {
247 return array1d_view<Iterator>(first, last);
248 }
249
250 template <typename Iterator>
251 array1d_view<Iterator> make_array1d_view(const array1d_view<Iterator>& a)
252 {
253 return make_array1d_view(a.begin(), a.end());
254 }
255
256 template <typename T, typename MemorySpace>
257 typename array1d<T,MemorySpace>::view make_array1d_view(array1d<T,MemorySpace>& a)
258 {
259 return make_array1d_view(a.begin(), a.end());
260 }
261
262 template <typename T, typename MemorySpace>
263 typename array1d<T,MemorySpace>::const_view make_array1d_view(const array1d<T,MemorySpace>& a)
264 {
265 return make_array1d_view(a.begin(), a.end());
266 }
267 /*! \}
268 */
269
270 } // end namespace cusp
271
272 #include <cusp/detail/array1d.inl>
273

  ViewVC Help
Powered by ViewVC 1.1.26