fml  0.1-0
Fused Matrix Library
eigen.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_MPI_LINALG_EIGEN_H
6 #define FML_MPI_LINALG_EIGEN_H
7 #pragma once
8 
9 
10 #include <stdexcept>
11 
12 #include "../../_internals/linalgutils.hh"
13 #include "../../cpu/cpuvec.hh"
14 
15 #include "../internals/bcutils.hh"
16 #include "../internals/mpi_utils.hh"
17 
18 #include "../copy.hh"
19 #include "../mpimat.hh"
20 
21 #include "internals/err.hh"
22 #include "internals/scalapack.hh"
23 
24 
25 namespace fml
26 {
27 namespace linalg
28 {
29  namespace
30  {
31  template <typename REAL>
32  int eig_sym_internals(const bool only_values, mpimat<REAL> &x,
33  cpuvec<REAL> &values, mpimat<REAL> &vectors)
34  {
35  if (!x.is_square())
36  throw std::runtime_error("'x' must be a square matrix");
37 
38  int info = 0;
39  int val_found, vec_found;
40  char jobz;
41 
42  len_t n = x.nrows();
43  values.resize(n);
44 
45  if (only_values)
46  jobz = 'N';
47  else
48  {
49  jobz = 'V';
50  vectors.resize(n, n, x.bf_rows(), x.bf_cols());
51  }
52 
53  REAL worksize;
54  int lwork, liwork;
55 
56  fml::scalapack::syevr(jobz, 'A', 'L', n, x.data_ptr(), x.desc_ptr(),
57  (REAL) 0.f, (REAL) 0.f, 0, 0, &val_found, &vec_found,
58  values.data_ptr(), vectors.data_ptr(), vectors.desc_ptr(),
59  &worksize, -1, &liwork, -1, &info);
60 
61  lwork = (int) worksize;
62  cpuvec<REAL> work(lwork);
63  cpuvec<int> iwork(liwork);
64 
65  fml::scalapack::syevr(jobz, 'A', 'L', n, x.data_ptr(), x.desc_ptr(),
66  (REAL) 0.f, (REAL) 0.f, 0, 0, &val_found, &vec_found,
67  values.data_ptr(), vectors.data_ptr(), vectors.desc_ptr(),
68  work.data_ptr(), lwork, iwork.data_ptr(), liwork, &info);
69 
70  return info;
71  }
72  }
73 
96  template <typename REAL>
98  {
99  mpimat<REAL> ignored(x.get_grid());
100 
101  int info = eig_sym_internals(true, x, values, ignored);
102  fml::linalgutils::check_info(info, "syevr");
103  }
104 
106  template <typename REAL>
107  void eigen_sym(mpimat<REAL> &x, cpuvec<REAL> &values, mpimat<REAL> &vectors)
108  {
109  err::check_grid(x, vectors);
110 
111  int info = eig_sym_internals(false, x, values, vectors);
112  fml::linalgutils::check_info(info, "syevr");
113  }
114 }
115 }
116 
117 
118 #endif
fml::mpimat
Matrix class for data distributed over MPI in the 2-d block cyclic format.
Definition: mpimat.hh:40
fml::cpuvec
Vector class for data held on a single CPU.
Definition: cpuvec.hh:31
fml
Core namespace.
Definition: dimops.hh:10
fml::linalg::eigen_sym
void eigen_sym(cpumat< REAL > &x, cpuvec< REAL > &values)
Compute the eigenvalues and optionally the eigenvectors for a symmetric matrix.
Definition: eigen.hh:95