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

daisytuner / sdfglib / 15512392058

07 Jun 2025 10:47PM UTC coverage: 62.261% (+4.8%) from 57.416%
15512392058

push

github

web-flow
Merge pull request #63 from daisytuner/schedules

Schedules

152 of 194 new or added lines in 24 files covered. (78.35%)

39 existing lines in 7 files now uncovered.

7467 of 11993 relevant lines covered (62.26%)

127.08 hits per line

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

59.74
/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_factory.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
    : CodeGenerator(sdfg, InstrumentationStrategy::NONE) {
5✔
12
    if (sdfg.type() != FunctionType_NV_GLOBAL) {
5✔
UNCOV
13
        throw std::runtime_error("CUDACodeGenerator can only be used for GPU SDFGs");
×
14
    }
15
};
5✔
16

NEW
17
CUDACodeGenerator::CUDACodeGenerator(StructuredSDFG& sdfg,
×
18
                                     InstrumentationStrategy instrumentation_strategy)
NEW
19
    : CodeGenerator(sdfg, instrumentation_strategy) {
×
NEW
20
    if (sdfg.type() != FunctionType_NV_GLOBAL) {
×
UNCOV
21
        throw std::runtime_error("CUDACodeGenerator can only be used for GPU SDFGs");
×
22
    }
23
};
×
24

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

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

42
    return "extern \"C\" __global__ void " + sdfg_.name() + "(" + arglist.str() + ")";
1✔
43
};
1✔
44

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

53
    std::ofstream ofs_source(source_path, std::ofstream::out);
×
54
    if (!ofs_source.is_open()) {
×
55
        return false;
×
56
    }
57

58
    std::ofstream ofs_library(library_path, std::ofstream::out);
×
59
    if (!ofs_library.is_open()) {
×
60
        return false;
×
61
    }
62

63
    ofs_header << "#pragma once" << std::endl;
×
64
    ofs_header << this->includes_stream_.str() << std::endl;
×
65
    ofs_header << this->classes_stream_.str() << std::endl;
×
66
    ofs_header.close();
×
67

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

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

82
    ofs_library << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
83
    ofs_library << library_content << std::endl;
×
84
    ofs_library.close();
×
85

86
    return true;
×
87
};
×
88

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

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

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

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

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

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

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

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

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

160
        this->classes_stream_ << "};" << std::endl;
3✔
161
    }
3✔
162
};
4✔
163

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

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

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

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

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

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

218
    // Add instrumentation
219
    auto instrumentation = create_instrumentation(instrumentation_strategy_, sdfg_);
4✔
220

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

225
}  // namespace codegen
226
}  // 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