ViennaCL - The Vienna Computing Library: tests/src/bisect.cpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bisect.cpp
Go to the documentation of this file.
1 /* =========================================================================
2 Copyright (c) 2010-2016, Institute for Microelectronics,
3 Institute for Analysis and Scientific Computing,
4 TU Wien.
5 Portions of this software are copyright by UChicago Argonne, LLC.
6
7 -----------------
8 ViennaCL - The Vienna Computing Library
9 -----------------
10
11 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12
13 (A list of authors and contributors can be found in the PDF manual)
14
15 License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17
22 // includes, system
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27
28 // includes, project
29
30 #include "viennacl/scalar.hpp"
31 #include "viennacl/vector.hpp"
32
33
34 #include "viennacl/linalg/bisect.hpp"
35 #include "viennacl/linalg/bisect_gpu.hpp"
36 #include "viennacl/linalg/tql2.hpp"
37
38 #define EPS 10.0e-4
39
40 typedef float NumericT;
42 // declaration, forward
43 bool runTest(unsigned int mat_size);
44
52 template<typename NumericT>
53 void initInputData(std::vector<NumericT> &diagonal, std::vector<NumericT> &superdiagonal, unsigned int mat_size)
54 {
55
56 srand(278217421);
57
58 #define RANDOM_VALUES false
59
60 if (RANDOM_VALUES == true)
61 {
62 // Initialize diagonal and superdiagonal elements with random values
63 for (unsigned int i = 0; i < mat_size; ++i)
64 {
65 diagonal[i] = static_cast<NumericT>(2.0 * (((double)rand()
66 / (double) RAND_MAX) - 0.5));
67 superdiagonal[i] = static_cast<NumericT>(2.0 * (((double)rand()
68 / (double) RAND_MAX) - 0.5));
69 }
70 }
71 else
72 {
73 // Initialize diagonal and superdiagonal elements with modulo values
74 // This will cause in many multiple eigenvalues.
75 for (unsigned int i = 0; i < mat_size; ++i)
76 {
77 diagonal[i] = ((NumericT)(i % 3)) - 4.5f;
78 superdiagonal[i] = ((NumericT)(i % 3)) - 5.5f;
79 }
80 }
81 // the first element of s is used as padding on the device (thus the
82 // whole vector is copied to the device but the kernels are launched
83 // with (s+1) as start address
84 superdiagonal[0] = 0.0f;
85 }
86
87
89 // Program main
91 int main()
92 {
93 bool test_result = false;
94
95 // run test for large matrix
96 test_result = runTest(550);
97 if(test_result == true)
98 {
99 std::cout << "First Test Succeeded!" << std::endl << std::endl;
100 }
101 else
102 {
103 std::cout << "---TEST FAILED---" << std::endl;
104 return EXIT_FAILURE;
105 }
106
107 // run test for small matrix
108 test_result = runTest(96);
109
110 if(test_result == true)
111 {
112 std::cout << std::endl << "---TEST SUCCESSFULLY COMPLETED---" << std::endl;
113 return EXIT_SUCCESS;
114 }
115 else
116 {
117 std::cout << "---TEST FAILED---" << std::endl;
118 return EXIT_FAILURE;
119 }
120 }
121
125 bool runTest(unsigned int mat_size)
126 {
127 bool bResult = false;
128
129 std::vector<NumericT> diagonal(mat_size);
130 std::vector<NumericT> superdiagonal(mat_size);
131 std::vector<NumericT> eigenvalues_bisect(mat_size);
132
133 // -------------Initialize data-------------------
134 // Fill the diagonal and superdiagonal elements of the vector
135 initInputData(diagonal, superdiagonal, mat_size);
136
137 // -------Start the bisection algorithm------------
138 std::cout << "Start the bisection algorithm" << std::endl;
139 std::cout << "Matrix size: " << mat_size << std::endl;
140 bResult = viennacl::linalg::bisect(diagonal, superdiagonal, eigenvalues_bisect);
141 // Exit if an error occured during the execution of the algorithm
142 if (bResult == false)
143 return false;
144
145 // ---------------Check the results---------------
146 // The results of the bisection algorithm will be checked with the tql algorithm
147 // Initialize Data for tql1 algorithm
148 std::vector<NumericT> diagonal_tql(mat_size);
149 std::vector<NumericT> superdiagonal_tql(mat_size);
150 diagonal_tql = diagonal;
151 superdiagonal_tql = superdiagonal;
152
153 // Start the tql algorithm
154 std::cout << "Start the tql algorithm..." << std::endl;
155 viennacl::linalg::tql1<NumericT>(mat_size, diagonal_tql, superdiagonal_tql);
156
157 // Ensure that eigenvalues from tql1 algorithm are sorted in ascending order
158 std::sort(diagonal_tql.begin(), diagonal_tql.end());
159
160 // Compare the results from the bisection algorithm with the results
161 // from the tql algorithm.
162 std::cout << "Start comparison..." << std::endl;
163 for (unsigned int i = 0; i < mat_size; i++)
164 {
165 if (std::abs(diagonal_tql[i] - eigenvalues_bisect[i]) > EPS)
166 {
167 std::cout << std::setprecision(12) << diagonal_tql[i] << " != " << eigenvalues_bisect[i] << "\n";
168 return false;
169 }
170 }
171 /*
172 // ------------Print the results---------------
173 std::cout << "mat_size = " << mat_size << std::endl;
174 for (unsigned int i = 0; i < mat_size; ++i)
175 {
176 std::cout << "Eigenvalue " << i << ": \tbisect: " << std::setprecision(14) << eigenvalues_bisect[i] << "\ttql: " << diagonal_tql[i] << std::endl;
177 }
178 */
179 return bResult;
180
181 }
EPS
#define EPS
Definition: bisect.cpp:38
main
int main()
Definition: bisect.cpp:91
tql2.hpp
Implementation of the tql2-algorithm for eigenvalue computations.
bisect_gpu.hpp
Implementation of an bisection algorithm for eigenvalues.
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::linalg::bisect
std::vector< typename viennacl::result_of::cpu_value_type< typename VectorT::value_type >::type > bisect(VectorT const &alphas, VectorT const &betas)
Implementation of the bisect-algorithm for the calculation of the eigenvalues of a tridiagonal matrix...
Definition: bisect.hpp:78
runTest
bool runTest(unsigned int mat_size)
Run a simple test.
Definition: bisect.cpp:125
initInputData
void initInputData(std::vector< NumericT > &diagonal, std::vector< NumericT > &superdiagonal, unsigned int mat_size)
initInputData Initialize the diagonal and superdiagonal elements of the matrix
Definition: bisect.cpp:53
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
RANDOM_VALUES
#define RANDOM_VALUES
bisect.hpp
Implementation of the algorithm for finding eigenvalues of a tridiagonal matrix.
scalar.hpp
Implementation of the ViennaCL scalar class.