NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #pragma once
33 
34 #include <nvbio/basic/types.h>
35 #include <nvbio/basic/iterator.h>
38 #include <thrust/host_vector.h>
39 #include <thrust/device_vector.h>
40 
41 namespace nvbio {
42 namespace cuda {
43 
44 // utility function to copy a thrust device vector to a thrust host vector
45 // the sole reason for this is to eliminate warnings from thrust when using the assignment operator
46 template<typename TTargetVector, typename TSourceVector>
47 static NVBIO_FORCEINLINE void thrust_copy_vector(TTargetVector& target, TSourceVector& source)
48 {
49  if (target.size() != source.size())
50  {
51  target.clear();
52  target.resize(source.size());
53  }
54 
55  thrust::copy(source.begin(), source.end(), target.begin());
56 }
57 
58 template<typename TTargetVector, typename TSourceVector>
59 static NVBIO_FORCEINLINE void thrust_copy_vector(TTargetVector& target, TSourceVector& source, uint32 count)
60 {
61  if (target.size() != count)
62  {
63  target.clear();
64  target.resize(count);
65  }
66 
67  thrust::copy(source.begin(), source.begin() + count, target.begin());
68 }
69 
70 } // namespace cuda
71 
74 template <typename system_tag, typename T>
75 struct vector {};
76 
79 template <typename T>
80 struct vector<host_tag,T> : public thrust::host_vector<T>
81 {
83 
84  typedef thrust::host_vector<T> base_type;
85  typedef typename base_type::const_iterator const_iterator;
86  typedef typename base_type::iterator iterator;
87  typedef typename base_type::value_type value_type;
88 
91 
94  vector<host_tag,T>(const size_t size = 0, const T val = T()) : base_type( size, val ) {}
95  vector<host_tag,T>(const thrust::host_vector<T>& v) : base_type( v ) {}
96  vector<host_tag,T>(const thrust::device_vector<T>& v) : base_type( v ) {}
97 
98  vector<host_tag,T>& operator= (const thrust::host_vector<T>& v) { cuda::thrust_copy_vector( *this, v ); return *this; }
99  vector<host_tag,T>& operator= (const thrust::device_vector<T>& v) { cuda::thrust_copy_vector( *this, v ); return *this; }
100 
103  operator plain_view_type() { return plain_view_type( base_type::size(), nvbio::raw_pointer( *this ) ); }
104 
107  operator const_plain_view_type() const { return const_plain_view_type( base_type::size(), nvbio::raw_pointer( *this ) ); }
108 };
109 
112 template <typename T>
113 struct vector<device_tag,T> : public thrust::device_vector<T>
114 {
116 
117  typedef thrust::device_vector<T> base_type;
118  typedef typename base_type::const_iterator const_iterator;
119  typedef typename base_type::iterator iterator;
120  typedef typename base_type::value_type value_type;
121 
124 
127  vector<device_tag,T>(const size_t size = 0, const T val = T()) : base_type( size, val ) {}
128  vector<device_tag,T>(const thrust::host_vector<T>& v) : base_type( v ) {}
129  vector<device_tag,T>(const thrust::device_vector<T>& v) : base_type( v ) {}
130 
131  vector<device_tag,T>& operator= (const thrust::host_vector<T>& v) { cuda::thrust_copy_vector( *this, v ); return *this; }
132  vector<device_tag,T>& operator= (const thrust::device_vector<T>& v) { cuda::thrust_copy_vector( *this, v ); return *this; }
133 
136  operator plain_view_type() { return plain_view_type( base_type::size(), nvbio::raw_pointer( *this ) ); }
137 
140  operator const_plain_view_type() const { return const_plain_view_type( base_type::size(), nvbio::raw_pointer( *this ) ); }
141 };
142 
145 template <typename T> struct device_iterator_type { typedef T type; };
146 template <typename T> struct device_iterator_type<T*> { typedef thrust::device_ptr<T> type; };
147 template <typename T> struct device_iterator_type<const T*> { typedef thrust::device_ptr<const T> type; };
148 
151 template <typename T>
153 {
154  // wrap the plain iterator
155  return typename device_iterator_type<T>::type( it );
156 }
157 
158 } // namespace nvbio