PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
Profiling.cc
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2016, 2021, 2022, 2023 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 <petsclog.h>
21 #include <petscviewer.h>
22 
23 #include "pism/util/Profiling.hh"
24 #include "pism/util/error_handling.hh"
25 
26 namespace pism {
27 
28 // PETSc profiling events
29 
31  PetscErrorCode ierr = PetscClassIdRegister("PISM", &m_classid);
32  PISM_CHK(ierr, "PetscClassIdRegister");
33 }
34 
35 //! Enable PETSc logging.
36 void Profiling::start() const {
37 #if PETSC_VERSION_LT(3,20,0)
38  PetscErrorCode ierr = PetscLogAllBegin(); PISM_CHK(ierr, "PetscLogAllBegin");
39 #else
40  PetscErrorCode ierr = PetscLogDefaultBegin(); PISM_CHK(ierr, "PetscLogAllBegin");
41 #endif
42 }
43 
44 //! Save detailed profiling data to a Python script.
45 void Profiling::report(const std::string &filename) const {
46  PetscErrorCode ierr;
47  PetscViewer log_viewer;
48 
49  ierr = PetscViewerCreate(PETSC_COMM_WORLD, &log_viewer);
50  PISM_CHK(ierr, "PetscViewerCreate");
51 
52  ierr = PetscViewerSetType(log_viewer, PETSCVIEWERASCII);
53  PISM_CHK(ierr, "PetscViewerSetType");
54 
55  ierr = PetscViewerFileSetName(log_viewer, filename.c_str());
56  PISM_CHK(ierr, "PetscViewerFileSetName");
57 
58  ierr = PetscViewerPushFormat(log_viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);
59  PISM_CHK(ierr, "PetscViewerPushFormat");
60 
61  ierr = PetscViewerSetUp(log_viewer);
62  PISM_CHK(ierr, "PetscViewerSetUp");
63 
64  ierr = PetscLogView(log_viewer);
65  PISM_CHK(ierr, "PetscLogView"); PISM_CHK(ierr, "PetscLogView");
66 
67  ierr = PetscViewerPopFormat(log_viewer);
68  PISM_CHK(ierr, "PetscViewerPopFormat");
69 
70  ierr = PetscViewerDestroy(&log_viewer);
71  PISM_CHK(ierr, "PetscViewerDestroy");
72 }
73 
74 
75 void Profiling::begin(const char * name) const {
76  PetscLogEvent event = 0;
77  PetscErrorCode ierr;
78 
79  if (m_events.find(name) == m_events.end()) {
80  // not registered yet
81  ierr = PetscLogEventRegister(name, m_classid, &event);
82  PISM_CHK(ierr, "PetscLogEventRegister");
83  m_events[name] = event;
84  } else {
85  event = m_events[name];
86  }
87  ierr = PetscLogEventBegin(event, 0, 0, 0, 0);
88  PISM_CHK(ierr, "PetscLogEventBegin");
89 }
90 
91 void Profiling::end(const char * name) const {
92 
93  if (m_events.find(name) == m_events.end()) {
95  "cannot end event \"%s\" because it was not started",
96  name);
97  }
98 
99  PetscErrorCode ierr = PetscLogEventEnd(m_events[name], 0, 0, 0, 0);
100  PISM_CHK(ierr, "PetscLogEventEnd");
101 }
102 
103 void Profiling::stage_begin(const char * name) const {
104  PetscLogStage stage = 0;
105  PetscErrorCode ierr;
106 
107  if (m_stages.find(name) == m_stages.end()) {
108  // not registered yet
109  ierr = PetscLogStageRegister(name, &stage);
110  PISM_CHK(ierr, "PetscLogStageRegister");
111  m_stages[name] = stage;
112  } else {
113  stage = m_stages[name];
114  }
115  ierr = PetscLogStagePush(stage);
116  PISM_CHK(ierr, "PetscLogStagePush");
117 }
118 
119 void Profiling::stage_end(const char * name) const {
120  (void) name;
121  PetscErrorCode ierr = PetscLogStagePop();
122  PISM_CHK(ierr, "PetscLogStagePop");
123 }
124 
125 } // end of namespace pism
void begin(const char *name) const
Definition: Profiling.cc:75
std::map< std::string, PetscLogEvent > m_events
Definition: Profiling.hh:40
void report(const std::string &filename) const
Save detailed profiling data to a Python script.
Definition: Profiling.cc:45
void end(const char *name) const
Definition: Profiling.cc:91
void start() const
Enable PETSc logging.
Definition: Profiling.cc:36
void stage_end(const char *name) const
Definition: Profiling.cc:119
PetscClassId m_classid
Definition: Profiling.hh:39
void stage_begin(const char *name) const
Definition: Profiling.cc:103
std::map< std::string, PetscLogStage > m_stages
Definition: Profiling.hh:41
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
#define PISM_CHK(errcode, name)
#define PISM_ERROR_LOCATION