docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
atomic_op_node.h
Go to the documentation of this file.
1#pragma once
2
5
8
9namespace sdfg {
10
11namespace data_flow {
13
14// The implementation_type selects the lowering; the semantics are identical:
15// atomically add `_src` into the location `_dst` points at. The concrete
16// implementations (AtomicScalarOpImpl) each own their implementation type name.
17
18enum class AtomicOpType { Add, Subtract, Min, Max };
19
20constexpr const char* atomic_op_type_to_string(AtomicOpType type) {
21 switch (type) {
23 return "Add";
25 return "Subtract";
27 return "Min";
29 return "Max";
30 default:
31 throw std::invalid_argument("Invalid AtomicOpType type");
32 }
33}
34
35constexpr AtomicOpType atomic_op_type_from_string(std::string_view str) {
36 if (str == "Add") {
37 return AtomicOpType::Add;
38 } else if (str == "Subtract") {
40 } else if (str == "Min") {
41 return AtomicOpType::Min;
42 } else if (str == "Max") {
43 return AtomicOpType::Max;
44 } else {
45 throw std::invalid_argument("Invalid AtomicOpType type: " + std::string(str));
46 }
47}
48
49
50class AtomicScalarOpNode;
51class AtomicScalarOpNodeDispatcher;
52
67public:
68 virtual ~AtomicScalarOpImpl() = default;
69
72
73 virtual std::string_view type_name() const = 0;
74
77 return StringEnum(std::string(this->type_name()));
78 }
79
83 virtual bool supports(types::PrimitiveType data_type, AtomicOpType op) const = 0;
84
85protected:
88};
89
96 types::PrimitiveType data_type_;
97 AtomicOpType atomic_op_;
98 const AtomicScalarOpImpl* impl_;
99
100public:
102 size_t element_id,
103 const DebugInfo& debug_info,
104 const graph::Vertex vertex,
105 DataFlowGraph& parent,
107 AtomicOpType op,
108 const AtomicScalarOpImpl* impl
109 );
110
112 return data_type_;
113 }
115 return atomic_op_;
116 }
117
119
120 void validate(const Function& function) const override;
121
122 symbolic::SymbolSet symbols() const override;
123
124 std::unique_ptr<DataFlowNode> clone(size_t element_id, const graph::Vertex vertex, DataFlowGraph& parent) const override;
125
126 void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override;
127
128 void replace(const symbolic::ExpressionMapping& replacements) override;
129
131
132 void verify_impl_exits() const;
133
134 static const AtomicScalarOpImpl* get_implementation(const std::string& type);
135};
136
138class AtomicScalarOpCPUImpl : public AtomicScalarOpImpl {
139public:
140 static constexpr const char* TYPE_NAME = "CPU";
141 static const AtomicScalarOpCPUImpl* instance();
142
143 std::string_view type_name() const override {
144 return TYPE_NAME;
145 }
146
147 bool supports(types::PrimitiveType data_type, AtomicOpType op) const override;
148
150 return {TYPE_NAME};
151 }
152
153private:
155 }
156};
157
164public:
165
166protected:
168};
169
171class AtomicScalarOpCudaImpl : public AtomicScalarOpGPUImpl {
172public:
173 static constexpr const char* TYPE_NAME = "CUDA";
174 static const AtomicScalarOpCudaImpl* instance();
175
176 std::string_view type_name() const override {
177 return TYPE_NAME;
178 }
179
180 bool supports(types::PrimitiveType data_type, AtomicOpType op) const override;
181
183 return {TYPE_NAME};
184 }
185
186private:
188 }
189};
190
192class AtomicScalarOpRocmImpl : public AtomicScalarOpGPUImpl {
193public:
194 static constexpr const char* TYPE_NAME = "ROCm";
195 static const AtomicScalarOpRocmImpl* instance();
196
197 std::string_view type_name() const override {
198 return TYPE_NAME;
199 }
200
201 bool supports(types::PrimitiveType data_type, AtomicOpType op) const override;
202
204 return {TYPE_NAME};
205 }
206
207private:
209 }
210};
211
213public:
214 nlohmann::json serialize(const sdfg::data_flow::LibraryNode& library_node) override;
215
217 const nlohmann::json& j,
220 ) override;
221};
222
224protected:
226 codegen::LanguageExtension& language_extension,
227 const Function& function,
228 const data_flow::DataFlowGraph& data_flow_graph,
230 );
231};
232
234public:
236 codegen::LanguageExtension& language_extension,
237 const Function& function,
238 const data_flow::DataFlowGraph& data_flow_graph,
240 )
241 : AtomicScalarOpNodeDispatcher(language_extension, function, data_flow_graph, node) {
242 }
243
246 std::vector<codegen::DispatchInput>& inputs,
247 std::vector<codegen::DispatchOutput>& outputs
248 ) override;
249};
250
252public:
254 codegen::LanguageExtension& language_extension,
255 const Function& function,
256 const data_flow::DataFlowGraph& data_flow_graph,
258 )
259 : AtomicScalarOpNodeDispatcher(language_extension, function, data_flow_graph, node) {
260 }
261
262public:
265 std::vector<codegen::DispatchInput>& inputs,
266 std::vector<codegen::DispatchOutput>& outputs
267 ) override;
268};
269
270
271} // namespace data_flow
272} // namespace sdfg
Dispatchers for dataflow blocks and library nodes.
Definition element.h:30
size_t element_id() const
const DebugInfo & debug_info() const
Definition function.h:43
Definition exceptions.h:28
Note: Even though the class references unique_ptr, it will never delete an SDFG it has a reference to...
Definition structured_sdfg_builder.h:44
Definition language_extension.h:39
Base class for library node code generation dispatchers.
Definition block_dispatcher.h:163
CPU (OpenMP) atomic implementation.
Definition atomic_op_node.h:138
static ImplementationType implementation_type()
Definition atomic_op_node.h:149
bool supports(types::PrimitiveType data_type, AtomicOpType op) const override
Whether this implementation supports the given (data type, op) combination.
static constexpr const char * TYPE_NAME
Definition atomic_op_node.h:140
static const AtomicScalarOpCPUImpl * instance()
std::string_view type_name() const override
Definition atomic_op_node.h:143
AtomicScalarOpCPUNodeDispatcher(codegen::LanguageExtension &language_extension, const Function &function, const data_flow::DataFlowGraph &data_flow_graph, const data_flow::AtomicScalarOpNode &node)
Definition atomic_op_node.h:235
void dispatch_code_with_edges(codegen::CodegenOutput &out, std::vector< codegen::DispatchInput > &inputs, std::vector< codegen::DispatchOutput > &outputs) override
CUDA atomic implementation.
Definition atomic_op_node.h:171
static constexpr const char * TYPE_NAME
Definition atomic_op_node.h:173
std::string_view type_name() const override
Definition atomic_op_node.h:176
static const AtomicScalarOpCudaImpl * instance()
bool supports(types::PrimitiveType data_type, AtomicOpType op) const override
Whether this implementation supports the given (data type, op) combination.
static ImplementationType implementation_type()
Definition atomic_op_node.h:182
Shared base for the GPU implementations (CUDA and ROCm).
Definition atomic_op_node.h:163
AtomicScalarOpImpl(const AtomicScalarOpImpl &)=delete
AtomicScalarOpGPUNodeDispatcher(codegen::LanguageExtension &language_extension, const Function &function, const data_flow::DataFlowGraph &data_flow_graph, const data_flow::AtomicScalarOpNode &node)
Definition atomic_op_node.h:253
void dispatch_code_with_edges(codegen::CodegenOutput &out, std::vector< codegen::DispatchInput > &inputs, std::vector< codegen::DispatchOutput > &outputs) override
A concrete lowering ("implementation") of an AtomicScalarOpNode.
Definition atomic_op_node.h:66
virtual bool supports(types::PrimitiveType data_type, AtomicOpType op) const =0
Whether this implementation supports the given (data type, op) combination.
virtual std::string_view type_name() const =0
AtomicScalarOpImpl & operator=(const AtomicScalarOpImpl &)=delete
AtomicScalarOpImpl(const AtomicScalarOpImpl &)=delete
AtomicScalarOpImpl()
Definition atomic_op_node.h:86
ImplementationType implementation_type() const
The implementation type this impl corresponds to.
Definition atomic_op_node.h:76
AtomicScalarOpNodeDispatcher(codegen::LanguageExtension &language_extension, const Function &function, const data_flow::DataFlowGraph &data_flow_graph, const data_flow::AtomicScalarOpNode &node)
Definition atomic_op_node.h:212
data_flow::LibraryNode & deserialize(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Block &parent) override
Deserialize a library node from JSON.
nlohmann::json serialize(const sdfg::data_flow::LibraryNode &library_node) override
Serialize a library node to JSON.
Wrapper around typical atomic operations (add, sub, min, max, swap) on a single scalar value.
Definition atomic_op_node.h:95
AtomicScalarOpNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, DataFlowGraph &parent, types::PrimitiveType data_type, AtomicOpType op, const AtomicScalarOpImpl *impl)
AtomicOpType atomic_op() const
Definition atomic_op_node.h:114
void validate(const Function &function) const override
Validates the element.
void replace(const symbolic::ExpressionMapping &replacements) override
void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override
symbolic::SymbolSet symbols() const override
Get all symbols used in this node.
void switch_implementation(const AtomicScalarOpImpl &new_impl)
types::PrimitiveType data_type() const
Definition atomic_op_node.h:111
std::unique_ptr< DataFlowNode > clone(size_t element_id, const graph::Vertex vertex, DataFlowGraph &parent) const override
Clone this node for graph transformations.
static const AtomicScalarOpImpl * get_implementation(const std::string &type)
ROCm atomic implementation.
Definition atomic_op_node.h:192
bool supports(types::PrimitiveType data_type, AtomicOpType op) const override
Whether this implementation supports the given (data type, op) combination.
static constexpr const char * TYPE_NAME
Definition atomic_op_node.h:194
static const AtomicScalarOpRocmImpl * instance()
static ImplementationType implementation_type()
Definition atomic_op_node.h:203
std::string_view type_name() const override
Definition atomic_op_node.h:197
Definition data_flow_graph.h:29
graph::Vertex vertex() const
Get the graph vertex for this node.
Base class for all library nodes in the dataflow graph.
Definition library_node.h:106
LibraryNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, DataFlowGraph &parent, const LibraryNodeCode &code, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs, const bool side_effect, const ImplementationType &implementation_type)
Protected constructor for library nodes.
Base class for library node serialization.
Definition json_serializer.h:195
A control flow node representing a basic block with dataflow computations.
Definition block.h:42
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
std::set< Symbol, SymEngine::RCPBasicKeyLess > SymbolSet
Ordered set of symbols.
Definition symbolic.h:95
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
Library node definitions for calling functions beyond simple instructions.
Definition function_builder.h:10
Definition access_node.h:67
constexpr const char * atomic_op_type_to_string(AtomicOpType type)
Definition atomic_op_node.h:20
StringEnum ImplementationType
Implementation type identifier for library nodes.
Definition library_node.h:75
StringEnum LibraryNodeCode
Definition library_node.h:65
LibraryNodeCode LibraryNodeType_AtomicScalarOp
Definition atomic_op_node.h:12
constexpr AtomicOpType atomic_op_type_from_string(std::string_view str)
Definition atomic_op_node.h:35
AtomicOpType
Definition atomic_op_node.h:18
@ Subtract
Definition atomic_op_node.h:18
@ Max
Definition atomic_op_node.h:18
@ Min
Definition atomic_op_node.h:18
@ Add
Definition atomic_op_node.h:18
boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition graph.h:25
PrimitiveType
Enumeration of all primitive data types supported by sdfglib.
Definition type.h:35
Definition analysis.h:7
Definition block_dispatcher.h:108