Debian Codegraph @

current/shared/SIMD.java

raw symbols SCIP
1 package shared;
2
3 import jdk.incubator.vector.ByteVector;
4 import jdk.incubator.vector.DoubleVector;
5 import jdk.incubator.vector.FloatVector;
6 import jdk.incubator.vector.IntVector;
7 import jdk.incubator.vector.LongVector;
8 import jdk.incubator.vector.ShortVector;
9 import jdk.incubator.vector.VectorMask;
10 import jdk.incubator.vector.VectorOperators;
11 import jdk.incubator.vector.VectorSpecies;
12 import ml.Cell;
13
14 /**
15 * Holds SIMD methods.
16 * @author Brian Bushnell
17 * @date Sep 12, 2013
18 *
19 */
20 final class SIMD {
21
22 //Example from https://medium.com/@Styp/java-18-vector-api-do-we-get-free-speed-up-c4510eda50d2
23 @SuppressWarnings("restriction")
24 private static final VectorSpecies<Float> FSPECIES=FloatVector.SPECIES_256;//FloatVector.SPECIES_PREFERRED; //This needs to be final or performance drops.
25 private static final int FWIDTH=FSPECIES.length();
26 // private static final int boundMask=~(FWIDTH-1);
27
28 @SuppressWarnings("restriction")
29 private static final VectorSpecies<Byte> BSPECIES=ByteVector.SPECIES_256;
30 private static final int BWIDTH=BSPECIES.length();
31
32 @SuppressWarnings("restriction")
33 private static final VectorSpecies<Integer> ISPECIES=IntVector.SPECIES_256;
34 private static final int IWIDTH=ISPECIES.length();
35
36 @SuppressWarnings("restriction")
37 private static final VectorSpecies<Short> SSPECIES=ShortVector.SPECIES_256;
38 private static final int SWIDTH=SSPECIES.length();
39
40 @SuppressWarnings("restriction")
41 private static final VectorSpecies<Double> DSPECIES=DoubleVector.SPECIES_256;
42 private static final int DWIDTH=DSPECIES.length();
43
44 @SuppressWarnings("restriction")
45 private static final VectorSpecies<Long> LSPECIES=LongVector.SPECIES_256;
46 private static final int LWIDTH=LSPECIES.length();
47
48 @SuppressWarnings("restriction")
49 /**
50 * Vectorized version of "c+=a[i]*b[i]" where a and b are equal-length arrays.
51 * @param a A vector to multiply.
52 * @param b A vector to multiply.
53 * @return Sum of products of vector elements.
54 */
55 static final float fma(final float[] a, final float[] b){
56 assert(a.length==b.length);
57
58 //Note: FSPECIES=FloatVector.SPECIES_256 and FWIDTH=8
59 final int limit=FSPECIES.loopBound(a.length);
60
61 FloatVector sum=FloatVector.zero(FSPECIES);
62 int i=0;
63 for(; i<limit; i+=FWIDTH) {//SIMD loop
64 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
65 FloatVector vb=FloatVector.fromArray(FSPECIES, b, i);
66 sum=va.fma(vb, sum);
67 }
68 float c=sum.reduceLanes(VectorOperators.ADD);
69 for (; i<a.length; i++) {//Residual scalar loop
70 c+=a[i]*b[i];
71 }
72 return c;
73 }
74
75 @SuppressWarnings("restriction")
76 /**
77 * Vectorized version of "c+=a[i]*b[bSet[i]]" where a and bSet are equal-length arrays,
78 * and bSet stores indices of b, in ascending contiguous blocks of 8.
79 * @param a A vector to multiply.
80 * @param b A vector to multiply.
81 * @return Sum of products of vector elements.
82 */
83 static final float fmaSparse(final float[] a, final float[] b, int[] bSet){
84 assert(a.length==bSet.length);
85 assert(a.length<b.length);//Otherwise should do normal fma
86
87 //Note: FSPECIES=FloatVector.SPECIES_256 and FWIDTH=8
88 final int limit=FSPECIES.loopBound(bSet.length);
89 // assert(FWIDTH==8);
90 // int elements=0;
91
92 FloatVector sum=FloatVector.zero(FSPECIES);
93 int i=0;
94 for(; i<limit; i+=FWIDTH) {//SIMD loop
95 int idx=bSet[i];
96 // elements+=FWIDTH;
97 // assert(idx%8==0) : idx+", "+i+", "+Arrays.toString(bSet);
98 // assert(bSet[i+1]==idx+1);
99 // assert(bSet[i+7]==idx+7);
100 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
101 FloatVector vb=FloatVector.fromArray(FSPECIES, b, idx);
102 sum=va.fma(vb, sum);
103 }
104 float c=sum.reduceLanes(VectorOperators.ADD);
105 for (; i<bSet.length; i++) {//Residual scalar loop
106 // elements++;
107 c+=a[i]*b[bSet[i]];
108 }
109
110 // float c2=0;
111 // for (int j=0; j<bSet.length; j++) {//Verification loop
112 // c2+=a[j]*b[bSet[j]];
113 // }
114 // assert(Tools.absdif(c, c2)<0.0001f) : c+", "+c2;
115 // assert(elements==bSet.length);
116
117 return c;
118 }
119
120 /**
121 * This is here to keep all the vector operations in a single loop,
122 * to prevent going in and out of SIMD mode too often... hopefully.
123 * ~20% measured speed increase compared to calling fma() for ScoreSequence.
124 */
125 @SuppressWarnings("restriction")
126 public static void feedForward(final Cell[] layer, final float[] b) {
127 assert(false) : "This was giving incorrect results for nets made made with simd=f and vice versa. Needs validation.";
128 final int limit=FSPECIES.loopBound(b.length);
129
130 for(int cnum=0; cnum<layer.length; cnum++) {
131 final Cell cell=layer[cnum];
132 final float[] a=cell.weights;
133 FloatVector sum=FloatVector.zero(FSPECIES);
134 for(int i=0; i<limit; i+=FWIDTH) {//SIMD loop
135 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
136 FloatVector vb=FloatVector.fromArray(FSPECIES, b, i);
137 sum=va.fma(vb, sum);
138 }
139 cell.sum=sum.reduceLanes(VectorOperators.ADD);
140 }
141
142 for(int cnum=0; cnum<layer.length; cnum++) {
143 final Cell cell=layer[cnum];
144 final float[] a=cell.weights;
145 float residual=cell.bias;
146 for (int i=limit+FWIDTH; i<a.length; i++) {//Residual scalar loop
147 residual+=a[i]*b[i];
148 }
149 cell.sum+=residual;
150 final float v=(float)cell.activation(cell.sum);
151 cell.setValue(v);
152 }
153 }
154
155 /**
156 * This is here to keep all the vector operations in a single loop,
157 * to prevent going in and out of SIMD mode too often... hopefully.
158 * ~20% measured speed increase compared to calling fma() for Train.
159 */
160 public static void backPropFma(Cell[] layer, float[] a, float[][] bb) {
161 final int limit=FSPECIES.loopBound(a.length);
162
163 for(int cnum=0; cnum<layer.length; cnum++) {
164 Cell cell=layer[cnum];
165 float[] b=bb[cnum];
166 FloatVector sum=FloatVector.zero(FSPECIES);
167 for(int i=0; i<limit; i+=FWIDTH) {//SIMD loop
168 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
169 FloatVector vb=FloatVector.fromArray(FSPECIES, b, i);
170 sum=va.fma(vb, sum);
171 }
172 cell.eTotalOverOut=sum.reduceLanes(VectorOperators.ADD);
173 }
174
175 if(limit+FWIDTH>=a.length) {return;}//Shortcut when length is divisible by 8.
176
177 for(int cnum=0; cnum<layer.length; cnum++) {
178 Cell cell=layer[cnum];
179 float[] b=bb[cnum];
180 float residual=0;
181 for (int i=limit+FWIDTH; i<a.length; i++) {//Residual scalar loop
182 residual+=a[i]*b[i];
183 }
184 cell.eTotalOverOut+=residual;
185 }
186 }
187
188 /**
189 * Performs "a+=incr" where a and incr are equal-length arrays.
190 * @param a A vector to increment.
191 * @param b Increment amount.
192 */
193 @SuppressWarnings("restriction")
194 static final void add(final float[] a, final float[] b){
195 //final int width=SPECIES.length();
196 final int limit=FSPECIES.loopBound(a.length);
197 // final int limit=a.length&boundMask;
198
199 int i=0;
200 for(; i<limit; i+=FWIDTH) {//SIMD loop
201 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
202 FloatVector vb=FloatVector.fromArray(FSPECIES, b, i);
203 FloatVector sum=va.add(vb);
204 sum.intoArray(a, i);
205 }
206 for (; i<a.length; i++) {//Residual scalar loop; TODO: replace with vector mask
207 a[i]+=b[i];
208 }
209 }
210
211 /**
212 * Performs "a+=b*mult" where a and b are equal-length arrays.
213 * @param a A vector to increment.
214 * @param b Increment amount.
215 * @param mult Increment multiplier.
216 */
217 @SuppressWarnings("restriction")
218 static final void addProduct(final float[] a, final float[] b, final float mult){
219 //final int width=SPECIES.length();
220 final int limit=FSPECIES.loopBound(a.length);
221 // final int limit=a.length&boundMask;
222
223 int i=0;
224 for(; i<limit; i+=FWIDTH) {//SIMD loop
225 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
226 FloatVector vb=FloatVector.fromArray(FSPECIES, b, i);
227 FloatVector sum=va.add(vb.mul(mult));
228 sum.intoArray(a, i);
229 }
230 for (; i<a.length; i++) {//Residual scalar loop; TODO: replace with vector mask
231 a[i]+=b[i]*mult;
232 }
233 }
234
235 @SuppressWarnings("restriction")
236 static final void addProductSparse(final float[] a, final float[] b, final int[] bSet, final float mult){
237 //final int width=SPECIES.length();
238 final int limit=FSPECIES.loopBound(bSet.length);
239 // final int limit=a.length&boundMask;
240
241 int i=0;
242 for(; i<limit; i+=FWIDTH) {//SIMD loop
243 int idx=bSet[i];
244 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
245 FloatVector vb=FloatVector.fromArray(FSPECIES, b, idx);
246 FloatVector sum=va.add(vb.mul(mult));
247 sum.intoArray(a, i);
248 }
249 for (; i<bSet.length; i++) {//Residual scalar loop; TODO: replace with vector mask
250 a[i]+=b[bSet[i]]*mult;
251 }
252 }
253
254 //a is dest
255 @SuppressWarnings("restriction")
256 static final void copy(final float[] a, final float[] b){
257 final int length=Tools.min(a.length, b.length);
258 //final int width=SPECIES.length();
259 final int limit=FSPECIES.loopBound(length);
260 // final int limit=a.length&boundMask;
261
262 int i=0;
263 for(; i<limit; i+=FWIDTH) {//SIMD loop
264 FloatVector vb=FloatVector.fromArray(FSPECIES, b, i);
265 vb.intoArray(a, i);
266 }
267 for (; i<length; i++) {//Residual scalar loop; TODO: replace with vector mask
268 a[i]=b[i];
269 }
270 }
271
272 /** Returns number of matches */
273 @SuppressWarnings("restriction")
274 static final int countMatches(final byte[] s1, final byte[] s2, int a1, int b1, int a2, int b2){
275 final int length=b2-a2+1;
276 final int limit0=BSPECIES.loopBound(length);
277 final int limit=a2+limit0;
278
279 int i=a1, j=a2;
280 int matches=0;
281 for(; j<limit; i+=BWIDTH, j+=BWIDTH) {//SIMD loop
282 ByteVector v1=ByteVector.fromArray(BSPECIES, s1, i);
283 ByteVector v2=ByteVector.fromArray(BSPECIES, s2, j);
284 VectorMask<Byte> x=v1.eq(v2);
285 matches+=x.trueCount();//This might be slow, or might not
286 }
287 for(; j<=b2; i++, j++) {
288 final byte x=s1[i], y=s2[j];
289 final int m=((x==y) ? 1 : 0);
290 matches+=m;
291 }
292 return matches;
293 }
294
295 /** Returns index of symbol */
296 @SuppressWarnings("restriction")
297 static final int find(final byte[] a, final byte symbol, final int from, final int to){//15% Slower than scalar code, at least for ByteFile1
298 final int length=to-from;//Intentionally exclusive
299 final int limit0=BSPECIES.loopBound(length);
300 final int limit=from+limit0;
301
302 int pos=from;
303 for(; pos<limit; pos+=BWIDTH) {//SIMD loop
304 ByteVector v=ByteVector.fromArray(BSPECIES, a, pos);
305 VectorMask<Byte> x=v.eq(symbol);
306 int t=x.firstTrue();
307 if(t<BWIDTH) {return pos+t;}
308 // if(x.anyTrue()) {break;}
309 }
310 while(pos<to && a[pos]!=symbol){pos++;}
311 return pos;
312 }
313
314 @SuppressWarnings("restriction")
315 /**
316 * Sums the array.
317 * @param a A vector.
318 * @return The sum.
319 */
320 static final float sum(final float[] a, final int from, final int to){
321 final int length=to-from+1;//Intentionally inclusive
322 final int limit0=FSPECIES.loopBound(length);
323 final int limit=from+limit0;
324
325 FloatVector sum=FloatVector.zero(FSPECIES);
326 int i=from;
327 for(; i<limit; i+=FWIDTH) {//SIMD loop
328 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
329 sum=sum.add(va);
330 }
331 float c=sum.reduceLanes(VectorOperators.ADD);
332 for (; i<=to; i++) {c+=a[i];}//Residual scalar loop
333 return c;
334 }
335
336 @SuppressWarnings("restriction")
337 /**
338 * Sums the array.
339 * @param a A vector.
340 * @return The sum.
341 */
342 static final long sum(final long[] a, final int from, final int to){
343 final int length=to-from+1;
344 final int limit0=LSPECIES.loopBound(length);
345 final int limit=from+limit0;
346
347 LongVector sum=LongVector.zero(LSPECIES);
348 int i=from;
349 for(; i<limit; i+=LWIDTH) {//SIMD loop
350 LongVector va=LongVector.fromArray(LSPECIES, a, i);
351 sum=sum.add(va);
352 }
353 long c=sum.reduceLanes(VectorOperators.ADD);
354 for (; i<=to; i++) {c+=a[i];}//Residual scalar loop
355 return c;
356 }
357
358 @SuppressWarnings("restriction")
359 /**
360 * Sums the array.
361 * @param a A vector.
362 * @return The sum.
363 */
364 static final long sum(final int[] a, final int from, final int to){//Tested as 1.5x scalar speed
365 final int length=to-from+1;
366 final int limit0=ISPECIES.loopBound(length);
367 final int limit=from+limit0;
368
369 int i=from;
370 long c=0;
371 for(; i<limit; i+=IWIDTH) {//SIMD loop
372 IntVector va=IntVector.fromArray(ISPECIES, a, i);
373 c+=va.reduceLanesToLong(VectorOperators.ADD);//This is probably slow
374 }
375 for (; i<=to; i++) {c+=a[i];}//Residual scalar loop
376 return c;
377 }
378
379 @SuppressWarnings("restriction")
380 /**
381 * Sums the array.
382 * @param a A vector.
383 * @return The sum.
384 */
385 static final long sum(final byte[] a, final int from, final int to){//Tested as 4x scalar speed
386 //TODO: Test speed.
387 final int length=to-from+1;
388 final int limit0=BSPECIES.loopBound(length);
389 final int limit=from+limit0;
390
391 int i=from;
392 long c=0;
393 for(; i<limit; i+=BWIDTH) {//SIMD loop
394 ByteVector va=ByteVector.fromArray(BSPECIES, a, i);
395 c+=va.reduceLanesToLong(VectorOperators.ADD);
396 }
397 for (; i<=to; i++) {c+=a[i];}//Residual scalar loop
398 return c;
399 }
400
401 @SuppressWarnings("restriction")
402 /**
403 * Sums the array.
404 * @param a A vector.
405 * @return The sum.
406 */
407 static final double sum(final double[] a, final int from, final int to){
408 final int length=to-from+1;
409 final int limit0=DSPECIES.loopBound(length);
410 final int limit=from+limit0;
411
412 DoubleVector sum=DoubleVector.zero(DSPECIES);
413 int i=from;
414 for(; i<limit; i+=DWIDTH) {//SIMD loop
415 DoubleVector va=DoubleVector.fromArray(DSPECIES, a, i);
416 sum=sum.add(va);
417 }
418 double c=sum.reduceLanes(VectorOperators.ADD);
419 for (; i<=to; i++) {c+=a[i];}//Residual scalar loop
420 return c;
421 }
422
423 @SuppressWarnings("restriction")
424 /**
425 * Finds the maximum value.
426 * @param a A vector.
427 * @return The max.
428 */
429 static final int max(final int[] a, final int from, final int to){//Tested as 5x scalar speed
430 final int length=to-from+1;
431 final int limit0=ISPECIES.loopBound(length);
432 final int limit=from+limit0;
433
434 int i=from;
435 IntVector max=IntVector.broadcast(ISPECIES, a[from]);
436 for(; i<limit; i+=IWIDTH) {//SIMD loop
437 IntVector va=IntVector.fromArray(ISPECIES, a, i);
438 max=max.max(va);
439 }
440 int c=max.reduceLanes(VectorOperators.MAX);
441 for (; i<=to; i++) {//Residual scalar loop
442 final int x=a[i];
443 c=(x>c ? x : c);
444 }
445 return c;
446 }
447
448 @SuppressWarnings("restriction")
449 /**
450 * Finds the maximum value.
451 * @param a A vector.
452 * @return The max.
453 */
454 static final long max(final long[] a, final int from, final int to){
455 final int length=to-from+1;
456 final int limit0=LSPECIES.loopBound(length);
457 final int limit=from+limit0;
458
459 int i=from;
460 LongVector max=LongVector.broadcast(LSPECIES, a[from]);
461 for(; i<limit; i+=LWIDTH) {//SIMD loop
462 LongVector va=LongVector.fromArray(LSPECIES, a, i);
463 max=max.max(va);
464 }
465 long c=max.reduceLanes(VectorOperators.MAX);
466 for (; i<=to; i++) {//Residual scalar loop
467 final long x=a[i];
468 c=(x>c ? x : c);
469 }
470 return c;
471 }
472
473 @SuppressWarnings("restriction")
474 /**
475 * Finds the maximum value.
476 * @param a A vector.
477 * @return The max.
478 */
479 static final float max(final float[] a, final int from, final int to){
480 final int length=to-from+1;
481 final int limit0=FSPECIES.loopBound(length);
482 final int limit=from+limit0;
483
484 int i=from;
485 FloatVector max=FloatVector.broadcast(FSPECIES, a[from]);
486 for(; i<limit; i+=FWIDTH) {//SIMD loop
487 FloatVector va=FloatVector.fromArray(FSPECIES, a, i);
488 max=max.max(va);
489 }
490 float c=max.reduceLanes(VectorOperators.MAX);
491 for (; i<=to; i++) {//Residual scalar loop
492 final float x=a[i];
493 c=(x>c ? x : c);
494 }
495 return c;
496 }
497
498
499 }