tProfiling.cc - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
tProfiling.cc (3686B)
---
1 /* Copyright (C) 2015, 2016 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <petscviewer.h>
21
22 #include "Profiling.hh"
23 #include "error_handling.hh"
24
25 namespace pism {
26
27 // PETSc profiling events
28
29 Profiling::Profiling() {
30 PetscErrorCode ierr = PetscClassIdRegister("PISM", &m_classid);
31 PISM_CHK(ierr, "PetscClassIdRegister");
32 }
33
34 //! Enable PETSc logging.
35 void Profiling::start() const {
36 #if PETSC_VERSION_LE(3,6,3)
37 PetscErrorCode ierr = PetscLogBegin(); PISM_CHK(ierr, "PetscLogBegin");
38 #else
39 PetscErrorCode ierr = PetscLogAllBegin(); PISM_CHK(ierr, "PetscLogAllBegin");
40 #endif
41 }
42
43 //! Save detailed profiling data to a Python script.
44 void Profiling::report(const std::string &filename) const {
45 PetscErrorCode ierr;
46 PetscViewer log_viewer;
47
48 ierr = PetscViewerCreate(PETSC_COMM_WORLD, &log_viewer);
49 PISM_CHK(ierr, "PetscViewerCreate");
50
51 ierr = PetscViewerSetType(log_viewer, PETSCVIEWERASCII);
52 PISM_CHK(ierr, "PetscViewerSetType");
53
54 ierr = PetscViewerFileSetName(log_viewer, filename.c_str());
55 PISM_CHK(ierr, "PetscViewerFileSetName");
56
57 ierr = PetscViewerPushFormat(log_viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);
58 PISM_CHK(ierr, "PetscViewerPushFormat");
59
60 ierr = PetscViewerSetUp(log_viewer);
61 PISM_CHK(ierr, "PetscViewerSetUp");
62
63 ierr = PetscLogView(log_viewer);
64 PISM_CHK(ierr, "PetscLogView"); PISM_CHK(ierr, "PetscLogView");
65
66 ierr = PetscViewerPopFormat(log_viewer);
67 PISM_CHK(ierr, "PetscViewerPopFormat");
68
69 ierr = PetscViewerDestroy(&log_viewer);
70 PISM_CHK(ierr, "PetscViewerDestroy");
71 }
72
73
74 void Profiling::begin(const char * name) const {
75 PetscLogEvent event = 0;
76 PetscErrorCode ierr;
77
78 if (m_events.find(name) == m_events.end()) {
79 // not registered yet
80 ierr = PetscLogEventRegister(name, m_classid, &event);
81 PISM_CHK(ierr, "PetscLogEventRegister");
82 m_events[name] = event;
83 } else {
84 event = m_events[name];
85 }
86 ierr = PetscLogEventBegin(event, 0, 0, 0, 0);
87 PISM_CHK(ierr, "PetscLogEventBegin");
88 }
89
90 void Profiling::end(const char * name) const {
91 PetscLogEvent event = 0;
92 if (m_events.find(name) == m_events.end()) {
93 throw RuntimeError::formatted(PISM_ERROR_LOCATION, "cannot end event \"%s\" because it was not started",
94 name);
95 } else {
96 event = m_events[name];
97 }
98 PetscErrorCode ierr = PetscLogEventEnd(event, 0, 0, 0, 0);
99 PISM_CHK(ierr, "PetscLogEventEnd");
100 }
101
102 void Profiling::stage_begin(const char * name) const {
103 PetscLogStage stage = 0;
104 PetscErrorCode ierr;
105
106 if (m_stages.find(name) == m_stages.end()) {
107 // not registered yet
108 ierr = PetscLogStageRegister(name, &stage);
109 PISM_CHK(ierr, "PetscLogStageRegister");
110 m_stages[name] = stage;
111 } else {
112 stage = m_stages[name];
113 }
114 ierr = PetscLogStagePush(stage);
115 PISM_CHK(ierr, "PetscLogStagePush");
116 }
117
118 void Profiling::stage_end(const char * name) const {
119 (void) name;
120 PetscErrorCode ierr = PetscLogStagePop();
121 PISM_CHK(ierr, "PetscLogStagePop");
122 }
123
124 } // end of namespace pism