diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp index d90ea999b16ebd72e404ce364f0c90dffd5dbfb2..17e04988fda7371f74d7b6ef36fbb2826aedb5d2 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 e527d3a8c7f8d608fc0e8b671bb68bc355994912..0d2eaed733dcb1b982d39d9f426d956f7b566bb1 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 96d9929528f3017da6c3eb64d60562f1c0f767ac..37d9220b7c58e5f2f2fa7dfc957b90f27709d616 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 0e8b81cfba61ef140998aa9a845b55cbfc153b09..15f12fc63ebe397b6f68bdeb4c3793d4f6e68148 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 74433adb83c0add16d71b24ce5d1c1b485a8e5db..a10318d7e4c4fb040439cbe201ddd732180ab44a 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 b3d9660d101f006a23fa25c5cc5a4f0d074d03e3..d65efa4191189671f4cc27a1525b8e786b7c294c 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