• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / sdfglib / 17300165647

28 Aug 2025 03:14PM UTC coverage: 60.049% (+0.3%) from 59.781%
17300165647

Pull #210

github

web-flow
Merge f6109d03a into 18d34db1e
Pull Request #210: New debug info

377 of 593 new or added lines in 37 files covered. (63.58%)

15 existing lines in 8 files now uncovered.

9588 of 15967 relevant lines covered (60.05%)

114.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/data_flow/library_nodes/math/ml/reduce_mean.cpp
1
#include "sdfg/data_flow/library_nodes/math/ml/reduce_mean.h"
2

3
#include "sdfg/analysis/analysis.h"
4
#include "sdfg/analysis/scope_analysis.h"
5
#include "sdfg/builder/structured_sdfg_builder.h"
6

7
namespace sdfg {
8
namespace math {
9
namespace ml {
10

11
ReduceMeanNode::ReduceMeanNode(
×
12
    size_t element_id,
13
    const DebugInfoRegion &debug_info,
14
    const graph::Vertex vertex,
15
    data_flow::DataFlowGraph &parent,
16
    int axis
17
)
18
    : MathNode(
×
NEW
19
          element_id,
×
NEW
20
          debug_info,
×
NEW
21
          vertex,
×
NEW
22
          parent,
×
23
          LibraryNodeType_ReduceMean,
NEW
24
          {"output"},
×
NEW
25
          {"input"},
×
26
          data_flow::ImplementationType_NONE
27
      ),
28
      axis_(axis) {}
×
29

30
void ReduceMeanNode::validate(const Function &) const { /* TODO */ }
×
31

32
bool ReduceMeanNode::expand(builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager) {
×
33
    auto &dataflow = this->get_parent();
×
34
    auto &block = static_cast<structured_control_flow::Block &>(*dataflow.get_parent());
×
35

36
    auto &scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
×
37
    auto &parent = static_cast<structured_control_flow::Sequence &>(*scope_analysis.parent_scope(&block));
×
38

39
    // Locate edges
40
    const data_flow::Memlet *iedge_input = nullptr;
×
41
    const data_flow::Memlet *oedge_output = nullptr;
×
42
    for (const auto &edge : dataflow.in_edges(*this)) {
×
43
        if (edge.dst_conn() == "input") {
×
44
            iedge_input = &edge;
×
45
        }
×
46
    }
47
    for (const auto &edge : dataflow.out_edges(*this)) {
×
48
        if (edge.src_conn() == "output") {
×
49
            oedge_output = &edge;
×
50
        }
×
51
    }
52
    if (!iedge_input || !oedge_output) return false;
×
53

54
    std::string input_name = static_cast<const data_flow::AccessNode &>(iedge_input->src()).data();
×
55
    std::string output_name = static_cast<const data_flow::AccessNode &>(oedge_output->dst()).data();
×
56

57
    // Create new sequence before
58
    auto &new_sequence = builder.add_sequence_before(parent, block, block.debug_info()).first;
×
59
    structured_control_flow::Sequence *last_scope = &new_sequence;
×
60

61
    // Create maps over output subset dims (parallel dims)
62
    data_flow::Subset domain_begin = oedge_output->begin_subset();
×
63
    data_flow::Subset domain_end = oedge_output->end_subset();
×
64

65
    std::vector<symbolic::Expression> loop_syms;
×
66
    structured_control_flow::Map *last_map = nullptr;
×
67
    for (size_t d = 0; d < domain_begin.size(); ++d) {
×
68
        std::string indvar_str = builder.find_new_name("_i");
×
69
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
×
70
        auto indvar = symbolic::symbol(indvar_str);
×
71
        auto init = domain_begin[d];
×
72
        auto update = symbolic::add(indvar, symbolic::one());
×
73
        auto cond = symbolic::Lt(indvar, symbolic::add(domain_end[d], symbolic::one()));
×
74
        last_map = &builder.add_map(
×
75
            *last_scope,
×
76
            indvar,
77
            cond,
78
            init,
79
            update,
80
            structured_control_flow::ScheduleType_Sequential,
81
            {},
×
82
            block.debug_info()
×
83
        );
84
        last_scope = &last_map->root();
×
85
        loop_syms.push_back(indvar);
×
86
    }
×
87

88
    // Initialize temp variable to zero
89
    std::string temp_name = builder.find_new_name("_tmp");
×
90
    types::Scalar temp_type(types::PrimitiveType::Float);
×
91
    builder.add_container(temp_name, temp_type);
×
92

93
    auto &init_block = builder.add_block(*last_scope);
×
94
    auto &init_tasklet = builder.add_tasklet(init_block, data_flow::TaskletCode::assign, "_out", {"0.0f"});
×
95
    auto &tmp_access_init = builder.add_access(init_block, temp_name);
×
96
    builder.add_computational_memlet(init_block, init_tasklet, "_out", tmp_access_init, {}, temp_type);
×
97

98
    // add reduction for loop
99
    symbolic::Expression red_begin;
×
100
    symbolic::Expression red_end;
×
101
    if (axis_ >= 0) {
×
102
        red_begin = iedge_input->begin_subset()[axis_];
×
103
        red_end = iedge_input->end_subset()[axis_];
×
104
    } else {
×
105
        red_begin = iedge_input->begin_subset().back();
×
106
        red_end = iedge_input->end_subset().back();
×
107
    }
108
    std::string red_name = builder.find_new_name("_i");
×
109
    builder.add_container(red_name, types::Scalar(types::PrimitiveType::UInt64));
×
110
    auto red_indvar = symbolic::symbol(red_name);
×
111
    auto red_init = red_begin;
×
112
    auto red_update = symbolic::add(red_indvar, symbolic::one());
×
113
    auto red_cond = symbolic::Lt(red_indvar, symbolic::add(red_end, symbolic::one()));
×
NEW
114
    auto red_map = &builder.add_for(*last_scope, red_indvar, red_cond, red_init, red_update, {}, block.debug_info());
×
115

116
    // Create innermost block
117
    auto &code_block = builder.add_block(red_map->root());
×
118

119
    // Create access nodes for input and output
120
    auto &input_access = builder.add_access(code_block, input_name);
×
121
    auto &tmp2_access = builder.add_access(code_block, temp_name);
×
122
    auto &tmp_access_out = builder.add_access(code_block, temp_name);
×
123
    auto &tmp_access_in = builder.add_access(code_block, temp_name);
×
124

125
    // Create index expressions for input and output
126
    std::vector<symbolic::Expression> input_subset = loop_syms;
×
127

128
    // Replace the reduction axis index with the reduction variable for input
129
    if (axis_ >= 0 && axis_ < static_cast<int>(input_subset.size())) {
×
130
        input_subset.insert(input_subset.begin() + axis_, red_indvar);
×
131
    } else if (axis_ < 0) {
×
132
        input_subset.push_back(red_indvar);
×
133
    }
×
134

135
    // Compute division
136
    auto num_elements = symbolic::add(red_end, symbolic::one());
×
137
    auto num_elements_int = SymEngine::rcp_static_cast<const SymEngine::Integer>(num_elements)->as_int();
×
NEW
138
    auto &div_tasklet = builder.add_tasklet(
×
NEW
139
        code_block, data_flow::TaskletCode::div, "_out", {"_in1", std::to_string(num_elements_int) + ".0f"}
×
140
    );
NEW
141
    builder
×
NEW
142
        .add_computational_memlet(code_block, input_access, div_tasklet, "_in", input_subset, iedge_input->base_type());
×
UNCOV
143
    builder.add_computational_memlet(code_block, div_tasklet, "_out", tmp2_access, {}, temp_type);
×
144

145
    // Add to temp (reduction)
146
    auto &add_tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::add, "_out", {"_in1", "_in2"});
×
147
    builder.add_computational_memlet(code_block, tmp_access_in, add_tasklet, "_in1", {}, temp_type);
×
148
    builder.add_computational_memlet(code_block, tmp2_access, add_tasklet, "_in2", {}, temp_type);
×
149
    builder.add_computational_memlet(code_block, add_tasklet, "_out", tmp_access_out, {}, temp_type);
×
150

151
    // Create writeback - assign the accumulated sum to output
152
    auto &writeback_block = builder.add_block(*last_scope);
×
153
    auto &tmp_access_wb = builder.add_access(writeback_block, temp_name);
×
154
    auto &output_access_wb = builder.add_access(writeback_block, output_name);
×
155
    auto &writeback_tasklet = builder.add_tasklet(writeback_block, data_flow::TaskletCode::assign, "_out", {"_in"});
×
156
    builder.add_computational_memlet(writeback_block, tmp_access_wb, writeback_tasklet, "_in", {}, temp_type);
×
NEW
157
    builder.add_computational_memlet(
×
NEW
158
        writeback_block, writeback_tasklet, "_out", output_access_wb, loop_syms, oedge_output->base_type()
×
159
    );
160

161
    // Cleanup old block
162
    builder.remove_memlet(block, *iedge_input);
×
163
    builder.remove_memlet(block, *oedge_output);
×
164
    builder.remove_node(block, *this);
×
165
    builder.remove_child(parent, block);
×
166

167
    return true;
×
168
}
×
169

170
std::unique_ptr<data_flow::DataFlowNode> ReduceMeanNode::
171
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
NEW
172
    return std::unique_ptr<
×
NEW
173
        data_flow::DataFlowNode>(new ReduceMeanNode(element_id, this->debug_info(), vertex, parent, axis_));
×
UNCOV
174
}
×
175

176
nlohmann::json ReduceMeanNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
177
    const ReduceMeanNode &node = static_cast<const ReduceMeanNode &>(library_node);
×
178
    nlohmann::json j;
×
179

180
    j["code"] = node.code().value();
×
181
    j["axis"] = node.axis();
×
182

183
    return j;
×
184
}
×
185

186
data_flow::LibraryNode &ReduceMeanNodeSerializer::deserialize(
×
187
    const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent
188
) {
189
    auto code = j["code"].get<std::string>();
×
190
    if (code != LibraryNodeType_ReduceMean.value()) {
×
191
        throw std::runtime_error("Invalid library node code");
×
192
    }
193

194
    sdfg::serializer::JSONSerializer serializer;
×
NEW
195
    DebugInfoRegion debug_info = serializer.json_to_debug_info_region(j["debug_info_region"], builder.debug_info());
×
196

197
    auto axis = j["axis"].get<int>();
×
198

199
    return builder.add_library_node<ReduceMeanNode>(parent, debug_info, axis);
×
200
}
×
201

202
} // namespace ml
203
} // namespace math
204
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc