ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
linear_test_utils.h
Go to the documentation of this file.
1#ifndef HSOLVER_LINEAR_TEST_UTILS_H
2#define HSOLVER_LINEAR_TEST_UTILS_H
3
7
8#include <complex>
9#include <stdexcept>
10#include <vector>
11
12namespace linear_test
13{
14using Complex = std::complex<double>;
15
17{
18#ifdef __MPI
19 MPICommGroup group(MPI_COMM_WORLD);
20 return hsolver::diag_comm_info(MPI_COMM_WORLD, group.grank, group.gsize);
21#else
22 return hsolver::diag_comm_info(0, 1);
23#endif
24}
25
26// Column-major matrices with a decoupled first eigenvector and a dense complex block.
27template <typename T>
28std::vector<T> hermitian(const int n)
29{
30 std::vector<T> h(n * n, T(0));
31 for (int j = 0; j < n; ++j)
32 {
33 h[j * n + j] = T(1.0 + 0.4 * j);
34 for (int i = 1; i < j; ++i)
35 {
36 const T value(0.12 / (j - i + 1), 0.07 / (i + j + 1));
37 h[j * n + i] = value;
38 h[i * n + j] = std::conj(value);
39 }
40 }
41 return h;
42}
43
44template <typename T>
45std::vector<T> multiply(const std::vector<T>& a, const std::vector<T>& x, const int n, const int nvec)
46{
47 std::vector<T> b(n * nvec, T(0));
48 for (int band = 0; band < nvec; ++band)
49 {
50 for (int j = 0; j < n; ++j)
51 {
52 for (int i = 0; i < n; ++i)
53 {
54 b[band * n + i] += a[j * n + i] * x[band * n + j];
55 }
56 }
57 }
58 return b;
59}
60
61// Extract the local rows of column-major reference data, leaving padding zero.
62template <typename T>
63std::vector<T> local_columns(const std::vector<T>& global, int n, int columns, int start, int dim, int ld)
64{
65 std::vector<T> local(ld * columns, T(0));
66 for (int j = 0; j < columns; ++j)
67 {
68 for (int i = 0; i < dim; ++i)
69 {
70 local[j * ld + i] = global[j * n + start + i];
71 }
72 }
73 return local;
74}
75
76// Solve in double precision even when the iterative input was rounded to float.
77template <typename T>
78std::vector<Complex> lapack_solve(const std::vector<T>& a, const std::vector<T>& b, const int n, const int nvec)
79{
80 std::vector<Complex> lu(a.begin(), a.end());
81 std::vector<Complex> x(b.begin(), b.end());
82 std::vector<int> pivots(n);
83 int info = 0;
84 container::lapackConnector::getrf(n, n, lu.data(), n, pivots.data(), info);
85 if (info != 0)
86 {
87 throw std::runtime_error("LAPACK reference factorization failed");
88 }
89 container::lapackConnector::getrs('N', n, nvec, lu.data(), n, pivots.data(), x.data(), n, info);
90 if (info != 0)
91 {
92 throw std::runtime_error("LAPACK reference solve failed");
93 }
94 return x;
95}
96} // namespace linear_test
97#endif
This is a direct wrapper of some LAPACK routines. Column-Major version. Direct wrapping of standard L...
Definition parallel_comm.h:15
int grank
rank of parent communicator
Definition parallel_comm.h:28
int gsize
size of parent communicator
Definition parallel_comm.h:27
#define T
Definition exp.cpp:237
Definition linear_test_utils.h:13
std::vector< Complex > lapack_solve(const std::vector< T > &a, const std::vector< T > &b, const int n, const int nvec)
Definition linear_test_utils.h:78
std::complex< double > Complex
Definition linear_test_utils.h:14
std::vector< T > multiply(const std::vector< T > &a, const std::vector< T > &x, const int n, const int nvec)
Definition linear_test_utils.h:45
std::vector< T > hermitian(const int n)
Definition linear_test_utils.h:28
std::vector< T > local_columns(const std::vector< T > &global, int n, int columns, int start, int dim, int ld)
Definition linear_test_utils.h:63
hsolver::diag_comm_info world_comm()
Definition linear_test_utils.h:16
Definition diag_comm_info.h:12
iclock::time_point start
Definition test_partition.cpp:22