Skip to content

Commit 202fd69

Browse files
committed
refining gpu puzzles site (WIP)
1 parent a11d2f5 commit 202fd69

File tree

4 files changed

+12
-50
lines changed

4 files changed

+12
-50
lines changed

experimental/fasthtml/components/code_editor.js

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function updateEditor(delta) {
77
if (delta.action === 'insert' && (delta.lines[0] === '.' || delta.lines[0] === ' ')) {
88
showCompletionSuggestion();
99
}
10-
1110
// Recover from errors TODO(avh): only do this if there's an error
1211
createModule().then((Module) => {
1312
// Keep your existing Module setup
@@ -16,7 +15,6 @@ function updateEditor(delta) {
1615
window.Module = Module;
1716
console.log("updateEditor() - Module ready");
1817
});
19-
2018
if (window.Module && window.Module.executeKernel) {
2119
console.log("Executing kernel");
2220
window.terminal.clear();

experimental/fasthtml/gpu_puzzles/build/.gitkeep

Whitespace-only changes.

experimental/fasthtml/gpu_puzzles/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const State = {
77
isModuleReady: false
88
};
99

10-
1110
function initializeApp() {
1211
initializeTerminal();
1312
initializeEditor();
1413
initializeModule();
1514
setupEventListeners();
15+
console.log("App initialized");
1616
}
1717

1818
function initializeTerminal() {
@@ -58,7 +58,7 @@ function initializeModule() {
5858
AppState.isModuleReady = true;
5959
console.log("Module initialized");
6060
// Attempt to run the kernel with the initial content
61-
updateEditor({ action: 'insert', lines: [''] });
61+
AppState.editor.updateEditor({ action: 'insert', lines: [''] });
6262
}).catch(error => {
6363
console.error("Failed to initialize module:", error);
6464
});

experimental/fasthtml/gpu_puzzles/run.py

+10-46
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,6 @@
1313
}
1414
"""
1515

16-
terminal_init = """
17-
const terminal = new Terminal();
18-
const fitAddon = new FitAddon.FitAddon();
19-
terminal.loadAddon(fitAddon);
20-
window.terminal = terminal;
21-
console.log("Terminal initialized");
22-
"""
23-
24-
print_script = """
25-
window.customPrint = function(text) {
26-
console.log(text);
27-
if (window.terminal) {
28-
window.terminal.writeln(text);
29-
} else {
30-
console.warn("Terminal not initialized");
31-
}
32-
};
33-
createModule().then((Module) => {
34-
Module.print = window.customPrint;
35-
Module.printErr = window.customPrint;
36-
window.Module = Module;
37-
console.log("Initial module created");
38-
});
39-
"""
40-
4116
bind_terminal = """
4217
window.terminal.open(document.getElementById('output'));
4318
fitAddon.fit();
@@ -60,23 +35,6 @@
6035
}
6136
""".strip()
6237

63-
cpp_code = """
64-
static constexpr size_t N = 4;
65-
Tensor a = createTensor(ctx, {N}, kf32, makeData<N>().data());
66-
Tensor output = createTensor(ctx, {N}, kf32);
67-
Kernel op = createKernel(ctx, {kPuzzle1, N}, Bindings{a, output},
68-
/*nWorkgroups */ {1, 1, 1});
69-
70-
std::promise<void> promise;
71-
std::future<void> future = promise.get_future();
72-
dispatchKernel(ctx, op, promise);
73-
std::array<float, R * C> outputArr;
74-
wait(ctx, future);
75-
toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
76-
printf("%s", show<float, R, C>(outputArr, "output").c_str());
77-
""".strip()
78-
79-
8038
def controls():
8139
# left and right buttons
8240
return Div(
@@ -202,13 +160,21 @@ def CodeEditor(initial_content: str):
202160
# cls="flex flex-col h-screen w-full", style="height: 100vh; overflow: hidden;"
203161
style="height: 33vh; overflow: hidden;",
204162
),
205-
# Script(editor_script(initial_content)),
163+
Script(editor_script(initial_content)),
206164
)
207165

208166

209167
# TODO(avh) : Global state handling of terminal binding, module creation, etc.
210168
# could be improved
211169

170+
init_app = """
171+
document.addEventListener('DOMContentLoaded', () => {
172+
window.AppState = Object.create(State);
173+
const AppState = window.AppState;
174+
initializeApp();
175+
});
176+
""".strip()
177+
212178
HDRS = (
213179
picolink,
214180
# ace code editor
@@ -217,15 +183,13 @@ def CodeEditor(initial_content: str):
217183
Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/xterm/css/xterm.css"),
218184
Script(src="https://cdn.jsdelivr.net/npm/xterm/lib/xterm.js"),
219185
Script(src="https://cdn.jsdelivr.net/npm/xterm-addon-fit/lib/xterm-addon-fit.js"),
220-
# Script(terminal_init),
221186
Script(src="/build/run.js"), # gpu.cpp runtime
222-
# Script(print_script),
223187
Style(global_style),
224188
Link(rel="stylesheet", href="https://unpkg.com/tippy.js@6/dist/tippy.css"),
225189
Script(src="https://unpkg.com/@popperjs/core@2"),
226190
Script(src="https://unpkg.com/tippy.js@6"),
227191
Script(src="/client.js"),
228-
Script("document.addEventListener('DOMContentLoaded', () => { initializeApp(); });"),
192+
Script(init_app),
229193
*Socials(
230194
title="gpu.cpp gpu puzzles",
231195
description="",

0 commit comments

Comments
 (0)