From c2470b096d19ceeeceb3fe8c1a1d7167280c3e5f Mon Sep 17 00:00:00 2001
From: Wenqing Wang <wenqing.wang@ufz.de>
Date: Thu, 16 Oct 2014 11:58:22 +0200
Subject: [PATCH] Added PETScWallClockTimer

---
 BaseLib/PETScWallClockTimer.h            | 57 ++++++++++++++++++++++++
 CMakeLists.txt                           |  2 +
 MathLib/LinAlg/PETSc/PETScLinearSolver.h |  1 +
 scripts/cmake/Find.cmake                 |  8 ++++
 4 files changed, 68 insertions(+)
 create mode 100644 BaseLib/PETScWallClockTimer.h

diff --git a/BaseLib/PETScWallClockTimer.h b/BaseLib/PETScWallClockTimer.h
new file mode 100644
index 00000000000..bca4226acf2
--- /dev/null
+++ b/BaseLib/PETScWallClockTimer.h
@@ -0,0 +1,57 @@
+/*!
+  \file PETScWallClockTimer.h
+  \author Wenqing Wang
+  \date   2014.08
+  \brief  Declare a class to record wall clock time in computation with PETSc.
+
+  \copyright
+  Copyright (c) 2014, OpenGeoSys Community (http://www.opengeosys.org)
+             Distributed under a Modified BSD License.
+               See accompanying file LICENSE.txt or
+               http://www.opengeosys.org/project/license
+
+*/
+
+#ifndef PETSC_WALL_CLOCK_TIMER_H
+#define PETSC_WALL_CLOCK_TIMER_H
+
+#include <petsctime.h>
+
+namespace BaseLib
+{
+
+/// Record wall clock time for computations with PETSc.
+class PETScWallClockTimer
+{
+    public:
+        /// Record the start time.
+        void start()
+        {
+#if (PETSC_VERSION_NUMBER > 3020)
+            PetscTime(&_start_time);
+#else
+            PetscGetTime(&_start_time);
+#endif
+        }
+
+        /// Return the elapsed time when this function is called.
+        PetscLogDouble elapsed() const
+        {
+            PetscLogDouble current_time;
+#if (PETSC_VERSION_NUMBER > 3020)
+            PetscTime(&current_time);
+#else
+            PetscGetTime(&current_time);
+#endif
+            return current_time - _start_time;
+        }
+
+    private:
+        /// Start time.
+        PetscLogDouble _start_time = 0.;
+};
+
+} // end namespace BaseLib
+
+#endif
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2c29e6ef67a..9e02a359028 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,6 +64,8 @@ OPTION(OGS_NO_EXTERNAL_LIBS "Builds OGS without any external dependencies." OFF)
 
 # Linear solvers
 OPTION(OGS_USE_LIS "Use Lis" OFF)
+
+# Parallel computing
 OPTION(OGS_USE_PETSC "Use PETSc routines" OFF)
 
 # Paralleization
diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.h b/MathLib/LinAlg/PETSc/PETScLinearSolver.h
index 59e86838ba1..b3d9660d101 100644
--- a/MathLib/LinAlg/PETSc/PETScLinearSolver.h
+++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.h
@@ -23,6 +23,7 @@
 
 #include "PETScMatrix.h"
 #include "PETScVector.h"
+#include "BaseLib/PETScWallClockTimer.h"
 
 namespace MathLib
 {
diff --git a/scripts/cmake/Find.cmake b/scripts/cmake/Find.cmake
index 9f7665a58e4..0046bf0d92b 100644
--- a/scripts/cmake/Find.cmake
+++ b/scripts/cmake/Find.cmake
@@ -134,6 +134,14 @@ IF(OGS_USE_PETSC)
     include_directories(
               ${PETSC_INCLUDES}
      )
+    FIND_PACKAGE(MPI REQUIRED)
+  
+    IF(WIN32)
+      ADD_DEFINITIONS(/DPETSC_VERSION_NUMBER=PETSC_VERSION_MAJOR*1000+PETSC_VERSION_MINOR*10)
+    ELSE()
+      ADD_DEFINITIONS(-DPETSC_VERSION_NUMBER=PETSC_VERSION_MAJOR*1000+PETSC_VERSION_MINOR*10)
+    ENDIF()  
+    
 ENDIF()
 
 IF(OGS_USE_MPI)
-- 
GitLab