File size: 4,536 Bytes
b7e49b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: Apache-2.0
//
// DepthPro2 DEC megakernel: plan-table access and the work schedule. The BRISC, NCRISC and TRISC walk the same
// schedule (frame groups x layers x my n-block groups x rounds x n-blocks x k-blocks), so their CB / ring traffic
// matches by construction. The plan table (host: plan.py Plan.table()) is loaded into CB_PLAN by the BRISC at boot.
#pragma once
#include <cstdint>
#include "md_defs.hpp"

#define MD_INL inline __attribute__((always_inline))

namespace mdx {

constexpr uint32_t GX = MD_GX;  // grid columns
constexpr uint32_t GY = MD_GY;  // grid rows

struct Plan {
    const uint32_t* t;
    uint32_t nm;
    MD_INL uint32_t hdr(uint32_t f) const { return t[f]; }
    MD_INL uint32_t mf(uint32_t m, uint32_t f) const { return t[NHF + m * NMF + f]; }
    MD_INL uint32_t lf(uint32_t l, uint32_t f) const { return t[NHF + nm * NMF + l * NLF + f]; }
};

MD_INL uint32_t cdiv(uint32_t a, uint32_t b) { return (a + b - 1) / b; }

// my share of a layer's work: n-block groups g = g0 + i * gstep (i < ng); in round k my unit is k * span + off.
// The layer runs on grid rows [row0, row0 + nrows) (phase merging); other rows have no work (ng = 0).
struct Sched {
    uint32_t g0, gstep, ng, span, off, rounds;
};
MD_INL void sched_rows(uint32_t nbo, uint32_t u, uint32_t x, uint32_t y, uint32_t NR, Sched& s) {
    if (nbo <= NR) {
        const uint32_t q = NR / nbo;
        s.gstep = 1;
        if (y < nbo * q) {
            s.g0 = y / q;
            s.ng = 1;
            s.off = (y % q) * GX + x;
            s.span = q * GX;
        } else {
            s.g0 = 0;
            s.ng = 0;
            s.off = 0;
            s.span = GX;
        }
    } else {
        s.g0 = y;
        s.gstep = NR;
        s.ng = y < nbo ? (nbo - y + NR - 1) / NR : 0;
        s.off = x;
        s.span = GX;
    }
    s.rounds = cdiv(u, s.span);
}

MD_INL void sched_of(const Plan& p, uint32_t l, uint32_t x, uint32_t y, Sched& s) {
    const uint32_t r0 = p.lf(l, L_ROW0), nr = p.lf(l, L_NROWS);
    if (y < r0 || y >= r0 + nr) {
        s.g0 = s.ng = s.off = 0;
        s.gstep = 1;
        s.span = GX;
        s.rounds = 0;
        return;
    }
    sched_rows(p.lf(l, L_NBO), p.lf(l, L_U), x, y - r0, nr, s);
}

// weight blocks: (nb, kb) of layer l -> first arena page, pages
MD_INL uint32_t blk_page(const Plan& p, uint32_t l, uint32_t nb, uint32_t kb) {
    const uint32_t bp0 = p.lf(l, L_BP0), bp = p.lf(l, L_BP);
    return p.lf(l, L_WPG) + nb * (bp0 + (p.lf(l, L_NKB) - 1) * bp) + (kb == 0 ? 0 : bp0 + (kb - 1) * bp);
}
MD_INL uint32_t blk_pages(const Plan& p, uint32_t l, uint32_t kb) { return kb == 0 ? p.lf(l, L_BP0) : p.lf(l, L_BP); }
// pages to skip so that a block of np pages starting at ring position pos does not wrap
MD_INL uint32_t ring_skip(uint32_t pos, uint32_t np, uint32_t ring) {
    const uint32_t r = pos % ring;
    return r + np > ring ? ring - r : 0;
}

// unit geometry (C3 / UP / HC): u -> frame f, first output row y0, first x tile kx0, rows bh, x tiles bw
struct Unit {
    uint32_t f, y0, kx0, bh, bw;
};
MD_INL void unit_of(const Plan& p, uint32_t l, uint32_t u, Unit& un) {
    const uint32_t yb = p.lf(l, L_YB), xb = p.lf(l, L_XB);
    const uint32_t BH = p.lf(l, L_BH), BW = p.lf(l, L_BW);
    un.f = u / (yb * xb);
    const uint32_t rem = u - un.f * yb * xb;
    const uint32_t by = rem / xb, bx = rem - by * xb;
    un.y0 = by * BH;
    un.kx0 = bx * BW;
    const uint32_t ho = p.lf(l, L_HO), kxo = p.lf(l, L_KXO);
    un.bh = ho - un.y0 < BH ? ho - un.y0 : BH;
    un.bw = kxo - un.kx0 < BW ? kxo - un.kx0 : BW;
}
// RA unit u = output map page: frame f (of the group), token row y, page pg -> tokens [t0, t1) of the frame
// (t = y gw + x, x in [32 pg, min(32 pg + 32, gw))); its token tiles [m0, m0 + mt) of the group's flat list (G * PT)
struct RaPage {
    uint32_t f, y, pg, t0, t1;
};
MD_INL void ra_page(const Plan& p, uint32_t l, uint32_t u, RaPage& r) {
    const uint32_t gh = p.lf(l, L_HO), gw = p.lf(l, L_WO);
    const uint32_t npg = (gw + 31) / 32;
    r.f = u / (gh * npg);
    const uint32_t rem = u - r.f * gh * npg;
    r.y = rem / npg;
    r.pg = rem - r.y * npg;
    r.t0 = r.y * gw + 32 * r.pg;
    r.t1 = r.y * gw + (32 * r.pg + 32 < gw ? 32 * r.pg + 32 : gw);
}
MD_INL void ra_unit(const Plan& p, uint32_t l, uint32_t u, uint32_t& m0, uint32_t& mt) {
    RaPage r;
    ra_page(p, l, u, r);
    const uint32_t pt = p.lf(l, L_KXO);
    m0 = r.f * pt + r.t0 / 32;
    mt = (r.t1 - 1) / 32 - r.t0 / 32 + 1;
}

}  // namespace mdx