egojoin.cpp 55.9 KB
Newer Older
Martin Perdacher's avatar
Martin Perdacher committed
1 2 3 4 5 6

#include "egojoin.h"

double EGO_epsilon;
int EGO_d;
int min_size_qsort;
Martin Perdacher's avatar
Martin Perdacher committed
7
long long * costref;
Martin Perdacher's avatar
Martin Perdacher committed
8 9 10 11 12 13 14 15 16 17 18

int epsilonGridCompare(const void *a, const void *b) {
    double * A = (double *) a;
    double * B = (double *) b;
    for (int i = 0; i < EGO_d; i++) {
        int h = (int) (floor(A[i] / EGO_epsilon) - floor(B[i] / EGO_epsilon));
        if (h != 0) return h;
    }
    return 0;
}

Martin Perdacher's avatar
Martin Perdacher committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void sampleHistograms(int n, int d, double epsilon, double *array, int *reorder_dim) {
    double mins[d];
    double maxs[d];
    memcpy(mins, array, d * sizeof (double));
    memcpy(maxs, array, d * sizeof (double));
#pragma omp parallel for reduction(min:mins[:d]) reduction(max:maxs[:d])
    for (int par = 0; par < NUM_THREADS; par++) {
        int i = par * n / NUM_THREADS;
        memcpy(mins, array + i*d, d * sizeof (double));
        memcpy(maxs, array + i*d, d * sizeof (double));
//        for (i++; i < (par + 1) * n / NUM_THREADS; i += (int) (-log(1. - drand48())*100.)) {
        for (i++; i < (par + 1) * n / NUM_THREADS; i++) {
            for (int j = 0; j < d; j++) {
                mins[j] = min(mins[j], array[i * d + j]);
                maxs[j] = max(maxs[j], array[i * d + j]);
            }
        }
    }
    int sizes[d];
    int cum = 0;
    int offs[d + 1];
    for (int j = 0; j < d; j++) {
        mins[j] = floor(mins[j] / epsilon);
        maxs[j] = floor(maxs[j] / epsilon);
        sizes[j] = (int) maxs[j] - (int) mins[j] + 1;
        offs[j] = cum - (int) mins[j];
        cum += sizes[j];
    }
    offs[d] = cum;
    long long * histos = (long long *) calloc(cum, sizeof (long long));
#pragma omp parallel for reduction(+:histos[:cum])
    for (int par = 0; par < NUM_THREADS; par++)
//        for (int i = par * n / NUM_THREADS; i < (par + 1) * n / NUM_THREADS; i += (int) (-log(1. - drand48())*100.))
        for (int i = par * n / NUM_THREADS; i < (par + 1) * n / NUM_THREADS; i++)
            for (int j = 0; j < d; j++)
                histos[(int) min(maxs[j],max(mins[j], floor(array[i * d + j] / epsilon))) + offs[j]]++;
    int h = 0;
    costref = (long long *) calloc(d, sizeof (long long));
    for (int i = 0; i < d; i++) {
#ifdef OUTPUT
        printf("%2d ", i);
#endif
        for (int j = 0; j < sizes[i]; j++) {
            costref[i] += histos[h]*(histos[h] - 1) / 2 + (j > 0 ? histos[h - 1] * histos[h] : 0);
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
64
            printf("%lld ", histos[h]);
Martin Perdacher's avatar
Martin Perdacher committed
65 66 67 68
#endif
            h++;
        }
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
69
        printf(" => %lld\n", costref[i]);
Martin Perdacher's avatar
Martin Perdacher committed
70 71 72 73 74 75 76 77
#endif
    }
    int * reorder_rev = (int*) malloc((d + 8) * sizeof (int));
    for (int j = 0; j < d + 8; j++)
        reorder_rev[j] = j;
    qsort(reorder_rev, d, sizeof (int), cmp_reorder_dim);
#ifdef OUTPUT
    for (int j = 0; j < d + 8; j++)
Martin Perdacher's avatar
Martin Perdacher committed
78
        printf("%2d %2d %lld\n", j, reorder_rev[j], j < d ? costref[reorder_rev[j]] : 0);
Martin Perdacher's avatar
Martin Perdacher committed
79 80 81 82 83 84 85 86
#endif
    // reorder_dim = (int*) malloc((d + 8) * sizeof (int));
    for (int j = 0; j < d + 8; j++)
        reorder_dim[reorder_rev[j]] = j;
    free(reorder_rev);
    free(costref);
    free(histos);
}
Martin Perdacher's avatar
Martin Perdacher committed
87

Martin Perdacher's avatar
Martin Perdacher committed
88
void test_ego_loop(size_t n, size_t d, double epsilon, double *array, long long *result){
Martin Perdacher's avatar
Martin Perdacher committed
89
    // int KBLOCK=8;
Martin Perdacher's avatar
Martin Perdacher committed
90 91
    *result = 0l;
    long long iresult = 0l;
Martin Perdacher's avatar
Martin Perdacher committed
92
    EGO_PARALLEL(n, d, epsilon, array)
Martin Perdacher's avatar
Martin Perdacher committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        #pragma omp parallel for reduction(+: iresult)
    EGO_PREPARE
        // printf("parallel %d\n", omp_get_thread_num() );
        veci64 resultvec = _mm512_setzero_si512();
    EGO_LOOP{
        resultvec += _mm512_xor_epi64(_mm512_set1_epi64(1ll), _mm512_srli_epi64(_mm512_castpd_si512(indicator), 63));
    }
    EGO_CONSOLIDATE{
        long long resultvecX[8];
        _mm512_storeu_si512((void *) resultvecX, resultvec);
        //    printf("%ld %ld %ld %ld %ld %ld %ld %ld\n", resultvecX[0],resultvecX[1],resultvecX[2],resultvecX[3],resultvecX[4],resultvecX[5],resultvecX[6],resultvecX[7]);
        //    printf("%lx %lx %lx %lx %lx %lx %lx %lx\n", resultvecX[0],resultvecX[1],resultvecX[2],resultvecX[3],resultvecX[4],resultvecX[5],resultvecX[6],resultvecX[7]);
        iresult += resultvecX[0] + resultvecX[1] + resultvecX[2] + resultvecX[3] + resultvecX[4] + resultvecX[5] + resultvecX[6] + resultvecX[7];
//        printf("par = %d: %d %d\n", par, result, _mm512_reduce_add_epi64(resultvec));
      }
    EGO_END

#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
111
    printf("count of join partners: %lld\n", iresult);
Martin Perdacher's avatar
Martin Perdacher committed
112 113 114 115
#endif
    *result = iresult;
}

Martin Perdacher's avatar
Martin Perdacher committed
116
void test_ego_loop3(size_t n, size_t d, double epsilon, double *array, long long *result){
Martin Perdacher's avatar
Martin Perdacher committed
117
    *result = 0l;
Martin Perdacher's avatar
Martin Perdacher committed
118
    // int KBLOCK=8;
Martin Perdacher's avatar
Martin Perdacher committed
119 120
    long long iresult=0l;
    omp_set_num_threads(NUM_THREADS);
Martin Perdacher's avatar
Martin Perdacher committed
121
    EGO_PARALLEL_TRAN(n, d, epsilon, 5, array)
Martin Perdacher's avatar
Martin Perdacher committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        #pragma omp parallel for reduction(+: iresult)
    EGO_PREPARE
        veci64 resultvec = _mm512_setzero_si512();
        veci64 eights = _mm512_set1_epi64(8ll) ;
    EGO_LOOP_TRAN{
        resultvec += eights - _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63) ;
    }
    EGO_CONSOLIDATE{
Martin Perdacher's avatar
Martin Perdacher committed
137
        iresult += _custom_mm512_reduce_add_epi64(resultvec);
Martin Perdacher's avatar
Martin Perdacher committed
138 139 140 141 142 143
//        double testres[8] __attribute__((aligned(64)));
//        _mm512_store_epi64(testres, resultvec);
//        printf("par = %d: %d %d\n", par, result, testres[0]+testres[1]+testres[2]+testres[3]+testres[4]+testres[5]+testres[6]+testres[7]);
    }
    EGO_END_TRAN
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
144
    printf("result %lld\n", iresult);
Martin Perdacher's avatar
Martin Perdacher committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158
#endif
    *result = iresult;
}

inline int ceilpowtwo(int m) {
    // determines the next power of two which is >=m, e.g. ceilpowtwo(13) = 16
    m--;
    m |= m >> 1;
    m |= m >> 2;
    m |= m >> 4;
    m |= m >> 8;
    return (m | (m >> 16)) + 1;
}

Martin Perdacher's avatar
Martin Perdacher committed
159
void epsilonGridOrdering(size_t n, size_t d, double epsilon, double* array) {
Martin Perdacher's avatar
Martin Perdacher committed
160 161 162 163 164 165 166
    EGO_epsilon = epsilon;
    EGO_d = d;
    min_size_qsort = n/NUM_THREADS*2 ;
#pragma omp parallel
#pragma omp master
    omp_qsort(array, n, d * sizeof (double), epsilonGridCompare);
#pragma omp taskwait
Martin Perdacher's avatar
Martin Perdacher committed
167

Martin Perdacher's avatar
Martin Perdacher committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
}


void epsilonGridFillList3(size_t n, size_t d, double epsilon, double * array, int *L1, int *L2, int *L3) {
    int a1, a2, a3;
    a1 = a2 = a3 = 0;
    int back1 = 0, back2 = 0, back3 = 0;
    EGO_epsilon = epsilon;
    EGO_d = d;
    double h1[d], h2[d], h3[d];
    for (int b = 0; b < n; b++) {
        for (int i = 0; i < d; i++)
            h1[i] = h3[i] = array[d * b + i] + epsilon;
        h1[0] = array[d * b];
        for (int i = 1; i < d; i++)
            h2[i] = array[d * b + i] - epsilon;
        h2[0] = array[d * b] + epsilon;
        while (a1 > 0 && epsilonGridCompare(array + a1 * d, h1) > 0) {
            a1--;
            back1++;
        }
        while (a1 < n && epsilonGridCompare(array + a1 * d, h1) <= 0)
            a1++;
        while (a2 > 0 && epsilonGridCompare(array + a2 * d, h2) >= 0) {
            a2--;
            back2++;
        }
        while (a2 < n && epsilonGridCompare(array + a2 * d, h2) < 0)
            a2++;
        while (a3 > 0 && epsilonGridCompare(array + a3 * d, h3) > 0) {
            a3--;
            back3++;
        }
        while (a3 < n && epsilonGridCompare(array + a3 * d, h3) <= 0)
            a3++;
        L1[b] = a1;
        L2[b] = a2;
        L3[b] = a3;
    }

#ifdef OUTPUT
    printf("Backsteps: %d %d %d\n", back1, back2, back3);
#endif

}

void epsilonGridCompleteListMax(size_t n, int *list) {
    int m = ceilpowtwo(n);
    for (size_t i = n; i < m; i++)
        list[i+m] = (n+7)/8;
    if (n % 2)
        list[n] = list[2 * n - 1];
    for (size_t i = n - 1; i > 0; i--)
Martin Perdacher's avatar
Martin Perdacher committed
221
        list[i] = hilbert_max(list[2 * i], list[2 * i + 1]);
Martin Perdacher's avatar
Martin Perdacher committed
222 223 224 225 226 227 228 229 230
}

void epsilonGridCompleteListMin(size_t n, int *list) {
    int m = ceilpowtwo(n);
    for (size_t i = n; i < m; i++)
        list[i+m] = 0;
    if (n % 2)
        list[n] = list[2 * n - 1];
    for (size_t i = n - 1; i > 0; i--)
Martin Perdacher's avatar
Martin Perdacher committed
231
        list[i] = hilbert_min(list[2 * i], list[2 * i + 1]);
Martin Perdacher's avatar
Martin Perdacher committed
232 233
}

Martin Perdacher's avatar
Martin Perdacher committed
234
static inline void transpose_8xd(size_t n, size_t d, double *EGO_array) {
Martin Perdacher's avatar
Martin Perdacher committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#pragma omp parallel for
    for (int par = 0; par < NUM_THREADS; par++) {
        veci64 mask1 = {0, 8, 2, 10, 4, 12, 6, 14};
        veci64 mask2 = {9, 1, 11, 3, 13, 5, 15, 7};
        veci64 mask3 = {0, 1, 8, 9, 4, 5, 12, 13};
        veci64 mask4 = {10, 11, 2, 3, 14, 15, 6, 7};
        veci64 mask5 = {0, 1, 2, 3, 8, 9, 10, 11};
        veci64 mask6 = {12, 13, 14, 15, 4, 5, 6, 7};
        double help[(d+7) * 8]__attribute__((aligned(64)));
        for (int i = par * n / NUM_THREADS / 8; i < (par + 1) * n / NUM_THREADS / 8; i++) {
            for (int k = 0; k < d; k += 8) {
                vec v1 = _mm512_loadu_pd(EGO_array + 8*i * d + k);
                vec v2 = _mm512_loadu_pd(EGO_array + (8*i + 1) * d + k);
                vec v3 = _mm512_loadu_pd(EGO_array + (8*i + 2) * d + k);
                vec v4 = _mm512_loadu_pd(EGO_array + (8*i + 3) * d + k);
                vec v5 = _mm512_loadu_pd(EGO_array + (8*i + 4) * d + k);
                vec v6 = _mm512_loadu_pd(EGO_array + (8*i + 5) * d + k);
                vec v7 = _mm512_loadu_pd(EGO_array + (8*i + 6) * d + k);
                vec v8 = _mm512_loadu_pd(EGO_array + (8*i + 7) * d + k);
                transposeAVX512(v1, v2, v3, v4, v5, v6, v7, v8);
                _mm512_store_pd(help + k * 8, v1);
                _mm512_store_pd(help + k * 8 + 8, v2);
                _mm512_store_pd(help + k * 8 + 16, v3);
                _mm512_store_pd(help + k * 8 + 24, v4);
                _mm512_store_pd(help + k * 8 + 32, v5);
                _mm512_store_pd(help + k * 8 + 40, v6);
                _mm512_store_pd(help + k * 8 + 48, v7);
                _mm512_store_pd(help + k * 8 + 56, v8);
            }
            memcpy(EGO_array + i * 8 * d, help, d * 8 * sizeof (double));
        }
    }
}


Martin Perdacher's avatar
Martin Perdacher committed
270
void prepareStripes(size_t n, size_t d, int numStripes, double epsilon, double *array, int ** lower, int ** upper, double *self){
Martin Perdacher's avatar
Martin Perdacher committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
//                    for (int a=0 ; a<d && a<20 ; a++)
//                        printf("%5.3lf ", array[17*1024*d+a]);
//                    printf("\n");
//                    for (int a=0 ; a<d && a<d ; a++)
//                        printf("%3.1lf ", (double)(int)(array[17*1024*d+a]/epsilon)*epsilon);
//                    printf("\n");
    double lowerDisplace[numStripes][d];
    double upperDisplace[numStripes][d];
    int neun = 1;
    int activeDimensions = 0;
    while (neun < 2*numStripes){
        neun*=3 ;
        activeDimensions++;
    }
//    printf("activeDimensions: %d, %d\n", activeDimensions, neun);
    for (int t=0 ; t<numStripes-1 ; t++){
        int k=numStripes+t+1;
        int i=(k*neun*2+2*numStripes)/numStripes/4;
        int i2=((k-1)*neun*2+2*numStripes)/numStripes/4;
        if ((i + 3) / 3 > (i2 + 3) / 3)
            i -= i % 3;
        int umrechnung = 0;
        int power = 1;
        for (int j=i; j>0 ; j/=3){
            umrechnung += power*(j%3) ;
            power *=10;
        }
        // printf("%d %d %d\n", t, i, umrechnung);
        i2 = i;
        for (int j = activeDimensions - 1; j >= 0; j--, i2 /= 3) {
            lowerDisplace[t + 1][j] = epsilon * (i2 % 3 - 1);
        }
        for (int j = activeDimensions; j < d; j++)
            lowerDisplace[t + 1][j] = (-epsilon);
        i2 = i - 1;
        for (int j = activeDimensions-1; j >= 0; j--, i2 /= 3) {
            upperDisplace[t][j] = epsilon * (i2 % 3 - 1);
        }
        for (int j = activeDimensions; j < d; j++)
            upperDisplace[t][j] = epsilon;
    }
    for (int j = 0; j < d; j++)
        lowerDisplace[0][j] = 0.0;
    for(int j=0; j<d ; j++)
        upperDisplace[numStripes-1][j] = epsilon;
//    for(int i=0 ; i<numStripes ; i++){
//        printf("%4.1lf %4.1lf %4.1lf %4.1lf %4.1lf     %4.1lf %4.1lf %4.1lf %4.1lf %4.1lf\n",
//                lowerDisplace[i][0],lowerDisplace[i][1],lowerDisplace[i][2],lowerDisplace[i][3],lowerDisplace[i][4],
//                upperDisplace[i][0],upperDisplace[i][1],upperDisplace[i][2],upperDisplace[i][3],upperDisplace[i][4]) ;
//    }
    int nn = ceilpowtwo(n);
    lower[0] = (int *) callocA64(sizeof (int) * 4 * nn * numStripes);
    for (int j=0 ; j<numStripes ; j++){
        lower[j] = (int*)((char *)(lower[0]) + j  * 4 * nn * sizeof(int)) ;
        upper[j] = (int*)((char *)(lower[j]) + 2 * nn * sizeof(int)) ;
    }
//    printf ("nn=%d %d\n", nn, sizeof (int) * 4 * nn * numStripes);
//    for (int j=0 ; j<numStripes ; j++)
//        printf("%d %ld %ld\n", j, (long long)lower[j] - (long long)lower[0], (long long) upper[j] - (long long)lower[0]);
Martin Perdacher's avatar
Martin Perdacher committed
330
    // printf("before parallel region!"); fflush(stdout);
Martin Perdacher's avatar
Martin Perdacher committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
#pragma omp parallel for
    for (int par = 0; par < NUM_THREADS; par++){
        int imin = par * n / NUM_THREADS / 8 * 8;
        int imax = (par + 1) * n / NUM_THREADS / 8 * 8;
        if (par+1 == NUM_THREADS)
            imax = n;
        double h[d];
        for(int i=imin ; i<imax ; i++)
            lower[0][i+nn] = i/8 ;

        for(int j=1; j<numStripes ; j++){
            for(int a=0 ; a<d ; a++)
                h[a] = array[imin*d+a]+lowerDisplace[j][a] ;
            int a = imin ;
            int b = n-1 ;
            int m = (a+b)/2;
            while (b-a > 1){
                if(epsilonGridCompare(array + m*d, h) >= 0)
                    b = m ;
                else
                    a = m ;
                m = (a+b)/2 ;
            }
//            while (m < n && epsilonGridCompare(array + m * d, h) < 0)
//                m++;
//            printf("lower: imin = %d; m = %d %d %d\n", imin, a,m,b);
//            lower[j][imin+nn] = m/8 ;
            for(int i=imin ; i<imax ; i++) {
                for(int a=0 ; a<d ; a++)
                    h[a] = array[i*d+a]+lowerDisplace[j][a] ;
                while (m > 0 && epsilonGridCompare(array + m * d, h) >= 0)
                    m--;
                while (m < n && epsilonGridCompare(array + m * d, h) < 0)
                    m++;
                lower[j][i+nn] = m/8 ;
//                if(i == 17*1024){
//                    printf("lower j=%d, i=%d, m=%d\n", j, i, m);
//                    for (int a=0 ; a<d && a<3 ; a++)
//                        printf("%5.3lf ", array[m*d-d+a]);
//                    for (int a=0 ; a<d && a<d ; a++)
//                        printf("%3.1lf ", (double)(int)(array[m*d-d+a]/epsilon)*epsilon);
//                    printf("\n");
//                    for (int a=0 ; a<d && a<3 ; a++)
//                        printf("%5.3lf ", array[m*d+a]);
//                    for (int a=0 ; a<d && a<d ; a++)
//                        printf("%3.1lf ", (double)(int)(array[m*d+a]/epsilon)*epsilon);
//                    printf("\n");
//                }
            }
        }
Martin Perdacher's avatar
Martin Perdacher committed
381 382 383 384
// if (par == 0)
// {
//         printf("*");fflush(stdout);
// }
Martin Perdacher's avatar
Martin Perdacher committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        for(int j=0; j<numStripes ; j++){
            for(int a=0 ; a<d ; a++)
                h[a] = array[imin*d+a]+upperDisplace[j][a] ;
            int a = imin ;
            int b = n-1 ;
            int m = (a+b)/2;
            while (b-a > 1){
                if(epsilonGridCompare(array + m*d, h) >= 0)
                    b = m ;
                else
                    a = m ;
                m = (a+b)/2 ;
            }
//            while (m < n && epsilonGridCompare(array + m * d, h) < 0)
//                m++;
//            printf("upper: imin = %d; m = %d %d %d\n", imin, a, m, b);
//            upper[j][imin+nn] = (m+7)/8 ;
Martin Perdacher's avatar
Martin Perdacher committed
402 403 404

    // printf("%d, ", omp_get_thread_num());fflush(stdout);

Martin Perdacher's avatar
Martin Perdacher committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
            for(int i=imin ; i<imax ; i++) {
                for(int a=0 ; a<d ; a++)
                    h[a] = array[i*d+a]+upperDisplace[j][a] ;
                while (m > 0 && epsilonGridCompare(array + m * d, h) >= 0)
                    m--;
                while (m < n && epsilonGridCompare(array + m * d, h) < 0)
                    m++;
                upper[j][i+nn] = (m+7)/8 ;
//                if(i == 17*1024){
//                    printf("upper j=%d, i=%d, m=%d\n", j, i, m);
//                    for (int a=0 ; a<d && a<3 ; a++)
//                        printf("%5.3lf ", array[m*d-d+a]);
//                    for (int a=0 ; a<d && a<d ; a++)
//                        printf("%3.1lf ", (double)(int)(array[m*d-d+a]/epsilon)*epsilon);
//                    printf("\n");
//                    for (int a=0 ; a<d && a<3 ; a++)
//                        printf("%5.3lf ", array[m*d+a]);
//                    for (int a=0 ; a<d && a<d ; a++)
//                        printf("%3.1lf ", (double)(int)(array[m*d+a]/epsilon)*epsilon);
//                    printf("\n");
//                }
            }
        }
        if(self) {
            double epsilon22 = epsilon * epsilon / 2;
            for (int i = imin; i < imax; i++) {
                double h = epsilon22;
                for (int j = 0; j < d; j++)
                    h -= array[i * d + j] * array[i * d + j];
                self[i] = h / 2;
            }
        }
        for(int j=0; j<numStripes ; j++){
            for (int i=imin/8 ; i<imax/8 ; i++)
                lower[j][i+nn/8] = min(min(min(lower[j][i*8+nn],lower[j][i*8+nn+1]),min(lower[j][i*8+nn+2], lower[j][i*8+nn+3])),
                        min(min(lower[j][i*8+nn+4],lower[j][i*8+nn+5]),min(lower[j][i*8+nn+6], lower[j][i*8+nn+7]))) ;
            for (int i=imin/8 ; i<imax/8 ; i++)
                upper[j][i+nn/8] = max(max(max(upper[j][i*8+nn],upper[j][i*8+nn+1]),max(upper[j][i*8+nn+2], upper[j][i*8+nn+3])),
                        max(max(upper[j][i*8+nn+4],upper[j][i*8+nn+5]),max(upper[j][i*8+nn+6], upper[j][i*8+nn+7]))) ;
        }
        for (int j=1 ; j<numStripes ; j++)
            for (int i=imin/8 ; i<imax/8 ; i++)
                upper[j-1][i+nn/8] = min(upper[j-1][i+nn/8], lower[j][i+nn/8]);
Martin Perdacher's avatar
Martin Perdacher committed
448 449 450 451
// if (par == 0)
// {
//         printf("-");fflush(stdout);
// }
Martin Perdacher's avatar
Martin Perdacher committed
452
    }
Martin Perdacher's avatar
Martin Perdacher committed
453
    // printf("\nend parallel region!\n"); fflush(stdout);
Martin Perdacher's avatar
Martin Perdacher committed
454 455 456 457 458 459 460 461 462 463 464 465
    for (int j=0 ; j<numStripes ; j++){
        epsilonGridCompleteListMin(nn/8, lower[j]);
        epsilonGridCompleteListMax(nn/8, upper[j]);
    }
//    printf("End prepare stripes\n");
//    for(int i=0; i<60; i++){
//        for(int j=0; j<numStripes; j++)
//            printf("%d %d ", lower[j][i+nn/65536], upper[j][i+nn/65536]);
//        printf("\n");
//    }
}

Martin Perdacher's avatar
Martin Perdacher committed
466
static inline long long _custom_mm512_reduce_add_epi64(__m512i a){
Martin Perdacher's avatar
Martin Perdacher committed
467 468 469 470 471 472 473 474 475
//    __m256i low  = _mm512_cvtepi64_epi32(a);
//    low = _mm256_hadd_epi32(low, low);
//    __m128i ulow = _mm_hadd_epi32(_mm256_castsi256_si128(low),_mm256_castsi256_si128(low));
//    return _mm_cvtsi128_si32(ulow) + _mm_extract_epi32(ulow, 1);
    __m256i b = _mm512_castsi512_si256(a) + _mm512_extracti64x4_epi64(a,1);
    __m128i c = _mm256_castsi256_si128(b) + _mm256_extracti128_si256(b,1);//_mm256_extracti64x2_epi64(b,1);
    return _mm_cvtsi128_si64(c) + _mm_extract_epi64(c, 1);
}

Martin Perdacher's avatar
Martin Perdacher committed
476
static inline void transpose_dx8(size_t n, size_t d, double *EGO_array) {
Martin Perdacher's avatar
Martin Perdacher committed
477 478 479 480 481 482 483
#pragma omp parallel for
    for (int par = 0; par < NUM_THREADS; par++) {
        veci64 mask1 = {0, 8, 2, 10, 4, 12, 6, 14};
        veci64 mask2 = {9, 1, 11, 3, 13, 5, 15, 7};
        veci64 mask3 = {0, 1, 8, 9, 4, 5, 12, 13};
        veci64 mask4 = {10, 11, 2, 3, 14, 15, 6, 7};
        veci64 mask5 = {0, 1, 2, 3, 8, 9, 10, 11};
Martin Perdacher's avatar
Martin Perdacher committed
484
        veci64 mask6 = {12, 13, 1<4, 15, 4, 5, 6, 7};
Martin Perdacher's avatar
Martin Perdacher committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
        int ddd = (d+7)/8*8 ;
        double help[ddd*8]__attribute__((aligned(64)));
        for (int i = par * n / NUM_THREADS / 8; i < (par + 1) * n / NUM_THREADS / 8; i++) {
            for (int k = 0; k < d; k += 8) {
                vec v1 = _mm512_load_pd(EGO_array + 8 * (i*d + k));
                vec v2 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 1));
                vec v3 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 2));
                vec v4 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 3));
                vec v5 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 4));
                vec v6 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 5));
                vec v7 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 6));
                vec v8 = _mm512_load_pd(EGO_array + 8 * (i*d + k + 7));
                transposeAVX512(v1, v2, v3, v4, v5, v6, v7, v8);
                _mm512_store_pd(help + k, v1);
                _mm512_store_pd(help + k+ddd, v2);
                _mm512_store_pd(help + k+2*ddd, v3);
                _mm512_store_pd(help + k+3*ddd, v4);
                _mm512_store_pd(help + k+4*ddd, v5);
                _mm512_store_pd(help + k+5*ddd, v6);
                _mm512_store_pd(help + k+6*ddd, v7);
                _mm512_store_pd(help + k+7*ddd, v8);
            }
            for (int k=0 ; k<8 ; k++)
                memcpy(EGO_array + (i*8+k)*d, help + ddd*k, d * sizeof (double));
        }
    }
}

Martin Perdacher's avatar
Martin Perdacher committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

int cmp_reorder_dim(const void * a, const void *b){
    long long A = costref[*(int*) a];
    long long B = costref[*(int*) b];
    if(A<B) return -1;
    if(A>B) return 1;
    return 0;
}


void outputStatistics(int n, int d, double epsilon, double *array, int *reorder_dim){
    // printf("Statistics\n");
    //    int first_int[d], last_int[d];
    //    for (int j=0 ; j<d ; j++)
    //        first_int[j] = last_int[j] = (int) floor(array[j]/epsilon);
    //    for (int i=0 ; i<n ; i++)
    //        for (int j=0 ; j<d ; j++){
    //            first_int[j] = min(first_int[j], (int) floor(array[i*d+j]/epsilon));
    //            last_int[j] = max(last_int[j], (int) floor(array[i*d+j]/epsilon));
    //        }
    costref = (long long *) malloc(d*sizeof(long long));
    for (int j = 0; j < d; j++) {
        double first_int = array[j];
        double last_int = first_int;
        int testcount = 0;
#pragma omp parallel for reduction(min:first_int) reduction(max:last_int) reduction(+:testcount)
        for(int par=0 ; par<NUM_THREADS ; par++)
        for (int i = par+1; i < n; i+=(int)(-log(1.-drand48())*100.*NUM_THREADS)){
            first_int = min(first_int, array[i * d + j]);
            last_int = max(last_int, array[i * d + j]);
            testcount ++;
        }
        first_int = floor(first_int/epsilon);
        last_int = floor(last_int/epsilon);
        if (first_int < -100000 || last_int > 100000) {
#ifdef OUTPUT
            printf("%3d %e %e ------------------ (VALUE RANGE EXCEEDED)\n", j, first_int, last_int);
#endif
        } else {
            int size = (int) last_int - (int) first_int + 1;
            long long * histo = (long long *) calloc(size, sizeof (long long));
            //memset(histo, 0, size*sizeof(long long));
            int testcount2 = 0;
#pragma omp parallel for reduction(+:histo[:size]) reduction(+:testcount2)
            for (int par = 0; par < NUM_THREADS; par++)
                for (int i = par + 1; i < n; i += (int) (-log(1. - drand48())*100. * NUM_THREADS), testcount2++)
                    histo[max(0, min(size - 1, (int) floor(array[i * d + j] / epsilon) - (int) first_int))]++;
            long long ref = 0;
            for (int i = 0; i < size; i++)
                ref += histo[i]*(histo[i] - 1) / 2;
            for (int i = 1; i < size; i++)
                ref += histo[i - 1] * histo[i];
            costref[j] = ref;
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
567
            printf("%3d %8d %8d %8d %20lld   [%lld", j, (int) first_int, (int) last_int, size, ref, histo[0]);
Martin Perdacher's avatar
Martin Perdacher committed
568 569

            for (int i = 1; i < size && i < 10; i++)
Martin Perdacher's avatar
Martin Perdacher committed
570
                printf(", %lld", histo[i]);
Martin Perdacher's avatar
Martin Perdacher committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584
            if (size > 10)
                printf(", ...] %d %d\n", testcount, testcount2);
            else
                printf("]\n");
#endif
            free(histo);
        }
    }
    int * reorder_rev = (int*) malloc ((d+8)*sizeof(int));
    for (int j=0 ; j<d+8 ; j++)
        reorder_rev[j] = j;
    qsort(reorder_rev, d, sizeof(int), cmp_reorder_dim);
#ifdef OUTPUT
    for (int j=0 ; j<d+8 ; j++)
Martin Perdacher's avatar
Martin Perdacher committed
585
        printf("%2d %2d %lld\n", j, reorder_rev[j], j<d?costref[reorder_rev[j]]:0);
Martin Perdacher's avatar
Martin Perdacher committed
586 587 588 589 590 591 592 593 594
#endif
    // reorder_dim = (int*) malloc ((d+8)*sizeof(int));
    for (int j=0 ; j<d+8 ; j++)
        reorder_dim[reorder_rev[j]] = j;
    free(reorder_rev);
    free(costref);
}

void omp_qsort(void* l, size_t num, size_t size, int (*compar)(const void*,const void*)){
Martin Perdacher's avatar
Martin Perdacher committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
//    printf ("Qsort %ld %d %d %d\n", (long long)l, num, omp_get_num_threads(), omp_get_thread_num());
    if(num <= min_size_qsort){
        qsort(l, num, size, compar);
        return ;
    }
    // a < b < c || c < b < a --> b
    // b < a < c || c < a < b --> a
    // a < c < b || b < c < a --> c
    char b[size] ;
    char *v = (char*)l + (num-1)*size ;
    char *r = v ;
    char *i = (char*)l - size ;
    char *j = (char*)l+num/2*size ;
    if(compar(l, j) < 0){
        if(compar(j, r) < 0){
Martin Perdacher's avatar
Martin Perdacher committed
610
            hilbert_swap(j,r)
Martin Perdacher's avatar
Martin Perdacher committed
611 612
        } else {
            if(compar(r, l) < 0)
Martin Perdacher's avatar
Martin Perdacher committed
613
                hilbert_swap(l,r)
Martin Perdacher's avatar
Martin Perdacher committed
614 615 616
            }
    } else {
        if(compar(r,j) < 0){
Martin Perdacher's avatar
Martin Perdacher committed
617
            hilbert_swap(j,r)
Martin Perdacher's avatar
Martin Perdacher committed
618
        } else if(compar(l, r) < 0)
Martin Perdacher's avatar
Martin Perdacher committed
619
            hilbert_swap(l,r)
Martin Perdacher's avatar
Martin Perdacher committed
620 621 622 623 624 625 626
    }
    j = r ;
    for (;;) {
        while (compar(i+=size, v) < 0) ;
        while (compar(v, j-=size) < 0)
            if(j==i) break ;
        if(i>=j) break ;
Martin Perdacher's avatar
Martin Perdacher committed
627
        hilbert_swap(i,j)
Martin Perdacher's avatar
Martin Perdacher committed
628
    }
Martin Perdacher's avatar
Martin Perdacher committed
629
    hilbert_swap(i,r)
Martin Perdacher's avatar
Martin Perdacher committed
630 631 632 633 634 635
#pragma omp task
        omp_qsort (l, ((long long)i-(long long)l) / size, size, compar);
#pragma omp task
        omp_qsort (i+size, num - ((long long)i-(long long)l)/size - 1, size, compar);
}

Martin Perdacher's avatar
Martin Perdacher committed
636 637 638 639 640 641 642 643 644 645 646 647
void reorder_dimensions(int n, int d, double *array, int *reorder_dim) {
    if (d > 128) {
#pragma omp parallel for
        for (int par = 0; par < NUM_THREADS; par++) {
            double help[d + 8]__attribute__((aligned(64)));
            for (int i = par * n / NUM_THREADS; i < (par + 1) * n / NUM_THREADS; i++) {
                memcpy(help, array + i*d, d * sizeof (double));
                for (int j = 0; j < d; j++)
                    array[reorder_dim[j]+i*d] = help[j];
            }
        }
        return;
Martin Perdacher's avatar
Martin Perdacher committed
648
    }
Martin Perdacher's avatar
Martin Perdacher committed
649 650 651 652
    // printf("first\n");fflush(stdout);
    // printf("reorder_dim[0]:%d\n", reorder_dim[0]);fflush(stdout);
    // printf("second");fflush(stdout);
#pragma omp parallel for
Martin Perdacher's avatar
Martin Perdacher committed
653
    for (int par = 0; par < NUM_THREADS; par++) {
Martin Perdacher's avatar
Martin Perdacher committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
        __m256i i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15;
        __m256i * rd = (__m256i *) reorder_dim;
        switch ((d - 1) / 8) {
            case 15: i15 = _mm256_loadu_si256(rd++); //rd += 8;
            case 14: i14 = _mm256_loadu_si256(rd++); //rd += 8;
            case 13: i13 = _mm256_loadu_si256(rd++); //rd += 8;
            case 12: i12 = _mm256_loadu_si256(rd++); //rd += 8;
            case 11: i11 = _mm256_loadu_si256(rd++); //rd += 8;
            case 10: i10 = _mm256_loadu_si256(rd++); //rd += 8;
            case 9: i9 = _mm256_loadu_si256(rd++); //rd += 8;
            case 8: i8 = _mm256_loadu_si256(rd++); //rd += 8;
            case 7: i7 = _mm256_loadu_si256(rd++); //rd += 8;
            case 6: i6 = _mm256_loadu_si256(rd++); //rd += 8;
            case 5: i5 = _mm256_loadu_si256(rd++); //rd += 8;
            case 4: i4 = _mm256_loadu_si256(rd++); //rd += 8;
            case 3: i3 = _mm256_loadu_si256(rd++); //rd += 8;
            case 2: i2 = _mm256_loadu_si256(rd++); //rd += 8;
            case 1: i1 = _mm256_loadu_si256(rd++); //rd += 8;
            case 0: i0 = _mm256_loadu_si256(rd);
        }
        __mmask8 k = 256 - (1 << ((128 - d) % 8));
        vec x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
        for (int i = par * n / NUM_THREADS; i < (par + 1) * n / NUM_THREADS; i++) {
            double * xd = array + i*d;
            switch ((d - 1) / 8) {
                case 15: x15 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 14: x14 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 13: x13 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 12: x12 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 11: x11 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 10: x10 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 9: x9 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 8: x8 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 7: x7 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 6: x6 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 5: x5 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 4: x4 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 3: x3 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 2: x2 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 1: x1 = _mm512_loadu_pd(xd);
                    xd += 8;
                case 0: x0 = _mm512_loadu_pd(xd);
            }
            xd = array + i*d;
            switch ((d - 1) / 8) {
                case 15: _mm512_i32scatter_pd(xd, i15, x15, 8);
                case 14: _mm512_i32scatter_pd(xd, i14, x14, 8);
                case 13: _mm512_i32scatter_pd(xd, i13, x13, 8);
                case 12: _mm512_i32scatter_pd(xd, i12, x12, 8);
                case 11: _mm512_i32scatter_pd(xd, i11, x11, 8);
                case 10: _mm512_i32scatter_pd(xd, i10, x10, 8);
                case 9: _mm512_i32scatter_pd(xd, i9, x9, 8);
                case 8: _mm512_i32scatter_pd(xd, i8, x8, 8);
                case 7: _mm512_i32scatter_pd(xd, i7, x7, 8);
                case 6: _mm512_i32scatter_pd(xd, i6, x6, 8);
                case 5: _mm512_i32scatter_pd(xd, i5, x5, 8);
                case 4: _mm512_i32scatter_pd(xd, i4, x4, 8);
                case 3: _mm512_i32scatter_pd(xd, i3, x3, 8);
                case 2: _mm512_i32scatter_pd(xd, i2, x2, 8);
                case 1: _mm512_i32scatter_pd(xd, i1, x1, 8);
                case 0: _mm512_mask_i32scatter_pd(xd, k, i0, x0, 8);
Martin Perdacher's avatar
Martin Perdacher committed
729 730 731 732 733
            }
        }
    }
}

Martin Perdacher's avatar
Martin Perdacher committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
// void test_ego_loop3_macro(size_t n, size_t d, size_t NUM_THREADS, double epsilon, double *array, size_t *countresult, int stripes, int KBLOCK, double *sorttime){
//     size_t result = 0;
//     CUtilTimer sortTimer;
//     sortTimer.start();
//     EGO_PARALLEL_TRAN(n, d, epsilon, 5, array,NUM_THREADS)
//         sortTimer.stop();
//         // printf("Ego_n= %d\n", n);
//         // printf("ceilpowtwo(n)= %d\n", ceilpowtwo(EGO_n));
//         // for ( int __i = 0 ; __i < 2 ; __i++ ){
//         //     for ( int __j =0 ; __j < 5 ; __j++ ){
//         //         printf("lower[%d][%d]:%7d ", __i, __j, lower[__i][__j]);
//         //     }
//         //     printf("\n");
//         //     for ( int __j =0 ; __j < 5 ; __j++ ){
//         //         printf("upper[%d][%d]:%7d ", __i, __j, upper[__i][__j]);
//         //     }
//         //     printf("\n\n");
//         // }
//         #pragma omp parallel for reduction(+:result)
//     EGO_PREPARE
//         veci64 resultvec = _mm512_setzero_si512();
//         veci64 eights = _mm512_set1_epi64(8ll) ;
//     EGO_LOOP_TRAN{
//         resultvec += eights - _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63);
//     }
//     EGO_CONSOLIDATE{
//         result += _mm512_reduce_add_epi64(resultvec);
// //        double testres[8] __attribute__((aligned(64)));
// //        _mm512_store_epi64(testres, resultvec);
// //        printf("par = %d: %d %d\n", par, result, testres[0]+testres[1]+testres[2]+testres[3]+testres[4]+testres[5]+testres[6]+testres[7]);
//     }
//     EGO_END_TRAN
//     *sorttime = sortTimer.get_time();
//     *countresult = result;
// }
Martin Perdacher's avatar
Martin Perdacher committed
776

Martin Perdacher's avatar
Martin Perdacher committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
void test_ego_loop3_macro(size_t n, size_t d, double epsilon, double *array, size_t *countresult, int stripes, double *sortTime, double *indextime, double *loadpercent){
    CUtilTimer index_timer;
    long long result = 0;
    long long refinements = 0;
    unsigned long long savedload[5*NUM_THREADS];
    index_timer.start();
    EGO_PARALLEL_TRAN(n, d, epsilon, stripes, array)
        index_timer.stop();
        // printf("saving result...\n");
        // char filename[50];
        // sprintf (filename, "uniform_1000000x32_normalized_sorted_%f.bin", epsilon);
        // save_binary_file(array, n, d, filename);
        // printf("saved.\n");
        CUtilTimer total_timer; total_timer.start();
        *indextime = index_timer.get_time() - sortTimer.get_time();
        *sortTime = sortTimer.get_time();
        // printf("timestamp index ready %6.2f\n",timestamp()-starttimestamp);
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
795
        printf("overall_load: %lld / %lld (=n*(n-1)/2 / 64) ==> %f\n", overall_load, (long long)n/128*(n-1), (double)overall_load/n/(n-1)*128);
Martin Perdacher's avatar
Martin Perdacher committed
796 797 798
#endif
        *loadpercent = (double)overall_load/n/(n-1)*128;
        #pragma omp parallel for reduction(+:result) reduction(+:refinements)
Martin Perdacher's avatar
Martin Perdacher committed
799 800 801
    EGO_PREPARE
        veci64 resultvec = _mm512_setzero_si512();
        veci64 eights = _mm512_set1_epi64(8ll) ;
Martin Perdacher's avatar
Martin Perdacher committed
802 803
        long long refineload = 0;
    EGO_LOOP_TRAN {
Martin Perdacher's avatar
Martin Perdacher committed
804 805 806 807 808 809 810 811
        resultvec += eights - _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63) ;
Martin Perdacher's avatar
Martin Perdacher committed
812
        refineload ++;
Martin Perdacher's avatar
Martin Perdacher committed
813 814
    }
    EGO_CONSOLIDATE{
Martin Perdacher's avatar
Martin Perdacher committed
815
        result += _custom_mm512_reduce_add_epi64(resultvec);
Martin Perdacher's avatar
Martin Perdacher committed
816 817 818 819 820 821
        refinements += refineload ;
        int curload=0;
        for(int i=loadstart[par] ; i<loadstart[par+1] ; i++)
            for(int s=0 ; s<EGO_stripes ; s++)
                curload += upper[s][i+nn/8] - lower[s][i+nn/8];
        total_timer.stop();
822
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
823
        printf("Consolidate %6.2f %d %d %d %d %d %lld %lld\n",total_timer.get_time(), par, omp_get_thread_num(), loadstart[par], loadstart[par+1]-loadstart[par], curload, refineload, result);
Martin Perdacher's avatar
Martin Perdacher committed
824 825
#endif

Martin Perdacher's avatar
Martin Perdacher committed
826 827 828 829 830
//        double testres[8] __attribute__((aligned(64)));
//        _mm512_store_epi64(testres, resultvec);
//        printf("par = %d: %d %d\n", par, result, testres[0]+testres[1]+testres[2]+testres[3]+testres[4]+testres[5]+testres[6]+testres[7]);
    }
    EGO_END_TRAN
Martin Perdacher's avatar
Martin Perdacher committed
831
    // printf("result %ld\n", result);
Martin Perdacher's avatar
Martin Perdacher committed
832
    *countresult = result;
Martin Perdacher's avatar
Martin Perdacher committed
833 834 835 836 837

    // printf("refinements %ld Mio (%ld 8x8)\n", refinements*64/1000000, refinements);
//    for(int par=0 ; par<NUM_THREADS ; par++, printf("\n"))
//        for(int s=0 ; s<5 ; s++)
//            printf("%ld ",savedload[NUM_THREADS*s+par]);
Martin Perdacher's avatar
Martin Perdacher committed
838
}
Martin Perdacher's avatar
Martin Perdacher committed
839

Martin Perdacher's avatar
Martin Perdacher committed
840
void test_ego_loop3_noself(const size_t nA, const size_t nB, const int d, const double epsilon, double *arrayA, double *arrayB, size_t *countresult, const int activedims, double *sortTime, double *indextime, double *loadpercent){
Martin Perdacher's avatar
Martin Perdacher committed
841
    CUtilTimer index_timer,total_timer,sortTimer;
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    long long result = 0;
    long long refinements = 0;
    unsigned long long savedload[5*NUM_THREADS];
    double starttimestamp = timestamp() ;
    index_timer.start();
    EGO_PARALLEL_TRAN_NOSELF(nA, nB, d, epsilon, 2, arrayA, arrayB)
        index_timer.stop();
        // printf("timestamp index ready %6.2f\n",timestamp()-starttimestamp);
//        for(int i=0 ; i<NUM_THREADS + 4 ; i+=4)
//            printf("%2d %9d %9d %9d %9d\n", i, loadstart[i], loadstart[i+1], loadstart[i+2], loadstart[i+3]);
//        for (int x=0 ; x<NUM_THREADS ; x++){
//            long long cum_load = 0;
//            for(int i = loadstart[x] ; i<loadstart[x+1] ; i++)
//                for (int j = 0; j < EGO_stripes; j++)
//                    cum_load += upper[j][i + nn / 8] - lower[j][i + nn / 8];
//            if (x%4 == 0) printf("\n %2d", x);
//            printf("%9ld (%5.2f%%)  ", cum_load, 100.*(double)cum_load/(double)overall_load);
//        }
        // printf("overall_load: %ld / %ld (=n/8*(n/8+1)/2) ==> %9.6f %%\n", overall_load, (long long)nA/8*(nA/8+1), (double)overall_load/(nA/8)/(nA/8-1)*200);
       total_timer.start();
       *indextime = index_timer.get_time() - sortTimer.get_time();
       *sortTime = sortTimer.get_time();

Martin Perdacher's avatar
Martin Perdacher committed
865
       *loadpercent = ((double)overall_load/(nA/8)/(nA/8-1)*200);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
       #pragma omp parallel for proc_bind(close) reduction(+:result) reduction(+:refinements)
    EGO_PREPARE
        veci64 resultvec = _mm512_setzero_si512();
        veci64 eights = _mm512_set1_epi64(8ll) ;
        long long refineload = 0;
    EGO_LOOP_TRAN_NOSELF {
        resultvec += eights - _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63)
                            - _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63) ;
        refineload ++;
    }
    EGO_CONSOLIDATE{
Martin Perdacher's avatar
Martin Perdacher committed
883
        result += _custom_mm512_reduce_add_epi64(resultvec);
884 885 886 887 888 889 890
        refinements += refineload ;
        int curload=0;
        for(int i=loadstart[par] ; i<loadstart[par+1] ; i++)
            for(int s=0 ; s<EGO_stripes ; s++)
                curload += upper[s][i+nn/8] - lower[s][i+nn/8];
        total_timer.stop();
#ifdef OUTPUT
Martin Perdacher's avatar
Martin Perdacher committed
891
        printf("Consolidate %6.2f %d %d %d %d %d %lld %lld\n",timestamp()-starttimestamp, par, omp_get_thread_num(), loadstart[par], loadstart[par+1]-loadstart[par], curload, refineload, result);
892 893 894 895 896 897 898 899 900 901 902 903 904 905
#endif

//        double testres[8] __attribute__((aligned(64)));
//        _mm512_store_epi64(testres, resultvec);
//        printf("par = %d: %d %d\n", par, result, testres[0]+testres[1]+testres[2]+testres[3]+testres[4]+testres[5]+testres[6]+testres[7]);
    }
    EGO_END_TRAN_NOSELF

    *countresult = result;
    // printf("result %ld\n", result);
    // printf("refinements %ld Mio (%ld 8x8)\n", refinements*64/1000000, refinements);

//    for(int par=0 ; par<NUM_THREADS ; par++, printf("\n"))
//        for(int s=0 ; s<5 ; s++)
Martin Perdacher's avatar
Martin Perdacher committed
906
//            printf("%ld ",savedload[NUM_THREADS*s+par]);
907 908 909
}


Martin Perdacher's avatar
Martin Perdacher committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
// void test_ego_loop3_macro_queue(size_t n, size_t d, size_t NUM_THREADS, double epsilon, double *array, size_t *countresult, int stripes, int KBLOCK, boost::lockfree::queue<join_pair> &jpartners, double *sorttime){
//     size_t result = 0;
//     CUtilTimer sortTimer;
//     sortTimer.start();
//     EGO_PARALLEL_TRAN(n, d, epsilon, 5, array,NUM_THREADS)
//         sortTimer.stop();
//         #pragma omp parallel
//         {
//             boost::lockfree::queue<join_pair> private_queue(128);
//             #pragma omp for
//             EGO_PREPARE
//                 veci64 resultvec = _mm512_setzero_si512();
//                 veci64 eights = _mm512_set1_epi64(8ll) ;
//             EGO_LOOP_TRAN{
//                 resultvec += eights - _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63)
//                                     - _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63);
//                 double outarray[64] __attribute__((aligned(64)));
//                 _mm512_store_pd(&outarray[0], sum1);
//                 _mm512_store_pd(&outarray[8], sum2);
//                 _mm512_store_pd(&outarray[16], sum3);
//                 _mm512_store_pd(&outarray[24], sum4);
//                 _mm512_store_pd(&outarray[32], sum5);
//                 _mm512_store_pd(&outarray[40], sum6);
//                 _mm512_store_pd(&outarray[48], sum7);
//                 _mm512_store_pd(&outarray[54], sum8);
//
//                 for ( int ii = 0 ; ii < 64 ; i++ ){
//                     if ( outarray[ii] > 0.0 ){
//                         join_pair p;
//                         p.p1 = i*8 + (ii % 8);  // i index
//                         p.p2 = j*8 + (ii / 8); // j index
//                         while ( ! private_queue.push(p) );
//                     }
//                 }
//             }
//             EGO_CONSOLIDATE{
//                 result += _mm512_reduce_add_epi64(resultvec);
//         //        double testres[8] __attribute__((aligned(64)));
//         //        _mm512_store_epi64(testres, resultvec);
//         //        printf("par = %d: %d %d\n", par, result, testres[0]+testres[1]+testres[2]+testres[3]+testres[4]+testres[5]+testres[6]+testres[7]);
//             } // END_CONSOLIDATE
//         }// END omp parallel
//     EGO_END_TRAN
//     *sorttime = sortTimer.get_time();
//     *countresult = result;
// }

// void test_ego_loop3_long_queue(size_t n, size_t d, size_t NUM_THREADS, double epsilon, double *array, size_t *countresult, int stripes, int KBLOCK, boost::lockfree::queue<join_pair> &jpartners){
//     size_t result = 0;
//
//     size_t EGO_n = (n);
//     EGO_d = (d);
//     EGO_epsilon = (epsilon);
//     double * EGO_array = (array);
//     int EGO_blocks = (EGO_d + KBLOCK - 1) / KBLOCK;
//     int EGO_stripes = (stripes);
//     epsilonGridOrdering(EGO_n, EGO_d, EGO_epsilon, EGO_array,NUM_THREADS);
//     int nn = ceilpowtwo(EGO_n);
//     int **lower = (int **) ddr_alloc (EGO_stripes*sizeof(int*));
//     int **upper = (int **) ddr_alloc (EGO_stripes*sizeof(int*));
//     double *self = callocA64(sizeof (double) * EGO_n * EGO_blocks);
//     prepareStripes(EGO_n, EGO_d, EGO_stripes, EGO_epsilon, EGO_array, lower, upper, (double *)0, NUM_THREADS);
//     EGO_epsilon = EGO_epsilon * EGO_epsilon / 2;
//     for (int i=0 ; i<EGO_n ; i++){
//         double h=EGO_epsilon;
//         for(int j=0 ; j<2*KBLOCK && j<EGO_d ; j++)
//             h-=EGO_array[i*EGO_d+j]*EGO_array[i*EGO_d+j];
//         self[i/8*8*EGO_blocks+i%8]=h/2;
//     }
//     for(int k=2 ; k<EGO_blocks ; k++)
//         for (int i=0 ; i<EGO_n ; i++){
//             double h=0;
//             for(int j=k * KBLOCK ; j<(k+1) * KBLOCK && j<EGO_d ; j++)
//             h-=EGO_array[i*EGO_d+j]*EGO_array[i*EGO_d+j];
//         self[(i/8*EGO_blocks+k)*8+i%8]=h/2;
//     }
//     transpose_8xd(EGO_n, EGO_d, EGO_array,NUM_THREADS);
//     long long overall_load = 0;
//     for (int i = 0; i < EGO_n / 8; i++)
//         for (int j = 0; j < EGO_stripes; j++)
//             overall_load += upper[j][i + nn / 8] - lower[j][i + nn / 8];
//     int loadstart[NUM_THREADS + 2];
//     for (int i = 0; i <= NUM_THREADS; i++)
//         loadstart[i] = 0;
//     long long cum_load = 0;
//     for (int i = 0; i < EGO_n / 8; i++) {
//         for (int j = 0; j < EGO_stripes; j++)
//             cum_load += upper[j][i + nn / 8] - lower[j][i + nn / 8];
//         loadstart[cum_load * NUM_THREADS / overall_load + 1] = i;
//     }
//     loadstart[NUM_THREADS] = EGO_n / 8;
//     for (int i = 1; i <= NUM_THREADS; i++)
//         if (loadstart[i] == 0)
//             loadstart[i] = loadstart[i - 1];
//
//         #pragma omp parallel for reduction(+:result)
//         for (int par = 0; par < NUM_THREADS; par++) {
//
//         veci64 resultvec = _mm512_setzero_si512();
//         veci64 eights = _mm512_set1_epi64(8ll) ;
//
//         int imin = loadstart[par];
//         int imax = loadstart[par + 1];
//         for (int s = 0; s < EGO_stripes; s++) {
//             int i = 0;
//             int j = 0;
//             register veci64 const1 = _mm512_set1_epi64(1LL) ;
//             register veci64 const2 = _mm512_add_epi64(const1, const1) ;
//             register veci64 const3 = _mm512_add_epi64(const1, const2) ;
//             register veci64 const4 = _mm512_add_epi64(const1, const3) ;
//             register veci64 const5 = _mm512_add_epi64(const1, const4) ;
//             register veci64 const6 = _mm512_add_epi64(const1, const5) ;
//             register veci64 const7 = _mm512_add_epi64(const1, const6) ;
//             register veci64 const0 = _mm512_setzero_si512();
//             FGF_HILBERT_FOR(i, j, EGO_n / 8, EGO_n / 8, i >= imin && i < imax && j >= lower[s][i + nn / 8] && j < upper[s][i + nn / 8],
//                     FURHIL_ub0 >= imin && FURHIL_lb0 < imax &&
//                     FURHIL_ub1 - 1 >= lower[s][(FURHIL_lb0 >> (FURHIL_clevel + 1))+(nn >> (FURHIL_clevel + 4))] &&
//                     FURHIL_lb1 < upper[s][((FURHIL_ub0 - 1)>>(FURHIL_clevel + 1))+(nn >> (FURHIL_clevel + 4))]) {
//                 register vec vi = _mm512_load_pd(self + i * 8 * EGO_blocks);
//                 register vec vj = _mm512_load_pd(self + j * 8 * EGO_blocks);
//                 register vec sum1 = vi + _mm512_permutexvar_pd(const0, vj);
//                 register vec sum2 = vi + _mm512_permutexvar_pd(const1, vj);
//                 register vec sum3 = vi + _mm512_permutexvar_pd(const2, vj);
//                 register vec sum4 = vi + _mm512_permutexvar_pd(const3, vj);
//                 register vec sum5 = vi + _mm512_permutexvar_pd(const4, vj);
//                 register vec sum6 = vi + _mm512_permutexvar_pd(const5, vj);
//                 register vec sum7 = vi + _mm512_permutexvar_pd(const6, vj);
//                 register vec sum8 = vi + _mm512_permutexvar_pd(const7, vj);
//                 vi = _mm512_load_pd(EGO_array + (EGO_d * i) * 8);
//                 vj = _mm512_load_pd(EGO_array + (EGO_d * j) * 8);
//                 sum1 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const0, vj), sum1);
//                 sum2 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const1, vj), sum2);
//                 sum3 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const2, vj), sum3);
//                 sum4 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const3, vj), sum4);
//                 sum5 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const4, vj), sum5);
//                 sum6 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const5, vj), sum6);
//                 sum7 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const6, vj), sum7);
//                 sum8 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const7, vj), sum8);
//                 int k; for (k=1 ; k<d ; k++){
//                     if(k % KBLOCK == 0 && k > KBLOCK){
//                         register veci64 allind = _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63);
//                         allind += _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63);
//                         if(_mm512_reduce_add_epi64(allind) >= 64) {k=d+1; break;}
//                         vi = _mm512_load_pd(self + (i * EGO_blocks + k/KBLOCK) * 8);
//                         vj = _mm512_load_pd(self + (j * EGO_blocks + k/KBLOCK) * 8);
//                         sum1 += vi + _mm512_permutexvar_pd(const0, vj);
//                         sum2 += vi + _mm512_permutexvar_pd(const1, vj);
//                         sum3 += vi + _mm512_permutexvar_pd(const2, vj);
//                         sum4 += vi + _mm512_permutexvar_pd(const3, vj);
//                         sum5 += vi + _mm512_permutexvar_pd(const4, vj);
//                         sum6 += vi + _mm512_permutexvar_pd(const5, vj);
//                         sum7 += vi + _mm512_permutexvar_pd(const6, vj);
//                         sum8 += vi + _mm512_permutexvar_pd(const7, vj);
//                     }
//                     vi = _mm512_load_pd(EGO_array + (EGO_d * i + k) * 8);
//                     vj = _mm512_load_pd(EGO_array + (EGO_d * j + k) * 8);
//                     sum1 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const0, vj), sum1);
//                     sum2 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const1, vj), sum2);
//                     sum3 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const2, vj), sum3);
//                     sum4 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const3, vj), sum4);
//                     sum5 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const4, vj), sum5);
//                     sum6 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const5, vj), sum6);
//                     sum7 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const6, vj), sum7);
//                     sum8 = _mm512_fmadd_pd(vi, _mm512_permutexvar_pd(const7, vj), sum8);
//                 }
//                 if(k<=d){{
//                     if (i==j){
//                         sum1 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum1), 255, 0xbff0000000000000ull));
//                         sum2 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum2), 254, 0xbff0000000000000ull));
//                         sum3 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum3), 252, 0xbff0000000000000ull));
//                         sum4 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum4), 248, 0xbff0000000000000ull));
//                         sum5 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum5), 240, 0xbff0000000000000ull));
//                         sum6 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum6), 224, 0xbff0000000000000ull));
//                         sum7 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum7), 192, 0xbff0000000000000ull));
//                         sum8 = _mm512_castsi512_pd(_mm512_mask_set1_epi64(_mm512_castpd_si512(sum8), 128, 0xbff0000000000000ull));
//                     }
//
//     {
//         resultvec += eights - _mm512_srli_epi64(_mm512_castpd_si512(sum1), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum2), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum3), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum4), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum5), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum6), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum7), 63)
//                             - _mm512_srli_epi64(_mm512_castpd_si512(sum8), 63) ;
//     }
//
//
//     }}} FGF_HILBERT_END(i, j); }
//
//         {
//             result += _mm512_reduce_add_epi64(resultvec);
//     //        double testres[8] __attribute__((aligned(64)));
//     //        _mm512_store_epi64(testres, resultvec);
//     //        printf("par = %d: %d %d\n", par, result, testres[0]+testres[1]+testres[2]+testres[3]+testres[4]+testres[5]+testres[6]+testres[7]);
//         }
//
//     }transpose_dx8(EGO_n, EGO_d, EGO_array, NUM_THREADS);}
//
//     // *countresult = result;
// }
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240


void prepareStripesNoSelf(int nA, int nB, int d, int activeDimensions, double epsilon, double *A, double *B, int ** lower, int **upper, double *selfA, double *selfB) {
    double starttimestamp = timestamp();
    int numStripes = 1;
    for (int i = 0; i < activeDimensions; i++)
        numStripes *= 3;
    double lowerDisplace[numStripes][d];
    double upperDisplace[numStripes][d];
    for (int i = 0; i < numStripes; i++)
        for (int j = 0; j < d; j++) {
            lowerDisplace[i][j] = (-epsilon);
            upperDisplace[i][j] = epsilon;
        }
    for (int i = 0; i < numStripes; i++) {
        int power = numStripes / 3;
        for (int j = 0; j < activeDimensions; j++) {
            lowerDisplace[i][j] = upperDisplace[i][j] = epsilon * (double) (i / power % 3 - 1);
            power /= 3;
        }
    }
    int nn = ceilpowtwo(nA);
    lower[0] = (int *) callocA64(sizeof (int) * 4 * nn * numStripes);
    for (int j=0 ; j<numStripes ; j++){
        lower[j] = (int*)((char *)(lower[0]) + j  * 4 * nn * sizeof(int)) ;
        upper[j] = (int*)((char *)(lower[j]) + 2 * nn * sizeof(int)) ;
    }
#pragma omp parallel for
    for (int par = 0; par < NUM_THREADS; par++){
        int imin = par * nA / NUM_THREADS / 8 * 8;
        int imax = (par + 1) * nA / NUM_THREADS / 8 * 8;
        if (par+1 == NUM_THREADS)
            imax = nA;
        double h[d];
        for(int j=0; j<numStripes ; j++){
            for(int a=0 ; a<d ; a++)
                h[a] = A[imin*d+a]+lowerDisplace[j][a] ;
            int a = 0 ;
            int b = nB-1 ;
            int m = (a+b)/2;
            while (b-a > 1){
                if(epsilonGridCompare(B + m*d, h) >= 0)
                    b = m ;
                else
                    a = m ;
                m = (a+b)/2 ;
            }
            for(int i=imin ; i<imax ; i++) {
                for(int a=0 ; a<d ; a++)
                    h[a] = A[i*d+a]+lowerDisplace[j][a] ;
                while (m > 0 && epsilonGridCompare(B + m * d, h) >= 0)
                    m--;
                while (m < nB && epsilonGridCompare(B + m * d, h) < 0)
                    m++;
                lower[j][i+nn] = m/8 ;
            }
        }
        for(int j=0; j<numStripes ; j++){
            for(int a=0 ; a<d ; a++)
                h[a] = A[imin*d+a]+upperDisplace[j][a] ;
            int a = imin ;
            int b = nB-1 ;
            int m = (a+b)/2;
            while (b-a > 1){
                if(epsilonGridCompare(B + m*d, h) >= 0)
                    b = m ;
                else
                    a = m ;
                m = (a+b)/2 ;
            }
            for(int i=imin ; i<imax ; i++) {
                for(int a=0 ; a<d ; a++)
                    h[a] = A[i*d+a]+upperDisplace[j][a] ;
                while (m > 0 && epsilonGridCompare(B + m * d, h) >= 0)
                    m--;
                while (m < nB && epsilonGridCompare(B + m * d, h) < 0)
                    m++;
                upper[j][i+nn] = (m+7)/8 ;
            }
        }
        double epsilon22 = epsilon * epsilon / 2;
        if(selfA) {
            for (int i = imin; i < imax; i++) {
                double h = epsilon22;
                for (int j = 0; j < d; j++)
                    h -= A[i * d + j] * A[i * d + j];
                selfA[i] = h / 2;
            }
        }
        if(selfB) {
            for (int i = par*nB/NUM_THREADS ; i < (par+1)*nB/NUM_THREADS; i++) {
                double h = epsilon22;
                for (int j = 0; j < d; j++)
                    h -= B[i * d + j] * B[i * d + j];
                selfB[i] = h / 2;
            }
        }
        for(int j=0; j<numStripes ; j++){
            for (int i=imin/8 ; i<imax/8 ; i++)
                lower[j][i+nn/8] = min(min(min(lower[j][i*8+nn],lower[j][i*8+nn+1]),min(lower[j][i*8+nn+2], lower[j][i*8+nn+3])),
                        min(min(lower[j][i*8+nn+4],lower[j][i*8+nn+5]),min(lower[j][i*8+nn+6], lower[j][i*8+nn+7]))) ;
            for (int i=imin/8 ; i<imax/8 ; i++)
                upper[j][i+nn/8] = max(max(max(upper[j][i*8+nn],upper[j][i*8+nn+1]),max(upper[j][i*8+nn+2], upper[j][i*8+nn+3])),
                        max(max(upper[j][i*8+nn+4],upper[j][i*8+nn+5]),max(upper[j][i*8+nn+6], upper[j][i*8+nn+7]))) ;
        }
        for (int j=1 ; j<numStripes ; j++)
            for (int i=imin/8 ; i<imax/8 ; i++)
                upper[j-1][i+nn/8] = min(upper[j-1][i+nn/8], lower[j][i+nn/8]);
#ifdef OUTPUT
        printf("Thread %d ready %7.2f %d %d\n", par, timestamp()-starttimestamp, imin, imax);
#endif
    }
    for (int j=0 ; j<numStripes ; j++){
        epsilonGridCompleteListMin(nn/8, lower[j]);
        epsilonGridCompleteListMax(nn/8, upper[j]);
    }
}