Debian Codegraph @

current/shared/Vector.java

raw symbols SCIP
1 package shared;
2
3 import ml.Cell;
4
5 /**
6 * Protects normal classes from seeing SIMD in case it doesn't compile or is absent.
7 * ...in theory.
8 * @author Brian Bushnell
9 * @date Sep 12, 2013
10 *
11 */
12 public final class Vector {
13
14 public static void main(String[] args) {
15 int[] array=new int[19999];
16 for(int i=0; i<array.length; i++) {
17 array[i]=(byte)(i&127);
18 }
19 Timer t=new Timer();
20
21 int loops=80000;
22
23 long sum=0, sum2=0, max=0;
24 for(int outer=0; outer<4; outer++) {
25 t.start();
26 sum=0; sum2=0;
27 Shared.SIMD=false;
28 for(int i=0; i<loops; i++) {
29 sum=sum(array);
30 sum2+=sum;
31 }
32 System.err.println(sum2);
33 t.stop("Scalar: ");
34
35 t.start();
36 sum=0; sum2=0;
37 Shared.SIMD=true;
38 for(int i=0; i<loops; i++) {
39 sum=sum(array);
40 sum2+=sum;
41 }
42 System.err.println(sum2);
43 t.stop("Vector: ");
44 }
45
46 for(int outer=0; outer<4; outer++) {
47 t.start();
48 max=0; sum=0; sum2=0;
49 Shared.SIMD=false;
50 for(int i=0; i<loops; i++) {
51 max=max(array);
52 sum2+=max;
53 }
54 System.err.println(max+", "+sum2);
55 t.stop("Scalar: ");
56
57 t.start();
58 max=0; sum=0; sum2=0;
59 Shared.SIMD=true;
60 for(int i=0; i<loops; i++) {
61 max=max(array);
62 sum2+=max;
63 }
64 System.err.println(max+", "+sum2);
65 t.stop("Vector: ");
66 }
67 }
68
69 /**
70 * Returns "c+=a[i]*b[i]" where a and b are equal-length arrays.
71 * @param a A vector to multiply.
72 * @param b A vector to multiply.
73 * @return Sum of products of vector elements.
74 */
75 public static final float fma(final float[] a, final float[] b){
76 assert(a.length==b.length);
77 if(Shared.SIMD && a.length>=MINLEN32) {return SIMD.fma(a, b);}
78 float c=0;
79 for(int i=0; i<a.length; i++) {c+=a[i]*b[i];}
80 return c;
81 }
82
83 /**
84 * Returns "c+=a[i]*b[bSet[i]]".
85 * @param a A vector to multiply.
86 * @param b A vector to multiply.
87 * @param bSet Subset of B's indices.
88 * @param blockSize bSet should be in sets of consecutive indices of this length,
89 * for example, blockSize=8 would allow AVX256 vector operations.
90 * @return Sum of products of vector elements.
91 */
92 public static final float fma(final float[] a, final float[] b, final int[] bSet,
93 final int blockSize, boolean allowSimd){
94 assert(a.length==bSet.length);
95 if(Shared.SIMD && a.length>=MINLEN32 && a.length==b.length) {return SIMD.fma(a, b);}
96 if(Shared.SIMD && a.length>=MINLEN32 && allowSimd && ((blockSize&7)==0)) {//This ensures length-8 blocks
97 return SIMD.fmaSparse(a, b, bSet);
98 }
99 float c=0;
100 for(int i=0; i<a.length; i++) {c+=a[i]*b[bSet[i]];}
101 return c;
102 }
103
104 public static final void feedForward(Cell[] layer, float[] valuesIn){
105 // assert(layer.length==valuesIn.length);
106 if(Shared.SIMD && valuesIn.length>=MINLEN32) {
107 SIMD.feedForward(layer, valuesIn);
108 return;
109 }
110
111 for(int cnum=0; cnum<layer.length; cnum++) {
112 Cell c=layer[cnum];
113 float[] weights=c.weights;
114 float sum=c.bias;
115 assert(valuesIn.length==weights.length) : valuesIn.length+", "+weights.length;
116 sum+=Vector.fma(valuesIn, weights);
117 c.sum=sum;
118 final float v=(float)c.activation(sum);
119 c.setValue(v);
120 }
121 }
122
123 public static final void feedForwardDense(Cell[] layer, float[] valuesIn){
124 // assert(layer.length==valuesIn.length);
125 if(false && Shared.SIMD && valuesIn.length>=MINLEN32) {//Discovered anomaly here; very different results
126 SIMD.feedForward(layer, valuesIn);
127 return;
128 }
129
130 for(int cnum=0; cnum<layer.length; cnum++) {
131 Cell c=layer[cnum];
132 float[] weights=c.weights;
133 float sum=c.bias;
134 assert(valuesIn.length==weights.length) : valuesIn.length+", "+weights.length;
135 sum+=Vector.fma(valuesIn, weights);
136 c.sum=sum;
137 final float v=(float)c.activation(sum);
138 c.setValue(v);
139 }
140 }
141
142 public static void backPropFma(Cell[] layer, float[] eOverNetNext, float[][] weightsOutLnum) {
143 if(Shared.SIMD && eOverNetNext.length>=MINLEN32) {
144 SIMD.backPropFma(layer, eOverNetNext, weightsOutLnum);
145 return;
146 }
147 for(int i=0; i<layer.length; i++){
148 Cell cell=layer[i];
149 cell.eTotalOverOut=Vector.fma(weightsOutLnum[i], eOverNetNext);
150 }
151 }
152
153 /**
154 * Performs "a+=incr" where a and incr are equal-length arrays.
155 * @param a A vector to increment.
156 * @param incr Increment amount.
157 */
158 public static final void add(final float[] a, final float[] incr){
159 assert(a.length==incr.length);
160 if(Shared.SIMD && a.length>=MINLEN32) {SIMD.add(a, incr); return;}
161 for(int i=0; i<a.length; i++) {a[i]+=incr[i];}
162 }
163
164 /**
165 * Performs "a[i]+=b[i]*mult" where a and b are equal-length arrays.
166 * @param a A vector to increment.
167 * @param b Increment amount.
168 * @param mult Increment multiplier.
169 */
170 public static final void addProduct(final float[] a, final float[] b, final float mult){
171 assert(a.length==b.length);
172 if(Shared.SIMD && a.length>=MINLEN32) {SIMD.addProduct(a, b, mult); return;}
173 for(int i=0; i<a.length; i++) {a[i]+=b[i]*mult;}
174 }
175
176 /**
177 * Performs "a[i]+=b[bSet[i]]*mult".
178 * @param a A vector to increment.
179 * @param b Increment amount.
180 * @param bSet Subset of B's indices.
181 * @param mult Increment multiplier.
182 */
183 public static final void addProduct(final float[] a, final float[] b, int[] bSet, final float mult, int blockSize){
184 assert(a.length==bSet.length);
185 if(Shared.SIMD && a.length>=MINLEN32 && a.length==b.length) {SIMD.addProduct(a, b, mult); return;}
186 if(Shared.SIMD && a.length>=MINLEN32 && SIMD_MULT_SPARSE && ((blockSize&7)==0)) {SIMD.addProductSparse(a, b, bSet, mult); return;}
187 for(int i=0; i<a.length; i++) {a[i]+=b[bSet[i]]*mult;}
188 }
189
190 public static void copy(float[] dest, float[] source) {
191 // assert(a.length==b.length);
192 if(SIMDCOPY && Shared.SIMD && dest.length>=MINLEN32) {SIMD.copy(dest, source); return;}
193 for(int i=0, max=Tools.min(dest.length, source.length); i<max; i++) {dest[i]=source[i];}
194 }
195
196 public static void copy(int[] dest, int[] source) {
197 // assert(a.length==b.length);
198 // if(SIMDCOPY && Shared.SIMD && dest.length>=MINLEN32) {SIMD.copy(dest, source); return;}//TODO
199 for(int i=0, max=Tools.min(dest.length, source.length); i<max; i++) {dest[i]=source[i];}
200 }
201
202 /** Returns number of matches */
203 public static final int countMatches(final byte[] s1, final byte[] s2, int a1, int b1, int a2, int b2){
204 assert(b1-a1==b2-a2) : a1+"-"+b1+", "+a2+"-"+b2+", len="+s1.length+", "+(b1-a1)+"!="+(b2-a2);
205 if(Shared.SIMD && b1-a1+1>=MINLEN8) {return SIMD.countMatches(s1, s2, a1, b1, a2, b2);}
206 int matches=0;
207 for(int i=a1, j=a2; j<=b2; i++, j++) {
208 final byte x=s1[i], y=s2[j];
209 final int m=((x==y) ? 1 : 0);//Does not take into account capitalization or undefined bases
210 matches+=m;
211 }
212 assert(matches>=0 && matches<=b1-a1+1);
213 return matches;
214 }
215
216 public static final int countMismatches(final byte[] s1, final byte[] s2, int a1, int b1, int a2, int b2){
217 return (b1-a1+1)-countMatches(s1, s2, a1, b1, a2, b2);
218 }
219
220 public static final int find(final byte[] a, final byte symbol, final int from, final int to){
221 // if(Shared.SIMD && to-from>=MINLEN8) {return SIMD.find(a, symbol, from, to);}
222 int len=from;
223 while(len<to && a[len]!=symbol){len++;}
224 return len;
225 }
226
227
228 public static double sum(float[] array){//
229 if(array==null){return 0;}
230 if(Shared.SIMD && array.length>=MINLEN32) {return SIMD.sum(array, 0, array.length-1);}
231 double x=0;
232 for(float y : array){x+=y;}
233 return x;
234 }
235
236 public static long sum(byte[] array){
237 if(array==null){return 0;}
238 if(Shared.SIMD && array.length>=MINLEN8) {return SIMD.sum(array, 0, array.length-1);}
239 long x=0;
240 for(byte y : array){x+=y;}
241 return x;
242 }
243
244 public static long sum(char[] array){
245 if(array==null){return 0;}
246 // if(Shared.SIMD && array.length>=SMINLEN) {return SIMD.sum(array, 0, array.length-1);}
247 long x=0;
248 for(char y : array){x+=y;}
249 return x;
250 }
251
252 public static long sum(short[] array){
253 if(array==null){return 0;}
254 // if(Shared.SIMD && array.length>=SMINLEN) {return SIMD.sum(array, 0, array.length-1);}
255 long x=0;
256 for(short y : array){x+=y;}
257 return x;
258 }
259
260 public static long sum(int[] array){
261 if(array==null){return 0;}
262 if(Shared.SIMD && array.length>=MINLEN32) {return SIMD.sum(array, 0, array.length-1);}
263 long x=0;
264 for(int y : array){x+=y;}
265 return x;
266 }
267
268 public static double sum(double[] array){
269 if(array==null){return 0;}
270 if(Shared.SIMD && array.length>=MINLEN64) {return SIMD.sum(array, 0, array.length-1);}
271 double x=0;
272 for(double y : array){x+=y;}
273 return x;
274 }
275
276 public static long sum(long[] array){
277 if(array==null){return 0;}
278 if(Shared.SIMD && array.length>=MINLEN64) {return SIMD.sum(array, 0, array.length-1);}
279 long x=0;
280 for(long y : array){x+=y;}
281 return x;
282 }
283
284 public static long sum(int[] array, int from, int to){
285 if(array==null){return 0;}
286 if(Shared.SIMD && array.length>=MINLEN32) {return SIMD.sum(array, 0, array.length-1);}
287 long x=0;
288 for(int i=from; i<=to; i++){x+=array[i];}
289 return x;
290 }
291
292 public static long sum(long[] array, int from, int to){
293 if(array==null){return 0;}
294 if(Shared.SIMD && array.length>=MINLEN64) {return SIMD.sum(array, 0, array.length-1);}
295 long x=0;
296 for(int i=from; i<=to; i++){x+=array[i];}
297 return x;
298 }
299
300 public static final int max(int[] array){
301 if(array==null){return 0;}
302 if(Shared.SIMD && array.length>=MINLEN32) {return SIMD.max(array, 0, array.length-1);}
303 int max=array[0];
304 for(int i=1; i<array.length; i++){
305 int x=array[i];
306 max=(x>max ? x : max);
307 }
308 return max;
309 }
310
311 public static final float max(float[] array){
312 if(array==null){return 0;}
313 if(Shared.SIMD && array.length>=MINLEN32) {return SIMD.max(array, 0, array.length-1);}
314 float max=array[0];
315 for(int i=1; i<array.length; i++){
316 float x=array[i];
317 max=(x>max ? x : max);
318 }
319 return max;
320 }
321
322 public static final long max(long[] array){
323 if(array==null){return 0;}
324 if(Shared.SIMD && array.length>=MINLEN32) {return SIMD.max(array, 0, array.length-1);}
325 long max=array[0];
326 for(int i=1; i<array.length; i++){
327 long x=array[i];
328 max=(x>max ? x : max);
329 }
330 return max;
331 }
332
333 public static final int MINLEN8=32;
334 public static final int MINLEN16=16;
335 public static final int MINLEN32=16;//16 or 32 are optimal; 0, 24, and 48 are worse.
336 public static final int MINLEN64=8;
337 public static boolean SIMDCOPY=false;//Does not seem to affect speed, but could increase power usage.
338 public static boolean SIMD_MULT_SPARSE=true;//Grants a speedup, and same results (but currently broken at ebs=1)
339 public static boolean SIMD_FMA_SPARSE=true;//Grants a speedup, slightly different results
340 }