forked from LLNL/Caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cxx-example.cpp
110 lines (84 loc) · 3.5 KB
/
cxx-example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2015-2020, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
// A C++ Caliper instrumentation and ConfigManager example
// Usage: $ cali-basic-annotations <configuration-string>
// For example, "$ cali-basic-annotations runtime-report" will print a
// hierarchical runtime summary for all annotated regions.
#include <caliper/cali.h>
#include <caliper/cali-manager.h>
#include <cstring>
#include <iostream>
#include <string>
void print_help(const cali::ConfigManager& mgr)
{
std::cerr << "Usage: cxx-example [caliper-config(arg=...,),...]."
<< "\nRuns \"runtime-report\" configuration by default."
<< "\nUse \"none\" to run without a ConfigManager configuration."
<< "\nAvailable configurations: ";
auto configs = mgr.available_config_specs();
// Print info on all available ConfigManager configurations.
for (auto str : configs)
std::cerr << "\n" << mgr.get_documentation_for_spec(str.c_str());
std::cerr << std::endl;
}
double foo(int i)
{
// A function annotation. Opens region "function=foo" in Caliper,
// and automatically closes it at the end of the function.
CALI_CXX_MARK_FUNCTION;
return 0.5*i;
}
int main(int argc, char* argv[])
{
// The ConfigManager manages built-in or custom Caliper measurement
// configurations, and provides an API to control performance profiling.
cali::ConfigManager mgr;
// We can set default parameters for the Caliper configurations.
// These can be overridden in the user-provided configuration string.
mgr.set_default_parameter("aggregate_across_ranks", "false");
// Use the "runtime-report" configuration by default to print a
// runtime summary for all annotated regions. Let users overwrite
// the default configuration with their own on the command line.
std::string configstr = "runtime-report";
if (argc > 1) {
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_help(mgr);
return 0;
} else {
configstr = argv[1];
}
}
if (configstr == "none")
configstr.clear();
// Enable the requested performance measurement channels and start
// profiling.
mgr.add(configstr.c_str());
if (mgr.error())
std::cerr << "Caliper config error: " << mgr.error_msg() << std::endl;
mgr.start();
// Mark begin of the current function. Must be manually closed.
// Opens region "function=main" in Caliper.
CALI_MARK_FUNCTION_BEGIN;
// Mark a code region. Opens region "annotation=init" in Caliper.
CALI_MARK_BEGIN("init");
int count = 4;
double t = 0;
CALI_MARK_END("init");
// Mark a loop. Opens region "loop=mainloop" in Caliper.
CALI_CXX_MARK_LOOP_BEGIN(loop_ann, "mainloop");
for (int i = 0; i < count; ++i) {
// Mark loop iterations of an annotated loop.
// Sets "iteration#main loop=<i> in Caliper.
CALI_CXX_MARK_LOOP_ITERATION(loop_ann, i);
// A Caliper snapshot taken at this point will contain
// { "function"="main", "loop"="mainloop", "iteration#main loop"=<i> }
t += foo(i);
}
// Mark the end of the "loop=mainloop" region.
CALI_CXX_MARK_LOOP_END(loop_ann);
// Mark the end of the "function=main" region.
CALI_MARK_FUNCTION_END;
// Trigger output in all Caliper control channels.
// This should be done after all measurement regions have been closed.
mgr.flush();
}