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

daisytuner / sdfglib / 15142284085

20 May 2025 03:58PM UTC coverage: 60.528% (-0.02%) from 60.543%
15142284085

push

github

web-flow
Merge pull request #23 from daisytuner/loops

LoopTrees and Instrumentation Strategies

80 of 182 new or added lines in 17 files covered. (43.96%)

11 existing lines in 6 files now uncovered.

7928 of 13098 relevant lines covered (60.53%)

104.16 hits per line

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

64.97
/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

5
#include "sdfg/codegen/instrumentation/instrumentation.h"
6
#include "sdfg/codegen/instrumentation/outermost_loops_instrumentation.h"
7

8
namespace sdfg {
9
namespace codegen {
10

11
CUDACodeGenerator::CUDACodeGenerator(ConditionalSchedule& schedule)
10✔
12
    : CodeGenerator(schedule, InstrumentationStrategy::NONE){
5✔
13

14
      };
5✔
15

NEW
16
CUDACodeGenerator::CUDACodeGenerator(ConditionalSchedule& schedule, InstrumentationStrategy instrumentation_strategy)
×
NEW
17
    : CodeGenerator(schedule, instrumentation_strategy){
×
18

UNCOV
19
      };
×
20

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

29
std::string CUDACodeGenerator::function_definition() {
1✔
30
    // Define SDFG as a function
31
    auto& function = this->schedule_.schedule(0).sdfg();
1✔
32

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

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

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

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

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

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

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

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

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

85
    return true;
×
86
};
×
87

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

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

100
void CUDACodeGenerator::dispatch_structures() {
4✔
101
    auto& function = this->schedule_.schedule(0).sdfg();
4✔
102

103
    // Forward declarations
104
    for (auto& structure : function.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 : function.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 = function.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 = function.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);
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
    auto& function = schedule_.schedule(0).sdfg();
4✔
166
    for (auto& container : function.externals()) {
5✔
167
        auto& type = function.type(container);
1✔
168
        if (type.address_space() != 3 && type.address_space() != 4) {
1✔
169
            this->globals_stream_ << "extern " << language_extension_.declaration(container, type)
2✔
170
                                  << ";" << std::endl;
1✔
171
        }
1✔
172
        if (type.address_space() == 4) {
1✔
173
            assert(type.initializer().empty());
×
174
            this->globals_stream_ << "__constant__ "
×
175
                                  << language_extension_.declaration(container, type, true) << ";"
×
176
                                  << std::endl;
×
177
        }
×
178
    }
179
};
4✔
180

181
void CUDACodeGenerator::dispatch_schedule() {
4✔
182
    auto& function = schedule_.schedule(0).sdfg();
4✔
183

184
    // Declare shared memory
185
    for (auto& container : function.externals()) {
5✔
186
        auto& type = function.type(container);
1✔
187
        if (type.address_space() == 3) {
1✔
188
            this->main_stream_ << language_extension_.declaration(container,
×
189
                                                                  function.type(container))
×
190
                               << ";" << std::endl;
×
191
        }
×
192
    }
193

194
    // Map external variables to internal variables
195
    for (auto& container : function.containers()) {
5✔
196
        if (!function.is_internal(container)) {
1✔
197
            continue;
1✔
198
        }
199

200
        std::string external_name =
201
            container.substr(0, container.length() - external_suffix.length());
×
202
        this->main_stream_ << language_extension_.declaration(container, function.type(container));
×
203
        this->main_stream_ << " = "
×
204
                           << "&" << external_name;
×
205
        this->main_stream_ << ";" << std::endl;
×
206
    }
×
207

208
    for (size_t i = 0; i < schedule_.size(); i++) {
8✔
209
        auto& schedule = schedule_.schedule(i);
4✔
210
        auto condition = schedule_.condition(i);
4✔
211
        
212
        // Add instrumentation
213
        auto instrumentation = create_instrumentation(instrumentation_strategy_, schedule);
4✔
214

215
        if (i > 0) {
4✔
NEW
216
            this->main_stream_ << "else ";
×
NEW
217
        }
×
218

219
        this->main_stream_ << "if (" << language_extension_.expression(condition)
8✔
220
                               << ") {\n";
4✔
221

222
        auto& function_i = schedule.builder().subject();
4✔
223
        auto dispatcher = create_dispatcher(language_extension_, schedule,
4✔
224
                                            function_i.root(), *instrumentation);
4✔
225
        dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
4✔
226

227
        this->main_stream_ << "}\n";
4✔
228
    }
4✔
229
};
4✔
230

231
}  // namespace codegen
232
}  // 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