QUDA  v1.1.0
A library for QCD on GPUs
array.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, NVIDIA Corporation
3 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 <organization> 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 <COPYRIGHT HOLDER> 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 
28 #pragma once
29 
30 namespace trove {
31 
32 template<typename T, int m>
33 struct array {
34  typedef T value_type;
35  typedef T head_type;
36  typedef array<T, m-1> tail_type;
37  static const int size = m;
40  __host__ __device__
41  array(head_type h, const tail_type& t) : head(h), tail(t) {}
42  __host__ __device__
43  array() : head(), tail() {}
44  __host__ __device__
45  array(const array& other) : head(other.head), tail(other.tail) {}
46  __host__ __device__
47  array& operator=(const array& other) {
48  head = other.head;
49  tail = other.tail;
50  return *this;
51  }
52  __host__ __device__
53  bool operator==(const array& other) const {
54  return (head == other.head) && (tail == other.tail);
55  }
56  __host__ __device__
57  bool operator!=(const array& other) const {
58  return !operator==(other);
59  }
60 };
61 
62 template<typename T>
63 struct array<T, 1> {
64  typedef T value_type;
65  typedef T head_type;
66  static const int size = 1;
68  __host__ __device__
69  array(head_type h) : head(h){}
70  __host__ __device__
71  array() : head() {}
72  __host__ __device__
73  array(const array& other) : head(other.head) {}
74  __host__ __device__
75  array& operator=(const array& other) {
76  head = other.head;
77  return *this;
78  }
79  __host__ __device__
80  bool operator==(const array& other) const {
81  return (head == other.head);
82  }
83  __host__ __device__
84  bool operator!=(const array& other) const {
85  return !operator==(other);
86  }
87 };
88 
89 template<typename T>
90 struct array<T, 0>{};
91 
92 namespace detail {
93 
94 template<typename T, int m, int i>
95 struct get_impl {
96  __host__ __device__ static T& impl(array<T, m>& src) {
98  }
99  __host__ __device__ static T impl(const array<T, m>& src) {
100  return get_impl<T, m-1, i-1>::impl(src.tail);
101  }
102 };
103 
104 template<typename T, int m>
105 struct get_impl<T, m, 0> {
106  __host__ __device__ static T& impl(array<T, m>& src) {
107  return src.head;
108  }
109  __host__ __device__ static T impl(const array<T, m>& src) {
110  return src.head;
111  }
112 };
113 
114 }
115 
116 template<int i, typename T, int m>
117 __host__ __device__
118 T& get(array<T, m>& src) {
120 }
121 
122 template<int i, typename T, int m>
123 __host__ __device__
124 T get(const array<T, m>& src) {
126 }
127 
128 template<typename T>
129 __host__ __device__
131  return array<T, 0>();
132 }
133 
134 template<typename T>
135 __host__ __device__
137  return array<T, 1>(a0);
138 }
139 
140 template<typename T>
141 __host__ __device__
142 array<T, 2> make_array(T a0, T a1) {
143  return array<T, 2>(a0,
144  make_array<T>(a1));
145 }
146 
147 template<typename T>
148 __host__ __device__
149 array<T, 3> make_array(T a0, T a1, T a2) {
150  return array<T, 3>(a0,
151  make_array<T>(a1, a2));
152 }
153 
154 template<typename T>
155 __host__ __device__
156 array<T, 4> make_array(T a0, T a1, T a2, T a3) {
157  return array<T, 4>(a0,
158  make_array<T>(a1, a2, a3));
159 }
160 
161 template<typename T>
162 __host__ __device__
163 array<T, 5> make_array(T a0, T a1, T a2, T a3, T a4) {
164  return array<T, 5>(a0,
165  make_array<T>(a1, a2, a3, a4));
166 }
167 
168 template<typename T>
169 __host__ __device__
170 array<T, 6> make_array(T a0, T a1, T a2, T a3, T a4,
171  T a5) {
172  return array<T, 6>(a0,
173  make_array<T>(a1, a2, a3, a4, a5));
174 }
175 
176 template<typename T>
177 __host__ __device__
178 array<T, 7> make_array(T a0, T a1, T a2, T a3, T a4,
179  T a5, T a6) {
180  return array<T, 7>(a0,
181  make_array<T>(a1, a2, a3, a4, a5,
182  a6));
183 }
184 
185 template<typename T>
186 __host__ __device__
187 array<T, 8> make_array(T a0, T a1, T a2, T a3, T a4,
188  T a5, T a6, T a7) {
189  return array<T, 8>(a0,
190  make_array<T>(a1, a2, a3, a4, a5,
191  a6, a7));
192 }
193 
194 template<typename T>
195 __host__ __device__
196 array<T, 9> make_array(T a0, T a1, T a2, T a3, T a4,
197  T a5, T a6, T a7, T a8) {
198  return array<T, 9>(a0,
199  make_array<T>(a1, a2, a3, a4, a5,
200  a6, a7, a8));
201 }
202 
203 template<typename T>
204 __host__ __device__
205 array<T, 10> make_array(T a0, T a1, T a2, T a3, T a4,
206  T a5, T a6, T a7, T a8, T a9) {
207  return array<T, 10>(a0,
208  make_array<T>(a1, a2, a3, a4, a5,
209  a6, a7, a8, a9));
210 }
211 
212 
213 namespace detail {
214 
215 template<typename T, int s>
218  __host__ __device__
219  static result_type impl(T ary[s]) {
220  return result_type(ary[0],
222  }
223 };
224 
225 template<typename T>
226 struct make_array_impl<T, 1> {
228  __host__ __device__
229  static result_type impl(T ary[1]) {
230  return result_type(ary[0]);
231  }
232 };
233 
234 
235 #if 0
236 template<typename T>
237 struct make_array_impl<T, 0> {
238  typedef array<T, 0> result_type;
239  __host__ __device__
240  static result_type impl(T ary[0]) {
241  return result_type();
242  }
243 };
244 #endif
245 
246 
247 template<typename T, int s>
250  __host__ __device__
251  static void impl(const array_type& ary, T result[s]) {
252  result[0] = ary.head;
253  make_carray_impl<T, s-1>::impl(ary.tail, result+1);
254  }
255 };
256 
257 template<typename T>
258 struct make_carray_impl<T, 1> {
260  __host__ __device__
261  static void impl(const array_type& ary, T result[1]) {
262  result[0] = ary.head;
263  }
264 };
265 
266 #if 0
267 template<typename T>
268 struct make_carray_impl<T, 0> {
269  __host__ __device__
270  static void impl(array<T, 0>, T result[0]) {}
271 };
272 #endif
273 
274 } //end namespace detail
275 
276 template<typename T, int s>
277 __host__ __device__
280 }
281 
282 template<typename T, int s>
283 __host__ __device__
284 void make_carray(const array<T, s>& ary,
285  T result[s]) {
287 }
288 
289 } //end namespace trove
Definition: alias.h:4
Definition: aos.h:38
__host__ __device__ void make_carray(const array< T, s > &ary, T result[s])
Definition: array.h:284
__host__ __device__ T & get(array< T, m > &src)
Definition: array.h:118
__host__ __device__ array< T, 0 > make_array()
Definition: array.h:130
__host__ __device__ array()
Definition: array.h:71
head_type head
Definition: array.h:67
__host__ __device__ bool operator!=(const array &other) const
Definition: array.h:84
__host__ __device__ array(const array &other)
Definition: array.h:73
__host__ __device__ array & operator=(const array &other)
Definition: array.h:75
__host__ __device__ array(head_type h)
Definition: array.h:69
__host__ __device__ bool operator==(const array &other) const
Definition: array.h:80
static const int size
Definition: array.h:37
T head_type
Definition: array.h:35
__host__ __device__ array & operator=(const array &other)
Definition: array.h:47
__host__ __device__ bool operator==(const array &other) const
Definition: array.h:53
head_type head
Definition: array.h:38
__host__ __device__ array()
Definition: array.h:43
array< T, m-1 > tail_type
Definition: array.h:36
__host__ __device__ array(head_type h, const tail_type &t)
Definition: array.h:41
T value_type
Definition: array.h:34
__host__ __device__ array(const array &other)
Definition: array.h:45
tail_type tail
Definition: array.h:39
__host__ __device__ bool operator!=(const array &other) const
Definition: array.h:57
__host__ static __device__ T & impl(array< T, m > &src)
Definition: array.h:106
__host__ static __device__ T impl(const array< T, m > &src)
Definition: array.h:109
__host__ static __device__ T impl(const array< T, m > &src)
Definition: array.h:99
__host__ static __device__ T & impl(array< T, m > &src)
Definition: array.h:96
__host__ static __device__ result_type impl(T ary[1])
Definition: array.h:229
array< T, s > result_type
Definition: array.h:217
__host__ static __device__ result_type impl(T ary[s])
Definition: array.h:219
__host__ static __device__ void impl(const array_type &ary, T result[1])
Definition: array.h:261
__host__ static __device__ void impl(const array_type &ary, T result[s])
Definition: array.h:251
array< T, s > array_type
Definition: array.h:249