From 0eff2f63d0d5bf214ce069373f8488220aca3677 Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Wed, 12 Sep 2018 20:21:03 +0200
Subject: [PATCH] Fixed problem with std::isnan on older g++.

---
 src/TNL/Math.h                          | 18 ++++++++++++++++++
 src/TNL/Solvers/IterativeSolver_impl.h  |  4 ++--
 src/TNL/Solvers/Linear/BICGStabL_impl.h |  2 +-
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/TNL/Math.h b/src/TNL/Math.h
index 67e9f5e082..2d81bea667 100644
--- a/src/TNL/Math.h
+++ b/src/TNL/Math.h
@@ -168,6 +168,24 @@ T sign( const T& a )
    return ( T ) 1;
 }
 
+template< class T >
+__cuda_callable__
+bool isNan( const T& v )
+{
+#if defined HAVE_CUDA
+   #if defined(__CUDA_ARCH__)
+      return isnan( v );
+   #else
+      #if defined (__GNUC__) && ( __GNUC__  < 5 )
+         return false;
+      #else
+         return std::isnan( v );
+      #endif
+   #endif
+#else
+   return std::isnan( v );
+#endif
+}
 template< typename Real >
 __cuda_callable__
 bool isSmall( const Real& v,
diff --git a/src/TNL/Solvers/IterativeSolver_impl.h b/src/TNL/Solvers/IterativeSolver_impl.h
index 333c38cd44..e65b3cdd41 100644
--- a/src/TNL/Solvers/IterativeSolver_impl.h
+++ b/src/TNL/Solvers/IterativeSolver_impl.h
@@ -107,7 +107,7 @@ bool IterativeSolver< Real, Index> :: checkNextIteration()
 {
    this->refreshSolverMonitor();
 
-   if( std::isnan( this->getResidue() ) ||
+   if( isNan( this->getResidue() ) ||
        this->getIterations() > this->getMaxIterations()  ||
        ( this->getResidue() > this->getDivergenceResidue() && this->getIterations() >= this->getMinIterations() ) ||
        ( this->getResidue() < this->getConvergenceResidue() && this->getIterations() >= this->getMinIterations() ) )
@@ -120,7 +120,7 @@ bool
 IterativeSolver< Real, Index>::
 checkConvergence()
 {
-   if( std::isnan( this->getResidue() ) )
+   if( isNan( this->getResidue() ) )
    {
       std::cerr << std::endl << "The residue is NaN." << std::endl;
       return false;
diff --git a/src/TNL/Solvers/Linear/BICGStabL_impl.h b/src/TNL/Solvers/Linear/BICGStabL_impl.h
index 6606bddd56..467f1a91a0 100644
--- a/src/TNL/Solvers/Linear/BICGStabL_impl.h
+++ b/src/TNL/Solvers/Linear/BICGStabL_impl.h
@@ -87,7 +87,7 @@ BICGStabL< Matrix >::solve( ConstVectorViewType b, VectorViewType x )
    }
 
    sigma[ 0 ] = r_0.lpNorm( 2.0 );
-   if( std::isnan( sigma[ 0 ] ) )
+   if( isNan( sigma[ 0 ] ) )
       throw std::runtime_error( "BiCGstab(ell): initial residue is NAN" );
 
    r_ast = r_0;
-- 
GitLab