docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
instrumentation_plan.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <unordered_map>
5#include <vector>
11#include "sdfg/element.h"
14#include "sdfg/options.h"
19
20namespace sdfg {
21namespace codegen {
22
23// Global opt-in for wrapping instrumented regions in an adaptive sampling loop.
24inline constexpr OptionKey<bool> INSTRUMENTATION_ADAPTIVE_SAMPLING{"instrumentation.adaptive_sampling"};
25
27protected:
29 std::unordered_set<const Element*> nodes_;
30 std::unordered_map<const Element*, ElementId> logical_region_ids_;
31 struct GroupSpan {
34 std::vector<const structured_control_flow::ControlFlowNode*> members;
35 };
36 std::vector<GroupSpan> group_spans_;
37 std::unordered_map<const structured_control_flow::ControlFlowNode*, size_t> group_start_by_node_;
38 std::unordered_set<const structured_control_flow::ControlFlowNode*> grouped_members_;
39 // When false, leaving_instrumentation_function does not emit finalize_all so a
40 // harness can resolve pending events once (e.g. after a warm sampling batch)
41 // instead of paying a host sync on every invocation of an SDFG.
43 // When true, each instrumented region is wrapped in an adaptive sampling loop.
45
46public:
49 const std::unordered_set<const Element*>& nodes,
50 bool emit_finalize_all = true,
51 bool sampling = false
52 )
53 : sdfg_(sdfg), nodes_(nodes), emit_finalize_all_(emit_finalize_all), sampling_(sampling) {
54 }
55
58
61
62 bool is_empty() const {
63 return nodes_.empty();
64 }
65
66 bool sampling() const {
67 return sampling_;
68 }
69
70 bool should_instrument(const Element& node) const;
71
73 const Element& node,
74 PrettyPrinter& stream,
75 LanguageExtension& language_extension,
76 const InstrumentationInfo& info
77 ) const;
78
80 const Element& node,
81 PrettyPrinter& stream,
82 LanguageExtension& language_extension,
83 const InstrumentationInfo& info
84 ) const;
85
86 void leaving_instrumentation_function(PrettyPrinter& stream, LanguageExtension& language_extension) const;
87
88 void insert(const Element* node) {
89 nodes_.insert(node);
90 }
91
92 std::optional<ElementId> logical_region_id(const Element& node) const {
93 auto it = logical_region_ids_.find(&node);
94 return it == logical_region_ids_.end() ? std::nullopt : std::optional<ElementId>(it->second);
95 }
96
97 std::optional<ElementId> source_loop_id(const Element& node) const {
98 return metadata::source_loop_id(node);
99 }
100
101 std::optional<nlohmann::json> expected_performance(const Element& node) const {
102 const auto result = metadata::rpc_optimization(node);
103 return result.has_value() ? result->expected_performance : std::nullopt;
104 }
105
106 std::optional<double> vector_distance(const Element& node) const {
107 const auto result = metadata::rpc_optimization(node);
108 return result.has_value() ? result->vector_distance : std::nullopt;
109 }
110
112 auto it = group_start_by_node_.find(&node);
113 return it == group_start_by_node_.end() ? nullptr : &group_spans_[it->second];
114 }
115
117 return grouped_members_.find(&node) != grouped_members_.end();
118 }
119
120 static std::unique_ptr<InstrumentationPlan> none(StructuredSDFG& sdfg);
121
122 static std::unique_ptr<InstrumentationPlan>
123 outermost_loops_plan(StructuredSDFG& sdfg, bool emit_finalize_all = true, bool sampling = false);
124
125 static std::unique_ptr<InstrumentationPlan>
126 provenance_grouped_outermost_loops_plan(StructuredSDFG& sdfg, bool emit_finalize_all = true, bool sampling = false);
127};
128
129} // namespace codegen
130} // namespace sdfg
Definition element.h:159
Definition structured_sdfg.h:30
Definition instrumentation_info.h:37
std::optional< ElementId > source_loop_id(const Element &node) const
Definition instrumentation_plan.h:97
bool emit_finalize_all_
Definition instrumentation_plan.h:42
bool sampling() const
Definition instrumentation_plan.h:66
InstrumentationPlan & operator=(const InstrumentationPlan &other)=delete
InstrumentationPlan & operator=(InstrumentationPlan &&other)=delete
InstrumentationPlan(const InstrumentationPlan &other)=delete
InstrumentationPlan(InstrumentationPlan &&other)=delete
std::optional< ElementId > logical_region_id(const Element &node) const
Definition instrumentation_plan.h:92
void begin_instrumentation(const Element &node, PrettyPrinter &stream, LanguageExtension &language_extension, const InstrumentationInfo &info) const
bool should_instrument(const Element &node) const
void end_instrumentation(const Element &node, PrettyPrinter &stream, LanguageExtension &language_extension, const InstrumentationInfo &info) const
void leaving_instrumentation_function(PrettyPrinter &stream, LanguageExtension &language_extension) const
std::unordered_map< const Element *, ElementId > logical_region_ids_
Definition instrumentation_plan.h:30
InstrumentationPlan(StructuredSDFG &sdfg, const std::unordered_set< const Element * > &nodes, bool emit_finalize_all=true, bool sampling=false)
Definition instrumentation_plan.h:47
static std::unique_ptr< InstrumentationPlan > provenance_grouped_outermost_loops_plan(StructuredSDFG &sdfg, bool emit_finalize_all=true, bool sampling=false)
bool is_group_member(const structured_control_flow::ControlFlowNode &node) const
Definition instrumentation_plan.h:116
StructuredSDFG & sdfg_
Definition instrumentation_plan.h:28
void insert(const Element *node)
Definition instrumentation_plan.h:88
static std::unique_ptr< InstrumentationPlan > outermost_loops_plan(StructuredSDFG &sdfg, bool emit_finalize_all=true, bool sampling=false)
bool is_empty() const
Definition instrumentation_plan.h:62
std::optional< nlohmann::json > expected_performance(const Element &node) const
Definition instrumentation_plan.h:101
std::unordered_set< const Element * > nodes_
Definition instrumentation_plan.h:29
std::vector< GroupSpan > group_spans_
Definition instrumentation_plan.h:36
static std::unique_ptr< InstrumentationPlan > none(StructuredSDFG &sdfg)
const GroupSpan * group_span_starting_at(const structured_control_flow::ControlFlowNode &node) const
Definition instrumentation_plan.h:111
std::optional< double > vector_distance(const Element &node) const
Definition instrumentation_plan.h:106
std::unordered_map< const structured_control_flow::ControlFlowNode *, size_t > group_start_by_node_
Definition instrumentation_plan.h:37
bool sampling_
Definition instrumentation_plan.h:44
std::unordered_set< const structured_control_flow::ControlFlowNode * > grouped_members_
Definition instrumentation_plan.h:38
Definition language_extension.h:39
Definition utils.h:16
Base class for all structured control flow nodes in a StructuredSDFG.
Definition control_flow_node.h:44
A sequential container of control flow nodes.
Definition sequence.h:52
Library node definitions for calling functions beyond simple instructions.
Definition argument_registry.h:10
constexpr OptionKey< bool > INSTRUMENTATION_ADAPTIVE_SAMPLING
Definition instrumentation_plan.h:24
std::optional< ElementId > source_loop_id(const Element &element)
Definition loop_provenance.h:14
std::optional< RpcOptimizationMetadata > rpc_optimization(const Element &element)
Definition rpc_optimization.h:20
Definition analysis.h:7
size_t ElementId
Definition element.h:215
Definition options.h:40
Definition instrumentation_plan.h:31
std::vector< const structured_control_flow::ControlFlowNode * > members
Definition instrumentation_plan.h:34
const structured_control_flow::Sequence * sequence
Definition instrumentation_plan.h:33
ElementId source_loop_id
Definition instrumentation_plan.h:32
Symbolic system for integer-based symbol analysis.