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

daisytuner / sdfglib / 15852980623

24 Jun 2025 02:16PM UTC coverage: 64.412% (+0.3%) from 64.145%
15852980623

push

github

web-flow
Merge pull request #72 from daisytuner/capture-instrumentation

Capture instrumentation

363 of 446 new or added lines in 19 files covered. (81.39%)

100 existing lines in 5 files now uncovered.

8389 of 13024 relevant lines covered (64.41%)

116.79 hits per line

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

61.18
/src/codegen/code_generators/cuda_code_generator.cpp
1
#include "sdfg/codegen/code_generators/cuda_code_generator.h"
2

3
#include "sdfg/codegen/dispatchers/node_dispatcher_registry.h"
4
#include "sdfg/codegen/instrumentation/instrumentation.h"
5
#include "sdfg/codegen/instrumentation/outermost_loops_instrumentation.h"
6

7
namespace sdfg {
8
namespace codegen {
9

10
CUDACodeGenerator::CUDACodeGenerator(StructuredSDFG& sdfg,
10✔
11
                                     InstrumentationStrategy instrumentation_strategy,
12
                                     bool capture_args_results)
13
    : CodeGenerator(sdfg, instrumentation_strategy, capture_args_results) {
5✔
14
    if (sdfg.type() != FunctionType_NV_GLOBAL) {
5✔
15
        throw std::runtime_error("CUDACodeGenerator can only be used for GPU SDFGs");
×
16
    }
17
    if (capture_args_results) {
5✔
NEW
18
        std::cerr << "CUDACodeGenerator does not support capturing args/results!";
×
NEW
19
    }
×
20
};
5✔
21

22
bool CUDACodeGenerator::generate() {
4✔
23
    this->dispatch_includes();
4✔
24
    this->dispatch_structures();
4✔
25
    this->dispatch_globals();
4✔
26
    this->dispatch_schedule();
4✔
27
    return true;
4✔
28
};
29

30
std::string CUDACodeGenerator::function_definition() {
1✔
31
    /********** Arglist **********/
32
    std::vector<std::string> args;
1✔
33
    for (auto& container : sdfg_.arguments()) {
1✔
34
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
35
    }
36
    std::stringstream arglist;
1✔
37
    arglist << sdfg::helpers::join(args, ", ");
1✔
38

39
    return "extern \"C\" __global__ void " + sdfg_.name() + "(" + arglist.str() + ")";
1✔
40
};
1✔
41

42
bool CUDACodeGenerator::as_source(const std::filesystem::path& header_path,
×
43
                                  const std::filesystem::path& source_path,
44
                                  const std::filesystem::path& library_path) {
45
    std::ofstream ofs_header(header_path, std::ofstream::out);
×
46
    if (!ofs_header.is_open()) {
×
47
        return false;
×
48
    }
49

50
    std::ofstream ofs_source(source_path, std::ofstream::out);
×
51
    if (!ofs_source.is_open()) {
×
52
        return false;
×
53
    }
54

55
    std::ofstream ofs_library(library_path, std::ofstream::out);
×
56
    if (!ofs_library.is_open()) {
×
57
        return false;
×
58
    }
59

60
    ofs_header << "#pragma once" << std::endl;
×
61
    ofs_header << this->includes_stream_.str() << std::endl;
×
62
    ofs_header << this->classes_stream_.str() << std::endl;
×
63
    ofs_header.close();
×
64

65
    ofs_source << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
66
    ofs_source << this->globals_stream_.str() << std::endl;
×
67
    ofs_source << this->function_definition() << std::endl;
×
68
    ofs_source << "{" << std::endl;
×
69
    ofs_source << this->main_stream_.str() << std::endl;
×
70
    ofs_source << "}" << std::endl;
×
71
    ofs_source.close();
×
72

73
    auto library_content = this->library_stream_.str();
×
74
    if (library_content.empty()) {
×
75
        ofs_library.close();
×
76
        return true;
×
77
    }
78

79
    ofs_library << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
80
    ofs_library << library_content << std::endl;
×
81
    ofs_library.close();
×
82

83
    return true;
×
84
};
×
85

86
void CUDACodeGenerator::dispatch_includes() {
4✔
87
    this->includes_stream_ << "#define "
4✔
88
                           << "__DAISY_NVVM__" << std::endl;
4✔
89
    this->includes_stream_ << "#include "
4✔
90
                           << "\"daisyrtl.h\"" << std::endl;
4✔
91
    if (instrumentation_strategy_ != InstrumentationStrategy::NONE)
4✔
92
        this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
×
93

94
    this->includes_stream_ << "#define __daisy_min(a,b) ((a)<(b)?(a):(b))" << std::endl;
4✔
95
    this->includes_stream_ << "#define __daisy_max(a,b) ((a)>(b)?(a):(b))" << std::endl;
4✔
96
    this->includes_stream_ << "#define __daisy_fma(a,b,c) a * b + c" << std::endl;
4✔
97
};
4✔
98

99
void CUDACodeGenerator::dispatch_structures() {
4✔
100
    // Forward declarations
101
    for (auto& structure : sdfg_.structures()) {
7✔
102
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
3✔
103
    }
104

105
    // Generate topology-sorted structure definitions
106
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
107
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
108
    std::vector<std::string> names;
4✔
109
    for (auto& structure : sdfg_.structures()) {
7✔
110
        names.push_back(structure);
3✔
111
    }
112
    structures_graph graph(names.size());
4✔
113

114
    for (auto& structure : names) {
7✔
115
        auto& definition = sdfg_.structure(structure);
3✔
116
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
117
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
118
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
119
                auto array_type = static_cast<const types::Array*>(member_type);
×
120
                member_type = &array_type->element_type();
×
121
            }
122

123
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
3✔
124
                boost::add_edge(
1✔
125
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
126
                    std::find(names.begin(), names.end(), structure) - names.begin(), graph);
1✔
127
            }
1✔
128
        }
3✔
129
    }
130

131
    std::list<Vertex> order;
4✔
132
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
4✔
133
    boost::topological_sort(graph, std::back_inserter(order),
8✔
134
                            boost::color_map(boost::make_assoc_property_map(vertex_colors)));
4✔
135
    order.reverse();
4✔
136

137
    for (auto& structure_index : order) {
7✔
138
        std::string structure = names.at(structure_index);
3✔
139
        auto& definition = sdfg_.structure(structure);
3✔
140
        this->classes_stream_ << "struct ";
3✔
141
        if (definition.is_packed()) {
3✔
142
            this->classes_stream_ << "__attribute__((packed)) ";
×
143
        }
×
144
        this->classes_stream_ << structure << std::endl;
3✔
145
        this->classes_stream_ << "{\n";
3✔
146

147
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
148
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
149
            if (dynamic_cast<const sdfg::types::Structure*>(&member_type)) {
3✔
150
                this->classes_stream_ << "struct ";
1✔
151
            }
1✔
152
            this->classes_stream_ << language_extension_.declaration("member_" + std::to_string(i),
3✔
153
                                                                     member_type, false, true);
3✔
154
            this->classes_stream_ << ";" << std::endl;
3✔
155
        }
3✔
156

157
        this->classes_stream_ << "};" << std::endl;
3✔
158
    }
3✔
159
};
4✔
160

161
void CUDACodeGenerator::dispatch_globals() {
4✔
162
    for (auto& container : sdfg_.externals()) {
5✔
163
        auto& type = sdfg_.type(container);
1✔
164
        if (type.storage_type() == types::StorageType_NV_Global) {
1✔
165
            this->globals_stream_ << "extern " << language_extension_.declaration(container, type)
2✔
166
                                  << ";" << std::endl;
1✔
167
        }
1✔
168
        if (type.storage_type() == types::StorageType_NV_Constant) {
1✔
169
            assert(type.initializer().empty());
×
170
            this->globals_stream_ << "__constant__ "
×
171
                                  << language_extension_.declaration(container, type, true) << ";"
×
172
                                  << std::endl;
×
173
        }
×
174
    }
175
};
4✔
176

177
void CUDACodeGenerator::dispatch_schedule() {
4✔
178
    // Declare shared memory
179
    for (auto& container : sdfg_.externals()) {
5✔
180
        auto& type = sdfg_.type(container);
1✔
181
        if (type.storage_type() == types::StorageType_NV_Shared) {
1✔
182
            this->main_stream_ << language_extension_.declaration(container, sdfg_.type(container))
×
183
                               << ";" << std::endl;
×
184
        }
×
185
    }
186

187
    // Map external variables to internal variables
188
    for (auto& container : sdfg_.containers()) {
5✔
189
        if (!sdfg_.is_internal(container)) {
1✔
190
            continue;
1✔
191
        }
192

193
        std::string external_name =
194
            container.substr(0, container.length() - external_suffix.length());
×
195
        this->main_stream_ << language_extension_.declaration(container, sdfg_.type(container));
×
196
        this->main_stream_ << " = "
×
197
                           << "&" << external_name;
×
198
        this->main_stream_ << ";" << std::endl;
×
199
    }
×
200

201
    // Declare transient containers
202
    for (auto& container : sdfg_.containers()) {
5✔
203
        if (!sdfg_.is_transient(container)) {
1✔
204
            continue;
1✔
205
        }
206

207
        std::string val =
208
            this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
209
        if (!val.empty()) {
×
210
            this->main_stream_ << val;
×
211
            this->main_stream_ << ";" << std::endl;
×
212
        }
×
213
    }
×
214

215
    // Add instrumentation
216
    auto instrumentation = create_instrumentation(instrumentation_strategy_, sdfg_);
4✔
217

218
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), *instrumentation);
4✔
219
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
4✔
220
};
4✔
221

222
}  // namespace codegen
223
}  // 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