Poplar and PopLibs
Type.hpp
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_Type_hpp
4#define poplar_Type_hpp
5#include <ostream>
6#include <poplar/Quarter.hpp>
7#include <poplar/SSOPointer.hpp>
8#include <poplar/StringRef.hpp>
9
10namespace poplar {
11namespace core {
12class Type;
13} // namespace core
14
42class Type {
43public:
44 Type();
45 ~Type();
46 Type(const Type &other);
47 Type(Type &&other) noexcept;
48 Type &operator=(const Type &other);
49 Type &operator=(Type &&other) noexcept;
50
55 StringRef toString() const;
56 bool operator==(const Type &other) const;
57 bool operator!=(const Type &other) const { return !(*this == other); }
58 bool operator<(const Type &other) const;
59
60 // Implementation
61 Type(SSOPointer<core::Type>);
62 const core::Type &getImpl() const;
63
64 std::size_t hash() const;
65
66 bool isFloatingPoint() const;
67
68 bool requiresMetadata() const;
69
70private:
71 SSOPointer<core::Type> impl;
72};
73
74inline std::ostream &operator<<(std::ostream &os, const Type &t) {
75 return os << t.toString();
76}
77
78/* Valid types are as follows.
79 *
80 * | Type | Kind | Signed |
81 * |-------------------------|-----------|---------|
82 * | `bool` | boolean | No |
83 * | `char` | integral | Yes |
84 * | `signed char` | integral | Yes |
85 * | `unsigned char` | integral | No |
86 * | `short` | integral | Yes |
87 * | `signed short` | integral | Yes |
88 * | `unsigned short` | integral | No |
89 * | `int` | integral | Yes |
90 * | `unsigned int` | integral | No |
91 * | `long` | integral | Yes |
92 * | `unsigned long` | integral | No |
93 * | `long long` | integral | Yes |
94 * | `unsigned long long` | integral | No |
95 * | `half` | real | Yes |
96 * | `float` | real | Yes |
97 * | `double` | real | Yes |
98 *
99 * The sizes follow the rules in the C specification: sizeof(short) >= 2,
100 * sizeof(int) >= sizeof(short), and so on. For using CPU-based Poplar backends,
101 * the type sizes will be the same as the host.
102 *
103 * For IPU based backends the following types are NOT supported:
104 *
105 * * long
106 * * unsigned long
107 * * double
108 *
109 *
110 * And limited support for operations with the following types:
111 *
112 * * long long
113 * * unsigned long long
114 *
115 * For supported types the sizes on the IPU architectures are:
116 *
117 * | Type | Size (bytes) |
118 * |-------------------------|--------------|
119 * | `bool` | 1 |
120 * | `char` | 1 |
121 * | `signed char` | 1 |
122 * | `unsigned char` | 1 |
123 * | `short` | 2 |
124 * | `signed short` | 2 |
125 * | `unsigned short` | 2 |
126 * | `int` | 4 |
127 * | `signed int` | 4 |
128 * | `signed` | 4 |
129 * | `unsigned int` | 4 |
130 * | `unsigned` | 4 |
131 * | `unsigned long long` | 8 |
132 * | `long long` | 8 |
133 * | `half` | 2 |
134 * | `float` | 4 |
135 *
136 */
137
139extern Type BOOL;
140
142extern Type CHAR;
143
145extern Type UNSIGNED_CHAR;
146
148extern Type SIGNED_CHAR;
149
151extern Type UNSIGNED_SHORT;
152
154extern Type SHORT;
155
157extern Type UNSIGNED_INT;
158
160extern Type INT;
161
163extern Type UNSIGNED_LONG;
164
166extern Type LONG;
167
170
172extern Type LONGLONG;
173
174extern Type QUARTER;
175
176extern Type QUARTER_METADATA;
177
179extern Type HALF;
180
182extern Type FLOAT;
183
192template <typename T> struct equivalent_device_type {};
193
194#define POPLAR_DECLARE_EQUIV_TYPE(T1, T2) \
195 template <> struct equivalent_device_type<T1> { \
196 const Type &value = T2; \
197 };
198
199POPLAR_DECLARE_EQUIV_TYPE(bool, BOOL)
200POPLAR_DECLARE_EQUIV_TYPE(char, CHAR)
201POPLAR_DECLARE_EQUIV_TYPE(unsigned char, UNSIGNED_CHAR)
202POPLAR_DECLARE_EQUIV_TYPE(signed char, SIGNED_CHAR)
203POPLAR_DECLARE_EQUIV_TYPE(unsigned short, UNSIGNED_SHORT)
204POPLAR_DECLARE_EQUIV_TYPE(signed short, SHORT)
205POPLAR_DECLARE_EQUIV_TYPE(unsigned int, UNSIGNED_INT)
206POPLAR_DECLARE_EQUIV_TYPE(signed int, INT)
207POPLAR_DECLARE_EQUIV_TYPE(unsigned long, UNSIGNED_LONG)
208POPLAR_DECLARE_EQUIV_TYPE(signed long, LONG)
209POPLAR_DECLARE_EQUIV_TYPE(unsigned long long, UNSIGNED_LONGLONG)
210POPLAR_DECLARE_EQUIV_TYPE(signed long long, LONGLONG)
211POPLAR_DECLARE_EQUIV_TYPE(float, FLOAT)
212POPLAR_DECLARE_EQUIV_TYPE(QuarterMetadata, QUARTER_METADATA)
213
214#undef POPLAR_DECLARE_EQUIV_TYPE
215
216} // end namespace poplar
217
218namespace std {
219
220template <> struct hash<poplar::Type> {
221 size_t operator()(const poplar::Type &t) const { return t.hash(); }
222};
223
224} // namespace std
225
226#endif // poplar_Type_hpp
Type
Padding types as per numpy.pad.
Definition: Pad.hpp:24
Quarter metadata type.
Definition: Quarter.hpp:37
Class representing device data types.
Definition: Type.hpp:42
StringRef toString() const
Get a string representation on a type.
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::ostream & operator<<(std::ostream &os, const DebugNameAndId &dnai)
Display the path name of the DebugNameAndId.
Type UNSIGNED_SHORT
Device type: unsigned short
Type LONGLONG
Device type: long long
Type LONG
Device type: long
Type UNSIGNED_CHAR
Device type: unsigned char
Type SHORT
Device type: short
Type FLOAT
Device type: float
Type CHAR
Device type: char
Type BOOL
Device type: bool
Type SIGNED_CHAR
Device type: signed char
Type UNSIGNED_INT
Device type: unsigned int
Type HALF
Device type: half
Type INT
Device type: int
Type UNSIGNED_LONGLONG
Device type: unsigned long long
Type UNSIGNED_LONG
Device type: unsigned long
Template structure to relate a host type to a device type.
Definition: Type.hpp:192