Skip to content

Commit

Permalink
Belos native Tpetra : fix compilation warnings (#9900)
Browse files Browse the repository at this point in the history
* Belos: fix compilation warnings (Shadowed declarations) in native Tpetra solver
* Belos: fix a typo in native Tpetra Gmres
* Belos: add option not to compute Ritz values for testing native Tpetra pipelined Gmres
  • Loading branch information
iyamazaki authored Nov 6, 2021
1 parent f6c69a3 commit a957598
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 71 deletions.
16 changes: 9 additions & 7 deletions packages/belos/tpetra/src/solvers/Belos_Tpetra_CgPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class CgPipeline : public Krylov<SC, MV, OP> {

SolverOutput<SC> output {};

MV R_AR (R_in.getMap (), 2); // [R, A*R]
bool zeroOut = false;
MV R_AR (R_in.getMap (), 2, zeroOut); // [R, A*R]
vec_type R = * (R_AR.getVectorNonConst (0));
vec_type AR = * (R_AR.getVectorNonConst (1));
Tpetra::deep_copy (R, R_in);
Expand All @@ -99,10 +100,10 @@ class CgPipeline : public Krylov<SC, MV, OP> {
auto RR_RAR_host = Kokkos::create_mirror_view (RR_RAR);
vec_type P (R, Teuchos::Copy);
vec_type AP (P.getMap ());
vec_type AAR(R.getMap ());
vec_type AAR(R.getMap (), zeroOut);
vec_type AW (R.getMap ());
vec_type MR (R.getMap ());
vec_type U (R.getMap ());
vec_type MR (R.getMap (), zeroOut);
vec_type U (R.getMap (), zeroOut);
vec_type Q (R.getMap ());

// local vars
Expand Down Expand Up @@ -162,6 +163,7 @@ class CgPipeline : public Krylov<SC, MV, OP> {
Kokkos::deep_copy (RR_RAR_host, RR_RAR);
RAR = RR_RAR_host(1);
beta_new = STS::real (RR_RAR_host(0));

r_norm = std::sqrt( beta_new );
if (iter == 0) {
r_norm_orig = r_norm;
Expand Down Expand Up @@ -205,9 +207,9 @@ class CgPipeline : public Krylov<SC, MV, OP> {

// alpha
alpha = beta_new / STS::real (PAP);
if (outPtr != nullptr) {
*outPtr << ", alpha: " << alpha << endl;
}
}
if (outPtr != nullptr) {
*outPtr << ", alpha: " << alpha << endl;
}
// beta_old
beta_old = beta_new;
Expand Down
27 changes: 14 additions & 13 deletions packages/belos/tpetra/src/solvers/Belos_Tpetra_Gmres.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ sortRitzValues (const LO m,
LO i = 0;
LO next_index = 0;
real_type next_value = std::abs (RR[0]);
for (int i = 1; i < m; ++i) {
for (i = 1; i < m; ++i) {
if (next_value < std::abs (RR[i])) {
next_index = i;
next_value = std::abs (RR[i]);
Expand All @@ -193,6 +193,7 @@ sortRitzValues (const LO m,
ritzValues[0] = RR[next_index];
RR[next_index] = RR[0];

i = 0;
if (! STS::isComplex) {
if (RR[i].imag() != 0.0) {

Expand Down Expand Up @@ -543,7 +544,7 @@ class Gmres : public Krylov<SC, MV, OP> {
vec_type Y (B.getMap (), zeroOut);
vec_type MP (B.getMap (), zeroOut);
MV Q (B.getMap (), restart+1, zeroOut);
vec_type P = * (Q.getVectorNonConst (0));
vec_type P0 = * (Q.getVectorNonConst (0));

// initial residual (making sure R = B - Ax)
{
Expand All @@ -555,12 +556,12 @@ class Gmres : public Krylov<SC, MV, OP> {
if (input.precoSide == "left") {
{
Teuchos::TimeMonitor LocalTimer (*precTimer);
M.apply (R, P);
M.apply (R, P0);
}
b_norm = P.norm2 (); // residual norm, left-preconditioned
b_norm = P0.norm2 (); // residual norm, left-preconditioned
}
else {
Tpetra::deep_copy (P, R);
Tpetra::deep_copy (P0, R);
b_norm = b0_norm;
}
real_type metric = this->getConvergenceMetric (b0_norm, b0_norm, input);
Expand All @@ -575,7 +576,7 @@ class Gmres : public Krylov<SC, MV, OP> {
output.numIters = 0;
output.converged = true;
// return residual norm as B
Tpetra::deep_copy (B, P);
Tpetra::deep_copy (B, P0);
return output;
} else if (outPtr != NULL) {
*outPtr << "Initial guess' residual norm " << b0_norm << endl;
Expand All @@ -597,7 +598,7 @@ class Gmres : public Krylov<SC, MV, OP> {
#endif

// initialize starting vector
P.scale (one / b_norm);
P0.scale (one / b_norm);
y[0] = SC {b_norm};

// main loop
Expand Down Expand Up @@ -741,19 +742,19 @@ class Gmres : public Krylov<SC, MV, OP> {
if (iter >= restart) {
// Restart: Initialize starting vector for restart
iter = 0;
P = * (Q.getVectorNonConst (0));
P0 = * (Q.getVectorNonConst (0));
if (input.precoSide == "left") {
{
Teuchos::TimeMonitor LocalTimer (*precTimer);
M.apply (R, P);
M.apply (R, P0);
}
r_norm = P.norm2 (); // norm
r_norm = P0.norm2 (); // norm
}
else {
// set the starting vector
Tpetra::deep_copy (P, R);
Tpetra::deep_copy (P0, R);
}
P.scale (one / r_norm);
P0.scale (one / r_norm);
y[0] = SC {r_norm};
for (int i=1; i < restart+1; i++) {
y[i] = STS::zero ();
Expand All @@ -779,7 +780,7 @@ class Gmres : public Krylov<SC, MV, OP> {
return output;
}

// ! compute condition number
// ! compute matrix norm
real_type
computeNorm(dense_matrix_type &T)
{
Expand Down
24 changes: 12 additions & 12 deletions packages/belos/tpetra/src/solvers/Belos_Tpetra_GmresPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class GmresPipeline : public Gmres<SC, MV, OP> {
vec_type R (B.getMap (), zeroOut);
vec_type Y (B.getMap (), zeroOut);
vec_type MZ (B.getMap (), zeroOut);
vec_type Z = * (V.getVectorNonConst (0));
vec_type Z0 = * (V.getVectorNonConst (0));

// initial residual (making sure R = B - Ax)
{
Expand All @@ -139,9 +139,9 @@ class GmresPipeline : public Gmres<SC, MV, OP> {
// TODO: this should be idot?
b0_norm = STM::squareroot (STS::real (R.dot (R))); // initial residual norm, no preconditioned
if (input.precoSide == "left") {
M.apply (R, Z);
M.apply (R, Z0);
// TODO: this should be idot?
b_norm = STS::real (Z.dot( Z )); //Z.norm2 (); // initial residual norm, preconditioned
b_norm = STS::real (Z0.dot( Z0 )); //Z.norm2 (); // initial residual norm, preconditioned
}
else {
b_norm = b0_norm;
Expand All @@ -162,16 +162,16 @@ class GmresPipeline : public Gmres<SC, MV, OP> {
return output; // standard GMRES converged
}
if (input.precoSide == "left") {
M.apply (R, Z);
r_norm = Z.norm2 (); // residual norm
M.apply (R, Z0);
r_norm = Z0.norm2 (); // residual norm
}
else {
r_norm = output.absResid;
}
output.numRests++;
}
if (input.precoSide != "left") {
Tpetra::deep_copy (Z, R);
Tpetra::deep_copy (Z0, R);
}

// for idot
Expand All @@ -194,11 +194,11 @@ class GmresPipeline : public Gmres<SC, MV, OP> {
}

// Normalize initial vector
MVT::MvScale (Z, one/std::sqrt(G(0, 0)));
MVT::MvScale (Z0, one/std::sqrt(G(0, 0)));

// Copy initial vector
vec_type AP = * (Q.getVectorNonConst (0));
Tpetra::deep_copy (AP, Z);
Tpetra::deep_copy (AP, Z0);
}

// Restart cycle
Expand Down Expand Up @@ -391,14 +391,14 @@ class GmresPipeline : public Gmres<SC, MV, OP> {
if (iter >= restart+ell) {
// Restart: Initialize starting vector for restart
iter = 0;
Z = * (V.getVectorNonConst (0));
Z0 = * (V.getVectorNonConst (0));
if (input.precoSide == "left") {
M.apply (R, Z);
r_norm = STS::real (Z.dot (Z)); //norm2 (); // residual norm
M.apply (R, Z0);
r_norm = STS::real (Z0.dot (Z0)); //norm2 (); // residual norm
}
else {
// set the starting vector
Tpetra::deep_copy (Z, R);
Tpetra::deep_copy (Z0, R);
}
G(0, 0) = r_norm;
r_norm = STM::squareroot (r_norm);
Expand Down
20 changes: 10 additions & 10 deletions packages/belos/tpetra/src/solvers/Belos_Tpetra_GmresS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ class GmresS : public Gmres<SC, MV, OP> {

vec_type MP (B.getMap ());
MV Q (B.getMap (), s+1);
vec_type P = * (Q.getVectorNonConst (0));
vec_type P0 = * (Q.getVectorNonConst (0));

mag_type r_norm = STM::zero ();
if (input.precoSide == "left") {
M.apply (R, P);
r_norm = P.norm2 (); // residual norm
M.apply (R, P0);
r_norm = P0.norm2 (); // residual norm
}
else {
Tpetra::deep_copy (P, R);
Tpetra::deep_copy (P0, R);
r_norm = output.absResid;
}
mag_type metric = this->getConvergenceMetric (r_norm, b0_norm, input);
Expand Down Expand Up @@ -206,7 +206,7 @@ class GmresS : public Gmres<SC, MV, OP> {
}

// initialize starting vector
P.scale (one / b_norm);
P0.scale (one / b_norm);
y[0] = SC {b_norm};

if (outPtr != nullptr) {
Expand Down Expand Up @@ -304,10 +304,10 @@ class GmresS : public Gmres<SC, MV, OP> {
}

// Compute explicit unpreconditioned residual
P = * (Q.getVectorNonConst (0));
A.apply (X, P);
P.update (one, B, -one);
r_norm = P.norm2 (); // residual norm
P0 = * (Q.getVectorNonConst (0));
A.apply (X, P0);
P0.update (one, B, -one);
r_norm = P0.norm2 (); // residual norm

output.numIters += s;
output.numRests++;
Expand All @@ -316,7 +316,7 @@ class GmresS : public Gmres<SC, MV, OP> {
metric = this->getConvergenceMetric (r_norm, b0_norm, input);
output.converged = (metric <= input.tol);

Tpetra::deep_copy (B, P); // return residual norm as B
Tpetra::deep_copy (B, P0); // return residual norm as B

if (outPtr != nullptr) {
*outPtr << "At end of GMRES(s) cycle:" << endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class GmresSingleReduce : public Gmres<SC, MV, OP> {
vec_type R (B.getMap (), zeroOut);
vec_type Y (B.getMap (), zeroOut);
vec_type MP (B.getMap (), zeroOut);
vec_type P = * (Q.getVectorNonConst (0));
vec_type P0 = * (Q.getVectorNonConst (0));

Teuchos::BLAS<LO ,SC> blas;
dense_matrix_type H (restart+1, restart, true);
Expand All @@ -533,9 +533,9 @@ class GmresSingleReduce : public Gmres<SC, MV, OP> {
if (input.precoSide == "left") {
{
Teuchos::TimeMonitor LocalTimer (*precTimer);
M.apply (R, P);
M.apply (R, P0);
}
r_norm = P.norm2 (); // initial residual norm, left-preconditioned
r_norm = P0.norm2 (); // initial residual norm, left-preconditioned
} else {
r_norm = b0_norm;
}
Expand Down Expand Up @@ -596,9 +596,9 @@ class GmresSingleReduce : public Gmres<SC, MV, OP> {
if (input.precoSide == "left") {
{
Teuchos::TimeMonitor LocalTimer (*precTimer);
M.apply (R, P);
M.apply (R, P0);
}
r_norm = P.norm2 (); // residual norm
r_norm = P0.norm2 (); // residual norm
}
else {
r_norm = output.absResid;
Expand All @@ -608,9 +608,9 @@ class GmresSingleReduce : public Gmres<SC, MV, OP> {

// initialize starting vector
if (input.precoSide != "left") {
Tpetra::deep_copy (P, R);
Tpetra::deep_copy (P0, R);
}
P.scale (one / r_norm);
P0.scale (one / r_norm);
y[0] = SC {r_norm};
const int s = getStepSize ();
// main loop
Expand Down Expand Up @@ -798,20 +798,20 @@ class GmresSingleReduce : public Gmres<SC, MV, OP> {
<< " since H(" << iter << ", " << iter-1 << ") = zero" << endl;
}
iter = 0;
P = * (Q.getVectorNonConst (0));
P0 = * (Q.getVectorNonConst (0));
if (input.precoSide == "left") {
{
Teuchos::TimeMonitor LocalTimer (*precTimer);
M.apply (R, P);
M.apply (R, P0);
}
// FIXME (mfh 14 Aug 2018) Didn't we already compute this above?
r_norm = P.norm2 ();
r_norm = P0.norm2 ();
}
else {
// set the starting vector
Tpetra::deep_copy (P, R);
Tpetra::deep_copy (P0, R);
}
P.scale (one / r_norm);
P0.scale (one / r_norm);
y[0] = SC {r_norm};
for (int i=1; i < restart+1; i++) {
y[i] = zero;
Expand Down
Loading

0 comments on commit a957598

Please sign in to comment.