From 2d26d5793c71bd58da8cee10bbad43e00eb686e9 Mon Sep 17 00:00:00 2001
From: Wenqing Wang <wenqing.wang@ufz.de>
Date: Thu, 16 Oct 2014 12:14:18 +0200
Subject: [PATCH] Changed classes of RunTime and CPUTime

---
 Applications/CLI/CMakeLists.txt               |  4 ++
 .../Utils/FileConverter/FEFLOW2OGS.cpp        |  1 -
 Applications/Utils/FileConverter/GMSH2OGS.cpp |  2 +-
 BaseLib/CMakeLists.txt                        |  4 ++
 BaseLib/CPUTime.cpp                           | 34 ------------
 BaseLib/CPUTime.h                             | 28 ++++++----
 BaseLib/RunTime.cpp                           | 46 ----------------
 BaseLib/RunTime.h                             | 53 +++++++++++++++----
 CMakeLists.txt                                |  6 ++-
 SimpleTests/MatrixTests/MatMult.cpp           |  3 --
 .../MatrixTests/MatTestRemoveRowsCols.cpp     |  2 -
 SimpleTests/MatrixTests/MatVecMultNDPerm.cpp  |  7 ---
 .../MatrixTests/MatVecMultNDPermOpenMP.cpp    |  7 ---
 .../MatrixTests/MatVecMultPthreads.cpp        |  3 --
 SimpleTests/MeshTests/MeshRead.cpp            |  2 +-
 SimpleTests/MeshTests/MeshSearchTest.cpp      |  1 -
 .../SolverTests/BiCGStabDiagPrecond.cpp       |  3 --
 ...onjugateGradientDiagonalPreconditioned.cpp |  3 --
 SimpleTests/SolverTests/GMResDiagPrecond.cpp  |  3 --
 19 files changed, 76 insertions(+), 136 deletions(-)
 delete mode 100644 BaseLib/CPUTime.cpp
 delete mode 100644 BaseLib/RunTime.cpp

diff --git a/Applications/CLI/CMakeLists.txt b/Applications/CLI/CMakeLists.txt
index edb87aa77c7..836daed4fc4 100644
--- a/Applications/CLI/CMakeLists.txt
+++ b/Applications/CLI/CMakeLists.txt
@@ -11,4 +11,8 @@ TARGET_LINK_LIBRARIES(ogs
     ${Boost_LIBRARIES}
 )
 
+IF(OGS_USE_PETSC)
+    TARGET_LINK_LIBRARIES(ogs ${ogs} ${PETSC_LIBRARIES})
+ENDIF()
+
 INCLUDE(Tests.cmake)
diff --git a/Applications/Utils/FileConverter/FEFLOW2OGS.cpp b/Applications/Utils/FileConverter/FEFLOW2OGS.cpp
index 9e795298e48..77138e6b22a 100644
--- a/Applications/Utils/FileConverter/FEFLOW2OGS.cpp
+++ b/Applications/Utils/FileConverter/FEFLOW2OGS.cpp
@@ -80,7 +80,6 @@ int main (int argc, char* argv[])
 	unsigned long mem_with_mesh (mem_watch.getVirtMemUsage());
 	INFO("Mem for mesh: %i MB", (mem_with_mesh - mem_without_mesh)/(1024*1024));
 #endif
-	run_time.stop();
 	INFO("Time for reading: %f seconds.", run_time.elapsed());
 	INFO("Read %d nodes and %d elements.", mesh->getNNodes(), mesh->getNElements());
 
diff --git a/Applications/Utils/FileConverter/GMSH2OGS.cpp b/Applications/Utils/FileConverter/GMSH2OGS.cpp
index 5c8d280aa45..82e40ea7e2c 100644
--- a/Applications/Utils/FileConverter/GMSH2OGS.cpp
+++ b/Applications/Utils/FileConverter/GMSH2OGS.cpp
@@ -84,7 +84,7 @@ int main (int argc, char* argv[])
 	unsigned long mem_with_mesh (mem_watch.getVirtMemUsage());
 	INFO("Mem for mesh: %i MB", (mem_with_mesh - mem_without_mesh)/(1024*1024));
 #endif
-	run_time.stop();
+
 	INFO("Time for reading: %f seconds.", run_time.elapsed());
 	INFO("Read %d nodes and %d elements.", mesh->getNNodes(), mesh->getNElements());
 
diff --git a/BaseLib/CMakeLists.txt b/BaseLib/CMakeLists.txt
index 4ccc1008d13..1fcbef437fa 100644
--- a/BaseLib/CMakeLists.txt
+++ b/BaseLib/CMakeLists.txt
@@ -2,6 +2,10 @@
 
 GET_SOURCE_FILES(SOURCES)
 
+IF(NOT OGS_USE_PETSC)
+	list(REMOVE_ITEM SOURCES PETScWallClockTimer.h)
+ENDIF()
+
 LIST(APPEND SOURCES "${CMAKE_CURRENT_BINARY_DIR}/BuildInfo.cpp" BuildInfo.h)
 
 # Create the library
diff --git a/BaseLib/CPUTime.cpp b/BaseLib/CPUTime.cpp
deleted file mode 100644
index adbf4777f68..00000000000
--- a/BaseLib/CPUTime.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
- * \file
- * \author Thomas Fischer
- * \date   2012-05-10
- * \brief  Implementation of the CPUTime class.
- *
- * \copyright
- * Copyright (c) 2012-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
- *
- */
-
-#include "CPUTime.h"
-
-namespace BaseLib {
-
-void CPUTime::start()
-{
-	_start = clock();
-}
-
-void CPUTime::stop()
-{
-	_stop = clock();
-}
-
-double CPUTime::elapsed()
-{
-	return (_stop-_start)/(double)(CLOCKS_PER_SEC);
-}
-
-} // end namespace BaseLib
diff --git a/BaseLib/CPUTime.h b/BaseLib/CPUTime.h
index ab76d96de36..e527d3a8c7f 100644
--- a/BaseLib/CPUTime.h
+++ b/BaseLib/CPUTime.h
@@ -1,7 +1,8 @@
 /**
  * \file
  * \author Thomas Fischer
- * \date   2012-05-10
+ * \author Wenqing Wang
+ * \date   2012-05-10, 2014.10.10
  * \brief  Definition of the CPUTime class.
  *
  * \copyright
@@ -17,17 +18,26 @@
 
 #include <ctime>
 
-namespace BaseLib {
+namespace BaseLib
+{
 
+/// Record CPU time
 class CPUTime
 {
-public:
-	void start();
-    void stop();
-    double elapsed();
-private:
-	clock_t _start;
-	clock_t _stop;
+    public:
+        /// Start the timer.
+        void start()
+        {
+            _timer = - clock()/static_cast<double>(CLOCKS_PER_SEC);
+        }
+
+        /// Get the epalsed time after started.
+        double elapsed()
+        {
+            return _timer + clock()/static_cast<double>(CLOCKS_PER_SEC);
+        }
+    private:
+        double _timer = 0.;
 };
 
 } // end namespace BaseLib
diff --git a/BaseLib/RunTime.cpp b/BaseLib/RunTime.cpp
deleted file mode 100644
index a8a718ce113..00000000000
--- a/BaseLib/RunTime.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-/**
- * \file
- * \author Thomas Fischer
- * \date   2012-05-10
- * \brief  Implementation of the RunTime class.
- *
- * \copyright
- * Copyright (c) 2012-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
- *
- */
-
-#include "RunTime.h"
-
-namespace BaseLib {
-
-void RunTime::start()
-{
-#ifndef _MSC_VER
-	gettimeofday(&_start, 0);
-#else
-	_start = timeGetTime();
-#endif
-}
-
-void RunTime::stop()
-{
-#ifndef _MSC_VER
-	gettimeofday(&_stop, 0);
-#else
-	_stop = timeGetTime();
-#endif
-}
-
-double RunTime::elapsed()
-{
-#ifndef _MSC_VER
-	return (_stop.tv_sec + _stop.tv_usec/1000000.0 - (_start.tv_sec + _start.tv_usec/1000000.0));
-#else
-	return (_stop - _start) / 1000.0;
-#endif
-}
-
-} // end namespace BaseLib
diff --git a/BaseLib/RunTime.h b/BaseLib/RunTime.h
index f258503074f..96d9929528f 100644
--- a/BaseLib/RunTime.h
+++ b/BaseLib/RunTime.h
@@ -1,7 +1,8 @@
 /**
  * \file
  * \author Thomas Fischer
- * \date   2012-05-10
+ * \author Wenqing Wang
+ * \date   2012-05-10, 2014-10.10
  * \brief  Definition of the RunTime class.
  *
  * \copyright
@@ -15,28 +16,58 @@
 #ifndef RUNTIME_H
 #define RUNTIME_H
 
+#if defined(USE_MPI) || defined(USE_PETSC)
+#include <mpi.h>
+#else
 #ifndef _MSC_VER
 #include <sys/time.h>
 #else
 #include <windows.h>
 #endif
+#endif
 
-namespace BaseLib {
+namespace BaseLib
+{
 
+/// Record the running time.
 class RunTime
 {
-public:
-	void start();
-	void stop();
-	double elapsed();
-private:
+    public:
+        /// Start the timer.
+        void start()
+        {
+#if defined(USE_MPI) || defined(USE_PETSC)
+            _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;
+#endif
+#endif
+        }
+
+        /// Get the epalsed time after started.
+        double elapsed()
+        {
+#if defined(USE_MPI) || defined(USE_PETSC)
+            return _timer + MPI_Wtime();
+#else
 #ifndef _MSC_VER
-	timeval _start;
-	timeval _stop;
+            timeval t;
+            gettimeofday(&t, 0);
+            _timer += t.tv_sec + t.tv_usec/1000000.0;
+            return _timer;
 #else
-	unsigned long _start;
-	unsigned long _stop;
+            return _timer + timeGetTime()/1000.0;
+#endif
 #endif
+        }
+
+    private:
+        double _timer = 0.;
 };
 
 } // end namespace BaseLib
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9e02a359028..0e8b81cfba6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -65,7 +65,7 @@ OPTION(OGS_NO_EXTERNAL_LIBS "Builds OGS without any external dependencies." OFF)
 # Linear solvers
 OPTION(OGS_USE_LIS "Use Lis" OFF)
 
-# Parallel computing
+# Parallel computing: vector and matrix algebraic caculation, solvers
 OPTION(OGS_USE_PETSC "Use PETSc routines" OFF)
 
 # Paralleization
@@ -122,6 +122,10 @@ 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/SimpleTests/MatrixTests/MatMult.cpp b/SimpleTests/MatrixTests/MatMult.cpp
index bdb2b33191e..2fb35be5354 100644
--- a/SimpleTests/MatrixTests/MatMult.cpp
+++ b/SimpleTests/MatrixTests/MatMult.cpp
@@ -129,7 +129,6 @@ int main(int argc, char *argv[])
 		BaseLib::RunTime timer;
 		timer.start();
 		CS_read(in, n, iA, jA, A);
-		timer.stop();
 		INFO("\t- took %e s", timer.elapsed());
 	} else {
 		ERR("error reading matrix from %s", fname_mat.c_str());
@@ -180,8 +179,6 @@ int main(int argc, char *argv[])
 	for (size_t k(0); k<n_mults; k++) {
 		mat.amux (1.0, x, y);
 	}
-	cpu_timer.stop();
-	run_timer.stop();
 
 	INFO("\t[MVM] - took %e sec cpu time, %e sec run time", cpu_timer.elapsed(), run_timer.elapsed());
 
diff --git a/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp b/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp
index ce4b7e5f799..e2d390bc5b5 100644
--- a/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp
+++ b/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp
@@ -43,7 +43,6 @@ int main(int argc, char *argv[])
 		timer.start();
 		CS_read(in, n, iA, jA, A);
 		in.close();
-		timer.stop();
 		if (verbose) {
 			std::cout << "ok, " << timer.elapsed() << " s" << std::endl;
 		}
@@ -68,7 +67,6 @@ int main(int argc, char *argv[])
 	std::cout << "erasing " << n_rows_cols_to_erase << " rows and columns ... " << std::flush;
 	timer.start();
 	mat->eraseEntries(n_rows_cols_to_erase, rows_cols_to_erase);
-	timer.stop();
 	std::cout << "ok, " << timer.elapsed() << " s" << std::endl;
 	delete[] rows_cols_to_erase;
 
diff --git a/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp b/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp
index 7960ecef89e..0048e5a61bc 100644
--- a/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp
+++ b/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp
@@ -119,7 +119,6 @@ int main(int argc, char *argv[])
 		BaseLib::RunTime timer;
 		timer.start();
 		CS_read(in, n, iA, jA, A);
-		timer.stop();
 		if (verbose) {
 			INFO("\t- took %e s", timer.elapsed());
 		}
@@ -156,8 +155,6 @@ int main(int argc, char *argv[])
 	for (unsigned k(0); k<n; k++)
 		op_perm[k] = po_perm[k] = k;
 	cluster_tree.createClusterTree(op_perm, po_perm, 1000);
-	cpu_timer.stop();
-	run_timer.stop();
 	if (verbose) {
 		INFO("\t[ND] - took %e sec \t%e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
@@ -169,8 +166,6 @@ int main(int argc, char *argv[])
 	run_timer.start();
 	cpu_timer.start();
 	mat.reorderMatrix(op_perm, po_perm);
-	cpu_timer.stop();
-	run_timer.stop();
 	if (verbose) {
 		INFO("\t[ND]: - took %e sec\t%e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
@@ -193,8 +188,6 @@ int main(int argc, char *argv[])
 	for (size_t k(0); k<n_mults; k++) {
 		mat.amux (1.0, x, y);
 	}
-	cpu_timer.stop();
-	run_timer.stop();
 
 	if (verbose) {
 		INFO("\t[MVM] - took %e sec\t %e sec", cpu_timer.elapsed(), run_timer.elapsed());
diff --git a/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp b/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp
index c39749b306e..c43d0db2eaf 100644
--- a/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp
+++ b/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp
@@ -129,7 +129,6 @@ int main(int argc, char *argv[])
 		BaseLib::RunTime timer;
 		timer.start();
 		CS_read(in, n, iA, jA, A);
-		timer.stop();
 		if (verbose) {
 			INFO("\t- took %e s", timer.elapsed());
 		}
@@ -174,8 +173,6 @@ int main(int argc, char *argv[])
 	for (unsigned k(0); k<n; k++)
 		op_perm[k] = po_perm[k] = k;
 	cluster_tree.createClusterTree(op_perm, po_perm, 1000);
-	cpu_timer.stop();
-	run_timer.stop();
 	if (verbose) {
 		INFO("\t[ND] - took %e sec \t%e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
@@ -187,8 +184,6 @@ int main(int argc, char *argv[])
 	run_timer.start();
 	cpu_timer.start();
 	mat.reorderMatrix(op_perm, po_perm);
-	cpu_timer.stop();
-	run_timer.stop();
 	if (verbose) {
 		INFO("\t[ND]: - took %e sec\t%e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
@@ -202,8 +197,6 @@ int main(int argc, char *argv[])
 	for (size_t k(0); k<n_mults; k++) {
 		mat.amux (1.0, x, y);
 	}
-	cpu_timer.stop();
-	run_timer.stop();
 
 	if (verbose) {
 		INFO("\t[MVM] - took %e sec cpu time, %e sec run time", cpu_timer.elapsed(), run_timer.elapsed());
diff --git a/SimpleTests/MatrixTests/MatVecMultPthreads.cpp b/SimpleTests/MatrixTests/MatVecMultPthreads.cpp
index 0e129397010..31dca413088 100644
--- a/SimpleTests/MatrixTests/MatVecMultPthreads.cpp
+++ b/SimpleTests/MatrixTests/MatVecMultPthreads.cpp
@@ -121,7 +121,6 @@ INFO("%s was build with compiler %s",
 		BaseLib::RunTime timer;
 		timer.start();
 		CS_read(in, n, iA, jA, A);
-		timer.stop();
 		INFO("\t- took %e s", timer.elapsed());
 	} else {
 		ERR("error reading matrix from %s", fname_mat.c_str());
@@ -151,8 +150,6 @@ INFO("%s was build with compiler %s",
 	for (size_t k(0); k<n_mults; k++) {
 		mat.amux (1.0, x, y);
 	}
-	cpu_timer.stop();
-	run_timer.stop();
 
 	INFO("\t[MVM] - took %e sec cpu time, %e sec run time", cpu_timer.elapsed(), run_timer.elapsed());
 
diff --git a/SimpleTests/MeshTests/MeshRead.cpp b/SimpleTests/MeshTests/MeshRead.cpp
index c07f045cde4..d5067e6ddf2 100644
--- a/SimpleTests/MeshTests/MeshRead.cpp
+++ b/SimpleTests/MeshTests/MeshRead.cpp
@@ -66,7 +66,7 @@ int main(int argc, char *argv[])
 //	std::cout << "mem for mesh: " << (mem_with_mesh - mem_without_mesh)/(1024*1024) << " MB" << std::endl;
 	INFO ("mem for mesh: %i MB", (mem_with_mesh - mem_without_mesh)/(1024*1024));
 #endif
-	run_time.stop();
+
 //	std::cout << "time for reading: " << run_time.elapsed() << " s" << std::endl;
 	INFO ("time for reading: %f s", run_time.elapsed());
 
diff --git a/SimpleTests/MeshTests/MeshSearchTest.cpp b/SimpleTests/MeshTests/MeshSearchTest.cpp
index ff299ccac73..9e78dfd3c1e 100644
--- a/SimpleTests/MeshTests/MeshSearchTest.cpp
+++ b/SimpleTests/MeshTests/MeshSearchTest.cpp
@@ -131,7 +131,6 @@ int main(int argc, char *argv[])
 	unsigned long mem_with_mesh (mem_watch.getVirtMemUsage());
 	INFO ("mem for mesh: %i MB", (mem_with_mesh - mem_without_mesh)/(1024*1024));
 #endif
-	run_time.stop();
 	INFO ("time for reading: %f s", run_time.elapsed());
 
 	// *** preparing test data
diff --git a/SimpleTests/SolverTests/BiCGStabDiagPrecond.cpp b/SimpleTests/SolverTests/BiCGStabDiagPrecond.cpp
index 7946ec31de2..621a3060a15 100644
--- a/SimpleTests/SolverTests/BiCGStabDiagPrecond.cpp
+++ b/SimpleTests/SolverTests/BiCGStabDiagPrecond.cpp
@@ -66,9 +66,6 @@ int main(int argc, char *argv[])
 
 	MathLib::BiCGStab ((*mat), b, x, eps, steps);
 
-	cpu_timer.stop();
-	run_timer.stop();
-
 	if (verbose) {
 		std::cout << " in " << steps << " iterations" << std::endl;
 		std::cout << "\t(residuum is " << eps << ") took " << cpu_timer.elapsed() << " sec time and " << run_timer.elapsed() << " sec" << std::endl;
diff --git a/SimpleTests/SolverTests/ConjugateGradientDiagonalPreconditioned.cpp b/SimpleTests/SolverTests/ConjugateGradientDiagonalPreconditioned.cpp
index a79fe47c0f0..35e08af7983 100644
--- a/SimpleTests/SolverTests/ConjugateGradientDiagonalPreconditioned.cpp
+++ b/SimpleTests/SolverTests/ConjugateGradientDiagonalPreconditioned.cpp
@@ -74,9 +74,6 @@ int main(int argc, char *argv[])
 		#endif
 	}
 
-	cpu_timer.stop();
-	run_timer.stop();
-
 	if (verbose) {
 		std::cout << " in " << steps << " iterations" << std::endl;
 		std::cout << "\t(residuum is " << eps << ") took " << cpu_timer.elapsed() << " sec time and " << run_timer.elapsed() << " sec" << std::endl;
diff --git a/SimpleTests/SolverTests/GMResDiagPrecond.cpp b/SimpleTests/SolverTests/GMResDiagPrecond.cpp
index ad742b91379..a7ceb91d593 100644
--- a/SimpleTests/SolverTests/GMResDiagPrecond.cpp
+++ b/SimpleTests/SolverTests/GMResDiagPrecond.cpp
@@ -70,9 +70,6 @@ int main(int argc, char *argv[])
 
 	MathLib::GMRes((*mat), b, x, eps, 30, steps);
 
-	cpu_timer.stop();
-	run_timer.stop();
-
 	if (verbose) {
 		std::cout << " in " << steps << " iterations" << std::endl;
 		std::cout << "\t(residuum is " << eps << ") took "
-- 
GitLab