1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2015-2016 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li // workaround issue between gcc >= 4.7 and cuda 5.5
11*bf2c3715SXin Li #if (defined __GNUC__) && (__GNUC__>4 || __GNUC_MINOR__>=7)
12*bf2c3715SXin Li #undef _GLIBCXX_ATOMIC_BUILTINS
13*bf2c3715SXin Li #undef _GLIBCXX_USE_INT128
14*bf2c3715SXin Li #endif
15*bf2c3715SXin Li
16*bf2c3715SXin Li #define EIGEN_TEST_NO_LONGDOUBLE
17*bf2c3715SXin Li #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
18*bf2c3715SXin Li
19*bf2c3715SXin Li #include "main.h"
20*bf2c3715SXin Li #include "gpu_common.h"
21*bf2c3715SXin Li
22*bf2c3715SXin Li // Check that dense modules can be properly parsed by nvcc
23*bf2c3715SXin Li #include <Eigen/Dense>
24*bf2c3715SXin Li
25*bf2c3715SXin Li // struct Foo{
26*bf2c3715SXin Li // EIGEN_DEVICE_FUNC
27*bf2c3715SXin Li // void operator()(int i, const float* mats, float* vecs) const {
28*bf2c3715SXin Li // using namespace Eigen;
29*bf2c3715SXin Li // // Matrix3f M(data);
30*bf2c3715SXin Li // // Vector3f x(data+9);
31*bf2c3715SXin Li // // Map<Vector3f>(data+9) = M.inverse() * x;
32*bf2c3715SXin Li // Matrix3f M(mats+i/16);
33*bf2c3715SXin Li // Vector3f x(vecs+i*3);
34*bf2c3715SXin Li // // using std::min;
35*bf2c3715SXin Li // // using std::sqrt;
36*bf2c3715SXin Li // Map<Vector3f>(vecs+i*3) << x.minCoeff(), 1, 2;// / x.dot(x);//(M.inverse() * x) / x.x();
37*bf2c3715SXin Li // //x = x*2 + x.y() * x + x * x.maxCoeff() - x / x.sum();
38*bf2c3715SXin Li // }
39*bf2c3715SXin Li // };
40*bf2c3715SXin Li
41*bf2c3715SXin Li template<typename T>
42*bf2c3715SXin Li struct coeff_wise {
43*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()coeff_wise44*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
45*bf2c3715SXin Li {
46*bf2c3715SXin Li using namespace Eigen;
47*bf2c3715SXin Li T x1(in+i);
48*bf2c3715SXin Li T x2(in+i+1);
49*bf2c3715SXin Li T x3(in+i+2);
50*bf2c3715SXin Li Map<T> res(out+i*T::MaxSizeAtCompileTime);
51*bf2c3715SXin Li
52*bf2c3715SXin Li res.array() += (in[0] * x1 + x2).array() * x3.array();
53*bf2c3715SXin Li }
54*bf2c3715SXin Li };
55*bf2c3715SXin Li
56*bf2c3715SXin Li template<typename T>
57*bf2c3715SXin Li struct complex_sqrt {
58*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()complex_sqrt59*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
60*bf2c3715SXin Li {
61*bf2c3715SXin Li using namespace Eigen;
62*bf2c3715SXin Li typedef typename T::Scalar ComplexType;
63*bf2c3715SXin Li typedef typename T::Scalar::value_type ValueType;
64*bf2c3715SXin Li const int num_special_inputs = 18;
65*bf2c3715SXin Li
66*bf2c3715SXin Li if (i == 0) {
67*bf2c3715SXin Li const ValueType nan = std::numeric_limits<ValueType>::quiet_NaN();
68*bf2c3715SXin Li typedef Eigen::Vector<ComplexType, num_special_inputs> SpecialInputs;
69*bf2c3715SXin Li SpecialInputs special_in;
70*bf2c3715SXin Li special_in.setZero();
71*bf2c3715SXin Li int idx = 0;
72*bf2c3715SXin Li special_in[idx++] = ComplexType(0, 0);
73*bf2c3715SXin Li special_in[idx++] = ComplexType(-0, 0);
74*bf2c3715SXin Li special_in[idx++] = ComplexType(0, -0);
75*bf2c3715SXin Li special_in[idx++] = ComplexType(-0, -0);
76*bf2c3715SXin Li // GCC's fallback sqrt implementation fails for inf inputs.
77*bf2c3715SXin Li // It is called when _GLIBCXX_USE_C99_COMPLEX is false or if
78*bf2c3715SXin Li // clang includes the GCC header (which temporarily disables
79*bf2c3715SXin Li // _GLIBCXX_USE_C99_COMPLEX)
80*bf2c3715SXin Li #if !defined(_GLIBCXX_COMPLEX) || \
81*bf2c3715SXin Li (_GLIBCXX_USE_C99_COMPLEX && !defined(__CLANG_CUDA_WRAPPERS_COMPLEX))
82*bf2c3715SXin Li const ValueType inf = std::numeric_limits<ValueType>::infinity();
83*bf2c3715SXin Li special_in[idx++] = ComplexType(1.0, inf);
84*bf2c3715SXin Li special_in[idx++] = ComplexType(nan, inf);
85*bf2c3715SXin Li special_in[idx++] = ComplexType(1.0, -inf);
86*bf2c3715SXin Li special_in[idx++] = ComplexType(nan, -inf);
87*bf2c3715SXin Li special_in[idx++] = ComplexType(-inf, 1.0);
88*bf2c3715SXin Li special_in[idx++] = ComplexType(inf, 1.0);
89*bf2c3715SXin Li special_in[idx++] = ComplexType(-inf, -1.0);
90*bf2c3715SXin Li special_in[idx++] = ComplexType(inf, -1.0);
91*bf2c3715SXin Li special_in[idx++] = ComplexType(-inf, nan);
92*bf2c3715SXin Li special_in[idx++] = ComplexType(inf, nan);
93*bf2c3715SXin Li #endif
94*bf2c3715SXin Li special_in[idx++] = ComplexType(1.0, nan);
95*bf2c3715SXin Li special_in[idx++] = ComplexType(nan, 1.0);
96*bf2c3715SXin Li special_in[idx++] = ComplexType(nan, -1.0);
97*bf2c3715SXin Li special_in[idx++] = ComplexType(nan, nan);
98*bf2c3715SXin Li
99*bf2c3715SXin Li Map<SpecialInputs> special_out(out);
100*bf2c3715SXin Li special_out = special_in.cwiseSqrt();
101*bf2c3715SXin Li }
102*bf2c3715SXin Li
103*bf2c3715SXin Li T x1(in + i);
104*bf2c3715SXin Li Map<T> res(out + num_special_inputs + i*T::MaxSizeAtCompileTime);
105*bf2c3715SXin Li res = x1.cwiseSqrt();
106*bf2c3715SXin Li }
107*bf2c3715SXin Li };
108*bf2c3715SXin Li
109*bf2c3715SXin Li template<typename T>
110*bf2c3715SXin Li struct complex_operators {
111*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()complex_operators112*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
113*bf2c3715SXin Li {
114*bf2c3715SXin Li using namespace Eigen;
115*bf2c3715SXin Li typedef typename T::Scalar ComplexType;
116*bf2c3715SXin Li typedef typename T::Scalar::value_type ValueType;
117*bf2c3715SXin Li const int num_scalar_operators = 24;
118*bf2c3715SXin Li const int num_vector_operators = 23; // no unary + operator.
119*bf2c3715SXin Li int out_idx = i * (num_scalar_operators + num_vector_operators * T::MaxSizeAtCompileTime);
120*bf2c3715SXin Li
121*bf2c3715SXin Li // Scalar operators.
122*bf2c3715SXin Li const ComplexType a = in[i];
123*bf2c3715SXin Li const ComplexType b = in[i + 1];
124*bf2c3715SXin Li
125*bf2c3715SXin Li out[out_idx++] = +a;
126*bf2c3715SXin Li out[out_idx++] = -a;
127*bf2c3715SXin Li
128*bf2c3715SXin Li out[out_idx++] = a + b;
129*bf2c3715SXin Li out[out_idx++] = a + numext::real(b);
130*bf2c3715SXin Li out[out_idx++] = numext::real(a) + b;
131*bf2c3715SXin Li out[out_idx++] = a - b;
132*bf2c3715SXin Li out[out_idx++] = a - numext::real(b);
133*bf2c3715SXin Li out[out_idx++] = numext::real(a) - b;
134*bf2c3715SXin Li out[out_idx++] = a * b;
135*bf2c3715SXin Li out[out_idx++] = a * numext::real(b);
136*bf2c3715SXin Li out[out_idx++] = numext::real(a) * b;
137*bf2c3715SXin Li out[out_idx++] = a / b;
138*bf2c3715SXin Li out[out_idx++] = a / numext::real(b);
139*bf2c3715SXin Li out[out_idx++] = numext::real(a) / b;
140*bf2c3715SXin Li
141*bf2c3715SXin Li out[out_idx] = a; out[out_idx++] += b;
142*bf2c3715SXin Li out[out_idx] = a; out[out_idx++] -= b;
143*bf2c3715SXin Li out[out_idx] = a; out[out_idx++] *= b;
144*bf2c3715SXin Li out[out_idx] = a; out[out_idx++] /= b;
145*bf2c3715SXin Li
146*bf2c3715SXin Li const ComplexType true_value = ComplexType(ValueType(1), ValueType(0));
147*bf2c3715SXin Li const ComplexType false_value = ComplexType(ValueType(0), ValueType(0));
148*bf2c3715SXin Li out[out_idx++] = (a == b ? true_value : false_value);
149*bf2c3715SXin Li out[out_idx++] = (a == numext::real(b) ? true_value : false_value);
150*bf2c3715SXin Li out[out_idx++] = (numext::real(a) == b ? true_value : false_value);
151*bf2c3715SXin Li out[out_idx++] = (a != b ? true_value : false_value);
152*bf2c3715SXin Li out[out_idx++] = (a != numext::real(b) ? true_value : false_value);
153*bf2c3715SXin Li out[out_idx++] = (numext::real(a) != b ? true_value : false_value);
154*bf2c3715SXin Li
155*bf2c3715SXin Li // Vector versions.
156*bf2c3715SXin Li T x1(in + i);
157*bf2c3715SXin Li T x2(in + i + 1);
158*bf2c3715SXin Li const int res_size = T::MaxSizeAtCompileTime * num_scalar_operators;
159*bf2c3715SXin Li const int size = T::MaxSizeAtCompileTime;
160*bf2c3715SXin Li int block_idx = 0;
161*bf2c3715SXin Li
162*bf2c3715SXin Li Map<VectorX<ComplexType>> res(out + out_idx, res_size);
163*bf2c3715SXin Li res.segment(block_idx, size) = -x1;
164*bf2c3715SXin Li block_idx += size;
165*bf2c3715SXin Li
166*bf2c3715SXin Li res.segment(block_idx, size) = x1 + x2;
167*bf2c3715SXin Li block_idx += size;
168*bf2c3715SXin Li res.segment(block_idx, size) = x1 + x2.real();
169*bf2c3715SXin Li block_idx += size;
170*bf2c3715SXin Li res.segment(block_idx, size) = x1.real() + x2;
171*bf2c3715SXin Li block_idx += size;
172*bf2c3715SXin Li res.segment(block_idx, size) = x1 - x2;
173*bf2c3715SXin Li block_idx += size;
174*bf2c3715SXin Li res.segment(block_idx, size) = x1 - x2.real();
175*bf2c3715SXin Li block_idx += size;
176*bf2c3715SXin Li res.segment(block_idx, size) = x1.real() - x2;
177*bf2c3715SXin Li block_idx += size;
178*bf2c3715SXin Li res.segment(block_idx, size) = x1.array() * x2.array();
179*bf2c3715SXin Li block_idx += size;
180*bf2c3715SXin Li res.segment(block_idx, size) = x1.array() * x2.real().array();
181*bf2c3715SXin Li block_idx += size;
182*bf2c3715SXin Li res.segment(block_idx, size) = x1.real().array() * x2.array();
183*bf2c3715SXin Li block_idx += size;
184*bf2c3715SXin Li res.segment(block_idx, size) = x1.array() / x2.array();
185*bf2c3715SXin Li block_idx += size;
186*bf2c3715SXin Li res.segment(block_idx, size) = x1.array() / x2.real().array();
187*bf2c3715SXin Li block_idx += size;
188*bf2c3715SXin Li res.segment(block_idx, size) = x1.real().array() / x2.array();
189*bf2c3715SXin Li block_idx += size;
190*bf2c3715SXin Li
191*bf2c3715SXin Li res.segment(block_idx, size) = x1; res.segment(block_idx, size) += x2;
192*bf2c3715SXin Li block_idx += size;
193*bf2c3715SXin Li res.segment(block_idx, size) = x1; res.segment(block_idx, size) -= x2;
194*bf2c3715SXin Li block_idx += size;
195*bf2c3715SXin Li res.segment(block_idx, size) = x1; res.segment(block_idx, size).array() *= x2.array();
196*bf2c3715SXin Li block_idx += size;
197*bf2c3715SXin Li res.segment(block_idx, size) = x1; res.segment(block_idx, size).array() /= x2.array();
198*bf2c3715SXin Li block_idx += size;
199*bf2c3715SXin Li
200*bf2c3715SXin Li const T true_vector = T::Constant(true_value);
201*bf2c3715SXin Li const T false_vector = T::Constant(false_value);
202*bf2c3715SXin Li res.segment(block_idx, size) = (x1 == x2 ? true_vector : false_vector);
203*bf2c3715SXin Li block_idx += size;
204*bf2c3715SXin Li // Mixing types in equality comparison does not work.
205*bf2c3715SXin Li // res.segment(block_idx, size) = (x1 == x2.real() ? true_vector : false_vector);
206*bf2c3715SXin Li // block_idx += size;
207*bf2c3715SXin Li // res.segment(block_idx, size) = (x1.real() == x2 ? true_vector : false_vector);
208*bf2c3715SXin Li // block_idx += size;
209*bf2c3715SXin Li res.segment(block_idx, size) = (x1 != x2 ? true_vector : false_vector);
210*bf2c3715SXin Li block_idx += size;
211*bf2c3715SXin Li // res.segment(block_idx, size) = (x1 != x2.real() ? true_vector : false_vector);
212*bf2c3715SXin Li // block_idx += size;
213*bf2c3715SXin Li // res.segment(block_idx, size) = (x1.real() != x2 ? true_vector : false_vector);
214*bf2c3715SXin Li // block_idx += size;
215*bf2c3715SXin Li }
216*bf2c3715SXin Li };
217*bf2c3715SXin Li
218*bf2c3715SXin Li template<typename T>
219*bf2c3715SXin Li struct replicate {
220*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()replicate221*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
222*bf2c3715SXin Li {
223*bf2c3715SXin Li using namespace Eigen;
224*bf2c3715SXin Li T x1(in+i);
225*bf2c3715SXin Li int step = x1.size() * 4;
226*bf2c3715SXin Li int stride = 3 * step;
227*bf2c3715SXin Li
228*bf2c3715SXin Li typedef Map<Array<typename T::Scalar,Dynamic,Dynamic> > MapType;
229*bf2c3715SXin Li MapType(out+i*stride+0*step, x1.rows()*2, x1.cols()*2) = x1.replicate(2,2);
230*bf2c3715SXin Li MapType(out+i*stride+1*step, x1.rows()*3, x1.cols()) = in[i] * x1.colwise().replicate(3);
231*bf2c3715SXin Li MapType(out+i*stride+2*step, x1.rows(), x1.cols()*3) = in[i] * x1.rowwise().replicate(3);
232*bf2c3715SXin Li }
233*bf2c3715SXin Li };
234*bf2c3715SXin Li
235*bf2c3715SXin Li template<typename T>
236*bf2c3715SXin Li struct alloc_new_delete {
237*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()alloc_new_delete238*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
239*bf2c3715SXin Li {
240*bf2c3715SXin Li int offset = 2*i*T::MaxSizeAtCompileTime;
241*bf2c3715SXin Li T* x = new T(in + offset);
242*bf2c3715SXin Li Eigen::Map<T> u(out + offset);
243*bf2c3715SXin Li u = *x;
244*bf2c3715SXin Li delete x;
245*bf2c3715SXin Li
246*bf2c3715SXin Li offset += T::MaxSizeAtCompileTime;
247*bf2c3715SXin Li T* y = new T[1];
248*bf2c3715SXin Li y[0] = T(in + offset);
249*bf2c3715SXin Li Eigen::Map<T> v(out + offset);
250*bf2c3715SXin Li v = y[0];
251*bf2c3715SXin Li delete[] y;
252*bf2c3715SXin Li }
253*bf2c3715SXin Li };
254*bf2c3715SXin Li
255*bf2c3715SXin Li template<typename T>
256*bf2c3715SXin Li struct redux {
257*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()redux258*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
259*bf2c3715SXin Li {
260*bf2c3715SXin Li using namespace Eigen;
261*bf2c3715SXin Li int N = 10;
262*bf2c3715SXin Li T x1(in+i);
263*bf2c3715SXin Li out[i*N+0] = x1.minCoeff();
264*bf2c3715SXin Li out[i*N+1] = x1.maxCoeff();
265*bf2c3715SXin Li out[i*N+2] = x1.sum();
266*bf2c3715SXin Li out[i*N+3] = x1.prod();
267*bf2c3715SXin Li out[i*N+4] = x1.matrix().squaredNorm();
268*bf2c3715SXin Li out[i*N+5] = x1.matrix().norm();
269*bf2c3715SXin Li out[i*N+6] = x1.colwise().sum().maxCoeff();
270*bf2c3715SXin Li out[i*N+7] = x1.rowwise().maxCoeff().sum();
271*bf2c3715SXin Li out[i*N+8] = x1.matrix().colwise().squaredNorm().sum();
272*bf2c3715SXin Li }
273*bf2c3715SXin Li };
274*bf2c3715SXin Li
275*bf2c3715SXin Li template<typename T1, typename T2>
276*bf2c3715SXin Li struct prod_test {
277*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()prod_test278*bf2c3715SXin Li void operator()(int i, const typename T1::Scalar* in, typename T1::Scalar* out) const
279*bf2c3715SXin Li {
280*bf2c3715SXin Li using namespace Eigen;
281*bf2c3715SXin Li typedef Matrix<typename T1::Scalar, T1::RowsAtCompileTime, T2::ColsAtCompileTime> T3;
282*bf2c3715SXin Li T1 x1(in+i);
283*bf2c3715SXin Li T2 x2(in+i+1);
284*bf2c3715SXin Li Map<T3> res(out+i*T3::MaxSizeAtCompileTime);
285*bf2c3715SXin Li res += in[i] * x1 * x2;
286*bf2c3715SXin Li }
287*bf2c3715SXin Li };
288*bf2c3715SXin Li
289*bf2c3715SXin Li template<typename T1, typename T2>
290*bf2c3715SXin Li struct diagonal {
291*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()diagonal292*bf2c3715SXin Li void operator()(int i, const typename T1::Scalar* in, typename T1::Scalar* out) const
293*bf2c3715SXin Li {
294*bf2c3715SXin Li using namespace Eigen;
295*bf2c3715SXin Li T1 x1(in+i);
296*bf2c3715SXin Li Map<T2> res(out+i*T2::MaxSizeAtCompileTime);
297*bf2c3715SXin Li res += x1.diagonal();
298*bf2c3715SXin Li }
299*bf2c3715SXin Li };
300*bf2c3715SXin Li
301*bf2c3715SXin Li template<typename T>
302*bf2c3715SXin Li struct eigenvalues_direct {
303*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()eigenvalues_direct304*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
305*bf2c3715SXin Li {
306*bf2c3715SXin Li using namespace Eigen;
307*bf2c3715SXin Li typedef Matrix<typename T::Scalar, T::RowsAtCompileTime, 1> Vec;
308*bf2c3715SXin Li T M(in+i);
309*bf2c3715SXin Li Map<Vec> res(out+i*Vec::MaxSizeAtCompileTime);
310*bf2c3715SXin Li T A = M*M.adjoint();
311*bf2c3715SXin Li SelfAdjointEigenSolver<T> eig;
312*bf2c3715SXin Li eig.computeDirect(A);
313*bf2c3715SXin Li res = eig.eigenvalues();
314*bf2c3715SXin Li }
315*bf2c3715SXin Li };
316*bf2c3715SXin Li
317*bf2c3715SXin Li template<typename T>
318*bf2c3715SXin Li struct eigenvalues {
319*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()eigenvalues320*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
321*bf2c3715SXin Li {
322*bf2c3715SXin Li using namespace Eigen;
323*bf2c3715SXin Li typedef Matrix<typename T::Scalar, T::RowsAtCompileTime, 1> Vec;
324*bf2c3715SXin Li T M(in+i);
325*bf2c3715SXin Li Map<Vec> res(out+i*Vec::MaxSizeAtCompileTime);
326*bf2c3715SXin Li T A = M*M.adjoint();
327*bf2c3715SXin Li SelfAdjointEigenSolver<T> eig;
328*bf2c3715SXin Li eig.compute(A);
329*bf2c3715SXin Li res = eig.eigenvalues();
330*bf2c3715SXin Li }
331*bf2c3715SXin Li };
332*bf2c3715SXin Li
333*bf2c3715SXin Li template<typename T>
334*bf2c3715SXin Li struct matrix_inverse {
335*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()matrix_inverse336*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
337*bf2c3715SXin Li {
338*bf2c3715SXin Li using namespace Eigen;
339*bf2c3715SXin Li T M(in+i);
340*bf2c3715SXin Li Map<T> res(out+i*T::MaxSizeAtCompileTime);
341*bf2c3715SXin Li res = M.inverse();
342*bf2c3715SXin Li }
343*bf2c3715SXin Li };
344*bf2c3715SXin Li
345*bf2c3715SXin Li template<typename T>
346*bf2c3715SXin Li struct numeric_limits_test {
347*bf2c3715SXin Li EIGEN_DEVICE_FUNC
operator ()numeric_limits_test348*bf2c3715SXin Li void operator()(int i, const typename T::Scalar* in, typename T::Scalar* out) const
349*bf2c3715SXin Li {
350*bf2c3715SXin Li EIGEN_UNUSED_VARIABLE(in)
351*bf2c3715SXin Li int out_idx = i * 5;
352*bf2c3715SXin Li out[out_idx++] = numext::numeric_limits<float>::epsilon();
353*bf2c3715SXin Li out[out_idx++] = (numext::numeric_limits<float>::max)();
354*bf2c3715SXin Li out[out_idx++] = (numext::numeric_limits<float>::min)();
355*bf2c3715SXin Li out[out_idx++] = numext::numeric_limits<float>::infinity();
356*bf2c3715SXin Li out[out_idx++] = numext::numeric_limits<float>::quiet_NaN();
357*bf2c3715SXin Li }
358*bf2c3715SXin Li };
359*bf2c3715SXin Li
360*bf2c3715SXin Li template<typename Type1, typename Type2>
verifyIsApproxWithInfsNans(const Type1 & a,const Type2 & b,typename Type1::Scalar * =0)361*bf2c3715SXin Li bool verifyIsApproxWithInfsNans(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
362*bf2c3715SXin Li {
363*bf2c3715SXin Li if (a.rows() != b.rows()) {
364*bf2c3715SXin Li return false;
365*bf2c3715SXin Li }
366*bf2c3715SXin Li if (a.cols() != b.cols()) {
367*bf2c3715SXin Li return false;
368*bf2c3715SXin Li }
369*bf2c3715SXin Li for (Index r = 0; r < a.rows(); ++r) {
370*bf2c3715SXin Li for (Index c = 0; c < a.cols(); ++c) {
371*bf2c3715SXin Li if (a(r, c) != b(r, c)
372*bf2c3715SXin Li && !((numext::isnan)(a(r, c)) && (numext::isnan)(b(r, c)))
373*bf2c3715SXin Li && !test_isApprox(a(r, c), b(r, c))) {
374*bf2c3715SXin Li return false;
375*bf2c3715SXin Li }
376*bf2c3715SXin Li }
377*bf2c3715SXin Li }
378*bf2c3715SXin Li return true;
379*bf2c3715SXin Li }
380*bf2c3715SXin Li
381*bf2c3715SXin Li template<typename Kernel, typename Input, typename Output>
test_with_infs_nans(const Kernel & ker,int n,const Input & in,Output & out)382*bf2c3715SXin Li void test_with_infs_nans(const Kernel& ker, int n, const Input& in, Output& out)
383*bf2c3715SXin Li {
384*bf2c3715SXin Li Output out_ref, out_gpu;
385*bf2c3715SXin Li #if !defined(EIGEN_GPU_COMPILE_PHASE)
386*bf2c3715SXin Li out_ref = out_gpu = out;
387*bf2c3715SXin Li #else
388*bf2c3715SXin Li EIGEN_UNUSED_VARIABLE(in);
389*bf2c3715SXin Li EIGEN_UNUSED_VARIABLE(out);
390*bf2c3715SXin Li #endif
391*bf2c3715SXin Li run_on_cpu (ker, n, in, out_ref);
392*bf2c3715SXin Li run_on_gpu(ker, n, in, out_gpu);
393*bf2c3715SXin Li #if !defined(EIGEN_GPU_COMPILE_PHASE)
394*bf2c3715SXin Li verifyIsApproxWithInfsNans(out_ref, out_gpu);
395*bf2c3715SXin Li #endif
396*bf2c3715SXin Li }
397*bf2c3715SXin Li
EIGEN_DECLARE_TEST(gpu_basic)398*bf2c3715SXin Li EIGEN_DECLARE_TEST(gpu_basic)
399*bf2c3715SXin Li {
400*bf2c3715SXin Li ei_test_init_gpu();
401*bf2c3715SXin Li
402*bf2c3715SXin Li int nthreads = 100;
403*bf2c3715SXin Li Eigen::VectorXf in, out;
404*bf2c3715SXin Li Eigen::VectorXcf cfin, cfout;
405*bf2c3715SXin Li
406*bf2c3715SXin Li #if !defined(EIGEN_GPU_COMPILE_PHASE)
407*bf2c3715SXin Li int data_size = nthreads * 512;
408*bf2c3715SXin Li in.setRandom(data_size);
409*bf2c3715SXin Li out.setConstant(data_size, -1);
410*bf2c3715SXin Li cfin.setRandom(data_size);
411*bf2c3715SXin Li cfout.setConstant(data_size, -1);
412*bf2c3715SXin Li #endif
413*bf2c3715SXin Li
414*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(coeff_wise<Vector3f>(), nthreads, in, out) );
415*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(coeff_wise<Array44f>(), nthreads, in, out) );
416*bf2c3715SXin Li
417*bf2c3715SXin Li #if !defined(EIGEN_USE_HIP)
418*bf2c3715SXin Li // FIXME
419*bf2c3715SXin Li // These subtests result in a compile failure on the HIP platform
420*bf2c3715SXin Li //
421*bf2c3715SXin Li // eigen-upstream/Eigen/src/Core/Replicate.h:61:65: error:
422*bf2c3715SXin Li // base class 'internal::dense_xpr_base<Replicate<Array<float, 4, 1, 0, 4, 1>, -1, -1> >::type'
423*bf2c3715SXin Li // (aka 'ArrayBase<Eigen::Replicate<Eigen::Array<float, 4, 1, 0, 4, 1>, -1, -1> >') has protected default constructor
424*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(replicate<Array4f>(), nthreads, in, out) );
425*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(replicate<Array33f>(), nthreads, in, out) );
426*bf2c3715SXin Li
427*bf2c3715SXin Li // HIP does not support new/delete on device.
428*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(alloc_new_delete<Vector3f>(), nthreads, in, out) );
429*bf2c3715SXin Li #endif
430*bf2c3715SXin Li
431*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(redux<Array4f>(), nthreads, in, out) );
432*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(redux<Matrix3f>(), nthreads, in, out) );
433*bf2c3715SXin Li
434*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(prod_test<Matrix3f,Matrix3f>(), nthreads, in, out) );
435*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(prod_test<Matrix4f,Vector4f>(), nthreads, in, out) );
436*bf2c3715SXin Li
437*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(diagonal<Matrix3f,Vector3f>(), nthreads, in, out) );
438*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(diagonal<Matrix4f,Vector4f>(), nthreads, in, out) );
439*bf2c3715SXin Li
440*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(matrix_inverse<Matrix2f>(), nthreads, in, out) );
441*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(matrix_inverse<Matrix3f>(), nthreads, in, out) );
442*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(matrix_inverse<Matrix4f>(), nthreads, in, out) );
443*bf2c3715SXin Li
444*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(eigenvalues_direct<Matrix3f>(), nthreads, in, out) );
445*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(eigenvalues_direct<Matrix2f>(), nthreads, in, out) );
446*bf2c3715SXin Li
447*bf2c3715SXin Li // Test std::complex.
448*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(complex_operators<Vector3cf>(), nthreads, cfin, cfout) );
449*bf2c3715SXin Li CALL_SUBTEST( test_with_infs_nans(complex_sqrt<Vector3cf>(), nthreads, cfin, cfout) );
450*bf2c3715SXin Li
451*bf2c3715SXin Li // numeric_limits
452*bf2c3715SXin Li CALL_SUBTEST( test_with_infs_nans(numeric_limits_test<Vector3f>(), 1, in, out) );
453*bf2c3715SXin Li
454*bf2c3715SXin Li #if defined(__NVCC__)
455*bf2c3715SXin Li // FIXME
456*bf2c3715SXin Li // These subtests compiles only with nvcc and fail with HIPCC and clang-cuda
457*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(eigenvalues<Matrix4f>(), nthreads, in, out) );
458*bf2c3715SXin Li typedef Matrix<float,6,6> Matrix6f;
459*bf2c3715SXin Li CALL_SUBTEST( run_and_compare_to_gpu(eigenvalues<Matrix6f>(), nthreads, in, out) );
460*bf2c3715SXin Li #endif
461*bf2c3715SXin Li }
462