From 72b39c43290b782c71a068c24cd8ab7c0a86d4e0 Mon Sep 17 00:00:00 2001
From: Wenqing Wang <wenqing.wang@ufz.de>
Date: Thu, 16 Oct 2014 12:35:00 +0200
Subject: [PATCH] More changes for CPUTime and RunTime

---
 Applications/CLI/ogs.cpp                   |  8 ++++++++
 BaseLib/CPUTime.h                          |  6 +++---
 BaseLib/RunTime.h                          | 16 ++++++++--------
 CMakeLists.txt                             |  4 ----
 MathLib/LinAlg/PETSc/PETScLinearSolver.cpp | 11 +++++++++--
 MathLib/LinAlg/PETSc/PETScLinearSolver.h   |  8 ++++++++
 6 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp
index d90ea999b16..17e04988fda 100644
--- a/Applications/CLI/ogs.cpp
+++ b/Applications/CLI/ogs.cpp
@@ -14,6 +14,10 @@
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
 
+#ifdef USE_PETSC
+#include <petscksp.h>
+#endif
+
 // ThirdParty/logog
 #include "logog/include/logog.hpp"
 
@@ -32,6 +36,10 @@
 
 int main(int argc, char *argv[])
 {
+#ifdef USE_PETSC
+	char help[] = "ogs6 with PETSc \n";
+	PetscInitialize(&argc, &argv, nullptr, help);
+#endif
 
 	using ConfigTree = boost::property_tree::ptree;
 
diff --git a/BaseLib/CPUTime.h b/BaseLib/CPUTime.h
index e527d3a8c7f..0d2eaed733d 100644
--- a/BaseLib/CPUTime.h
+++ b/BaseLib/CPUTime.h
@@ -21,20 +21,20 @@
 namespace BaseLib
 {
 
-/// Record CPU time
+/// Count CPU time
 class CPUTime
 {
     public:
         /// Start the timer.
         void start()
         {
-            _timer = - clock()/static_cast<double>(CLOCKS_PER_SEC);
+            _timer = clock();
         }
 
         /// Get the epalsed time after started.
         double elapsed()
         {
-            return _timer + clock()/static_cast<double>(CLOCKS_PER_SEC);
+            return (clock() - _timer)/static_cast<double>(CLOCKS_PER_SEC);
         }
     private:
         double _timer = 0.;
diff --git a/BaseLib/RunTime.h b/BaseLib/RunTime.h
index 96d9929528f..37d9220b7c5 100644
--- a/BaseLib/RunTime.h
+++ b/BaseLib/RunTime.h
@@ -16,7 +16,7 @@
 #ifndef RUNTIME_H
 #define RUNTIME_H
 
-#if defined(USE_MPI) || defined(USE_PETSC)
+#if defined(USE_MPI)
 #include <mpi.h>
 #else
 #ifndef _MSC_VER
@@ -29,22 +29,22 @@
 namespace BaseLib
 {
 
-/// Record the running time.
+/// Count the running time.
 class RunTime
 {
     public:
         /// Start the timer.
         void start()
         {
-#if defined(USE_MPI) || defined(USE_PETSC)
-            _timer = -MPI_Wtime();
+#if defined(USE_MPI)
+            _timer = MPI_Wtime();
 #else
 #ifndef _MSC_VER
             timeval t;
             gettimeofday(&t, 0);
             _timer = -t.tv_sec - t.tv_usec/1000000.0;
 #else
-            _timer = -timeGetTime()/1000.0;
+            _timer = timeGetTime();
 #endif
 #endif
         }
@@ -52,8 +52,8 @@ class RunTime
         /// Get the epalsed time after started.
         double elapsed()
         {
-#if defined(USE_MPI) || defined(USE_PETSC)
-            return _timer + MPI_Wtime();
+#if defined(USE_MPI)
+            return _timer - MPI_Wtime();
 #else
 #ifndef _MSC_VER
             timeval t;
@@ -61,7 +61,7 @@ class RunTime
             _timer += t.tv_sec + t.tv_usec/1000000.0;
             return _timer;
 #else
-            return _timer + timeGetTime()/1000.0;
+            return (timeGetTime() - _timer)/1000.0;
 #endif
 #endif
         }
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0e8b81cfba6..15f12fc63eb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -122,10 +122,6 @@ IF(OGS_USE_LIS)
     INCLUDE_DIRECTORIES(SYSTEM ${LIS_INCLUDE_DIR})
 ENDIF()
 
-IF(OGS_USE_MPI)
-    ADD_DEFINITIONS(-DUSE_MPI)
-ENDIF()
-
 IF(OGS_USE_PETSC)
     ADD_DEFINITIONS(-DUSE_PETSC)
     SET(OGS_USE_MPI ON)
diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp
index 74433adb83c..a10318d7e4c 100644
--- a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp
+++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp
@@ -15,10 +15,12 @@
 */
 
 #include "PETScLinearSolver.h"
+#include "BaseLib/PETScWallClockTimer.h"
 
 namespace MathLib
 {
-PETScLinearSolver::PETScLinearSolver(PETScMatrix &A, const std::string &prefix) : _A(A)
+PETScLinearSolver::PETScLinearSolver(PETScMatrix &A, const std::string &prefix)
+    : _A(A), _elapsed_ctime(0.)
 {
     KSPCreate(PETSC_COMM_WORLD, &_solver);
 
@@ -35,13 +37,16 @@ PETScLinearSolver::PETScLinearSolver(PETScMatrix &A, const std::string &prefix)
 
 bool PETScLinearSolver::solve(const PETScVector &b, PETScVector &x)
 {
+    BaseLib::PETScWallClockTimer ctimer;
+    ctimer.start();
+    	
 // define TEST_MEM_PETSC
 #ifdef TEST_MEM_PETSC
     PetscLogDouble mem1, mem2;
     PetscMemoryGetCurrentUsage(&mem1);
 #endif
 
-#if (PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR > 4 || PETSC_VERSION_MAJOR > 3)
+#if (PETSC_VERSION_NUMBER > 3040)
     KSPSetOperators(_solver, _A.getRawMatrix(), _A.getRawMatrix());
 #else
     KSPSetOperators(_solver, _A.getRawMatrix(), _A.getRawMatrix(), DIFFERENT_NONZERO_PATTERN);
@@ -101,6 +106,8 @@ bool PETScLinearSolver::solve(const PETScVector &b, PETScVector &x)
     PetscPrintf(PETSC_COMM_WORLD, "###Memory usage by solver. Before :%f After:%f Increase:%d\n", mem1, mem2, (int)(mem2 - mem1));
 #endif
 
+    _elapsed_ctime += ctimer.elapsed();
+
     return converged;
 }
 
diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.h b/MathLib/LinAlg/PETSc/PETScLinearSolver.h
index b3d9660d101..d65efa41911 100644
--- a/MathLib/LinAlg/PETSc/PETScLinearSolver.h
+++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.h
@@ -67,6 +67,12 @@ class PETScLinearSolver
             return its;
         }
 
+        /// Get elapsed wall clock time.
+        PetscLogDouble getElapsedTime() const
+        {
+            return _elapsed_ctime;
+        }
+
     private:
         /// Matrix, kept as a member only for solving successive linear equation
         /// that preconditioner matrix may vary.
@@ -74,6 +80,8 @@ class PETScLinearSolver
 
         KSP _solver; ///< Solver type.
         PC _pc;      ///< Preconditioner type.
+
+        PetscLogDouble _elapsed_ctime; ///< Clock time
 };
 
 } // end namespace
-- 
GitLab