Skip to content

Commit da399fe

Browse files
committed
GPU: gathered calls in their own kernel, a growing arena, small chunks
- A lane that reaches a gathered call (PC_KQ) leaves the main kernel, and an idle lane leaves once no lane runs (A[KA_ACTIVE]). The host then runs the waiting calls with bend_kq, a small kernel with none of the main kernel's register pressure, and dispatches the main kernel again. - The arena starts at 64 MB and grows 4x when a call fills it (a buffer's first use costs with its size: 30 ms for 1 GB); BEND_GPU_MB caps it. - Heap chunks are 256 words (all lanes' first chunks: 16 MB, not 256 MB), and an object may span several. - The fork depth defaults to log2(lanes): one task per lane. leaves_gpu 14: 0.083s -> 0.065s (official 0.077s); forks_gpu 28: 0.07s -> 0.061s (0.098s); forks_gpu 30: 0.145s -> 0.126s (0.186s).
1 parent 466635a commit da399fe

5 files changed

Lines changed: 221 additions & 94 deletions

File tree

‎bendc.bend‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4576,7 +4576,7 @@ def Gen.gpu.emit.errs(+g: G, +ts: List<&2, String>, errs: List<&2, String>, +cod
45764576
"\n// The device code of the !-calls: rt/gpu.h, then this program's part\n#include \"gpu.h\"\n\n" ++ code ++
45774577
"\n#include \"gpuhost.h\"\n\nstatic const char K_SRC[] =\n" ++ K.lit(code) ++
45784578
";\n\nstatic const GpuFn K_FNS[] = {" ++ Gen.gpu.fns(wraps) ++
4579-
"{0, 0}};\nstatic GpuProg K_PROG = {K_SRC, bend_kernel, K_FNS};\n\n" ++ Gen.gpu.stubs(g, ts, True{})
4579+
"{0, 0}};\nstatic GpuProg K_PROG = {K_SRC, bend_kernel, bend_kq, K_FNS};\n\n" ++ Gen.gpu.stubs(g, ts, True{})
45804580
case e <> t:
45814581
"\n// The !-calls run on the CPU:\n" ++ Gen.gpu.errs(e <> t) ++ Gen.gpu.stubs(g, ts, False{})
45824582

‎rt/gpu.h‎

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static inline KW k_u_of(float f) { union { uint32_t i; float f; } x; x.f = f; re
7272
#define KA_HEAP 2
7373
#define KA_QHEAD 3
7474
#define KA_QTAIL 4
75+
#define KA_ACTIVE 5
7576
#define KA_SEQ 16
7677

7778
// Where things are, in words of the arena (H) unless said otherwise.
@@ -100,7 +101,7 @@ typedef struct {
100101
#define KQ_ARGS 8
101102
#define K_LANE 24 // words of a lane's saved state
102103

103-
#define K_CHUNK 4096
104+
#define K_CHUNK 256
104105

105106
// The errors a lane can hit.
106107
#define KE_FX 2 // an effect, or a closure with no device code
@@ -164,7 +165,7 @@ KINLINE KW k_alloc(KTHR KCtx *c, KW n) {
164165
KW k = (need + K_CHUNK - 1) / K_CHUNK;
165166
KW at = (KW)K_ADD(&c->A[KA_HEAP], (KU)k);
166167
KW start = c->P->heap0 + at * K_CHUNK;
167-
if (n + 1 > K_CHUNK || start + k * K_CHUNK > c->P->heap0 + c->P->heapw) {
168+
if (start + k * K_CHUNK > c->P->heap0 + c->P->heapw) {
168169
k_fail(c, KE_HEAP);
169170
c->hp = c->P->heap0;
170171
c->he = c->hp + K_CHUNK;
@@ -506,16 +507,8 @@ KINLINE void k_call_clo(KTHR KCtx *c, KW f, KW x, KW ret) {
506507
c->pc = l;
507508
}
508509

509-
#ifdef __METAL_VERSION__
510-
kernel void bend_kernel(device coherent(device) KW *H [[buffer(0)]], device KAU *A [[buffer(1)]],
511-
constant KParams &PP [[buffer(2)]], device KW *G [[buffer(3)]], uint lane [[thread_position_in_grid]]) {
512-
constant KParams *P = &PP;
513-
#else
514-
static void bend_kernel(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {
515-
#endif
516-
if (lane >= P->nlanes) return;
517-
KCtx cx;
518-
KTHR KCtx *c = &cx;
510+
// A lane's state, from and to its slot of the arena.
511+
KINLINE void k_load(KTHR KCtx *c, KCOH KW *H, KDEV KAU *A, KCP KParams *P, KDEV KW *G, KU lane) {
519512
c->H = H;
520513
c->G = G;
521514
c->Q = (KDEV KW *)H + P->q0;
@@ -538,28 +531,53 @@ static void bend_kernel(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {
538531
c->kqret = H[ls + 8];
539532
c->kqfb = H[ls + 9];
540533
for (int i = 0; i < KQ_ARGS; i++) c->kqa[i] = H[ls + 10 + i];
534+
}
535+
536+
KINLINE void k_save(KTHR KCtx *c) {
537+
KW ls = c->P->lane0 + (KW)c->lane * K_LANE;
538+
c->H[ls] = c->pc;
539+
c->H[ls + 1] = c->fp;
540+
c->H[ls + 2] = c->rv;
541+
c->H[ls + 3] = c->dep;
542+
c->H[ls + 4] = c->hp;
543+
c->H[ls + 5] = c->he;
544+
c->H[ls + 6] = c->ax;
545+
c->H[ls + 7] = c->kq;
546+
c->H[ls + 8] = c->kqret;
547+
c->H[ls + 9] = c->kqfb;
548+
for (int i = 0; i < KQ_ARGS; i++) c->H[ls + 10 + i] = c->kqa[i];
549+
}
550+
551+
KINLINE bool k_active(KW pc) { return pc != PC_IDLE && pc != PC_KQ; }
552+
553+
// The kernel: each lane runs blocks, taking tasks from the queue when it has
554+
// none. A lane that reaches a gathered call (PC_KQ) leaves the dispatch, and
555+
// so does an idle one once no lane is running (A[KA_ACTIVE] counts them):
556+
// the host then runs the calls with bend_kq, all at once, and dispatches this
557+
// again. (A long call run here, inside this big kernel, runs 3-5x slower.)
558+
#ifdef __METAL_VERSION__
559+
kernel void bend_kernel(device coherent(device) KW *H [[buffer(0)]], device KAU *A [[buffer(1)]],
560+
constant KParams &PP [[buffer(2)]], device KW *G [[buffer(3)]], uint lane [[thread_position_in_grid]]) {
561+
constant KParams *P = &PP;
562+
#else
563+
static void bend_kernel(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {
564+
#endif
565+
if (lane >= P->nlanes) return;
566+
KCtx cx;
567+
KTHR KCtx *c = &cx;
568+
k_load(c, H, A, P, G, lane);
541569
KW budget = P->budget;
542570
for (KW step = 0; step < budget; step++) {
571+
if (c->pc == PC_KQ) break;
543572
if (c->pc == PC_IDLE) {
544573
if (K_LOAD(&A[KA_DONE]) != 0 || K_LOAD(&A[KA_ERR]) != 0) break;
545-
k_pop_task(c);
546-
}
547-
// A KQ_ call runs a whole subtree in one step. The lanes of a SIMD group
548-
// run in lockstep, so one lane alone in its call would hold up the rest:
549-
// a lane waits (PC_KQ) until each of its group waits too or has nothing
550-
// to do, and they make their calls together.
551-
bool kq = c->pc == PC_KQ;
552-
if (K_SIMD_ALL(kq || c->pc == PC_IDLE)) {
553-
if (kq) {
554-
bool ok = true;
555-
KW r = k_kq(c, &ok);
556-
c->rv = r;
557-
c->pc = ok ? c->kqret : c->kqfb;
558-
}
559-
} else if (!kq) switch (c->pc) {
560-
case PC_IDLE: {
561-
break;
574+
if (!k_pop_task(c)) {
575+
if (K_LOAD(&A[KA_ACTIVE]) == 0) break;
576+
continue;
562577
}
578+
K_ADD(&A[KA_ACTIVE], 1u);
579+
}
580+
switch (c->pc) {
563581
case PC_ROOT: {
564582
H[2] = c->rv;
565583
K_FENCE();
@@ -585,21 +603,35 @@ static void bend_kernel(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {
585603
break;
586604
}
587605
}
606+
if (!k_active(c->pc)) K_SUB(&A[KA_ACTIVE], 1u);
588607
if (c->err != 0) {
589608
KU z = 0;
590609
K_CAS(&A[KA_ERR], z, c->err);
591610
break;
592611
}
593612
}
594-
H[ls] = c->pc;
595-
H[ls + 1] = c->fp;
596-
H[ls + 2] = c->rv;
597-
H[ls + 3] = c->dep;
598-
H[ls + 4] = c->hp;
599-
H[ls + 5] = c->he;
600-
H[ls + 6] = c->ax;
601-
H[ls + 7] = c->kq;
602-
H[ls + 8] = c->kqret;
603-
H[ls + 9] = c->kqfb;
604-
for (int i = 0; i < KQ_ARGS; i++) H[ls + 10 + i] = c->kqa[i];
613+
k_save(c);
614+
}
615+
616+
// The waiting calls, a lane each: a small kernel, which runs them fast.
617+
#ifdef __METAL_VERSION__
618+
kernel void bend_kq(device coherent(device) KW *H [[buffer(0)]], device KAU *A [[buffer(1)]],
619+
constant KParams &PP [[buffer(2)]], device KW *G [[buffer(3)]], uint lane [[thread_position_in_grid]]) {
620+
constant KParams *P = &PP;
621+
#else
622+
static void bend_kq(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {
623+
#endif
624+
if (lane >= P->nlanes || H[P->lane0 + (KW)lane * K_LANE] != PC_KQ) return;
625+
KCtx cx;
626+
KTHR KCtx *c = &cx;
627+
k_load(c, H, A, P, G, lane);
628+
bool ok = true;
629+
KW r = k_kq(c, &ok);
630+
c->rv = r;
631+
c->pc = ok ? c->kqret : c->kqfb;
632+
if (c->err != 0) {
633+
KU z = 0;
634+
K_CAS(&A[KA_ERR], z, c->err);
635+
}
636+
k_save(c);
605637
}

‎rt/gpu_src.h‎

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static const char K_GPU_H[] =
7676
"#define KA_HEAP 2\n"
7777
"#define KA_QHEAD 3\n"
7878
"#define KA_QTAIL 4\n"
79+
"#define KA_ACTIVE 5\n"
7980
"#define KA_SEQ 16\n"
8081
"\n"
8182
"// Where things are, in words of the arena (H) unless said otherwise.\n"
@@ -104,7 +105,7 @@ static const char K_GPU_H[] =
104105
"#define KQ_ARGS 8\n"
105106
"#define K_LANE 24 // words of a lane's saved state\n"
106107
"\n"
107-
"#define K_CHUNK 4096\n"
108+
"#define K_CHUNK 256\n"
108109
"\n"
109110
"// The errors a lane can hit.\n"
110111
"#define KE_FX 2 // an effect, or a closure with no device code\n"
@@ -168,7 +169,7 @@ static const char K_GPU_H[] =
168169
" KW k = (need + K_CHUNK - 1) / K_CHUNK;\n"
169170
" KW at = (KW)K_ADD(&c->A[KA_HEAP], (KU)k);\n"
170171
" KW start = c->P->heap0 + at * K_CHUNK;\n"
171-
" if (n + 1 > K_CHUNK || start + k * K_CHUNK > c->P->heap0 + c->P->heapw) {\n"
172+
" if (start + k * K_CHUNK > c->P->heap0 + c->P->heapw) {\n"
172173
" k_fail(c, KE_HEAP);\n"
173174
" c->hp = c->P->heap0;\n"
174175
" c->he = c->hp + K_CHUNK;\n"
@@ -510,16 +511,8 @@ static const char K_GPU_H[] =
510511
" c->pc = l;\n"
511512
"}\n"
512513
"\n"
513-
"#ifdef __METAL_VERSION__\n"
514-
"kernel void bend_kernel(device coherent(device) KW *H [[buffer(0)]], device KAU *A [[buffer(1)]],\n"
515-
" constant KParams &PP [[buffer(2)]], device KW *G [[buffer(3)]], uint lane [[thread_position_in_grid]]) {\n"
516-
" constant KParams *P = &PP;\n"
517-
"#else\n"
518-
"static void bend_kernel(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {\n"
519-
"#endif\n"
520-
" if (lane >= P->nlanes) return;\n"
521-
" KCtx cx;\n"
522-
" KTHR KCtx *c = &cx;\n"
514+
"// A lane's state, from and to its slot of the arena.\n"
515+
"KINLINE void k_load(KTHR KCtx *c, KCOH KW *H, KDEV KAU *A, KCP KParams *P, KDEV KW *G, KU lane) {\n"
523516
" c->H = H;\n"
524517
" c->G = G;\n"
525518
" c->Q = (KDEV KW *)H + P->q0;\n"
@@ -542,28 +535,53 @@ static const char K_GPU_H[] =
542535
" c->kqret = H[ls + 8];\n"
543536
" c->kqfb = H[ls + 9];\n"
544537
" for (int i = 0; i < KQ_ARGS; i++) c->kqa[i] = H[ls + 10 + i];\n"
538+
"}\n"
539+
"\n"
540+
"KINLINE void k_save(KTHR KCtx *c) {\n"
541+
" KW ls = c->P->lane0 + (KW)c->lane * K_LANE;\n"
542+
" c->H[ls] = c->pc;\n"
543+
" c->H[ls + 1] = c->fp;\n"
544+
" c->H[ls + 2] = c->rv;\n"
545+
" c->H[ls + 3] = c->dep;\n"
546+
" c->H[ls + 4] = c->hp;\n"
547+
" c->H[ls + 5] = c->he;\n"
548+
" c->H[ls + 6] = c->ax;\n"
549+
" c->H[ls + 7] = c->kq;\n"
550+
" c->H[ls + 8] = c->kqret;\n"
551+
" c->H[ls + 9] = c->kqfb;\n"
552+
" for (int i = 0; i < KQ_ARGS; i++) c->H[ls + 10 + i] = c->kqa[i];\n"
553+
"}\n"
554+
"\n"
555+
"KINLINE bool k_active(KW pc) { return pc != PC_IDLE && pc != PC_KQ; }\n"
556+
"\n"
557+
"// The kernel: each lane runs blocks, taking tasks from the queue when it has\n"
558+
"// none. A lane that reaches a gathered call (PC_KQ) leaves the dispatch, and\n"
559+
"// so does an idle one once no lane is running (A[KA_ACTIVE] counts them):\n"
560+
"// the host then runs the calls with bend_kq, all at once, and dispatches this\n"
561+
"// again. (A long call run here, inside this big kernel, runs 3-5x slower.)\n"
562+
"#ifdef __METAL_VERSION__\n"
563+
"kernel void bend_kernel(device coherent(device) KW *H [[buffer(0)]], device KAU *A [[buffer(1)]],\n"
564+
" constant KParams &PP [[buffer(2)]], device KW *G [[buffer(3)]], uint lane [[thread_position_in_grid]]) {\n"
565+
" constant KParams *P = &PP;\n"
566+
"#else\n"
567+
"static void bend_kernel(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {\n"
568+
"#endif\n"
569+
" if (lane >= P->nlanes) return;\n"
570+
" KCtx cx;\n"
571+
" KTHR KCtx *c = &cx;\n"
572+
" k_load(c, H, A, P, G, lane);\n"
545573
" KW budget = P->budget;\n"
546574
" for (KW step = 0; step < budget; step++) {\n"
575+
" if (c->pc == PC_KQ) break;\n"
547576
" if (c->pc == PC_IDLE) {\n"
548577
" if (K_LOAD(&A[KA_DONE]) != 0 || K_LOAD(&A[KA_ERR]) != 0) break;\n"
549-
" k_pop_task(c);\n"
550-
" }\n"
551-
" // A KQ_ call runs a whole subtree in one step. The lanes of a SIMD group\n"
552-
" // run in lockstep, so one lane alone in its call would hold up the rest:\n"
553-
" // a lane waits (PC_KQ) until each of its group waits too or has nothing\n"
554-
" // to do, and they make their calls together.\n"
555-
" bool kq = c->pc == PC_KQ;\n"
556-
" if (K_SIMD_ALL(kq || c->pc == PC_IDLE)) {\n"
557-
" if (kq) {\n"
558-
" bool ok = true;\n"
559-
" KW r = k_kq(c, &ok);\n"
560-
" c->rv = r;\n"
561-
" c->pc = ok ? c->kqret : c->kqfb;\n"
562-
" }\n"
563-
" } else if (!kq) switch (c->pc) {\n"
564-
" case PC_IDLE: {\n"
565-
" break;\n"
578+
" if (!k_pop_task(c)) {\n"
579+
" if (K_LOAD(&A[KA_ACTIVE]) == 0) break;\n"
580+
" continue;\n"
566581
" }\n"
582+
" K_ADD(&A[KA_ACTIVE], 1u);\n"
583+
" }\n"
584+
" switch (c->pc) {\n"
567585
" case PC_ROOT: {\n"
568586
" H[2] = c->rv;\n"
569587
" K_FENCE();\n"
@@ -589,23 +607,37 @@ static const char K_GPU_H[] =
589607
" break;\n"
590608
" }\n"
591609
" }\n"
610+
" if (!k_active(c->pc)) K_SUB(&A[KA_ACTIVE], 1u);\n"
592611
" if (c->err != 0) {\n"
593612
" KU z = 0;\n"
594613
" K_CAS(&A[KA_ERR], z, c->err);\n"
595614
" break;\n"
596615
" }\n"
597616
" }\n"
598-
" H[ls] = c->pc;\n"
599-
" H[ls + 1] = c->fp;\n"
600-
" H[ls + 2] = c->rv;\n"
601-
" H[ls + 3] = c->dep;\n"
602-
" H[ls + 4] = c->hp;\n"
603-
" H[ls + 5] = c->he;\n"
604-
" H[ls + 6] = c->ax;\n"
605-
" H[ls + 7] = c->kq;\n"
606-
" H[ls + 8] = c->kqret;\n"
607-
" H[ls + 9] = c->kqfb;\n"
608-
" for (int i = 0; i < KQ_ARGS; i++) H[ls + 10 + i] = c->kqa[i];\n"
617+
" k_save(c);\n"
618+
"}\n"
619+
"\n"
620+
"// The waiting calls, a lane each: a small kernel, which runs them fast.\n"
621+
"#ifdef __METAL_VERSION__\n"
622+
"kernel void bend_kq(device coherent(device) KW *H [[buffer(0)]], device KAU *A [[buffer(1)]],\n"
623+
" constant KParams &PP [[buffer(2)]], device KW *G [[buffer(3)]], uint lane [[thread_position_in_grid]]) {\n"
624+
" constant KParams *P = &PP;\n"
625+
"#else\n"
626+
"static void bend_kq(KW *H, KAU *A, const KParams *P, KW *G, uint32_t lane) {\n"
627+
"#endif\n"
628+
" if (lane >= P->nlanes || H[P->lane0 + (KW)lane * K_LANE] != PC_KQ) return;\n"
629+
" KCtx cx;\n"
630+
" KTHR KCtx *c = &cx;\n"
631+
" k_load(c, H, A, P, G, lane);\n"
632+
" bool ok = true;\n"
633+
" KW r = k_kq(c, &ok);\n"
634+
" c->rv = r;\n"
635+
" c->pc = ok ? c->kqret : c->kqfb;\n"
636+
" if (c->err != 0) {\n"
637+
" KU z = 0;\n"
638+
" K_CAS(&A[KA_ERR], z, c->err);\n"
639+
" }\n"
640+
" k_save(c);\n"
609641
"}\n"
610642
"\n"
611643
;

0 commit comments

Comments
 (0)