1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/defines.h>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <complex>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::abs;
using af::cfloat;
using af::cdouble;
///////////////////////////////// CPP ////////////////////////////////////
//
template<typename T>
void solveTester(const int m, const int n, const int k, double eps)
{
af::deviceGC();
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
#if 1
af::array A = cpu_randu<T>(af::dim4(m, n));
af::array X0 = cpu_randu<T>(af::dim4(n, k));
#else
af::array A = af::randu(m, n, (af::dtype)af::dtype_traits<T>::af_type);
af::array X0 = af::randu(n, k, (af::dtype)af::dtype_traits<T>::af_type);
#endif
af::array B0 = af::matmul(A, X0);
//! [ex_solve]
af::array X1 = af::solve(A, B0);
//! [ex_solve]
//! [ex_solve_recon]
af::array B1 = af::matmul(A, X1);
//! [ex_solve_recon]
ASSERT_NEAR(0, af::sum<double>(af::abs(real(B0 - B1))) / (m * k), eps);
ASSERT_NEAR(0, af::sum<double>(af::abs(imag(B0 - B1))) / (m * k), eps);
}
template<typename T>
void solveLUTester(const int n, const int k, double eps)
{
af::deviceGC();
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
#if 1
af::array A = cpu_randu<T>(af::dim4(n, n));
af::array X0 = cpu_randu<T>(af::dim4(n, k));
#else
af::array A = af::randu(n, n, (af::dtype)af::dtype_traits<T>::af_type);
af::array X0 = af::randu(n, k, (af::dtype)af::dtype_traits<T>::af_type);
#endif
af::array B0 = af::matmul(A, X0);
//! [ex_solve_lu]
af::array A_lu, pivot;
af::lu(A_lu, pivot, A);
af::array X1 = af::solveLU(A_lu, pivot, B0);
//! [ex_solve_lu]
af::array B1 = af::matmul(A, X1);
ASSERT_NEAR(0, af::sum<double>(af::abs(real(B0 - B1))) / (n * k), eps);
ASSERT_NEAR(0, af::sum<double>(af::abs(imag(B0 - B1))) / (n * k), eps);
}
template<typename T>
void solveTriangleTester(const int n, const int k, bool is_upper, double eps)
{
af::deviceGC();
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
#if 1
af::array A = cpu_randu<T>(af::dim4(n, n));
af::array X0 = cpu_randu<T>(af::dim4(n, k));
#else
af::array A = af::randu(n, n, (af::dtype)af::dtype_traits<T>::af_type);
af::array X0 = af::randu(n, k, (af::dtype)af::dtype_traits<T>::af_type);
#endif
af::array L, U, pivot;
af::lu(L, U, pivot, A);
af::array AT = is_upper ? U : L;
af::array B0 = af::matmul(AT, X0);
af::array X1;
if (is_upper) {
//! [ex_solve_upper]
af::array X = af::solve(AT, B0, AF_MAT_UPPER);
//! [ex_solve_upper]
X1 = X;
} else {
//! [ex_solve_lower]
af::array X = af::solve(AT, B0, AF_MAT_LOWER);
//! [ex_solve_lower]
X1 = X;
}
af::array B1 = af::matmul(AT, X1);
ASSERT_NEAR(0, af::sum<double>(af::abs(real(B0 - B1))) / (n * k), eps);
ASSERT_NEAR(0, af::sum<double>(af::abs(imag(B0 - B1))) / (n * k), eps);
}
#define SOLVE_TESTS(T, eps) \
TEST(SOLVE_LU, T##Reg) \
{ \
solveLUTester<T>(1000, 100, eps); \
} \
TEST(SOLVE_LU, T##RegMultiple) \
{ \
solveLUTester<T>(2048, 512, eps); \
} \
TEST(SOLVE_Upper, T##Reg) \
{ \
solveTriangleTester<T>(1000, 100, true, eps); \
} \
TEST(SOLVE_Upper, T##RegMultiple) \
{ \
solveTriangleTester<T>(2048, 512, true, eps); \
} \
TEST(SOLVE_Lower, T##Reg) \
{ \
solveTriangleTester<T>(1000, 100, false, eps); \
} \
TEST(SOLVE_Lower, T##RegMultiple) \
{ \
solveTriangleTester<T>(2048, 512, false, eps); \
} \
TEST(SOLVE, T##Square) \
{ \
solveTester<T>(1000, 1000, 100, eps); \
} \
TEST(SOLVE, T##SquareMultiple) \
{ \
solveTester<T>(2048, 2048, 512, eps); \
} \
TEST(SOLVE, T##RectUnder) \
{ \
solveTester<T>(800, 1000, 200, eps); \
} \
TEST(SOLVE, T##RectUnderMultiple) \
{ \
solveTester<T>(1536, 2048, 400, eps); \
} \
TEST(SOLVE, T##RectOverMultiple) \
{ \
solveTester<T>(1536, 1024, 1, eps); \
}
SOLVE_TESTS(float, 0.01)
SOLVE_TESTS(double, 1E-5)
SOLVE_TESTS(cfloat, 0.01)
SOLVE_TESTS(cdouble, 1E-5)
#undef SOLVE_TESTS
#define SOLVE_TESTS(T, eps) \
TEST(SOLVE, T##RectOver) \
{ \
solveTester<T>(800, 600, 64, eps); \
}
SOLVE_TESTS(float, 0.01)
SOLVE_TESTS(double, 1E-5)
SOLVE_TESTS(cfloat, 0.01)
SOLVE_TESTS(cdouble, 1E-5)
#undef SOLVE_TESTS
|