CUSP
Loading...
Searching...
No Matches
array1d.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2014 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
22#pragma once
23
24#include <cusp/detail/config.h>
25
26#include <cusp/memory.h>
27#include <cusp/detail/format.h>
28#include <cusp/exception.h>
29
31
32#include <thrust/copy.h>
33#include <thrust/host_vector.h>
34#include <thrust/device_vector.h>
35#include <thrust/detail/vector_base.h>
36#include <thrust/iterator/constant_iterator.h>
37#include <thrust/iterator/counting_iterator.h>
38
39namespace cusp
40{
41
42// forward declaration of array1d_view
43template <typename RandomAccessIterator> class array1d_view;
44
97template <typename T, typename MemorySpace>
98class array1d : public thrust::detail::vector_base<T, typename cusp::default_memory_allocator<T, MemorySpace>::type>
99{
100private:
101 typedef typename cusp::default_memory_allocator<T, MemorySpace>::type Alloc;
102 typedef typename thrust::detail::vector_base<T,Alloc> Parent;
103
104public:
106 typedef MemorySpace memory_space;
107 typedef cusp::array1d_format format;
108
109 typedef typename Parent::iterator iterator;
110 typedef typename Parent::const_iterator const_iterator;
111 typedef typename Parent::value_type value_type;
112 typedef typename Parent::size_type size_type;
113
114 typedef cusp::array1d<T,MemorySpace> container;
115 typedef typename cusp::array1d_view<iterator> view;
116 typedef typename cusp::array1d_view<const_iterator> const_view;
117
118 template<typename MemorySpace2>
119 struct rebind {
121 };
127 : Parent() {}
128
133 explicit array1d(size_type n)
134 : Parent(n) {}
135
141 explicit array1d(size_type n, const value_type &value)
142 : Parent(n, value) {}
143
147 array1d(const array1d &v)
148 : Parent(v) {}
149
154 { Parent::operator=(v); return *this; }
155
161 template<typename OtherT, typename OtherMem>
163 : Parent(v) {}
164
170 template<typename OtherT, typename OtherMem>
172 { Parent::operator=(v); return *this; }
173
179 template<typename OtherT, typename OtherAlloc>
180 array1d(const std::vector<OtherT, OtherAlloc> &v)
181 : Parent(v) {}
182
188 template<typename OtherT, typename OtherAlloc>
189 array1d &operator=(const std::vector<OtherT, OtherAlloc> &v)
190 { Parent::operator=(v); return *this; }
191
197 template<typename OtherT, typename OtherAlloc>
198 array1d(const thrust::host_vector<OtherT, OtherAlloc> &v)
199 : Parent(v) {}
200
206 template<typename OtherT, typename OtherAlloc>
207 array1d &operator=(const thrust::host_vector<OtherT, OtherAlloc> &v)
208 { Parent::operator=(v); return *this; }
209
215 template<typename OtherT, typename OtherAlloc>
216 array1d(const thrust::device_vector<OtherT, OtherAlloc> &v)
217 : Parent(v) {}
218
224 template<typename OtherT, typename OtherAlloc>
225 array1d &operator=(const thrust::device_vector<OtherT, OtherAlloc> &v)
226 { Parent::operator=(v); return *this; }
227
233 template<typename InputIterator>
234 array1d(InputIterator first, InputIterator last)
235 : Parent(first, last) {}
236
241 template<typename InputIterator>
243 : Parent(v.begin(), v.end()) {}
244
275 view subarray(size_type start_index, size_type num_entries);
276
307 const_view subarray(size_type start_index, size_type num_entries) const;
308
309}; // end class array1d
360template<typename RandomAccessIterator>
361class array1d_view : public thrust::iterator_adaptor<array1d_view<RandomAccessIterator>, RandomAccessIterator>
362{
363private :
364 typedef thrust::iterator_adaptor<array1d_view<RandomAccessIterator>, RandomAccessIterator> Parent;
365 typedef typename thrust::iterator_system<RandomAccessIterator>::type space;
366
367 friend class thrust::iterator_core_access;
368
369public :
370
372 typedef RandomAccessIterator iterator;
373 typedef cusp::array1d_format format;
374
375 typedef typename cusp::iterator_system<space>::type memory_space;
376 typedef typename ::cuda::std::iterator_traits<RandomAccessIterator>::value_type value_type;
377 typedef typename ::cuda::std::iterator_traits<RandomAccessIterator>::pointer pointer;
378 typedef typename ::cuda::std::iterator_traits<RandomAccessIterator>::reference reference;
379 typedef typename ::cuda::std::iterator_traits<RandomAccessIterator>::difference_type difference_type;
380 // typedef typename ::cuda::std::iterator_traits<RandomAccessIterator>::difference_type size_type;
381 typedef size_t size_type;
382
383 typedef typename cusp::array1d<value_type,memory_space> container;
384
385 // TODO: Fixed view types, view == const_view???
386 typedef typename cusp::array1d_view<RandomAccessIterator> view;
387
388 typedef RandomAccessIterator const_iterator;
389 typedef typename cusp::array1d_view<const_iterator> const_view;
395 : m_size(0), m_capacity(0) {}
396
401 template<typename ArrayType>
402 array1d_view(ArrayType &v)
403 : Parent(v.begin()), m_size(v.size()), m_capacity(v.capacity()) {}
404
409 template<typename InputIterator>
410 array1d_view(InputIterator begin, InputIterator end)
411 : Parent(begin), m_size(end-begin), m_capacity(end-begin) {}
412
418
423 reference front(void) const;
424
429 reference back(void) const;
430
439 reference operator[](size_type n) const;
440
445 iterator begin(void) const;
446
451 iterator end(void) const;
452
455 size_type size(void) const;
456
460 size_type capacity(void) const;
461
462 // Thrust does not export iterator pointer types so data is not valid
463 // see http://thrust.github.io/doc/classthrust_1_1iterator__facade.html
464 // pointer data(void)
465 // {
466 // return &front();
467 // }
468
477 void resize(size_type new_size);
478
510 view subarray(size_type start_index, size_type num_entries);
511
512
518
519protected:
523 size_type m_size;
524
528 size_type m_capacity;
529};
530
563template <typename ValueType>
565 : public cusp::array1d_view< thrust::counting_iterator<ValueType> >
566{
567private:
568
570
571public:
572
574 typedef typename Parent::iterator iterator;
575 typedef typename Parent::size_type size_type;
583 counting_array(size_type size, ValueType init = ValueType(0))
584 : Parent(iterator(init), iterator(init) + size) {}
585};
586
613template <typename ValueType>
615 : public cusp::array1d_view< thrust::constant_iterator<ValueType> >
616{
617private:
618
620
621public:
622
624 typedef typename Parent::iterator iterator;
625 typedef typename Parent::size_type size_type;
633 constant_array(size_type size, ValueType value)
634 : Parent(iterator(value), iterator(value) + size) {}
635};
636
661template <typename ValueType>
663 public cusp::array1d_view< typename cusp::random_iterator<ValueType>::iterator >
664{
665private:
666
668
669public:
670
672 typedef typename cusp::random_iterator<ValueType> rand;
673 typedef typename Parent::size_type size_type;
681 random_array(size_type size, size_type seed = 0)
682 : Parent(rand(seed).begin(), rand(seed).begin() + size) {}
683};
684
685
694template <typename Iterator>
695array1d_view<Iterator> make_array1d_view(Iterator first, Iterator last)
696{
697 array1d_view<Iterator> view(first, last);
698
699 return view;
700}
701
710template <typename T, typename MemorySpace>
712{
713 return make_array1d_view(v.begin(), v.end());
714}
715
723template <typename Iterator>
728
736template <typename Iterator>
741
750template <typename T, typename MemorySpace>
752{
753 return make_array1d_view(v.begin(), v.end());
754}
758} // end namespace cusp
759
760#include <cusp/detail/array1d.inl>
761
The array1d_view class is a 1D vector container that wraps data from various iterators in a array1d d...
Definition array1d.h:362
void swap(array1d_view &v)
view subarray(size_type start_index, size_type num_entries)
array1d_view & operator=(const array1d_view &v)
size_type size(void) const
size_type m_size
Definition array1d.h:523
size_type m_capacity
Definition array1d.h:528
reference back(void) const
size_type capacity(void) const
iterator end(void) const
array1d_view(InputIterator begin, InputIterator end)
Definition array1d.h:410
array1d_view(ArrayType &v)
Definition array1d.h:402
reference front(void) const
reference operator[](size_type n) const
Subscript access to the data contained in this array1d_view.
iterator begin(void) const
void resize(size_type new_size)
Resizes this array1d_view to the specified number of elements.
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition array1d.h:99
array1d(size_type n)
Definition array1d.h:133
array1d(const thrust::device_vector< OtherT, OtherAlloc > &v)
Definition array1d.h:216
array1d(const array1d &v)
Definition array1d.h:147
array1d(const std::vector< OtherT, OtherAlloc > &v)
Definition array1d.h:180
array1d & operator=(const array1d &v)
Definition array1d.h:153
array1d(InputIterator first, InputIterator last)
Definition array1d.h:234
array1d(const array1d_view< InputIterator > &v)
Definition array1d.h:242
view subarray(size_type start_index, size_type num_entries)
array1d(void)
Definition array1d.h:126
array1d & operator=(const thrust::device_vector< OtherT, OtherAlloc > &v)
Definition array1d.h:225
const_view subarray(size_type start_index, size_type num_entries) const
array1d(size_type n, const value_type &value)
Definition array1d.h:141
array1d(const array1d< OtherT, OtherMem > &v)
Definition array1d.h:162
array1d(const thrust::host_vector< OtherT, OtherAlloc > &v)
Definition array1d.h:198
array1d & operator=(const thrust::host_vector< OtherT, OtherAlloc > &v)
Definition array1d.h:207
array1d & operator=(const array1d< OtherT, OtherMem > &v)
Definition array1d.h:171
array1d & operator=(const std::vector< OtherT, OtherAlloc > &v)
Definition array1d.h:189
Specialized array1d_view wrapping thrust::constant_iterator.
Definition array1d.h:616
constant_array(size_type size, ValueType value)
Definition array1d.h:633
Specialized array1d_view wrapping thrust::counting_iterator.
Definition array1d.h:566
counting_array(size_type size, ValueType init=ValueType(0))
Definition array1d.h:583
Specialized array1d_view wrapping cusp::random_iterator.
Definition array1d.h:664
random_array(size_type size, size_type seed=0)
Definition array1d.h:681
Iterator for generating random values.
array1d_view< Iterator > make_array1d_view(Iterator first, Iterator last)
Definition array1d.h:695
An iterator which generates random entries.