streamlife.h 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#pragma once

#include "lifetree.h"
#include "avxlife/directions.h"

namespace apg {

    template<typename I>
    struct streammeta {

        I res;
        I aux;
        uint64_t lanes;

    };

17 18
    template<typename I, int N>
    class streamtree : public lifetree_generic<I, N, streammeta<I> > {
19 20

        public:
21 22 23 24 25 26 27 28
        using lifetree_generic<I, N, streammeta<I> >::htree;
        using lifetree_generic<I, N, streammeta<I> >::iterate_recurse;
        using lifetree_generic<I, N, streammeta<I> >::iterate_recurse1;
        using lifetree_generic<I, N, streammeta<I> >::ind2ptr_leaf;
        using lifetree_generic<I, N, streammeta<I> >::ind2ptr_nonleaf;
        using lifetree_generic<I, N, streammeta<I> >::ninechildren;
        using lifetree_generic<I, N, streammeta<I> >::fourchildren;
        using lifetree_generic<I, N, streammeta<I> >::make_nonleaf;
29
        using lifetree_abstract<I>::breach;
30 31 32 33 34 35 36 37 38 39

        streamtree(uint64_t maxmem) {
            // maxmem is specified in MiB, so we left-shift by 20:
            this->gc_threshold = maxmem << 20;
        }

        /*
        * Streamlife operates on pairs of hashtiles instead of individual
        * hashtiles. We want to memoize the results of these computations.
        *
40
        * Key:   nicearray<I, 4> = (beszel_tile,  ulqoma_tile, depth, gencount)
41 42
        * Value: nicearray<I, 2> = (beszel_tile', ulqoma_tile')
        */
43
        kivtable<nicearray<I, 4>, I, nicearray<I, 2> > biresults;
44

45
        bool threshold_gc(uint64_t threshold) {
46 47 48

            if (this->htree.gc_partial()) {
                // We have invalidated biresults, so we empty it:
49
                biresults.clear();
50 51 52
                return true;
            }

53 54 55
            if (threshold) {
                uint64_t oldsize = this->htree.total_bytes() + biresults.total_bytes();
                if (oldsize >= threshold) {
56
                    std::cerr << "Emptying streamlife biresults (" << oldsize << " >= " << threshold << ")" << std::endl;
57
                    biresults.clear();
58
                    uint64_t newsize = this->htree.total_bytes() + biresults.total_bytes();
59 60 61 62 63
                    if (newsize >= ((threshold * 3) / 4)) {
                        std::cerr << "Insufficient memory liberated; entering full garbage collection..." << std::endl;
                        this->htree.gc_full();
                        newsize = this->htree.total_bytes() + biresults.total_bytes();
                    }
64
                    std::cerr << "Streamlife size reduced from " << oldsize << " to " << newsize << " bytes." << std::endl;
65 66 67 68 69 70
                    return true;
                }
            }
            return false;
        }

Adam P. Goucher's avatar
Adam P. Goucher committed
71 72 73 74 75 76 77 78 79 80 81 82
        void printnode(hypernode<I> hnode) {
            for (int y = 0; y < (16 << hnode.depth); y++) {
                for (int x = 0; x < (16 << hnode.depth); x++) {
                    uint64_t c = this->getcell_recurse(hnode, x, y);
                    if (c > 1) {std::cout << "\033[3" << ((c >> 1) % 7) + 1 << ";1m"; }
                    if (c > 0) {std::cout << "_*.o"[c & 3]; } else { std::cout << "."; }
                    if (c > 1) {std::cout << "\033[0m"; }
                }
                std::cout << std::endl;
            }
        }

83
        uint64_t node2lanes(uint64_t rule, I depth, I index) {
84 85

            if (index == 0) {
Adam P. Goucher's avatar
Adam P. Goucher committed
86
                // std::cerr << "index = 0" << std::endl;
87 88 89 90 91 92 93 94
                return 0xffff;
            }

            if (depth == 0) {
                auto pptr = this->ind2ptr_leaf(index);
                if ((pptr->value.lanes & 0xffff0000ull) != ((rule + 1) << 16)) {
                    pptr->value.lanes = determine_direction(rule, pptr->key.x) | ((rule + 1) << 16);
                }
Adam P. Goucher's avatar
Adam P. Goucher committed
95 96 97 98 99 100 101
                /*
                if (pptr->value.lanes & 255) {
                    std::cout << (pptr->value.lanes & 255) << std::endl;
                    printnode(hypernode<I>(index, depth));
                }
                */
                // std::cerr << pptr->value.lanes << std::endl;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                return pptr->value.lanes & 0xffffffff0000ffffull;
            } else {
                auto pptr = this->ind2ptr_nonleaf(depth, index);
                if ((pptr->value.lanes & 0xffff0000ull) != ((rule + 1) << 16)) {

                    uint64_t childlanes[9];
                    uint64_t adml = 0xff;

                    /* 
                    * Short-circuit evaluation using the corner children.
                    * This will handle the vast majority of random tiles.
                    */
                    if (adml != 0) { childlanes[0] = node2lanes(rule, depth - 1, pptr->key.x[0]); adml &= childlanes[0]; }
                    if (adml != 0) { childlanes[2] = node2lanes(rule, depth - 1, pptr->key.x[1]); adml &= childlanes[2]; }
                    if (adml != 0) { childlanes[6] = node2lanes(rule, depth - 1, pptr->key.x[2]); adml &= childlanes[6]; }
                    if (adml != 0) { childlanes[8] = node2lanes(rule, depth - 1, pptr->key.x[3]); adml &= childlanes[8]; }
                    if (adml == 0) { pptr->value.lanes = ((rule + 1) << 16); return 0; }

                    if (depth == 1) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134
                        uint64_t* tlx  =  ind2ptr_leaf(pptr->key.x[0])->key.x;
                        uint64_t* trx  =  ind2ptr_leaf(pptr->key.x[1])->key.x;
                        uint64_t* blx  =  ind2ptr_leaf(pptr->key.x[2])->key.x;
                        uint64_t* brx  =  ind2ptr_leaf(pptr->key.x[3])->key.x;

                        nicearray<uint64_t, 4*N> cc, tc, bc, cl, cr;

                        for (uint64_t i = 0; i < N; i++) {
                            uint64_t arr[20] = {tlx[3], trx[2], blx[1], brx[0],
                                                tlx[1], trx[0], tlx[3], trx[2],
                                                blx[1], brx[0], blx[3], brx[2],
                                                tlx[2], tlx[3], blx[0], blx[1],
                                                trx[2], trx[3], brx[0], brx[1]};

Adam P. Goucher's avatar
Adam P. Goucher committed
135 136 137 138 139
                            std::memcpy(cc.x + 4*i, arr,      32);
                            std::memcpy(tc.x + 4*i, arr + 4,  32);
                            std::memcpy(bc.x + 4*i, arr + 8,  32);
                            std::memcpy(cl.x + 4*i, arr + 12, 32);
                            std::memcpy(cr.x + 4*i, arr + 16, 32);
140 141 142
                            tlx += 4; trx += 4; blx += 4; brx += 4;
                        }

143 144 145 146 147
                        childlanes[1] = node2lanes(rule, depth - 1, this->make_leaf(tc));
                        childlanes[3] = node2lanes(rule, depth - 1, this->make_leaf(cl));
                        childlanes[4] = node2lanes(rule, depth - 1, this->make_leaf(cc));
                        childlanes[5] = node2lanes(rule, depth - 1, this->make_leaf(cr));
                        childlanes[7] = node2lanes(rule, depth - 1, this->make_leaf(bc));
Adam P. Goucher's avatar
Adam P. Goucher committed
148
                        adml &= (childlanes[1] & childlanes[3] & childlanes[4] & childlanes[5] & childlanes[7]);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                    } else {
                        auto pptr_tl  =  ind2ptr_nonleaf(depth - 1, pptr->key.x[0]);
                        auto pptr_tr  =  ind2ptr_nonleaf(depth - 1, pptr->key.x[1]);
                        auto pptr_bl  =  ind2ptr_nonleaf(depth - 1, pptr->key.x[2]);
                        auto pptr_br  =  ind2ptr_nonleaf(depth - 1, pptr->key.x[3]);
                        nicearray<I, 4> cc = {pptr_tl->key.x[3], pptr_tr->key.x[2], pptr_bl->key.x[1], pptr_br->key.x[0]};
                        nicearray<I, 4> tc = {pptr_tl->key.x[1], pptr_tr->key.x[0], pptr_tl->key.x[3], pptr_tr->key.x[2]};
                        nicearray<I, 4> bc = {pptr_bl->key.x[1], pptr_br->key.x[0], pptr_bl->key.x[3], pptr_br->key.x[2]};
                        nicearray<I, 4> cl = {pptr_tl->key.x[2], pptr_tl->key.x[3], pptr_bl->key.x[0], pptr_bl->key.x[1]};
                        nicearray<I, 4> cr = {pptr_tr->key.x[2], pptr_tr->key.x[3], pptr_br->key.x[0], pptr_br->key.x[1]};
                        childlanes[1] = node2lanes(rule, depth - 1, make_nonleaf(depth - 1, tc));
                        childlanes[3] = node2lanes(rule, depth - 1, make_nonleaf(depth - 1, cl));
                        childlanes[4] = node2lanes(rule, depth - 1, make_nonleaf(depth - 1, cc));
                        childlanes[5] = node2lanes(rule, depth - 1, make_nonleaf(depth - 1, cr));
                        childlanes[7] = node2lanes(rule, depth - 1, make_nonleaf(depth - 1, bc));
Adam P. Goucher's avatar
Adam P. Goucher committed
164 165 166 167 168 169
                        adml &= (childlanes[1] & childlanes[3] & childlanes[4] & childlanes[5] & childlanes[7]);
                    }
                    /*
                    if ((adml == 4) && (depth <= 3)) {
                        std::cout << (adml) << std::endl;
                        printnode(hypernode<I>(index, depth));
170
                    }
Adam P. Goucher's avatar
Adam P. Goucher committed
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 221 222 223 224 225 226 227 228


                    for (uint64_t i = 0; i < 9; i++) {
                        childlanes[i] >>= 32;
                    }
                    uint64_t lanes = 0;

                    #define ROTR32(X, Y) (((X) >> (Y)) | ((X) << (32 - (Y))))
                    #define ROTL32(X, Y) (((X) << (Y)) | ((X) >> (32 - (Y))))

                    /*
                    * Lane numbers are modulo 32, with each lane being either
                    * 8 rows, 8 columns, or 8hd (in either diagonal direction)
                    */
                    uint64_t a = (depth < 6) ? (1 << (depth - 1)) : 0;
                    uint64_t a2 = (2 * a) & 31;

                    if (adml & 0x88) {
                        // Horizontal lanes
                        lanes |= ROTL32(childlanes[0] | childlanes[1] | childlanes[2], a);
                        lanes |=       (childlanes[3] | childlanes[4] | childlanes[5]);
                        lanes |= ROTR32(childlanes[6] | childlanes[7] | childlanes[8], a);
                    }

                    if (adml & 0x44) {
                        lanes |=                 ROTL32(childlanes[0], a2);
                        lanes |=         ROTL32(childlanes[3] | childlanes[1], a);
                        lanes |=       (childlanes[6] | childlanes[4] | childlanes[2]);
                        lanes |=         ROTR32(childlanes[7] | childlanes[5], a);
                        lanes |=                 ROTR32(childlanes[8], a2);
                    }

                    if (adml & 0x22) {
                        // Vertical lanes
                        lanes |= ROTL32(childlanes[0] | childlanes[3] | childlanes[6], a);
                        lanes |=       (childlanes[1] | childlanes[4] | childlanes[7]);
                        lanes |= ROTR32(childlanes[2] | childlanes[5] | childlanes[8], a);
                    }

                    if (adml & 0x11) {
                        lanes |=                 ROTL32(childlanes[2], a2);
                        lanes |=         ROTL32(childlanes[1] | childlanes[5], a);
                        lanes |=       (childlanes[0] | childlanes[4] | childlanes[8]);
                        lanes |=         ROTR32(childlanes[3] | childlanes[7], a);
                        lanes |=                 ROTR32(childlanes[6], a2);
                    }

                    pptr->value.lanes = adml | ((rule + 1) << 16) | (lanes << 32);
                }
                return pptr->value.lanes & 0xffffffff0000ffffull;
            }

        }

        uint64_t is_solitonic(hypernode<I> hnode, int rule) {

            uint64_t lanes1 = node2lanes(rule, hnode.depth, hnode.index);
Adam P. Goucher's avatar
Adam P. Goucher committed
229
            if ((lanes1 & 255) == 0) { return 0; }
230
            uint64_t lanes2 = node2lanes(rule, hnode.depth, hnode.index2);
Adam P. Goucher's avatar
Adam P. Goucher committed
231
            if ((lanes2 & 255) == 0) { return 0; }
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            uint64_t commonlanes = (lanes1 & lanes2) >> 32;
            if (commonlanes) { return 0; }
            return ((((lanes1 >> 4) & lanes2) | ((lanes2 >> 4) & lanes1)) & 15);

        }


        hypernode<I> iterate_recurse(hypernode<I> hnode, uint64_t mantissa, uint64_t exponent,
                                        int rule, int history) {

            hypernode<I> part1(hnode.index,  hnode.depth);
            hypernode<I> part2(hnode.index2, hnode.depth);

            if (is_solitonic(hnode, rule)) {
                // BESZEL and ULQOMA tiles are provably non-interacting:
Adam P. Goucher's avatar
Adam P. Goucher committed
247
                // std::cerr << "is solitonic" << std::endl;
248 249
                I i1 = iterate_recurse1(part1, mantissa, exponent, rule, history).index;
                I i2 = iterate_recurse1(part2, mantissa, exponent, rule, history).index;
Adam P. Goucher's avatar
Adam P. Goucher committed
250 251 252 253 254 255 256 257 258 259 260 261 262

                if ((hnode.index == 0) || (hnode.index2 == 0)) {
                    I i3 = i1 | i2;
                    I ind3 = hnode.index | hnode.index2;
                    auto lanes = node2lanes(rule, hnode.depth, ind3);
                    if (lanes & 240) {
                        return hypernode<I>(0, i3, hnode.depth - 1);
                    } else {
                        return hypernode<I>(i3, 0, hnode.depth - 1);
                    }
                } else {
                    return hypernode<I>(i1, i2, hnode.depth - 1);
                }
263 264 265 266

            } else {

                uint64_t hrule = (rule << 1) + (history & 1);
Adam P. Goucher's avatar
Adam P. Goucher committed
267 268
                uint64_t effexp = (hnode.depth < (1 + exponent)) ? hnode.depth : (1 + exponent);
                I gcdesc = (effexp << 7) | (hrule << 3) | (mantissa - 1);
269 270 271 272 273 274 275 276
                nicearray<I, 4> k = {hnode.index, hnode.index2, hnode.depth, gcdesc};

                I p = biresults.getnode(k, false);
                if (p == ((I) -1)) {

                    hypernode<I> res(0, 0, hnode.depth - 1);

                    if (hnode.depth == 1) {
277
                        hypernode<I> hnode2 = breach(hnode);
278
                        I i3 = iterate_recurse1(hnode2, mantissa, exponent, rule, history).index;
279 280 281

                        if (i3 != 0) {
                            uint64_t lanes = node2lanes(rule, hnode2.depth, hnode2.index);
282
                            // if (lanes & 255) { std::cerr << lanes << std::endl; }
283
                            if (lanes & 240) {
284
                                // std::cerr << "lanes & 240" << std::endl;
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
                                res.index2 = i3;
                            } else {
                                res.index = i3;
                            }
                        }
                    } else {

                        auto ch91 = ninechildren(part1);
                        auto ch92 = ninechildren(part2);

                        if (mantissa == 0) {
                            res.index = ch91.x[4];
                            res.index2 = ch92.x[4];
                        } else {
                            bool bothstages = (hnode.depth <= (1 + exponent));
                            uint64_t newmant = bothstages ? mantissa : 0;
                            for (uint64_t i = 0; i < 9; i++) {
                                auto fh = iterate_recurse(hypernode<I>(ch91.x[i], ch92.x[i], hnode.depth - 1), newmant, exponent, rule, history);
                                ch91.x[i] = fh.index; ch92.x[i] = fh.index2;
                            }

                            auto ch41 = fourchildren(part1, ch91);
                            auto ch42 = fourchildren(part2, ch92);

                            for (uint64_t i = 0; i < 4; i++) {
                                auto fh = iterate_recurse(hypernode<I>(ch41.x[i], ch42.x[i], hnode.depth - 1), mantissa, exponent, rule, history);
                                ch41.x[i] = fh.index; ch42.x[i] = fh.index2;
                            }

                            res.index =  make_nonleaf(hnode.depth - 1, ch41);
                            res.index2 = make_nonleaf(hnode.depth - 1, ch42);
                        }
                    }

                    nicearray<I, 2> v = {res.index, res.index2};
                    p = biresults.setnode(k, v);
                }
Adam P. Goucher's avatar
Adam P. Goucher committed
322

323
                auto xptr = biresults.ind2ptr(p);
Adam P. Goucher's avatar
Adam P. Goucher committed
324
                return hypernode<I>(xptr->value.x[0], xptr->value.x[1], hnode.depth - 1);
325 326 327 328 329 330 331 332

            }

        }

    };

}