interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / vec1 / tool / vec1_algo_test.c

787 lines · 22663 bytes Raw Blame History
1
2#include <stdlib.h>
3#include <stdio.h>
4#include <assert.h>
5#include <string.h>
6#include <stdint.h>
7#include <math.h>
8
9#include "vec1.c"
10
11
12#ifdef VEC1_HAVE_NEON
13/*
14** Run nIter iterations of a loop containing N independent, back-to-back
15** FMA chains (N float32x4_t accumulators, each updated once per
16** iteration) and return the elapsed time on vec1HardwareTimer().
17**
18** N is a literal compile-time constant at every call site (see
19** MAKE_FMA_TEST below), so the "for(c...)" loop below is unrolled by
20** the compiler into straight-line code - there is no loop-control
21** instruction diluting the measurement, and for N==1 this is a single
22** dependent chain (true FMA latency); for larger N it is N independent
23** chains (FMA throughput, once N is large enough to hide the latency).
24*/
25#define MAKE_FMA_TEST(N) \
26static u64 vec1FmaTest##N(int nIter){ \
27 float32x4_t acc[N]; \
28 float32x4_t a = vdupq_n_f32(1.000001f); \
29 float32x4_t b = vdupq_n_f32(0.999999f); \
30 int c, r; \
31 u64 t; \
32 for(c=0; c<N; c++) acc[c] = vdupq_n_f32(1.0f + 0.001f*(float)c); \
33 t = vec1HardwareTimer(); \
34 for(r=0; r<nIter; r++){ \
35 for(c=0; c<N; c++) acc[c] = vfmaq_f32(acc[c], a, b); \
36 } \
37 t = vec1HardwareTimer() - t; \
38 /* Force the result to be used, so the compiler can't prove the */ \
39 /* whole loop is dead and delete it. */ \
40 { \
41 float s = 0.0f; \
42 for(c=0; c<N; c++) s += vaddvq_f32(acc[c]); \
43 if( s!=s ) fprintf(stderr, "unreachable: %f\n", (double)s); \
44 } \
45 return t; \
46}
47MAKE_FMA_TEST(1)
48MAKE_FMA_TEST(2)
49MAKE_FMA_TEST(3)
50MAKE_FMA_TEST(4)
51MAKE_FMA_TEST(6)
52MAKE_FMA_TEST(8)
53MAKE_FMA_TEST(10)
54MAKE_FMA_TEST(12)
55MAKE_FMA_TEST(16)
56MAKE_FMA_TEST(20)
57MAKE_FMA_TEST(24)
58MAKE_FMA_TEST(28)
59MAKE_FMA_TEST(32)
60#undef MAKE_FMA_TEST
61
62/*
63** Measure NEON FMA latency and throughput on this host and use
64** Little's Law (accumulators_needed = latency * throughput) to derive
65** the minimum number of independent accumulator chains required to
66** keep the FMA pipeline(s) saturated.
67*/
68static void vec1FmaBench(void){
69 const int nIter = 200*1000*1000;
70 struct { int n; u64 t; } a[13];
71 int i;
72 double latency; /* ticks per FMA, N==1 (dependent chain) */
73 double bestRate = 0.0; /* FMAs per tick, best N tested */
74 int nOptimal;
75
76 a[0].n=1; a[0].t = vec1FmaTest1(nIter);
77 a[1].n=2; a[1].t = vec1FmaTest2(nIter);
78 a[2].n=3; a[2].t = vec1FmaTest3(nIter);
79 a[3].n=4; a[3].t = vec1FmaTest4(nIter);
80 a[4].n=6; a[4].t = vec1FmaTest6(nIter);
81 a[5].n=8; a[5].t = vec1FmaTest8(nIter);
82 a[6].n=10; a[6].t = vec1FmaTest10(nIter);
83 a[7].n=12; a[7].t = vec1FmaTest12(nIter);
84 a[8].n=16; a[8].t = vec1FmaTest16(nIter);
85 a[9].n=20; a[9].t = vec1FmaTest20(nIter);
86 a[10].n=24; a[10].t = vec1FmaTest24(nIter);
87 a[11].n=28; a[11].t = vec1FmaTest28(nIter);
88 a[12].n=32; a[12].t = vec1FmaTest32(nIter);
89
90 printf("%-12s %-16s %-16s %-10s\n", "nChains", "ticks/iter", "ticks/FMA", "FMA/tick");
91 for(i=0; i<(int)(sizeof(a)/sizeof(a[0])); i++){
92 double ticksPerIter = (double)a[i].t / nIter;
93 double ticksPerFma = ticksPerIter / a[i].n;
94 double fmaPerTick = 1.0 / ticksPerFma;
95 printf("%-12d %-16.3f %-16.4f %-10.3f\n",
96 a[i].n, ticksPerIter, ticksPerFma, fmaPerTick);
97 if( i==0 ) latency = ticksPerFma;
98 if( fmaPerTick>bestRate ) bestRate = fmaPerTick;
99 }
100
101 nOptimal = (int)ceil(latency * bestRate);
102
103 printf("\n");
104 printf("FMA latency (N=1, dependent chain): %.3f ticks\n", latency);
105 printf("FMA throughput (best of N tested): %.3f FMA/tick\n", bestRate);
106 printf("Little's Law: accumulators needed = latency * throughput\n");
107 printf(" = %.3f * %.3f = %.2f -> use %d independent accumulators\n",
108 latency, bestRate, latency*bestRate, nOptimal);
109}
110#endif /* VEC1_HAVE_NEON */
111
112/*
113** Populate aTrans[] with transposed version of the array of centroids in
114** aCentroid[].
115*/
116static void vec1TransposeCentroids(
117 const float *aCentroid, /* Packed array of centroids */
118 int nElem, /* Number of elements in each vector */
119 int nVector, /* Number of centroid vectors */
120 int nWidth, /* Transpose width */
121 float *aTrans /* Populate this array with transformation */
122){
123 int c, d;
124 float *pOut = aTrans;
125
126 /* nVector must be an integer multiple of nWidth */
127 assert( (nVector % nWidth)==0 );
128
129 for(c=0; c<nVector; c+=nWidth){
130 for(d=0; d<nElem; d++){
131 int ii;
132 for(ii=0; ii<nWidth; ii++){
133 (*pOut++) = aCentroid[ (c+ii)*nElem + d ];
134 }
135 }
136 }
137
138 assert( pOut==&aTrans[nElem * nVector] );
139}
140
141static double vec1RandomFloat(Vec1Random *p) {
142 return (vec1RandomNext(p) >> 8) * (1.0 / 16777216.0); // 24-bit mantissa
143}
144
145static void vec1RandomArray(Vec1Random *p, float *aRnd, int nRnd){
146 int ii;
147 for(ii=0; ii<nRnd; ii++){
148 aRnd[ii] = vec1RandomFloat(p);
149 }
150}
151
152static void usage(const char *zExec){
153 fprintf(stderr,
154 "usage: %s NELEM NCENTROID NTRAIN NREPEAT ALGORITHM\n", zExec
155 );
156 exit(-1);
157}
158
159static void xLloydsNaive(
160 int nElem,
161 const float *aCentroid,
162 int nCentroid,
163 const float *aTrain,
164 int nTrain,
165 int *aBest
166){
167 int ii;
168
169 for(ii=0; ii<nTrain; ii++){
170 const float *pVec = &aTrain[ii*nElem];
171 double fBest = INFINITY;
172 int iBest = -1;
173 int jj;
174
175 for(jj=0; jj<nCentroid; jj++){
176 double fNew = vec1L2Dist(pVec, &aCentroid[jj*nElem], nElem);
177 if( fNew<fBest ){
178 iBest = jj;
179 fBest = fNew;
180 }
181 }
182 aBest[ii] = iBest;
183 }
184}
185
186static void xLloydsStride(
187 int nElem,
188 const float *aCentroid,
189 int nCentroid,
190 const float *aTrain,
191 int nTrain,
192 int *aBest
193){
194#define VEC1_LLOYDS_STRIDE 8
195 const int nStride = VEC1_LLOYDS_STRIDE;
196 int ii;
197
198 for(ii=0; ii<nTrain; ii+=nStride){
199 float aDist[VEC1_LLOYDS_STRIDE];
200 int aRes[VEC1_LLOYDS_STRIDE];
201 int cc;
202 int jj;
203
204 for(cc=0; cc<nStride; cc++) aDist[cc] = INFINITY;
205
206 for(jj=0; jj<nCentroid; jj++){
207 const float *pCent = &aCentroid[jj * nElem];
208 for(cc=0; cc<nStride; cc++){
209 float fDist = vec1L2Dist(pCent, &aTrain[(cc+ii)*nElem], nElem);
210 if( fDist<aDist[cc] ){
211 aDist[cc] = fDist;
212 aRes[cc] = jj;
213 }
214 }
215 }
216
217 memcpy(&aBest[ii], aRes, sizeof(aRes));
218 }
219}
220
221#if defined(VEC1_HAVE_NEON)
222# define TEST_BMT_NVEC 8
223
224/*
225** NEON version of vec1BestMatchTransposeN(). Array aCentNT[] contains the
226** centroids transposed in blocks of TEST_BMT_NVEC centroids (the same
227** width as the number of vectors - see xLloydsTransposed2()). Each block
228** of TEST_BMT_NVEC centroids occupies TEST_BMT_NREG float32x4_t registers
229** per training vector.
230*/
231# define TEST_BMT_NREG (TEST_BMT_NVEC/4)
232
233static void vec1BestMatchTransposeNNEON(
234 int nElem,
235 const float *aCent,
236 const float *aCentNT,
237 int nCent,
238 const float *aVec[TEST_BMT_NVEC],
239 int aBestOut[TEST_BMT_NVEC],
240 double *pfTotalOut
241){
242 const int nWidth = TEST_BMT_NVEC;
243 float aDist[TEST_BMT_NVEC];
244 int aBest[TEST_BMT_NVEC];
245 int vv;
246 int iCent;
247 const float *pTrans = aCentNT;
248
249 /* All the code below assumes TEST_BMT_NVEC is a multiple of 4 */
250 assert( (TEST_BMT_NVEC % 4)==0 );
251
252 for(vv=0; vv<TEST_BMT_NVEC; vv++){
253 aDist[vv] = INFINITY;
254 aBest[vv] = 0;
255 }
256
257 for(iCent=0; iCent<=(nCent-nWidth); iCent+=nWidth){
258 float32x4_t aAcc[TEST_BMT_NVEC][TEST_BMT_NREG];
259 int d;
260 int rr; /* For looping 0..TEST_BMT_NREG-1 */
261
262 for(vv=0; vv<TEST_BMT_NVEC; vv++){
263 for(rr=0; rr<TEST_BMT_NREG; rr++){
264 aAcc[vv][rr] = vdupq_n_f32(0.0f);
265 }
266 }
267
268 for(d=0; d<nElem; d++){
269 float32x4_t cval[TEST_BMT_NREG];
270 for(rr=0; rr<TEST_BMT_NREG; rr++){
271 cval[rr] = vld1q_f32(&pTrans[rr*4]);
272 }
273 for(vv=0; vv<TEST_BMT_NVEC; vv++){
274 float32x4_t sval = vld1q_dup_f32(&aVec[vv][d]);
275 for(rr=0; rr<TEST_BMT_NREG; rr++){
276 float32x4_t diff = vsubq_f32(cval[rr], sval);
277 aAcc[vv][rr] = vfmaq_f32(aAcc[vv][rr], diff, diff);
278 }
279 }
280 pTrans += nWidth;
281 }
282
283 /* At this point, the TEST_BMT_NREG registers in aAcc[vv] contain the
284 ** distances from training vector vv to centroids iCent to
285 ** (iCent + TEST_BMT_NVEC - 1). Populate the aDist[] and aBest[] scalar
286 ** arrays accordingly. */
287 for(vv=0; vv<TEST_BMT_NVEC; vv++){
288 float32x4_t vMin = aAcc[vv][0];
289 for(rr=1; rr<TEST_BMT_NREG; rr++){
290 vMin = vminq_f32(vMin, aAcc[vv][rr]);
291 }
292 if( vminvq_f32(vMin)<aDist[vv] ){
293 /* This branch runs if aAcc[vv] contains one of more values less
294 ** than scalar value aDist[vv]. */
295 int is;
296 float aScalar[TEST_BMT_NVEC];
297 for(rr=0; rr<TEST_BMT_NREG; rr++){
298 vst1q_f32(&aScalar[rr*4], aAcc[vv][rr]);
299 }
300 for(is=0; is<TEST_BMT_NVEC; is++){
301 if( aScalar[is]<aDist[vv] ){
302 aDist[vv] = aScalar[is];
303 aBest[vv] = is + iCent;
304 }
305 }
306 }
307 }
308 }
309
310 for( ; iCent<nCent; iCent++){
311 const float *cent = &aCent[iCent*nElem];
312 for(vv=0; vv<TEST_BMT_NVEC; vv++){
313 float dist = vec1L2Dist(aVec[vv], cent, nElem);
314 if( dist<aDist[vv] ){
315 aDist[vv] = dist;
316 aBest[vv] = iCent;
317 }
318 }
319 }
320
321 memcpy(aBestOut, aBest, sizeof(aBest));
322 if( pfTotalOut ){
323 double total = 0.0;
324 for(vv=0; vv<TEST_BMT_NVEC; vv++){
325 total += aDist[vv];
326 }
327 *pfTotalOut += total;
328 }
329}
330# define vec1BestMatchTransposeN vec1BestMatchTransposeNNEON
331
332#elif defined(VEC1_HAVE_AVX2)
333# define TEST_BMT_NVEC VEC1_MULTIMATCH_NVEC
334#endif
335
336static void xLloydsTransposed2(
337 int nElem,
338 const float *aCentroid,
339 int nCentroid,
340 const float *aTrain,
341 int nTrain,
342 int *aBest
343){
344 float *aTrans = 0;
345 int ii;
346
347 aTrans = (float*)malloc(nElem * nCentroid * sizeof(float));
348 vec1TransposeCentroids(
349 aCentroid, nElem, nCentroid, TEST_BMT_NVEC, aTrans
350 );
351
352 for(ii=0; ii<nTrain; ii+=TEST_BMT_NVEC){
353 const float *aVec[TEST_BMT_NVEC];
354 int vv;
355 for(vv=0; vv<TEST_BMT_NVEC; vv++){
356 aVec[vv] = &aTrain[(ii+vv)*nElem];
357 }
358
359 vec1BestMatchTransposeN(
360 nElem, aCentroid, aTrans, nCentroid, aVec, &aBest[ii], 0
361 );
362
363 }
364
365 free(aTrans);
366}
367
368static void xLloydsModelTDist(
369 int nElem,
370 const float *aCentroid,
371 int nCentroid,
372 const float *aTrain,
373 int nTrain,
374 int *aBest
375){
376 float *aTrans = 0;
377 int ii;
378
379 aTrans = (float*)malloc(nElem * nCentroid * sizeof(float));
380 vec1TransposeCentroids(
381 aCentroid, nElem, nCentroid, VEC1_TRANSPOSE_WIDTH, aTrans
382 );
383
384 for(ii=0; ii<nTrain; ii++){
385 const float *pTrans = aTrans;
386 float fBestDist = INFINITY;
387 const float *vec = &aTrain[ii*nElem];
388 int K;
389 for(K=0; K<=(nCentroid-VEC1_TRANSPOSE_WIDTH); K+=VEC1_TRANSPOSE_WIDTH){
390 int cc;
391 float aDist[VEC1_TRANSPOSE_WIDTH];
392
393 vec1ModelTDist(pTrans, vec, nElem, aDist);
394 pTrans += (nElem * VEC1_TRANSPOSE_WIDTH);
395
396 for(cc=0; cc<VEC1_TRANSPOSE_WIDTH; cc++){
397 if( aDist[cc]<fBestDist ){
398 aBest[ii] = K+cc;
399 fBestDist = aDist[cc];
400 }
401 }
402 }
403
404 for(; K<nCentroid; K++){
405 float dist = vec1L2Dist(&aCentroid[K*nElem], vec, nElem);
406 if( dist<fBestDist ){
407 fBestDist = dist;
408 aBest[ii] = K;
409 }
410 }
411 }
412
413 free(aTrans);
414}
415
416
417#if 0
418static void xLloydsTransposed(
419 int nElem,
420 const float *aCentroid,
421 int nCentroid,
422 const float *aTrain,
423 int nTrain,
424 int *aBest
425){
426#if !defined(VEC1_HAVE_AVX2) && !defined(VEC1_HAVE_NEON)
427 fprintf(stderr, "must define either VEC1_HAVE_AVX2 or VEC1_HAVE_NEON\n");
428 exit(1);
429#else
430 float *aTrans = 0;
431 int ii;
432
433/*
434#if defined(VEC1_HAVE_AVX2)
435# define VEC1_LLOYDS_NACC 8
436# define VEC1_LLOYDS_WIDTH 8
437#else
438# define VEC1_LLOYDS_NACC 8
439# define VEC1_LLOYDS_WIDTH 4
440#endif
441*/
442
443 aTrans = (float*)malloc(nElem * nCentroid * sizeof(float));
444 vec1TransposeCentroids(aCentroid, nElem, nCentroid, VEC1_NTRANSPOSE_WIDTH, aTrans);
445
446 for(ii=0; ii<nTrain; ii+=VEC1_LLOYDS_NACC){
447 const float *pTrans = aTrans;
448 int cc;
449 int iCent;
450
451 int aRes[VEC1_LLOYDS_NACC];
452 float aDist[VEC1_LLOYDS_NACC];
453
454 /* Initialize the aDist[] array. */
455 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
456 aDist[cc] = INFINITY;
457 }
458
459 for(iCent=0; iCent<nCentroid; iCent+=VEC1_LLOYDS_WIDTH){
460 int d;
461
462#if defined(VEC1_HAVE_AVX2)
463 /* All the AVX2 code below assumes VEC1_LLOYDS_WIDTH==8, so
464 ** assert() that here. */
465 assert( VEC1_LLOYDS_WIDTH==8 );
466
467 __m256 aAcc[VEC1_LLOYDS_NACC];
468 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
469 aAcc[cc] = _mm256_setzero_ps();
470 }
471 for(d=0; d<nElem; d++){
472 __m256 cval = LOADU( pTrans );
473 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
474 __m256 sval = _mm256_set1_ps( aTrain[(ii+cc)*nElem+d] );
475 __m256 diff = _mm256_sub_ps( cval, sval );
476 aAcc[cc] = FMADD(diff, diff, aAcc[cc]);
477 }
478 pTrans += VEC1_LLOYDS_WIDTH;
479 }
480
481 /* At this point, each register in aAcc[] contains the distances
482 ** from the corresponding training vector (vector ii + cc) to
483 ** centroids iCent to (iCent + VEC1_SIMD_WIDTH). Populate the aDist[]
484 ** and aRes[] scalar arrays accordingly. */
485 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
486 __m256 vMin = _mm256_set1_ps(aDist[cc]);
487 __m256 mask = _mm256_cmp_ps(aAcc[cc], vMin, _CMP_LT_OQ);
488 if( _mm256_movemask_ps(mask)!=0 ){
489 /* This branch runs if aAcc[cc] contains one of more values less
490 ** than scalar value aDist[cc]. */
491 int is;
492 float aScalar[VEC1_LLOYDS_WIDTH];
493 _mm256_storeu_ps(aScalar, aAcc[cc]);
494 for(is=0; is<VEC1_LLOYDS_WIDTH; is++){
495 if( aScalar[is]<aDist[cc] ){
496 aDist[cc] = aScalar[is];
497 aRes[cc] = is + iCent;
498 }
499 }
500 }
501 }
502#else
503 /* All the NEON code below assumes VEC1_LLOYDS_WIDTH==4, so
504 ** assert() that here. */
505 assert( VEC1_LLOYDS_WIDTH==4 );
506
507 float32x4_t aAcc[VEC1_LLOYDS_NACC];
508 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
509 aAcc[cc] = vdupq_n_f32(0.0f);
510 }
511 for(d=0; d<nElem; d++){
512 float32x4_t cval = vld1q_f32( pTrans );
513 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
514 float32x4_t sval = vdupq_n_f32( aTrain[(ii+cc)*nElem+d] );
515 float32x4_t diff = vsubq_f32( cval, sval );
516 aAcc[cc] = vfmaq_f32( aAcc[cc], diff, diff );
517 }
518 pTrans += VEC1_LLOYDS_WIDTH;
519 }
520
521 /* Same "did anything improve" check as the AVX2 movemask test,
522 ** using a horizontal max over the comparison mask instead. Any
523 ** lane less-than gives an all-1s (0xFFFFFFFF) mask in that lane,
524 ** so a nonzero horizontal max means at least one lane improved.
525 ** vmaxvq_u32 is aarch64-only (no 32-bit ARMv7 equivalent without
526 ** a pairwise-max fallback). */
527 for(cc=0; cc<VEC1_LLOYDS_NACC; cc++){
528 float32x4_t vMin = vdupq_n_f32(aDist[cc]);
529 uint32x4_t mask = vcltq_f32(aAcc[cc], vMin);
530 if( vmaxvq_u32(mask)!=0 ){
531 int is;
532 float aScalar[VEC1_LLOYDS_WIDTH];
533 vst1q_f32(aScalar, aAcc[cc]);
534 for(is=0; is<VEC1_LLOYDS_WIDTH; is++){
535 if( aScalar[is]<aDist[cc] ){
536 aDist[cc] = aScalar[is];
537 aRes[cc] = is + iCent;
538 }
539 }
540 }
541 }
542#endif
543 }
544
545 /* Scalar array aRes[] now contains the index of the best centroid
546 ** for each of the VEC1_LLOYDS_NACC training vectors being considered
547 ** by this loop. */
548 memcpy(&aBest[ii], aRes, sizeof(aRes));
549 }
550#endif
551}
552#endif
553
554#ifdef VEC1_USE_TRANSPOSITION
555/* If VEC1_USE_TRANSPOSITION is defined in vec1.c, then there is no
556** implementation of vec1BestMatchN() to import. So in this case we
557** create one here. VEC1_USE_TRANSPOSITION is only ever defined on
558** AVX2 builds, so the implementation may assume AVX2. */
559#if !defined(VEC1_HAVE_AVX2)
560# error "A non-AVX2 host with VEC1_USE_TRANSPOSITION defined..."
561#endif
562
563static void vec1BestMatchN(
564 int nElem, /* Number of elements in vectors */
565 const float *aCent, /* nCent packed centroids */
566 int nCent, /* Number of centroids */
567 const float *aVec[VEC1_MULTIMATCH_NVEC],
568 int aBest[VEC1_MULTIMATCH_NVEC],
569 double *pfTotalDist /* IN/OUT: Increment by total distortion */
570){
571 int iCent;
572 int vv;
573 float aBestDist[VEC1_MULTIMATCH_NVEC];
574
575 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
576 aBestDist[vv] = INFINITY;
577 }
578
579 for(iCent=0; iCent<nCent; iCent++){
580 __m256 aAcc[VEC1_MULTIMATCH_NVEC];
581 float aDist[VEC1_MULTIMATCH_NVEC];
582 int d;
583
584 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
585 aAcc[vv] = _mm256_setzero_ps();
586 }
587
588 for(d=0; d<=(nElem-8); d+=8){
589 __m256 cent = LOADU( &aCent[iCent*nElem + d] );
590 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
591 __m256 diff = _mm256_sub_ps(cent, LOADU(&aVec[vv][d]));
592 aAcc[vv] = FMADD(diff, diff, aAcc[vv]);
593 }
594 }
595
596 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
597 HORIZONTAL_SUM(aDist[vv], aAcc[vv]);
598 }
599
600 for(; d<nElem; d++){
601 float cent = aCent[iCent*nElem + d];
602 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
603 float diff = cent - aVec[vv][d];
604 aDist[vv] += (diff * diff);
605 }
606 }
607
608 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
609 if( aDist[vv]<aBestDist[vv] ){
610 aBestDist[vv] = aDist[vv];
611 aBest[vv] = iCent;
612 }
613 }
614 }
615
616 if( pfTotalDist ){
617 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
618 *pfTotalDist += aBestDist[vv];
619 }
620 }
621}
622
623#endif
624
625/*
626** A register-blocked microkernel, GEMM-style.
627*/
628static void xLloydsTiled(
629 int nElem,
630 const float *aCentroid,
631 int nCentroid,
632 const float *aTrain,
633 int nTrain,
634 int *aBest
635){
636 int ii;
637 for(ii=0; ii<=(nTrain-VEC1_MULTIMATCH_NVEC); ii+=VEC1_MULTIMATCH_NVEC){
638 const float *aVec[VEC1_MULTIMATCH_NVEC];
639 int vv;
640 for(vv=0; vv<VEC1_MULTIMATCH_NVEC; vv++){
641 aVec[vv] = &aTrain[(ii+vv) * nElem];
642 }
643
644 vec1BestMatchN(nElem, aCentroid, nCentroid, aVec, &aBest[ii], 0);
645 }
646
647 for(; ii<nTrain; ii++){
648 aBest[ii] = vec1BestMatchSimple(
649 aCentroid, nCentroid, &aTrain[ii*nElem], nElem, 0
650 );
651 }
652}
653
654int main(int argc, char **argv){
655#ifdef VEC1_HAVE_NEON
656 if( argc==2 && strcmp(argv[1], "fmabench")==0 ){
657 vec1FmaBench();
658 return 0;
659 }
660#endif
661 const struct Alg {
662 const char *zName;
663 void (*x)(int, const float*, int, const float*, int, int*);
664 } aAlg[] = {
665 { "naive", xLloydsNaive },
666 { "strided", xLloydsStride },
667 { "transposed", xLloydsTransposed2 },
668 { "tiled", xLloydsTiled },
669 { "modeltdist", xLloydsModelTDist },
670 };
671 /* Command line parameters */
672 int nElem = 0;
673 int nCentroid = 0;
674 int nTrain = 0;
675 int nRepeat = 0;
676
677 float *aCentroid; /* nCentroid centroids, each nElem floats */
678 float *aTrain; /* nTrain training vectors, each nElem f.. */
679 int *aRes; /* Result array */
680
681 u64 t; /* Hardware timer value */
682 int ii; /* Loop counter */
683 void (*xLloyds)(int, const float*, int, const float*, int, int*) = 0;
684
685 Vec1Random rnd;
686 vec1RandomInit(&rnd, vec1HardwareTimer(), 0);
687
688 if( argc!=6 ) usage(argv[0]);
689
690 nElem = atoi( argv[1] );
691 nCentroid = atoi( argv[2] );
692 nTrain = atoi( argv[3] );
693 nRepeat = atoi( argv[4] );
694
695 /* Vector size must be a multiple of 8 */
696 if( nElem<=0 || (nElem%8)!=0 ){
697 fprintf(stderr, "NELEM must be a multiple of 8 greater than 0\n");
698 return 1;
699 }
700
701 /* Number of centroids must be a multiple of 32 */
702 if( nCentroid<=0 || (nCentroid%32)!=0 ){
703 fprintf(stderr, "NCENTROID must be a multiple of 32 greater than 0\n");
704 return 1;
705 }
706
707 /* Number of training vectors must be a multiple of 32 */
708 if( nTrain<=0 || (nTrain%32)!=0 ){
709 fprintf(stderr, "NTRAIN must be a multiple of 32 greater than 0\n");
710 return 1;
711 }
712
713 /* Number of repeats must be greater than 0 */
714 if( nRepeat<=0 ){
715 fprintf(stderr, "NREPEAT must greater than 0\n");
716 return 1;
717 }
718
719 for(ii=0; ii<(int)(sizeof(aAlg)/sizeof(aAlg[0])); ii++){
720 if( strcmp(argv[5], aAlg[ii].zName)==0 ){
721 xLloyds = aAlg[ii].x;
722 }
723 }
724 if( xLloyds==0 ){
725 fprintf(stderr, "no such algorithm: %s\n", argv[5]);
726 return 1;
727 }
728
729 aCentroid = (float*)malloc( nCentroid * nElem * sizeof(float) );
730 aTrain = (float*)malloc( nTrain * nElem * sizeof(float) );
731 aRes = (int*)malloc( nTrain * sizeof(int) );
732
733 vec1RandomArray(&rnd, aCentroid, nCentroid * nElem);
734 vec1RandomArray(&rnd, aTrain, nTrain * nElem);
735
736 t = vec1HardwareTimer();
737
738 for(ii=0; ii<nRepeat; ii++){
739 xLloyds(nElem, aCentroid, nCentroid, aTrain, nTrain, aRes);
740 }
741
742 t = vec1HardwareTimer() - t;
743
744#if 1
745 for(ii=0; ii<nTrain; ii++){
746 float *pVec = &aTrain[ii*nElem];
747 int iBest = 0;
748 double fBest = vec1L2Dist(pVec, aCentroid, nElem);
749 int jj;
750 for(jj=1; jj<nCentroid; jj++){
751 double fNew = vec1L2Dist(pVec, &aCentroid[jj*nElem], nElem);
752 if( fNew<fBest ){
753 iBest = jj;
754 fBest = fNew;
755 }
756 }
757
758 if( iBest!=aRes[ii] ){
759 const double fTol = 0.00001;
760 double fDist = vec1L2Dist(pVec, &aCentroid[aRes[ii]*nElem], nElem);
761 if( (fDist>fBest && (fDist-fBest)/fDist>fTol)
762 || (fDist<fBest && (fBest-fDist)/fBest>fTol)
763 ){
764 fprintf(stderr, "algorithm FAILED...\n");
765 return 1;
766 }
767 }
768 }
769#endif
770
771 printf("Cycles: ");
772 if( t>(1000*1000*1000) ){
773 printf("%.2fG\n", (double)t / 1000000000.0);
774 }else if( t>(10*1000*1000) ){
775 printf("%dM\n", (int)(t / (1000*1000)));
776 }else if( t>(1*1000*1000) ){
777 printf("%.2fM\n", (double)t / (1000*1000));
778 }else{
779 printf("%d\n", (int)t);
780 }
781
782 return 0;
783}
784
785
786
787