DFT-EFE
 
Loading...
Searching...
No Matches
Profiler.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2021. *
3 * The Regents of the University of Michigan and DFT-EFE developers. *
4 * *
5 * This file is part of the DFT-EFE code. *
6 * *
7 * DFT-EFE is free software: you can redistribute it and/or modify *
8 * it under the terms of the Lesser GNU General Public License as *
9 * published by the Free Software Foundation, either version 3 of *
10 * the License, or (at your option) any later version. *
11 * *
12 * DFT-EFE is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15 * See the Lesser GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License at the top level of DFT-EFE distribution. If not, see *
19 * <https://www.gnu.org/licenses/>. *
20 ******************************************************************************/
21
22/*
23 * @author Avirup Sircar
24 */
25
26#ifndef dftefeProfiler_h
27#define dftefeProfiler_h
28
29#include <utils/TypeConfig.h>
30#include <utils/MPITypes.h>
31#include <list>
32#include <map>
33#include <iomanip>
34#include <utils/Timer.h>
35#include <iostream>
36#include <string>
37#include <vector>
38#include <chrono>
39#include "sys/types.h"
40#include "sys/sysinfo.h"
41#ifdef DFTEFE_WITH_DEVICE
42# include <utils/DeviceAPICalls.h>
43#endif
44namespace dftefe
45{
46 namespace utils
47 {
48 template <dftefe::utils::MemorySpace memorySpace>
50 {
51 public:
52 struct Section
53 {
57 };
58
59 Profiler(const std::string &profileName = "");
60
61 Profiler(const mpi::MPIComm &mpiComm,
62 const std::string & profileName = "");
63
64 Profiler(const mpi::MPIComm & mpiComm,
65 const ConditionalOStream &stream,
66 const std::string & profileName = "");
67
68 ~Profiler();
69
70 void
71 registerStart(const std::string &sectionName);
72 void
73 registerEnd(const std::string &sectionName = "");
74 void
75 print() const;
76 Timer
77 getSectionTimer(const std::string &sectionName) const;
78 double
79 getSectionTotalWallTime(const std::string &sectionName) const;
81 getSectionCalls(const std::string &sectionName) const;
82 void
83 reset();
84
85 private:
86 std::map<std::string, Section> d_SectionsMap;
87 std::vector<std::string> d_insertionOrder;
88 std::list<std::string> d_activeSections;
92 std::string d_profileName;
93
94 }; // end of class Profiler
95
96 //
97 // helper function
98 //
99 template <dftefe::utils::MemorySpace memorySpace>
100 static inline void
102 const std::string message)
103 {
104 int rank;
105 mpi::MPICommRank(mpiComm, &rank);
106 ConditionalOStream cout((ConditionalOStream(std::cout)));
107 cout.setCondition(rank == 0);
108 mpi::MPIBarrier(mpiComm);
109
110 // --- Host memory ---
111 struct sysinfo memInfo;
112 sysinfo(&memInfo);
113 double totalVirtualMem = memInfo.totalram;
114 totalVirtualMem += memInfo.totalswap;
115 totalVirtualMem *= memInfo.mem_unit;
116 double virtualMemUsed = memInfo.totalram - memInfo.freeram;
117 virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
118 virtualMemUsed *= memInfo.mem_unit;
119 auto minMaxAvg =
120 mpi::MPIAllreduceMinMaxAvg<double, utils::MemorySpace::HOST>(
121 virtualMemUsed, mpiComm);
122 const double maxHostBytes = minMaxAvg.max;
123 // --- Device (GPU) memory ---
124 if constexpr (memorySpace == dftefe::utils::MemorySpace::DEVICE)
125 {
126#ifdef DFTEFE_WITH_DEVICE
127 std::size_t freeGPU = 0, totalGPU = 0;
128 {
129 deviceError_t err = deviceMemGetInfo(&freeGPU, &totalGPU);
130 DEVICE_API_CHECK(err);
131 }
132 double gpuUsed = static_cast<double>(totalGPU - freeGPU);
133 double gpuTotal = static_cast<double>(totalGPU);
134 auto gpuMinMaxAvg =
135 mpi::MPIAllreduceMinMaxAvg<double, utils::MemorySpace::HOST>(
136 gpuUsed, mpiComm);
137 cout << std::endl
138 << message << ", CPU: " << maxHostBytes / 1073741824.0
139 << " out of " << totalVirtualMem / 1073741824.0
140 << " GB, GPU: " << gpuMinMaxAvg.max / 1073741824.0 << " out of "
141 << gpuTotal / 1073741824.0 << " GB" << std::endl
142 << std::endl;
143#endif
144 }
145 else
146 {
147 cout << std::endl
148 << message << ", CPU: " << maxHostBytes / 1073741824.0
149 << " out of " << totalVirtualMem / 1073741824.0 << " GB"
150 << std::endl
151 << std::endl;
152 }
153
154 mpi::MPIBarrier(mpiComm);
155 }
156
157 } // end of namespace utils
158} // end of namespace dftefe
159#include "Profiler.t.cpp"
160#endif // dftefeProfiler_h
#define DEVICE_API_CHECK(cmd)
Definition: DeviceExceptions.cu.h:21
Provides an interface to print based on whether a certain condition is met or not....
Definition: ConditionalOStream.h:47
void setCondition(const bool active)
Function to set the condition for printing to the output stream associated with this object.
Definition: ConditionalOStream.cpp:48
Definition: Profiler.h:50
void registerEnd(const std::string &sectionName="")
Definition: Profiler.t.cpp:128
std::string d_profileName
Definition: Profiler.h:92
size_type getSectionCalls(const std::string &sectionName) const
Definition: Profiler.t.cpp:314
Timer d_totalTime
Definition: Profiler.h:91
~Profiler()
Definition: Profiler.t.cpp:65
void registerStart(const std::string &sectionName)
Definition: Profiler.t.cpp:76
void reset()
Definition: Profiler.t.cpp:333
std::vector< std::string > d_insertionOrder
Definition: Profiler.h:87
ConditionalOStream d_stream
Definition: Profiler.h:89
Timer getSectionTimer(const std::string &sectionName) const
Definition: Profiler.t.cpp:275
double getSectionTotalWallTime(const std::string &sectionName) const
Definition: Profiler.t.cpp:294
std::list< std::string > d_activeSections
Definition: Profiler.h:88
std::map< std::string, Section > d_SectionsMap
Definition: Profiler.h:86
const mpi::MPIComm d_mpiComm
Definition: Profiler.h:90
void print() const
Definition: Profiler.t.cpp:174
Definition: Timer.h:42
int MPICommRank(MPIComm comm, int *rank)
Definition: MPIWrapper.cpp:302
int MPIComm
Definition: MPITypes.h:84
int MPIBarrier(MPIComm comm)
Definition: MPIWrapper.cpp:268
cudaError_t deviceError_t
Definition: DeviceTypeConfig.cu.h:26
static void printCurrentMemoryUsage(const utils::mpi::MPIComm &mpiComm, const std::string message)
Definition: Profiler.h:101
dealii includes
Definition: AtomFieldDataSpherical.cpp:31
std::uint64_t size_type
Definition: TypeConfig.h:9
Definition: Profiler.h:53
Timer timer
Definition: Profiler.h:54
size_type nCalls
Definition: Profiler.h:56
double totalWallTime
Definition: Profiler.h:55