docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
gpu_offload_schedule_type.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "sdfg/exceptions.h"
9
10namespace sdfg {
11namespace gpu {
12
22
23inline std::string to_string(const TargetLevel& target_level) {
24 switch (target_level) {
26 return "X_GRID";
28 return "Y_GRID";
30 return "Z_GRID";
32 return "X_BLOCK";
34 return "Y_BLOCK";
36 return "Z_BLOCK";
38 return "WARP";
39 }
40 throw InvalidSDFGException("Invalid TargetLevel");
41}
42
43inline TargetLevel target_level_from_string(const std::string& value) {
44 if (value == "X_GRID") {
46 } else if (value == "Y_GRID") {
48 } else if (value == "Z_GRID") {
50 } else if (value == "X_BLOCK") {
52 } else if (value == "Y_BLOCK") {
54 } else if (value == "Z_BLOCK") {
56 } else if (value == "WARP") {
57 return TargetLevel::WARP;
58 }
59 throw InvalidSDFGException("Invalid TargetLevel: " + value);
60}
61
72
73inline std::string reduce_strategy_to_string(ReduceStrategy strategy) {
74 switch (strategy) {
76 return "register";
78 return "shared";
80 return "global";
81 }
82 throw InvalidSDFGException("Invalid ReduceStrategy");
83}
84
85inline ReduceStrategy reduce_strategy_from_string(const std::string& value) {
86 if (value == "register") {
88 } else if (value == "shared") {
90 } else if (value == "global") {
92 }
93 throw InvalidSDFGException("Invalid ReduceStrategy: " + value);
94}
95
96// The mechanism a target level uses by default: warp -> register+shuffle,
97// block -> shared tree, grid -> global atomics. Reproduces the historical
98// (pre-partial_storage) codegen when the property is unset.
100 switch (target_level) {
107 default:
109 }
110}
111
122
124public:
129 schedule.set_property("target_level", gpu::to_string(target_level));
130 }
131
136 return gpu::target_level_from_string(schedule.properties().at("target_level"));
137 }
138
139 static std::optional<TargetLevel> target_level_if_exists(const structured_control_flow::ScheduleType& schedule) {
140 auto it = schedule.properties().find("target_level");
141 if (it == schedule.properties().end()) {
142 return std::nullopt;
143 }
144 return gpu::target_level_from_string(it->second);
145 }
146
154
159 if (schedule.properties().find("parallel_size") == schedule.properties().end()) {
160 throw InvalidSDFGException("Parallel size not set for schedule type: " + schedule.value());
161 }
162 std::string expr_str = schedule.properties().at("parallel_size");
163 return symbolic::integer(std::stoi(expr_str));
164 }
165
171 auto it = schedule.properties().find("lanes");
172 if (it == schedule.properties().end()) {
173 return symbolic::integer(1);
174 }
175 return symbolic::integer(std::stoi(it->second));
176 }
177
183 if (target_level(schedule) != TargetLevel::X_BLOCK) {
184 throw InvalidSDFGException("lanes can only be set on an X_BLOCK schedule");
185 }
186 if (lanes->as_int() < 1) {
187 throw InvalidSDFGException("lanes must be positive");
188 }
190 schedule.set_property("lanes", serializer.expression(lanes));
191 }
192
197 return symbolic::integer(parallel_size(schedule)->as_int() * lanes(schedule)->as_int());
198 }
199
204 if (schedule.properties().find("nested_sync") == schedule.properties().end()) {
205 return false;
206 }
207 std::string val = schedule.properties().at("nested_sync");
208 return val == "true";
209 }
210
215 schedule.set_property("nested_sync", nested_sync ? "true" : "false");
216 }
217
223 auto it = schedule.properties().find("partial_storage");
224 if (it == schedule.properties().end()) {
225 return default_reduce_strategy(target_level(schedule));
226 }
227 return reduce_strategy_from_string(it->second);
228 }
229
234 schedule.set_property("partial_storage", reduce_strategy_to_string(strategy));
235 }
236
245 static std::string partial_container(const structured_control_flow::ScheduleType& schedule) {
246 auto it = schedule.properties().find("partial_container");
247 return it == schedule.properties().end() ? std::string() : it->second;
248 }
249
253 static void partial_container(structured_control_flow::ScheduleType& schedule, const std::string& container) {
254 schedule.set_property("partial_container", container);
255 }
256
260 template<typename Derived>
262 create(const TargetLevel& target_level_, const symbolic::Integer& parallel_size_) {
263 auto schedule_type = structured_control_flow::
264 ScheduleType(Derived::value(), structured_control_flow::ScheduleTypeCategory::Offloader);
265 target_level(schedule_type, target_level_);
266 parallel_size(schedule_type, parallel_size_);
267 return schedule_type;
268 }
269
271 create(const GpuArch& arch, const TargetLevel& target_level_, const symbolic::Integer& parallel_size_) {
272 auto sched_type = arch.create_schedule_type();
273 target_level(sched_type, target_level_);
274 parallel_size(sched_type, parallel_size_);
275 return sched_type;
276 }
277};
278
283 return target_level_from_string(schedule.properties().at("target_level"));
284}
285
289inline void gpu_target_level(structured_control_flow::ScheduleType& schedule, const TargetLevel& target_level) {
290 schedule.set_property("target_level", to_string(target_level));
291}
292
293} // namespace gpu
294} // namespace sdfg
Definition exceptions.h:8
Definition gpu_arch.h:14
virtual structured_control_flow::ScheduleType create_schedule_type() const =0
Base class for GPU schedule types (CUDA/ROCm) using CRTP pattern.
Definition gpu_offload_schedule_type.h:123
static structured_control_flow::ScheduleType create(const GpuArch &arch, const TargetLevel &target_level_, const symbolic::Integer &parallel_size_)
Definition gpu_offload_schedule_type.h:271
static symbolic::Integer lanes(const structured_control_flow::ScheduleType &schedule)
Lanes that jointly execute each iteration (1 = one thread per iteration; the warp size = one wave per...
Definition gpu_offload_schedule_type.h:170
static void partial_container(structured_control_flow::ScheduleType &schedule, const std::string &container)
Set the container name for this reduction's shared partials.
Definition gpu_offload_schedule_type.h:253
static std::string partial_container(const structured_control_flow::ScheduleType &schedule)
Name of the SDFG container holding this reduction's shared partials.
Definition gpu_offload_schedule_type.h:245
static void nested_sync(structured_control_flow::ScheduleType &schedule, const bool nested_sync)
Set nested synchronization flag.
Definition gpu_offload_schedule_type.h:214
static std::optional< TargetLevel > target_level_if_exists(const structured_control_flow::ScheduleType &schedule)
Definition gpu_offload_schedule_type.h:139
static symbolic::Integer threads(const structured_control_flow::ScheduleType &schedule)
Hardware threads the schedule occupies along its dimension: parallel_size * lanes.
Definition gpu_offload_schedule_type.h:196
static ReduceStrategy partial_storage(const structured_control_flow::ScheduleType &schedule)
Where this (reduction) schedule's partials live / how they combine.
Definition gpu_offload_schedule_type.h:222
static structured_control_flow::ScheduleType create(const TargetLevel &target_level_, const symbolic::Integer &parallel_size_)
Create a new GPU schedule type.
Definition gpu_offload_schedule_type.h:262
static symbolic::Integer parallel_size(const structured_control_flow::ScheduleType &schedule)
Get the parallel size from a schedule.
Definition gpu_offload_schedule_type.h:158
static void target_level(structured_control_flow::ScheduleType &schedule, const TargetLevel &target_level)
Set the target level for a schedule.
Definition gpu_offload_schedule_type.h:128
static TargetLevel target_level(const structured_control_flow::ScheduleType &schedule)
Get the target level from a schedule.
Definition gpu_offload_schedule_type.h:135
static void lanes(structured_control_flow::ScheduleType &schedule, const symbolic::Integer lanes)
Set the lanes per iteration.
Definition gpu_offload_schedule_type.h:182
static void parallel_size(structured_control_flow::ScheduleType &schedule, const symbolic::Integer parallel_size)
Set the parallel size for a schedule.
Definition gpu_offload_schedule_type.h:150
static bool nested_sync(const structured_control_flow::ScheduleType &schedule)
Check if nested synchronization is enabled.
Definition gpu_offload_schedule_type.h:203
static void partial_storage(structured_control_flow::ScheduleType &schedule, const ReduceStrategy strategy)
Set the reduction partial-storage strategy.
Definition gpu_offload_schedule_type.h:233
Definition json_serializer.h:22
Represents a schedule type for Map nodes.
Definition structured_loop.h:31
const std::unordered_map< std::string, std::string > & properties() const
Get all schedule properties.
Definition structured_loop.h:53
void set_property(const std::string &key, const std::string &value)
Set a schedule property.
Definition structured_loop.h:70
const std::string & value() const
Get the schedule type identifier.
Definition structured_loop.h:45
Integer integer(int64_t value)
Creates an integer constant.
SymEngine::RCP< const SymEngine::Integer > Integer
Reference-counted pointer to a SymEngine Integer constant.
Definition symbolic.h:77
Definition cuda_arch.h:11
TargetLevel
Definition gpu_offload_schedule_type.h:13
@ WARP
Definition gpu_offload_schedule_type.h:20
@ Z_BLOCK
Definition gpu_offload_schedule_type.h:19
@ Y_BLOCK
Definition gpu_offload_schedule_type.h:18
@ Y_GRID
Definition gpu_offload_schedule_type.h:15
@ Z_GRID
Definition gpu_offload_schedule_type.h:16
@ X_GRID
Definition gpu_offload_schedule_type.h:14
@ X_BLOCK
Definition gpu_offload_schedule_type.h:17
ReduceStrategy
Where a reduction's per-thread partials live, and hence how they combine.
Definition gpu_offload_schedule_type.h:71
@ Register
Definition gpu_offload_schedule_type.h:71
@ Global
Definition gpu_offload_schedule_type.h:71
@ Shared
Definition gpu_offload_schedule_type.h:71
ReduceStrategy default_reduce_strategy(TargetLevel target_level)
Definition gpu_offload_schedule_type.h:99
TargetLevel gpu_target_level(const structured_control_flow::ScheduleType &schedule)
Get the GPU target level from any GPU schedule type.
Definition gpu_offload_schedule_type.h:282
TargetLevel target_level_from_string(const std::string &value)
Definition gpu_offload_schedule_type.h:43
std::string to_string(const TargetLevel &target_level)
Definition gpu_offload_schedule_type.h:23
std::string reduce_strategy_to_string(ReduceStrategy strategy)
Definition gpu_offload_schedule_type.h:73
ReduceStrategy reduce_strategy_from_string(const std::string &value)
Definition gpu_offload_schedule_type.h:85
Definition cutout_serializer.h:13
@ Offloader
Definition structured_loop.h:22
Definition analysis.h:7