QUDA  v1.1.0
A library for QCD on GPUs
utility.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 #include <trove/array.h>
30 
31 namespace trove {
32 
33 template<typename T>
34 struct counting_array{};
35 
36 template<typename T, int s>
37 struct counting_array<array<T, s> > {
38  typedef array<T, s> Array;
39  __host__ __device__
40  static Array impl(T v=0, T i=1) {
41  return Array(v,
42  counting_array<array<T, s-1> >::impl(v + i, i));
43  }
44 };
45 
46 template<typename T>
47 struct counting_array<array<T, 1> > {
48  __host__ __device__
49  static array<T, 1> impl(T v, T i=1) {
50  return make_array(v);
51  }
52 };
53 
54 template<typename T>
55 struct sum_array {};
56 
57 template<typename T, int s>
58 struct sum_array<array<T, s> > {
59  typedef array<T, s> Array;
60  __host__ __device__
61  static T impl(const Array& a, const T& p) {
63  }
64 };
65 
66 template<typename T>
67 struct sum_array<array<T, 1> > {
68  typedef array<T, 1> Array;
69  __host__ __device__
70  static T impl(const Array& a, const T& p) {
71  return p + a.head;
72  }
73 };
74 
75 template<typename T, int s>
76 __host__ __device__ T sum(const array<T, s>& a) {
77  return sum_array<array<T, s> >::impl(a, 0);
78 }
79 
80 template<int m>
81 struct static_log {
82  static const int value = 1 + static_log< (m >> 1) >::value;
83 };
84 
85 template<>
86 struct static_log<1> {
87  static const int value = 0;
88 };
89 
90 template<>
91 struct static_log<0> {
92  //This functions as a static assertion
93  //Don't take the log of 0!!
94 };
95 
96 template<int m>
98  static const bool value = (m & (m-1)) == 0;
99 };
100 
101 template<int m>
102 struct is_odd {
103  static const bool value = (m & 1) == 1;
104 };
105 
106 template<bool cond, typename T, typename Then, typename Else>
107 struct value_if {
108  static const T value = Then::value;
109 };
110 
111 template<typename T, typename Then, typename Else>
112 struct value_if<false, T, Then, Else> {
113  static const T value = Else::value;
114 };
115 
116 template<typename T, T x>
118  static const T value = x;
119 };
120 
121 template<typename T, template<T> class Fn, T x, T p=0>
122 struct inverse {
123  static const T value =
124  value_if<Fn<p>::value == x, T,
126 };
127 
128 struct null_type{};
129 
130 template<typename T, T i, typename Tail=null_type>
131 struct cons_c {
132  static const T head = i;
133  typedef Tail tail;
134 };
135 
136 template<int k, int l>
137 struct static_range {
138  static const int head = k;
140 };
141 
142 template<int f>
143 struct static_range<f, f> {
144  static const int head = f;
145  typedef null_type tail;
146 };
147 
148 template<bool b, typename T=void>
149 struct enable_if {
150  typedef T type;
151 };
152 
153 template<typename T>
154 struct enable_if<false, T> {};
155 
156 template<typename T, int p>
158  static const bool value = (sizeof(T) & ((1 << p) - 1)) == 0;
159 };
160 
161 
162 }
Definition: aos.h:38
__host__ __device__ T sum(const array< T, s > &a)
Definition: utility.h:76
__host__ __device__ array< T, 0 > make_array()
Definition: array.h:130
head_type head
Definition: array.h:67
head_type head
Definition: array.h:38
tail_type tail
Definition: array.h:39
static const T head
Definition: utility.h:132
__host__ static __device__ array< T, 1 > impl(T v, T i=1)
Definition: utility.h:49
__host__ static __device__ Array impl(T v=0, T i=1)
Definition: utility.h:40
static const T value
Definition: utility.h:123
static const bool value
Definition: utility.h:103
static const bool value
Definition: utility.h:98
static const int value
Definition: utility.h:82
static const int head
Definition: utility.h:138
static_range< k+1, l > tail
Definition: utility.h:139
__host__ static __device__ T impl(const Array &a, const T &p)
Definition: utility.h:70
__host__ static __device__ T impl(const Array &a, const T &p)
Definition: utility.h:61
static const T value
Definition: utility.h:118
static const T value
Definition: utility.h:108