]> gitweb.factorcode.org Git - factor.git/blob - basis/math/vectors/simd/simd-docs.factor
factor: trim using lists
[factor.git] / basis / math / vectors / simd / simd-docs.factor
1 USING: classes.tuple.private cpu.architecture help.markup
2 help.syntax kernel.private math.vectors
3 math.vectors.simd.intrinsics sequences ;
4 IN: math.vectors.simd
5
6 ARTICLE: "math.vectors.simd.intro" "Introduction to SIMD support"
7 "Modern CPUs support a form of data-level parallelism, where arithmetic operations on fixed-size short vectors can be done on all components in parallel. This is known as single-instruction-multiple-data (SIMD)."
8 $nl
9 "SIMD support in the processor takes the form of instruction sets which operate on vector registers. By operating on multiple scalar values at the same time, code which operates on points, colors, and other vector data can be sped up."
10 $nl
11 "In Factor, SIMD support is exposed in the form of special-purpose SIMD " { $link "sequence-protocol" } " implementations. These are fixed-length, homogeneous sequences. They are referred to as vectors, but should not be confused with Factor's " { $link "vectors" } ", which can hold any type of object and can be resized."
12 $nl
13 "The words in the " { $vocab-link "math.vectors" } " vocabulary, which can be used with any sequence of numbers, are special-cased by the compiler. If the compiler can prove that only SIMD vectors are used, it expands " { $link "math-vectors" } " into " { $link "math.vectors.simd.intrinsics" } ". While in the general case, SIMD intrinsics operate on heap-allocated SIMD vectors, that too can be optimized since in many cases the compiler unbox SIMD vectors, storing them directly in registers."
14 $nl
15 "Since the only difference between ordinary code and SIMD-accelerated code is that the latter uses special fixed-length SIMD sequences, the SIMD library is very easy to use. To ensure your code compiles to use vector instructions without boxing and unboxing overhead, follow the guidelines for " { $link "math.vectors.simd.efficiency" } "."
16 $nl
17 "There should never be any reason to use " { $link "math.vectors.simd.intrinsics" } " directly, but they too have a straightforward, but lower-level, interface." ;
18
19 ARTICLE: "math.vectors.simd.support" "Supported SIMD instruction sets and operations"
20 "At present, the SIMD support makes use of a subset of SSE up to SSE4.1. The subset used depends on the current CPU type."
21 $nl
22 "SSE1 only supports single-precision SIMD (" { $snippet "float-4" } ")."
23 $nl
24 "SSE2 introduces double-precision SIMD (" { $snippet "double-2" } ") and integer SIMD (all types). Integer SIMD is missing a few features; in particular, the " { $link vmin } " and " { $link vmax } " operations only work on " { $snippet "uchar-16" } " and " { $snippet "short-8" } "."
25 $nl
26 "SSE3 introduces horizontal adds (summing all components of a single vector register), which are useful for computing dot products. Where available, SSE3 operations are used to speed up " { $link sum } ", " { $link vdot } ", " { $link norm-sq } ", " { $link norm } ", and " { $link distance } "."
27 $nl
28 "SSSE3 introduces " { $link vabs } " for " { $snippet "char-16" } ", " { $snippet "short-8" } " and " { $snippet "int-4" } "."
29 $nl
30 "SSE4.1 introduces " { $link vmin } " and " { $link vmax } " for all remaining integer types, a faster instruction for " { $link vdot } ", and a few other things."
31 $nl
32 "On PowerPC, or older x86 chips without SSE, software fallbacks are used for all high-level vector operations. SIMD code can run with no loss in functionality, just decreased performance."
33 $nl
34 "The primitives in the " { $vocab-link "math.vectors.simd.intrinsics" } " vocabulary do not have software fallbacks, but they should not be called directly in any case." ;
35
36 ARTICLE: "math.vectors.simd.types" "SIMD vector types"
37 "Each SIMD vector type is named " { $snippet "scalar-count" } ", where " { $snippet "scalar" } " is a scalar C type and " { $snippet "count" } " is a vector dimension."
38 $nl
39 "The following 128-bit vector types are defined in the " { $vocab-link "math.vectors.simd" } " vocabulary:"
40 { $code
41     "char-16"
42     "uchar-16"
43     "short-8"
44     "ushort-8"
45     "int-4"
46     "uint-4"
47     "longlong-2"
48     "ulonglong-2"
49     "float-4"
50     "double-2"
51 }
52 "Double-width 256-bit vector types are defined in the " { $vocab-link "math.vectors.simd.cords" } " vocabulary:"
53 { $code
54     "char-32"
55     "uchar-32"
56     "short-16"
57     "ushort-16"
58     "int-8"
59     "uint-8"
60     "longlong-4"
61     "ulonglong-4"
62     "float-8"
63     "double-4"
64 } ;
65
66 ARTICLE: "math.vectors.simd.words" "SIMD vector words"
67 "For each SIMD vector type, several words are defined, where " { $snippet "type" } " is the type in question:"
68 { $table
69     { { $strong "Word" } { $strong "Stack effect" } { $strong "Description" } }
70     { { $snippet "type-with" } { $snippet "( x -- simd-array )" } "creates a new instance where all components are set to a single scalar" }
71     { { $snippet "type-boa" } { $snippet "( ... -- simd-array )" } "creates a new instance where components are read from the stack" }
72     { { $snippet "type-cast" } { $snippet "( simd-array -- simd-array' )" } "creates a new SIMD array where the underlying data is taken from another SIMD array, with no format conversion" }
73     { { $snippet ">type" } { $snippet "( seq -- simd-array )" } "creates a new instance initialized with the elements of an existing sequence, which must have the correct length" }
74     { { $snippet "type{" } { $snippet "type{ elements... }" } "parsing word defining literal syntax for an SIMD vector; the correct number of elements must be given" }
75 }
76 "To actually perform vector arithmetic on SIMD vectors, use " { $link "math-vectors" } " words."
77 { $see-also "c-types-specs" } ;
78
79 ARTICLE: "math.vectors.simd.efficiency" "Writing efficient SIMD code"
80 "Since SIMD vectors are heap-allocated objects, it is important to write code in a style which is conducive to the compiler being able to inline generic dispatch and eliminate allocation."
81 $nl
82 "If the inputs to a " { $vocab-link "math.vectors" } " word are statically known to be SIMD vectors, the call is converted into an SIMD primitive, and the output is then also known to be an SIMD vector (or scalar, depending on the operation); this information propagates forward within a single word (together with any inlined words and macro expansions). Any intermediate values which are not stored into collections, or returned from the word, are furthermore unboxed."
83 $nl
84 "To check if optimizations are being performed, pass a quotation to the " { $snippet "optimizer-report." } " and " { $snippet "optimized." } " words in the " { $vocab-link "compiler.tree.debugger" } " vocabulary, and look for calls to " { $link "math.vectors.simd.intrinsics" } " as opposed to high-level " { $link "math-vectors" } "."
85 $nl
86 "For example, in the following, no SIMD operations are used at all, because the compiler's propagation pass does not consider dynamic variable usage:"
87 { $code
88 "USING: compiler.tree.debugger math.vectors
89 math.vectors.simd ;
90 SYMBOLS: x y ;
91
92 [
93     float-4{ 1.5 2.0 3.7 0.4 } x set
94     float-4{ 1.5 2.0 3.7 0.4 } y set
95     x get y get v+
96 ] optimizer-report." }
97 "The following word benefits from SIMD optimization, because it begins with an unsafe declaration:"
98 { $code
99 "USING: compiler.tree.debugger kernel.private
100 math.vectors math.vectors.simd ;
101 IN: simd-demo
102
103 : interpolate ( v a b -- w )
104     { float-4 float-4 float-4 } declare
105     [ v* ] [ [ 1.0 ] dip n-v v* ] bi-curry* bi v+ ;
106
107 \\ interpolate optimizer-report." }
108 "Note that using " { $link declare } " is not recommended. Safer ways of getting type information for the input parameters to a word include defining methods on a generic word (the value being dispatched upon has a statically known type in the method body), as well as using " { $link "hints" } " and " { $link POSTPONE: inline } " declarations."
109 $nl
110 "Here is a better version of the " { $snippet "interpolate" } " words above that uses hints:"
111 { $code
112 "USING: compiler.tree.debugger hints
113 math.vectors math.vectors.simd ;
114 IN: simd-demo
115
116 : interpolate ( v a b -- w )
117     [ v* ] [ [ 1.0 ] dip n-v v* ] bi-curry* bi v+ ;
118
119 HINTS: interpolate float-4 float-4 float-4 ;
120
121 \\ interpolate optimizer-report. " }
122 "This time, the optimizer report lists calls to both SIMD primitives and high-level vector words, because hints cause two code paths to be generated. The " { $snippet "optimized." } " word can be used to make sure that the fast code path consists entirely of calls to primitives."
123 $nl
124 "If the " { $snippet "interpolate" } " word was to be used in several places with different types of vectors, it would be best to declare it " { $link POSTPONE: inline } "."
125 $nl
126 "In the " { $snippet "interpolate" } " word, there is still a call to the " { $link <tuple-boa> } " primitive, because the return value at the end is being boxed on the heap. In the next example, no memory allocation occurs at all because the SIMD vectors are stored inside a struct class (see " { $link "classes.struct" } "); also note the use of inlining:"
127 { $code
128 "USING: compiler.tree.debugger math.vectors math.vectors.simd ;
129 IN: simd-demo
130
131 STRUCT: actor
132 { id int }
133 { position float-4 }
134 { velocity float-4 }
135 { acceleration float-4 } ;
136
137 GENERIC: advance ( dt object -- )
138
139 : update-velocity ( dt actor -- )
140     [ acceleration>> n*v ] [ velocity>> v+ ] [ ] tri
141     velocity<< ; inline
142
143 : update-position ( dt actor -- )
144     [ velocity>> n*v ] [ position>> v+ ] [ ] tri
145     position<< ; inline
146
147 M: actor advance ( dt actor -- )
148     [ >float ] dip
149     [ update-velocity ] [ update-position ] 2bi ;
150
151 M\\ actor advance optimized."
152 }
153 "The " { $vocab-link "compiler.cfg.debugger" } " vocabulary can give a lower-level picture of the generated code, that includes register assignments and other low-level details. To look at low-level optimizer output, call " { $snippet "regs." } " on a word or quotation:"
154 { $code
155 "USE: compiler.tree.debugger
156
157 M\\ actor advance regs." }
158 "Example of a high-performance algorithms that use SIMD primitives can be found in the following vocabularies:"
159 { $list
160     { $vocab-link "benchmark.nbody-simd" }
161     { $vocab-link "benchmark.raytracer-simd" }
162     { $vocab-link "random.sfmt" }
163 } ;
164
165 ARTICLE: "math.vectors.simd.intrinsics" "Low-level SIMD primitives"
166 "The words in the " { $vocab-link "math.vectors.simd.intrinsics" } " vocabulary are used to implement SIMD support. These words have three disadvantages compared to the higher-level " { $link "math-vectors" } " words:"
167 { $list
168     "They operate on raw byte arrays, with a separate “representation” parameter passed in to determine the type of the operands and result."
169     "They are unsafe; passing values which are not byte arrays, or byte arrays with the wrong size, will dereference invalid memory and possibly crash Factor."
170 }
171 "The compiler converts " { $link "math-vectors" } " into SIMD primitives automatically in cases where it is safe; this means that the input types are known to be SIMD vectors, and the CPU supports SIMD."
172 $nl
173 "It is best to avoid calling SIMD primitives directly. To write efficient high-level code that compiles down to primitives and avoids memory allocation, see " { $link "math.vectors.simd.efficiency" } "."
174 $nl
175 "There are two primitives which are used to implement accessing SIMD vector fields of " { $link "classes.struct" } ":"
176 { $subsections
177     alien-vector
178     set-alien-vector
179 }
180 "For the most part, the above primitives correspond directly to vector arithmetic words. They take a representation parameter, which is one of the singleton members of the " { $link vector-rep } " union in the " { $vocab-link "cpu.architecture" } " vocabulary." ;
181
182 ARTICLE: "math.vectors.simd.alien" "SIMD data in struct classes"
183 "Struct classes may contain fields which store SIMD data; for each SIMD vector type listed in " { $snippet "math.vectors.simd.types" } " there is a C type with the same name."
184 $nl
185 "Only SIMD struct fields are allowed at the moment; passing SIMD data as function parameters is not yet supported." ;
186
187 ARTICLE: "math.vectors.simd.accuracy" "Numerical accuracy of SIMD primitives"
188 "No guarantees are made that " { $vocab-link "math.vectors.simd" } " words will give identical results on different SSE versions, or between the hardware intrinsics and the software fallbacks."
189 $nl
190 "In particular, horizontal operations on " { $snippet "float-4" } " vectors are affected by this. They are computed with lower precision in intrinsics than the software fallback. Horizontal operations include anything involving adding together the components of a vector, such as " { $link sum } " or " { $link normalize } "." ;
191
192 ARTICLE: "math.vectors.simd" "Hardware vector arithmetic (SIMD)"
193 "The " { $vocab-link "math.vectors.simd" } " vocabulary extends the " { $vocab-link "math.vectors" } " vocabulary to support efficient vector arithmetic on small, fixed-size vectors."
194 { $subsections
195     "math.vectors.simd.intro"
196     "math.vectors.simd.types"
197     "math.vectors.simd.words"
198     "math.vectors.simd.support"
199     "math.vectors.simd.accuracy"
200     "math.vectors.simd.efficiency"
201     "math.vectors.simd.alien"
202     "math.vectors.simd.intrinsics"
203 } ;
204
205 ABOUT: "math.vectors.simd"