Skip to content

Commit f55a88e

Browse files
committed
Split PPCG helper headers
1 parent 227aaef commit f55a88e

3 files changed

Lines changed: 76 additions & 75 deletions

File tree

source/source_hsolver/diago_ppcg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "diago_ppcg.h"
22

3-
#include "ppcg/diago_ppcg_lapack.hpp"
3+
#include "ppcg/diago_ppcg_reduce.hpp"
4+
#include "ppcg/diago_ppcg_small_eigen.hpp"
45
#include "ppcg/diago_ppcg_ops.hpp"
56
#include "ppcg/diago_ppcg_subspace.hpp"
67
#include "ppcg/diago_ppcg_orth.hpp"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "source_base/parallel_reduce.h"
2+
3+
#include <cstdlib>
4+
#include <fstream>
5+
6+
namespace hsolver {
7+
namespace {
8+
9+
template <typename Value>
10+
void reduce_pool_if_mpi_ready(Value& value)
11+
{
12+
#ifdef __MPI
13+
int initialized = 0;
14+
int finalized = 0;
15+
MPI_Initialized(&initialized);
16+
MPI_Finalized(&finalized);
17+
if (initialized && !finalized)
18+
Parallel_Reduce::reduce_pool(value);
19+
#endif
20+
}
21+
22+
template <typename Value>
23+
void reduce_pool_if_mpi_ready(Value* value, const int n)
24+
{
25+
#ifdef __MPI
26+
int initialized = 0;
27+
int finalized = 0;
28+
MPI_Initialized(&initialized);
29+
MPI_Finalized(&finalized);
30+
if (initialized && !finalized)
31+
Parallel_Reduce::reduce_pool(value, n);
32+
#endif
33+
}
34+
35+
template <typename T, typename Real>
36+
Real max_generalized_residual(
37+
const T* hpsi,
38+
const T* spsi,
39+
const Real* eigenvalue,
40+
int ld,
41+
int n_dim,
42+
int ncol)
43+
{
44+
Real max_res = 0;
45+
std::vector<double> nrm2_all(ncol, 0.0);
46+
#ifdef _OPENMP
47+
#pragma omp parallel for schedule(static) if (n_dim * ncol > 4096)
48+
#endif
49+
for (int j = 0; j < ncol; ++j)
50+
{
51+
double nrm2 = 0.0;
52+
for (int ig = 0; ig < n_dim; ++ig)
53+
{
54+
const T r = hpsi[ig + j * ld] - T(eigenvalue[j]) * spsi[ig + j * ld];
55+
nrm2 += static_cast<double>(std::norm(r));
56+
}
57+
nrm2_all[j] = nrm2;
58+
}
59+
reduce_pool_if_mpi_ready(nrm2_all.data(), ncol);
60+
for (int j = 0; j < ncol; ++j)
61+
{
62+
max_res = std::max(max_res, std::sqrt(static_cast<Real>(nrm2_all[j])));
63+
}
64+
return max_res;
65+
}
66+
67+
template <typename T>
68+
inline void set_zero(std::vector<T>& x)
69+
{
70+
std::fill(x.begin(), x.end(), T(0));
71+
}
72+
73+
} // anonymous namespace
74+
} // namespace hsolver

source/source_hsolver/ppcg/diago_ppcg_lapack.hpp renamed to source/source_hsolver/ppcg/diago_ppcg_small_eigen.hpp

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,8 @@
11
#include <ATen/kernels/lapack.h>
22

3-
#include "source_base/parallel_reduce.h"
4-
5-
#include <cstdlib>
6-
#include <fstream>
7-
83
namespace hsolver {
9-
10-
// =============================================================================
11-
// LAPACK wrapper (specialized per real type)
12-
// =============================================================================
134
namespace {
145

15-
template <typename Value>
16-
void reduce_pool_if_mpi_ready(Value& value)
17-
{
18-
#ifdef __MPI
19-
int initialized = 0;
20-
int finalized = 0;
21-
MPI_Initialized(&initialized);
22-
MPI_Finalized(&finalized);
23-
if (initialized && !finalized)
24-
Parallel_Reduce::reduce_pool(value);
25-
#endif
26-
}
27-
28-
template <typename Value>
29-
void reduce_pool_if_mpi_ready(Value* value, const int n)
30-
{
31-
#ifdef __MPI
32-
int initialized = 0;
33-
int finalized = 0;
34-
MPI_Initialized(&initialized);
35-
MPI_Finalized(&finalized);
36-
if (initialized && !finalized)
37-
Parallel_Reduce::reduce_pool(value, n);
38-
#endif
39-
}
40-
41-
template <typename T, typename Real>
42-
Real max_generalized_residual(
43-
const T* hpsi,
44-
const T* spsi,
45-
const Real* eigenvalue,
46-
int ld,
47-
int n_dim,
48-
int ncol)
49-
{
50-
Real max_res = 0;
51-
std::vector<double> nrm2_all(ncol, 0.0);
52-
#ifdef _OPENMP
53-
#pragma omp parallel for schedule(static) if (n_dim * ncol > 4096)
54-
#endif
55-
for (int j = 0; j < ncol; ++j)
56-
{
57-
double nrm2 = 0.0;
58-
for (int ig = 0; ig < n_dim; ++ig)
59-
{
60-
const T r = hpsi[ig + j * ld] - T(eigenvalue[j]) * spsi[ig + j * ld];
61-
nrm2 += static_cast<double>(std::norm(r));
62-
}
63-
nrm2_all[j] = nrm2;
64-
}
65-
reduce_pool_if_mpi_ready(nrm2_all.data(), ncol);
66-
for (int j = 0; j < ncol; ++j)
67-
{
68-
max_res = std::max(max_res, std::sqrt(static_cast<Real>(nrm2_all[j])));
69-
}
70-
return max_res;
71-
}
72-
736
template <typename Scalar>
747
struct HermitianLapack
758
{
@@ -178,12 +111,5 @@ struct HermitianLapack
178111
}
179112
};
180113

181-
template <typename T>
182-
inline void set_zero(std::vector<T>& x)
183-
{
184-
std::fill(x.begin(), x.end(), T(0));
185-
}
186-
187114
} // anonymous namespace
188-
189115
} // namespace hsolver

0 commit comments

Comments
 (0)