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

daisytuner / sdfglib / 16779575603

06 Aug 2025 02:17PM UTC coverage: 64.3%. First build
16779575603

Pull #172

github

web-flow
Merge 4d3cf21e9 into ec716bbcb
Pull Request #172: Opaque pointers, typed memlets, untyped tasklet connectors

330 of 462 new or added lines in 38 files covered. (71.43%)

8865 of 13787 relevant lines covered (64.3%)

116.73 hits per line

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

44.22
/src/data_flow/library_nodes/math/blas/gemm.cpp
1
#include "sdfg/data_flow/library_nodes/math/blas/gemm.h"
2

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

6
#include "sdfg/analysis/scope_analysis.h"
7

8
namespace sdfg {
9
namespace math {
10
namespace blas {
11

12
GEMMNode::GEMMNode(
1✔
13
    size_t element_id,
14
    const DebugInfo& debug_info,
15
    const graph::Vertex vertex,
16
    data_flow::DataFlowGraph& parent,
17
    const data_flow::ImplementationType& implementation_type,
18
    const BLAS_Precision& precision,
19
    const BLAS_Layout& layout,
20
    const BLAS_Transpose& trans_a,
21
    const BLAS_Transpose& trans_b,
22
    symbolic::Expression m,
23
    symbolic::Expression n,
24
    symbolic::Expression k,
25
    symbolic::Expression lda,
26
    symbolic::Expression ldb,
27
    symbolic::Expression ldc,
28
    const std::string& alpha,
29
    const std::string& beta
30
)
31
    : MathNode(
1✔
32
          element_id,
1✔
33
          debug_info,
1✔
34
          vertex,
1✔
35
          parent,
1✔
36
          LibraryNodeType_GEMM,
37
          {"C"},
1✔
38
          {"A", "B", "C", alpha, beta},
1✔
39
          implementation_type
1✔
40
      ),
41
      precision_(precision), layout_(layout), trans_a_(trans_a), trans_b_(trans_b), m_(m), n_(n), k_(k), lda_(lda),
1✔
42
      ldb_(ldb), ldc_(ldc) {}
2✔
43

44
BLAS_Precision GEMMNode::precision() const { return this->precision_; };
×
45

46
BLAS_Layout GEMMNode::layout() const { return this->layout_; };
×
47

48
BLAS_Transpose GEMMNode::trans_a() const { return this->trans_a_; };
×
49

50
BLAS_Transpose GEMMNode::trans_b() const { return this->trans_b_; };
×
51

52
symbolic::Expression GEMMNode::m() const { return this->m_; };
1✔
53

54
symbolic::Expression GEMMNode::n() const { return this->n_; };
1✔
55

56
symbolic::Expression GEMMNode::k() const { return this->k_; };
1✔
57

58
symbolic::Expression GEMMNode::lda() const { return this->lda_; };
1✔
59

60
symbolic::Expression GEMMNode::ldb() const { return this->ldb_; };
1✔
61

62
symbolic::Expression GEMMNode::ldc() const { return this->ldc_; };
1✔
63

64
const std::string& GEMMNode::alpha() const { return this->inputs_.at(3); };
1✔
65

66
const std::string& GEMMNode::beta() const { return this->inputs_.at(4); };
1✔
67

68
void GEMMNode::validate(const Function& function) const {
×
69
    auto& graph = this->get_parent();
×
70

71
    if (this->inputs_.size() != 5) {
×
72
        throw InvalidSDFGException("GEMMNode must have 5 inputs: A, B, C, (alpha), (beta)");
×
73
    }
74

75
    int input_edge_count = graph.in_degree(*this);
×
76
    if (input_edge_count < 3 || input_edge_count > 5) {
×
77
        throw InvalidSDFGException("GEMMNode must have 3-5 inputs");
×
78
    }
79
    if (graph.out_degree(*this) != 1) {
×
80
        throw InvalidSDFGException("GEMMNode must have 1 output");
×
81
    }
82

83
    // // Check if all inputs are connected A, B, C, (alpha), (beta)
84
    std::unordered_map<std::string, const data_flow::Memlet*> memlets;
×
85
    for (auto& input : this->inputs_) {
×
86
        bool found = false;
×
87
        for (auto& iedge : graph.in_edges(*this)) {
×
88
            if (iedge.dst_conn() == input) {
×
89
                found = true;
×
90
                memlets[input] = &iedge;
×
91
                break;
×
92
            }
93
        }
94
        if (!found && (input == "A" || input == "B" || input == "C")) {
×
95
            throw InvalidSDFGException("GEMMNode input " + input + " not found");
×
96
        }
97
    }
98

99
    // Check if output is connected to C
100
    auto& oedge = *graph.out_edges(*this).begin();
×
101
    if (oedge.src_conn() != this->outputs_.at(0)) {
×
102
        throw InvalidSDFGException("GEMMNode output " + this->outputs_.at(0) + " not found");
×
103
    }
104

105
    // Check dimensions of A, B, C
106
    auto& a_memlet = memlets.at("A");
×
107
    auto& a_subset_begin = a_memlet->begin_subset();
×
108
    auto& a_subset_end = a_memlet->end_subset();
×
109
    if (a_subset_begin.size() != 1) {
×
110
        throw InvalidSDFGException("GEMMNode input A must have 1 dimensions");
×
111
    }
112
    data_flow::Subset a_dims;
×
113
    for (size_t i = 0; i < a_subset_begin.size(); i++) {
×
114
        a_dims.push_back(symbolic::sub(a_subset_end[i], a_subset_begin[i]));
×
115
    }
×
116

117
    auto& b_memlet = memlets.at("B");
×
118
    auto& b_subset_begin = b_memlet->begin_subset();
×
119
    auto& b_subset_end = b_memlet->end_subset();
×
120
    if (b_subset_begin.size() != 1) {
×
121
        throw InvalidSDFGException("GEMMNode input B must have 1 dimensions");
×
122
    }
123
    data_flow::Subset b_dims;
×
124
    for (size_t i = 0; i < b_subset_begin.size(); i++) {
×
125
        b_dims.push_back(symbolic::sub(b_subset_end[i], b_subset_begin[i]));
×
126
    }
×
127

128
    auto& c_memlet = memlets.at("C");
×
129
    auto& c_subset_begin = c_memlet->begin_subset();
×
130
    auto& c_subset_end = c_memlet->end_subset();
×
131
    if (c_subset_begin.size() != 1) {
×
132
        throw InvalidSDFGException("GEMMNode input C must have 1 dimensions");
×
133
    }
134
    data_flow::Subset c_dims;
×
135
    for (size_t i = 0; i < c_subset_begin.size(); i++) {
×
136
        c_dims.push_back(symbolic::sub(c_subset_end[i], c_subset_begin[i]));
×
137
    }
×
138

139
    // TODO: Check if dimensions of A, B, C are valid
140
}
×
141

142
types::PrimitiveType GEMMNode::scalar_primitive() const {
1✔
143
    switch (this->precision_) {
1✔
144
        case BLAS_Precision::s:
145
            return types::PrimitiveType::Float;
1✔
146
        case BLAS_Precision::d:
147
            return types::PrimitiveType::Double;
×
148
        case BLAS_Precision::h:
149
            return types::PrimitiveType::Half;
×
150
        default:
151
            return types::PrimitiveType::Void;
×
152
    }
153
}
1✔
154

155
bool GEMMNode::expand(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
1✔
156
    auto& sdfg = builder.subject();
1✔
157
    auto& dataflow = this->get_parent();
1✔
158
    auto& block = static_cast<structured_control_flow::Block&>(*dataflow.get_parent());
1✔
159

160
    auto& scope_analyisis = analysis_manager.get<analysis::ScopeAnalysis>();
1✔
161
    auto& parent = static_cast<structured_control_flow::Sequence&>(*scope_analyisis.parent_scope(&block));
1✔
162

163
    if (trans_a_ != BLAS_Transpose::No || trans_b_ != BLAS_Transpose::No) {
1✔
164
        return false;
×
165
    }
166

167
    auto& alpha = this->alpha();
1✔
168
    auto& beta = this->beta();
1✔
169

170
    auto primitive_type = scalar_primitive();
1✔
171
    if (primitive_type == types::PrimitiveType::Void) {
1✔
172
        return false;
×
173
    }
174

175
    types::Scalar scalar_type(primitive_type);
1✔
176

177
    auto in_edges = dataflow.in_edges(*this);
1✔
178
    auto in_edges_it = in_edges.begin();
1✔
179

180
    data_flow::Memlet* iedge_a = nullptr;
1✔
181
    data_flow::Memlet* iedge_b = nullptr;
1✔
182
    data_flow::Memlet* iedge_c = nullptr;
1✔
183
    data_flow::Memlet* alpha_edge = nullptr;
1✔
184
    data_flow::Memlet* beta_edge = nullptr;
1✔
185
    while (in_edges_it != in_edges.end()) {
4✔
186
        auto& edge = *in_edges_it;
3✔
187
        auto dst_conn = edge.dst_conn();
3✔
188
        if (dst_conn == "A") {
3✔
189
            iedge_a = &edge;
1✔
190
        } else if (dst_conn == "B") {
3✔
191
            iedge_b = &edge;
1✔
192
        } else if (dst_conn == "C") {
2✔
193
            iedge_c = &edge;
1✔
194
        } else if (dst_conn == alpha) {
1✔
195
            alpha_edge = &edge;
×
196
        } else if (dst_conn == beta) {
×
197
            beta_edge = &edge;
×
198
        } else {
×
199
            throw InvalidSDFGException("GEMMNode has unexpected input: " + dst_conn);
×
200
        }
201
        ++in_edges_it;
3✔
202
    }
3✔
203

204
    auto& oedge = *dataflow.out_edges(*this).begin();
1✔
205

206
    // Checks if legal
207
    auto* input_node_a = dynamic_cast<data_flow::AccessNode*>(&iedge_a->src());
1✔
208
    auto* input_node_b = dynamic_cast<data_flow::AccessNode*>(&iedge_b->src());
1✔
209
    auto* input_node_c = dynamic_cast<data_flow::AccessNode*>(&iedge_c->src());
1✔
210
    auto* output_node = dynamic_cast<data_flow::AccessNode*>(&oedge.dst());
1✔
211
    data_flow::AccessNode* alpha_node = nullptr;
1✔
212
    data_flow::AccessNode* beta_node = nullptr;
1✔
213

214
    if (alpha_edge) {
1✔
215
        alpha_node = dynamic_cast<data_flow::AccessNode*>(&alpha_edge->src());
×
216
    }
×
217
    if (beta_edge) {
1✔
218
        beta_node = dynamic_cast<data_flow::AccessNode*>(&beta_edge->src());
×
219
    }
×
220

221
    // we must be the only thing in this block, as we do not support splitting a block into pre, expanded lib-node, post
222
    if (!input_node_a || dataflow.in_degree(*input_node_a) != 0 || !input_node_b ||
2✔
223
        dataflow.in_degree(*input_node_b) != 0 || !input_node_c || dataflow.in_degree(*input_node_c) != 0 ||
1✔
224
        !output_node || dataflow.out_degree(*output_node) != 0) {
1✔
225
        return false; // data nodes are not standalone
×
226
    }
227
    if ((alpha_node && dataflow.in_degree(*alpha_node) != 0) || (beta_node && dataflow.in_degree(*beta_node) != 0)) {
1✔
228
        return false; // alpha and beta are not standalone
×
229
    }
230
    for (auto* nd : dataflow.data_nodes()) {
5✔
231
        if (nd != input_node_a && nd != input_node_b && nd != input_node_c && nd != output_node &&
4✔
232
            (!alpha_node || nd != alpha_node) && (!beta_node || nd != beta_node)) {
×
233
            return false; // there are other nodes in here that we could not preserve correctly
×
234
        }
235
    }
236

237
    auto& A_var = input_node_a->data();
1✔
238
    auto& B_var = input_node_b->data();
1✔
239
    auto& C_in_var = input_node_c->data();
1✔
240
    auto& C_out_var = output_node->data();
1✔
241

242

243
    // Add new graph after the current block
244
    auto& new_sequence = builder.add_sequence_before(parent, block, block.debug_info()).first;
1✔
245

246
    // Add maps
247
    std::vector<symbolic::Expression> indvar_ends{this->m(), this->n(), this->k()};
1✔
248
    auto& begin_subsets_out = oedge.begin_subset();
1✔
249
    auto& end_subsets_out = oedge.end_subset();
1✔
250
    auto& begin_subsets_in_a = iedge_a->begin_subset();
1✔
251
    auto& end_subsets_in_a = iedge_a->end_subset();
1✔
252
    data_flow::Subset new_subset;
1✔
253
    structured_control_flow::Sequence* last_scope = &new_sequence;
1✔
254
    structured_control_flow::Map* last_map = nullptr;
1✔
255
    structured_control_flow::Map* output_loop = nullptr;
1✔
256
    std::vector<std::string> indvar_names{"_i", "_j", "_k"};
1✔
257

258
    std::string sum_var = builder.find_new_name("_sum");
1✔
259
    builder.add_container(sum_var, scalar_type);
1✔
260

261
    for (size_t i = 0; i < 3; i++) {
4✔
262
        auto dim_begin = symbolic::zero();
3✔
263
        auto& dim_end = indvar_ends[i];
3✔
264

265
        std::string indvar_str = builder.find_new_name(indvar_names[i]);
3✔
266
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
3✔
267

268
        auto indvar = symbolic::symbol(indvar_str);
3✔
269
        auto init = dim_begin;
3✔
270
        auto update = symbolic::add(indvar, symbolic::one());
3✔
271
        auto condition = symbolic::Lt(indvar, dim_end);
3✔
272
        last_map = &builder.add_map(
6✔
273
            *last_scope,
3✔
274
            indvar,
275
            condition,
276
            init,
3✔
277
            update,
278
            structured_control_flow::ScheduleType_Sequential,
279
            {},
3✔
280
            block.debug_info()
3✔
281
        );
282
        last_scope = &last_map->root();
3✔
283

284
        if (i == 1) {
3✔
285
            output_loop = last_map;
1✔
286
        }
1✔
287

288
        new_subset.push_back(indvar);
3✔
289
    }
3✔
290

291

292
    // Add code
293
    auto& init_block = builder.add_block_before(output_loop->root(), *last_map, block.debug_info()).first;
1✔
294
    auto& sum_init = builder.add_access(init_block, sum_var, block.debug_info());
1✔
295

296
    auto& init_tasklet = builder.add_tasklet(init_block, data_flow::assign, "_out", {"0.0"}, block.debug_info());
1✔
297

298
    builder.add_computational_memlet(init_block, init_tasklet, "_out", sum_init, {}, block.debug_info());
1✔
299

300
    auto& code_block = builder.add_block(*last_scope, {}, block.debug_info());
1✔
301
    auto& input_node_a_new = builder.add_access(code_block, A_var, input_node_a->debug_info());
1✔
302
    auto& input_node_b_new = builder.add_access(code_block, B_var, input_node_b->debug_info());
1✔
303

304
    auto& core_fma =
1✔
305
        builder.add_tasklet(code_block, data_flow::fma, "_out", {"_in1", "_in2", "_in3"}, block.debug_info());
1✔
306
    auto& sum_in = builder.add_access(code_block, sum_var, block.debug_info());
1✔
307
    auto& sum_out = builder.add_access(code_block, sum_var, block.debug_info());
1✔
308
    builder.add_computational_memlet(code_block, sum_in, core_fma, "_in3", {}, block.debug_info());
1✔
309

310
    symbolic::Expression a_idx = symbolic::add(symbolic::mul(lda(), new_subset[0]), new_subset[2]);
1✔
311
    builder.add_computational_memlet(
2✔
312
        code_block, input_node_a_new, core_fma, "_in1", {a_idx}, iedge_a->base_type(), iedge_a->debug_info()
1✔
313
    );
314
    symbolic::Expression b_idx = symbolic::add(symbolic::mul(ldb(), new_subset[2]), new_subset[1]);
1✔
315
    builder.add_computational_memlet(
2✔
316
        code_block, input_node_b_new, core_fma, "_in2", {b_idx}, iedge_b->base_type(), iedge_b->debug_info()
1✔
317
    );
318
    builder.add_computational_memlet(code_block, core_fma, "_out", sum_out, {}, oedge.debug_info());
1✔
319

320
    auto& flush_block = builder.add_block_after(output_loop->root(), *last_map, block.debug_info()).first;
1✔
321
    auto& sum_final = builder.add_access(flush_block, sum_var, block.debug_info());
1✔
322
    auto& input_node_c_new = builder.add_access(flush_block, C_in_var, input_node_c->debug_info());
1✔
323
    symbolic::Expression c_idx = symbolic::add(symbolic::mul(ldc(), new_subset[0]), new_subset[1]);
1✔
324

325
    auto& scale_sum_tasklet =
1✔
326
        builder.add_tasklet(flush_block, data_flow::mul, "_out", {"_in1", alpha}, block.debug_info());
1✔
327
    builder.add_computational_memlet(flush_block, sum_final, scale_sum_tasklet, "_in1", {}, block.debug_info());
1✔
328
    if (alpha_node) {
1✔
329
        auto& alpha_node_new = builder.add_access(flush_block, alpha_node->data(), block.debug_info());
×
330
        builder.add_computational_memlet(flush_block, scale_sum_tasklet, alpha, alpha_node_new, {}, block.debug_info());
×
331
    }
×
332

333
    std::string scaled_sum_temp = builder.find_new_name("scaled_sum_temp");
1✔
334
    builder.add_container(scaled_sum_temp, scalar_type);
1✔
335
    auto& scaled_sum_final = builder.add_access(flush_block, scaled_sum_temp, block.debug_info());
1✔
336
    builder.add_computational_memlet(
2✔
337
        flush_block, scale_sum_tasklet, "_out", scaled_sum_final, {}, scalar_type, block.debug_info()
1✔
338
    );
339

340
    auto& scale_input_tasklet =
1✔
341
        builder.add_tasklet(flush_block, data_flow::mul, "_out", {"_in1", beta}, block.debug_info());
1✔
342
    builder.add_computational_memlet(
2✔
343
        flush_block, input_node_c_new, scale_input_tasklet, "_in1", {c_idx}, iedge_c->base_type(), iedge_c->debug_info()
1✔
344
    );
345
    if (beta_node) {
1✔
346
        auto& beta_node_new = builder.add_access(flush_block, beta_node->data(), block.debug_info());
×
NEW
347
        builder.add_computational_memlet(
×
NEW
348
            flush_block, scale_sum_tasklet, beta, beta_node_new, {}, scalar_type, block.debug_info()
×
349
        );
350
    }
×
351

352
    std::string scaled_input_temp = builder.find_new_name("scaled_input_temp");
1✔
353
    builder.add_container(scaled_input_temp, scalar_type);
1✔
354
    auto& scaled_input_c = builder.add_access(flush_block, scaled_input_temp, block.debug_info());
1✔
355
    builder.add_computational_memlet(
2✔
356
        flush_block, scale_input_tasklet, "_out", scaled_input_c, {}, scalar_type, block.debug_info()
1✔
357
    );
358

359
    auto& flush_add_tasklet =
1✔
360
        builder.add_tasklet(flush_block, data_flow::add, "_out", {"_in1", "_in2"}, block.debug_info());
1✔
361
    auto& output_node_new = builder.add_access(flush_block, C_out_var, output_node->debug_info());
1✔
362
    builder.add_computational_memlet(
2✔
363
        flush_block, scaled_sum_final, flush_add_tasklet, "_in1", {}, scalar_type, block.debug_info()
1✔
364
    );
365
    builder.add_computational_memlet(
2✔
366
        flush_block, scaled_input_c, flush_add_tasklet, "_in2", {}, scalar_type, block.debug_info()
1✔
367
    );
368
    builder.add_computational_memlet(
2✔
369
        flush_block, flush_add_tasklet, "_out", output_node_new, {c_idx}, iedge_c->base_type(), iedge_c->debug_info()
1✔
370
    );
371

372

373
    // Clean up block
374
    builder.remove_memlet(block, *iedge_a);
1✔
375
    builder.remove_memlet(block, *iedge_b);
1✔
376
    builder.remove_memlet(block, *iedge_c);
1✔
377
    if (alpha_edge) {
1✔
378
        builder.remove_memlet(block, *alpha_edge);
×
379
        builder.remove_node(block, *alpha_node);
×
380
    }
×
381
    if (beta_edge) {
1✔
382
        builder.remove_memlet(block, *beta_edge);
×
383
        builder.remove_node(block, *beta_node);
×
384
    }
×
385
    builder.remove_memlet(block, oedge);
1✔
386
    builder.remove_node(block, *input_node_a);
1✔
387
    builder.remove_node(block, *input_node_b);
1✔
388
    builder.remove_node(block, *input_node_c);
1✔
389
    builder.remove_node(block, *output_node);
1✔
390
    builder.remove_node(block, *this);
1✔
391
    builder.remove_child(parent, block);
1✔
392

393
    return true;
1✔
394
}
1✔
395

396
std::unique_ptr<data_flow::DataFlowNode> GEMMNode::
397
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph& parent) const {
×
398
    auto node_clone = std::unique_ptr<GEMMNode>(new GEMMNode(
×
399
        element_id,
×
400
        this->debug_info(),
×
401
        vertex,
×
402
        parent,
×
403
        this->implementation_type_,
×
404
        this->precision_,
×
405
        this->layout_,
×
406
        this->trans_a_,
×
407
        this->trans_b_,
×
408
        this->m_,
×
409
        this->n_,
×
410
        this->k_,
×
411
        this->lda_,
×
412
        this->ldb_,
×
413
        this->ldc_,
×
414
        this->alpha(),
×
415
        this->beta()
×
416
    ));
417
    return std::move(node_clone);
×
418
}
×
419

420
std::string GEMMNode::toStr() const {
1✔
421
    return LibraryNode::toStr() + "(" + static_cast<char>(precision_) + ", " +
3✔
422
           std::string(BLAS_Layout_to_short_string(layout_)) + ", " + BLAS_Transpose_to_char(trans_a_) +
3✔
423
           BLAS_Transpose_to_char(trans_b_) + ", " + m_->__str__() + ", " + n_->__str__() + ", " + k_->__str__() +
2✔
424
           ", " + lda_->__str__() + ", " + ldb_->__str__() + ", " + ldc_->__str__() + ")";
1✔
425
}
×
426

427
nlohmann::json GEMMNodeSerializer::serialize(const data_flow::LibraryNode& library_node) {
×
428
    const GEMMNode& gemm_node = static_cast<const GEMMNode&>(library_node);
×
429
    nlohmann::json j;
×
430

431
    serializer::JSONSerializer serializer;
×
432
    j["code"] = gemm_node.code().value();
×
433
    j["precision"] = gemm_node.precision();
×
434
    j["layout"] = gemm_node.layout();
×
435
    j["trans_a"] = gemm_node.trans_a();
×
436
    j["trans_b"] = gemm_node.trans_b();
×
437
    j["m"] = serializer.expression(gemm_node.m());
×
438
    j["n"] = serializer.expression(gemm_node.n());
×
439
    j["k"] = serializer.expression(gemm_node.k());
×
440
    j["lda"] = serializer.expression(gemm_node.lda());
×
441
    j["ldb"] = serializer.expression(gemm_node.ldb());
×
442
    j["ldc"] = serializer.expression(gemm_node.ldc());
×
443
    j["alpha"] = gemm_node.alpha();
×
444
    j["beta"] = gemm_node.beta();
×
445

446
    return j;
×
447
}
×
448

449
data_flow::LibraryNode& GEMMNodeSerializer::deserialize(
×
450
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
451
) {
452
    // Assertions for required fields
453
    assert(j.contains("element_id"));
×
454
    assert(j.contains("code"));
×
455
    assert(j.contains("debug_info"));
×
456

457
    auto code = j["code"].get<std::string>();
×
458
    if (code != LibraryNodeType_GEMM.value()) {
×
459
        throw std::runtime_error("Invalid library node code");
×
460
    }
461

462
    // Extract debug info using JSONSerializer
463
    sdfg::serializer::JSONSerializer serializer;
×
464
    DebugInfo debug_info = serializer.json_to_debug_info(j["debug_info"]);
×
465

466
    auto precision = j.at("precision").get<BLAS_Precision>();
×
467
    auto layout = j.at("layout").get<BLAS_Layout>();
×
468
    auto trans_a = j.at("trans_a").get<BLAS_Transpose>();
×
469
    auto trans_b = j.at("trans_b").get<BLAS_Transpose>();
×
470
    auto m = SymEngine::Expression(j.at("m"));
×
471
    auto n = SymEngine::Expression(j.at("n"));
×
472
    auto k = SymEngine::Expression(j.at("k"));
×
473
    auto lda = SymEngine::Expression(j.at("lda"));
×
474
    auto ldb = SymEngine::Expression(j.at("ldb"));
×
475
    auto ldc = SymEngine::Expression(j.at("ldc"));
×
476
    auto alpha = j.at("alpha").get<std::string>();
×
477
    auto beta = j.at("beta").get<std::string>();
×
478

479
    auto implementation_type = j.at("implementation_type").get<std::string>();
×
480

481
    return builder.add_library_node<GEMMNode>(
×
482
        parent, debug_info, implementation_type, precision, layout, trans_a, trans_b, m, n, k, lda, ldb, ldc, alpha, beta
×
483
    );
484
}
×
485

486
GEMMNodeDispatcher_BLAS::GEMMNodeDispatcher_BLAS(
×
487
    codegen::LanguageExtension& language_extension,
488
    const Function& function,
489
    const data_flow::DataFlowGraph& data_flow_graph,
490
    const GEMMNode& node
491
)
492
    : codegen::LibraryNodeDispatcher(language_extension, function, data_flow_graph, node) {}
×
493

494
void GEMMNodeDispatcher_BLAS::dispatch(
×
495
    codegen::PrettyPrinter& stream,
496
    codegen::PrettyPrinter& globals_stream,
497
    codegen::CodeSnippetFactory& library_snippet_factory
498
) {
499
    stream << "{" << std::endl;
×
500
    stream.setIndent(stream.indent() + 4);
×
501

502
    auto& gemm_node = static_cast<const GEMMNode&>(this->node_);
×
503

504
    sdfg::types::Scalar base_type(types::PrimitiveType::Void);
×
505
    switch (gemm_node.precision()) {
×
506
        case BLAS_Precision::h:
507
            base_type = types::Scalar(types::PrimitiveType::Half);
×
508
            break;
×
509
        case BLAS_Precision::s:
510
            base_type = types::Scalar(types::PrimitiveType::Float);
×
511
            break;
×
512
        case BLAS_Precision::d:
513
            base_type = types::Scalar(types::PrimitiveType::Double);
×
514
            break;
×
515
        default:
516
            throw std::runtime_error("Invalid BLAS_Precision value");
×
517
    }
518

519
    auto& graph = this->node_.get_parent();
×
520
    for (auto& iedge : graph.in_edges(this->node_)) {
×
521
        auto& access_node = static_cast<const data_flow::AccessNode&>(iedge.src());
×
522
        std::string name = access_node.data();
×
523
        auto& type = this->function_.type(name);
×
524

525
        stream << this->language_extension_.declaration(iedge.dst_conn(), type);
×
526
        stream << " = " << name << ";" << std::endl;
×
527
    }
×
528

529
    if (std::find(gemm_node.inputs().begin(), gemm_node.inputs().end(), "alpha") ==
×
530
        gemm_node.inputs().end()) { // TODO obsolute, must be an input!
×
531
        stream << this->language_extension_.declaration("alpha", base_type);
×
532
        stream << " = " << gemm_node.alpha() << ";" << std::endl;
×
533
    }
×
534
    if (std::find(gemm_node.inputs().begin(), gemm_node.inputs().end(), "beta") == gemm_node.inputs().end()) {
×
535
        stream << this->language_extension_.declaration("beta", base_type);
×
536
        stream << " = " << gemm_node.beta() << ";" << std::endl;
×
537
    }
×
538

539
    stream << "cblas_" << BLAS_Precision_to_string(gemm_node.precision()) << "gemm(";
×
540
    stream.setIndent(stream.indent() + 4);
×
541
    stream << BLAS_Layout_to_string(gemm_node.layout());
×
542
    stream << ", ";
×
543
    stream << BLAS_Transpose_to_string(gemm_node.trans_a());
×
544
    stream << ", ";
×
545
    stream << BLAS_Transpose_to_string(gemm_node.trans_b());
×
546
    stream << ", ";
×
547
    stream << this->language_extension_.expression(gemm_node.m());
×
548
    stream << ", ";
×
549
    stream << this->language_extension_.expression(gemm_node.n());
×
550
    stream << ", ";
×
551
    stream << this->language_extension_.expression(gemm_node.k());
×
552
    stream << ", ";
×
553
    stream << "alpha";
×
554
    stream << ", ";
×
555
    stream << "A";
×
556
    stream << ", ";
×
557
    stream << this->language_extension_.expression(gemm_node.lda());
×
558
    stream << ", ";
×
559
    stream << "B";
×
560
    stream << ", ";
×
561
    stream << this->language_extension_.expression(gemm_node.ldb());
×
562
    stream << ", ";
×
563
    stream << "beta";
×
564
    stream << ", ";
×
565
    stream << "C";
×
566
    stream << ", ";
×
567
    stream << this->language_extension_.expression(gemm_node.ldc());
×
568

569
    stream.setIndent(stream.indent() - 4);
×
570
    stream << ");" << std::endl;
×
571

572
    stream.setIndent(stream.indent() - 4);
×
573
    stream << "}" << std::endl;
×
574
}
×
575

576
GEMMNodeDispatcher_CUBLAS::GEMMNodeDispatcher_CUBLAS(
×
577
    codegen::LanguageExtension& language_extension,
578
    const Function& function,
579
    const data_flow::DataFlowGraph& data_flow_graph,
580
    const GEMMNode& node
581
)
582
    : codegen::LibraryNodeDispatcher(language_extension, function, data_flow_graph, node) {}
×
583

584
void GEMMNodeDispatcher_CUBLAS::dispatch(
×
585
    codegen::PrettyPrinter& stream,
586
    codegen::PrettyPrinter& globals_stream,
587
    codegen::CodeSnippetFactory& library_snippet_factory
588
) {
589
    throw std::runtime_error("GEMMNodeDispatcher_CUBLAS not implemented");
×
590
}
×
591

592
} // namespace blas
593
} // namespace math
594
} // 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