Poplar and PopLibs
cyclesTables.hpp
Go to the documentation of this file.
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
8#ifndef poputil_cyclesTables_hpp
9#define poputil_cyclesTables_hpp
10
11#include <poplar/Graph.hpp>
12#include <poplar/PerfEstimateFunc.hpp>
13#include <poplar/Target.hpp>
14#include <poplar/VertexIntrospector.hpp>
17
18#include <functional>
19#include <iterator>
20#include <regex>
21#include <string>
22
35
36#define MAKE_PERF_ESTIMATOR_NAME(codelet) getPerfEstimateFor##codelet
37#define CYCLE_ESTIMATOR_ENTRY_NOPARAMS(ns, codelet) \
38 poputil::internal::makePerfEstimatorEntry(#ns "::" #codelet, \
39 MAKE_PERF_ESTIMATOR_NAME(codelet))
40#define CYCLE_ESTIMATOR_ENTRY(ns, codelet, ...) \
41 poputil::internal::makePerfEstimatorEntry( \
42 #ns "::" #codelet, MAKE_PERF_ESTIMATOR_NAME(codelet), __VA_ARGS__)
43
44// These macros reduce boiler plate code when accessing
45// the codelet fields in cycle estimators:
46#define CODELET_FIELD(field) const auto field = vertex.getFieldInfo(#field)
47#define CODELET_SCALAR_VAL(field, type) \
48 const auto field = vertex.getFieldInfo(#field).getInitialValue<type>(target);
49#define CODELET_VECTOR_VALS(field, type) \
50 const auto field = vertex.getFieldInfo(#field).getInitialValues<type>(target);
51#define CODELET_VECTOR_2D_VALS(field, type) \
52 const auto field = \
53 vertex.getFieldInfo(#field).getInitialValues<std::vector<type>>(target);
54
55namespace poputil {
56namespace internal {
57
58using PerfEstimatorTable =
59 std::vector<std::pair<std::string, poplar::PerfEstimateFunc>>;
60
61template <typename F, typename... Args>
62inline std::pair<std::string, poplar::PerfEstimateFunc>
63makePerfEstimatorEntry(const std::string &codelet, F f, Args &&...args) {
64 using std::placeholders::_1;
65 using std::placeholders::_2;
66 return std::make_pair(
67 poputil::templateVertex(codelet, std::forward<Args>(args)...),
68 std::bind(f, _1, _2, std::forward<Args>(args)...));
69}
70
71inline void registerPerfFunctions(poplar::Graph &graph,
72 const PerfEstimatorTable &table) {
73 for (auto &kv : table) {
74 graph.registerPerfEstimator(kv.first, poplar::PerfEstimateFunc(kv.second));
75 }
76}
77
78inline std::uint64_t getUnpackCost(const poplar::layout::Vector layout) {
79 switch (layout) {
80 case poplar::layout::Vector::NotAVector:
81 case poplar::layout::Vector::Span:
82 case poplar::layout::Vector::OnePtr:
83 return 0;
84 case poplar::layout::Vector::ShortSpan:
85 return 2;
86 case poplar::layout::Vector::ScaledPtr32:
87 // shl + add (note: doesn't include setzi that is shared among fields)
88 return 2;
89 case poplar::layout::Vector::ScaledPtr64:
90 case poplar::layout::Vector::ScaledPtr128:
91 // shl
92 return 1;
93 }
94
95 throw poputil::poplibs_error("Unknown layout");
96}
97
98inline std::uint64_t getUnpackCost(const poplar::layout::VectorList layout) {
99 switch (layout) {
100 case poplar::layout::VectorList::NotAVector:
101 case poplar::layout::VectorList::OnePtr:
102 case poplar::layout::VectorList::ScaledPtr32:
103 case poplar::layout::VectorList::ScaledPtr64:
104 case poplar::layout::VectorList::ScaledPtr128:
105 throw poputil::poplibs_error("Unknown cost for this layout");
106 case poplar::layout::VectorList::DeltaN:
107 return poputil::internal::getUnpackCost(
108 poplar::layout::Vector::ScaledPtr32);
109 case poplar::layout::VectorList::DeltaNElements:
110 // (shl ; and) to get base and nA and again to get deltaN and nB then an
111 // add to combine nA and nB
112 return 5;
113 }
114
115 throw poputil::poplibs_error("Unknown layout");
116}
117
118inline std::uint64_t
119getVectorListDeltaUnpackCost(const poplar::layout::VectorList layout) {
120 switch (layout) {
121 case poplar::layout::VectorList::NotAVector:
122 case poplar::layout::VectorList::OnePtr:
123 case poplar::layout::VectorList::ScaledPtr32:
124 case poplar::layout::VectorList::ScaledPtr64:
125 case poplar::layout::VectorList::ScaledPtr128:
126 throw poputil::poplibs_error("Unknown cost for this layout");
127 case poplar::layout::VectorList::DeltaN:
128 // shl (offset), shr ; shl (n)
129 return 3;
130 case poplar::layout::VectorList::DeltaNElements:
131 // shl (offset), and (n)
132 return 2;
133 }
134
135 throw poputil::poplibs_error("Unknown layout");
136}
137
138} // end namespace internal
139} // end namespace poputil
140
141#endif // poputil_cyclesTables_hpp
Generate a string describing a vertex type.
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
void registerPerfEstimator(StringRef vertexTypeName, PerfEstimateFunc f)
Vector
An enumeration used to state what type of pointer is used for a Vector vertex field.
Definition: VectorLayout.hpp:15
VectorList
An enumeration used to state what type of pointer is used for a VectorList vertex field.
Definition: VectorLayout.hpp:31
std::function< VertexPerfEstimate(const VertexIntrospector &v, const Target &target)> PerfEstimateFunc
Functions of this type can be used as performance estimator callbacks for new vertex types.
Definition: PerfEstimateFunc.hpp:25
General utility functions for building graphs.
Definition: GfloatExprUtil.hpp:23
std::string templateVertex(const std::string &name, Args &&...args)
Generate a string representation of a Vertex type for use by poplar::Graph::addVertex().
Definition: VertexTemplates.hpp:96
Define a PopLibs exception.
Class for PopLibs exceptions.
Definition: exceptions.hpp:17