docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
cmath_node.h
Go to the documentation of this file.
1#pragma once
2
4#include "sdfg/types/type.h"
5
9
10namespace sdfg {
11namespace math {
12namespace cmath {
13
16
21enum class CMathFunction {
22 // Trigonometric functions
30
31 // Hyperbolic functions
38
39 // Exponential and logarithmic functions
48
49 // Power functions
54
55 // Error and gamma functions
60
61 // Rounding and remainder functions
76
77 // Floating-point manipulation functions
88
89 // Minimum, maximum, difference functions
93
94 // Other functions
96};
97
104 switch (func) {
106 return 1;
108 return 1;
110 return 1;
112 return 1;
114 return 1;
116 return 1;
118 return 2;
120 return 1;
122 return 1;
124 return 1;
126 return 1;
128 return 1;
130 return 1;
132 return 1;
134 return 1;
136 return 1;
138 return 1;
140 return 1;
142 return 1;
144 return 1;
146 return 1;
148 return 2;
150 return 1;
152 return 1;
154 return 2;
156 return 1;
158 return 1;
160 return 1;
162 return 1;
164 return 1;
166 return 1;
168 return 1;
170 return 1;
172 return 1;
174 return 1;
176 return 1;
178 return 1;
180 return 1;
182 return 1;
184 return 1;
186 return 1;
188 return 2;
190 return 2;
192 return 2;
194 return 2;
196 return 1;
198 return 1;
200 return 1;
202 return 1;
204 return 1;
206 return 2;
208 return 2;
210 return 2;
212 return 2;
214 return 2;
216 return 2;
218 return 3;
219 }
220}
221
230constexpr uint64_t cmath_function_to_flop(CMathFunction func) {
231 switch (func) {
232 // Rounding, sign, classification, manipulation, min/max, remainder, sqrt: one fp op.
261 return 1;
262 // Fused multiply-add.
264 return 2;
265 // Exponential / logarithmic / root: a short polynomial evaluation.
276 return 10;
277 // Trig / hyperbolic / inverse, power, error and gamma: range reduction plus a longer
278 // polynomial.
297 return 20;
298 }
299}
300
306constexpr const char* cmath_function_to_stem(CMathFunction func) {
307 switch (func) {
309 return "sin";
311 return "cos";
313 return "tan";
315 return "asin";
317 return "acos";
319 return "atan";
321 return "atan2";
323 return "sinh";
325 return "cosh";
327 return "tanh";
329 return "asinh";
331 return "acosh";
333 return "atanh";
335 return "exp";
337 return "exp2";
339 return "exp10";
341 return "expm1";
343 return "log";
345 return "log10";
347 return "log2";
349 return "log1p";
351 return "pow";
353 return "sqrt";
355 return "cbrt";
357 return "hypot";
359 return "erf";
361 return "erfc";
363 return "tgamma";
365 return "lgamma";
367 return "fabs";
369 return "ceil";
371 return "floor";
373 return "trunc";
375 return "round";
377 return "lround";
379 return "llround";
381 return "roundeven";
383 return "nearbyint";
385 return "rint";
387 return "lrint";
389 return "llrint";
391 return "fmod";
393 return "remainder";
395 return "frexp";
397 return "ldexp";
399 return "modf";
401 return "scalbn";
403 return "scalbln";
405 return "ilogb";
407 return "logb";
409 return "nextafter";
411 return "nexttoward";
413 return "copysign";
415 return "fmax";
417 return "fmin";
419 return "fdim";
421 return "fma";
422 }
423}
424
426
440 std::string base_name = cmath_function_to_stem(func);
441
442 switch (prim_type) {
444 return base_name + "f";
446 return base_name;
450 return base_name + "l";
453 // Half and BFloat are typically promoted to float for C math operations
454 // as there are no standard Half/BFloat intrinsics in C math library
455 return base_name + "f";
456 default:
457 throw InvalidSDFGException("Unsupported primitive type for C math intrinsic name generation.");
458 }
459}
460
466 if (prim_type != types::PrimitiveType::Float) {
467 return "";
468 }
469 switch (func) {
479 return std::string("__") + cmath_function_to_stem(func) + "f";
480 default:
481 return "";
482 }
483}
484
486private:
487 CMathFunction function_;
488 types::PrimitiveType primitive_type_;
489 // Permits a faster, less accurate device intrinsic (e.g. __expf) where the target has one.
490 bool approximate_ = false;
491
492public:
494 size_t element_id,
495 const DebugInfo& debug_info,
496 const graph::Vertex vertex,
500 bool approximate = false
501 );
502
505 std::string name() const;
506
507 bool approximate() const;
509
510 void validate(const Function& function) const override;
511
512 symbolic::SymbolSet symbols() const override;
513
514 // Elementwise math reads scalar values from its inputs and never captures a pointer.
516
517 void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override;
518
519 void replace(const symbolic::ExpressionMapping& replacements) override;
520
521 std::unique_ptr<data_flow::DataFlowNode>
522 clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph& parent) const override;
523
524 virtual symbolic::Expression flop() const override;
525
526 std::string toStr() const override;
527
530};
531
533public:
534 nlohmann::json serialize(const data_flow::LibraryNode& library_node) override;
535
538 ) override;
539};
540
542public:
544 codegen::LanguageExtension& language_extension,
545 const Function& function,
546 const data_flow::DataFlowGraph& data_flow_graph,
547 const CMathNode& node
548 );
549
552 codegen::PrettyPrinter& globals_stream,
553 codegen::CodeSnippetFactory& library_snippet_factory
554 ) override;
555};
556
557} // namespace cmath
558} // namespace math
559} // 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:8
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
Originally started out as a handler for additional source files, like hardware-accelerator kernels th...
Definition code_snippet_factory.h:69
Definition language_extension.h:39
Base class for library node code generation dispatchers.
Definition block_dispatcher.h:163
Definition utils.h:16
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
Edge in the dataflow graph representing data movement.
Definition memlet.h:131
CMathNodeDispatcher(codegen::LanguageExtension &language_extension, const Function &function, const data_flow::DataFlowGraph &data_flow_graph, const CMathNode &node)
void dispatch_code(codegen::PrettyPrinter &stream, codegen::PrettyPrinter &globals_stream, codegen::CodeSnippetFactory &library_snippet_factory) override
Generate the operation-specific code.
Definition cmath_node.h:532
data_flow::LibraryNode & deserialize(const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent) override
Deserialize a library node from JSON.
nlohmann::json serialize(const data_flow::LibraryNode &library_node) override
Serialize a library node to JSON.
Definition cmath_node.h:485
virtual symbolic::Expression flop() const override
Calculate floating point operations for this node.
data_flow::PointerAccessType pointer_access_type(int input_idx) const override
Describes what a pointer is used for.
data_flow::EdgeRemoveOption can_remove_out_edge(const data_flow::DataFlowGraph &graph, const data_flow::Memlet *memlet) const override
Is the edge in question removable, or will this make the node its an output on invalid?
void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override
void validate(const Function &function) const override
Validates the element.
CMathNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, data_flow::DataFlowGraph &parent, CMathFunction function, types::PrimitiveType primitive_type, bool approximate=false)
void replace(const symbolic::ExpressionMapping &replacements) override
void approximate(bool approximate)
types::PrimitiveType primitive_type() const
symbolic::SymbolSet symbols() const override
Get all symbols used in this node.
std::string name() const
std::unique_ptr< data_flow::DataFlowNode > clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const override
Clone this node for graph transformations.
CMathFunction function() const
std::string toStr() const override
Convert node to string representation.
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
EdgeRemoveOption
Definition data_flow_node.h:71
StringEnum LibraryNodeCode
Definition library_node.h:65
std::unique_ptr< PointerAccessMeta, PtrAccessDeleter > PointerAccessType
Definition pointer_metadata.h:32
Definition graph.h:22
boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition graph.h:25
Definition cmath_node.h:12
std::string get_cmath_intrinsic_name(CMathFunction func, types::PrimitiveType prim_type)
Get the correct C math intrinsic name for a given function and primitive type.
Definition cmath_node.h:439
constexpr size_t cmath_function_to_arity(CMathFunction func)
Get arity of CMathFunction enum.
Definition cmath_node.h:103
data_flow::LibraryNodeCode LibraryNodeType_CMath("CMath")
constexpr const char * cmath_function_to_stem(CMathFunction func)
Convert CMathFunction enum to function name stem (without type suffix).
Definition cmath_node.h:306
data_flow::LibraryNodeCode LibraryNodeType_CMath_Deprecated("Intrinsic")
CMathFunction string_to_cmath_function(const std::string &name)
constexpr uint64_t cmath_function_to_flop(CMathFunction func)
Approximate flop-equivalent cost of a libm call.
Definition cmath_node.h:230
std::string get_cmath_approximate_intrinsic_name(CMathFunction func, types::PrimitiveType prim_type)
Fast, approximate CUDA/HIP device intrinsic for a function, if one exists.
Definition cmath_node.h:465
CMathFunction
Enumeration of C math library functions.
Definition cmath_node.h:21
@ atanh
Definition cmath_node.h:37
@ lgamma
Definition cmath_node.h:59
@ cbrt
Definition cmath_node.h:52
@ cosh
Definition cmath_node.h:33
@ acosh
Definition cmath_node.h:36
@ llround
Definition cmath_node.h:68
@ nearbyint
Definition cmath_node.h:70
@ erf
Definition cmath_node.h:56
@ pow
Definition cmath_node.h:50
@ fabs
Definition cmath_node.h:62
@ hypot
Definition cmath_node.h:53
@ lround
Definition cmath_node.h:67
@ fdim
Definition cmath_node.h:92
@ lrint
Definition cmath_node.h:72
@ scalbln
Definition cmath_node.h:82
@ cos
Definition cmath_node.h:24
@ tan
Definition cmath_node.h:25
@ tanh
Definition cmath_node.h:34
@ nexttoward
Definition cmath_node.h:86
@ asin
Definition cmath_node.h:26
@ fmax
Definition cmath_node.h:90
@ scalbn
Definition cmath_node.h:81
@ atan
Definition cmath_node.h:28
@ ceil
Definition cmath_node.h:63
@ floor
Definition cmath_node.h:64
@ expm1
Definition cmath_node.h:43
@ log2
Definition cmath_node.h:46
@ sin
Definition cmath_node.h:23
@ rint
Definition cmath_node.h:71
@ tgamma
Definition cmath_node.h:58
@ erfc
Definition cmath_node.h:57
@ fma
Definition cmath_node.h:95
@ remainder
Definition cmath_node.h:75
@ frexp
Definition cmath_node.h:78
@ fmin
Definition cmath_node.h:91
@ fmod
Definition cmath_node.h:74
@ modf
Definition cmath_node.h:80
@ round
Definition cmath_node.h:66
@ log1p
Definition cmath_node.h:47
@ logb
Definition cmath_node.h:84
@ nextafter
Definition cmath_node.h:85
@ sinh
Definition cmath_node.h:32
@ exp
Definition cmath_node.h:40
@ roundeven
Definition cmath_node.h:69
@ acos
Definition cmath_node.h:27
@ atan2
Definition cmath_node.h:29
@ llrint
Definition cmath_node.h:73
@ ilogb
Definition cmath_node.h:83
@ exp10
Definition cmath_node.h:42
@ log10
Definition cmath_node.h:45
@ copysign
Definition cmath_node.h:87
@ log
Definition cmath_node.h:44
@ sqrt
Definition cmath_node.h:51
@ trunc
Definition cmath_node.h:65
@ exp2
Definition cmath_node.h:41
@ ldexp
Definition cmath_node.h:79
@ asinh
Definition cmath_node.h:35
Definition batched_gemm_node.h:12
PrimitiveType
Enumeration of all primitive data types supported by sdfglib.
Definition type.h:35
@ Half
16-bit floating-point (IEEE 754 half precision)
Definition type.h:48
@ FP128
128-bit floating-point (IEEE 754 quadruple precision)
Definition type.h:53
@ Double
64-bit floating-point (IEEE 754 double precision)
Definition type.h:51
@ PPC_FP128
128-bit floating-point (PowerPC double-double)
Definition type.h:54
@ BFloat
16-bit brain floating-point
Definition type.h:49
@ X86_FP80
80-bit extended precision floating-point (x86)
Definition type.h:52
@ Float
32-bit floating-point (IEEE 754 single precision)
Definition type.h:50
Definition analysis.h:7
Symbolic system for integer-based symbol analysis.
Core type system definitions for sdfglib.