Skip to content

Commit 17e7cc0

Browse files
committed
gpu puzzles improve output buffering
1 parent 94c5ab5 commit 17e7cc0

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

experimental/fasthtml/gpu_puzzles/client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const PuzzleSpec = [
9292

9393
function initializeApp() {
9494
initializeTerminal();
95-
initializeEditor("[replace this with your code]");
95+
initializeEditor();
9696
initializeModule();
9797
setupEventListeners();
9898
console.log("App initialized");
@@ -108,7 +108,7 @@ function initializeTerminal() {
108108
console.log("Terminal initialized");
109109
}
110110

111-
function initializeEditor(initialContent) {
111+
function initializeEditor() {
112112
AppState.editor = ace.edit("editor");
113113
// AppState.editor.setTheme("ace/theme/monokai");
114114
AppState.editor.setTheme("ace/theme/dracula");
@@ -121,7 +121,7 @@ function initializeEditor(initialContent) {
121121
wrap: true,
122122
});
123123
AppState.editor.setKeyboardHandler("ace/keyboard/vim");
124-
AppState.editor.setValue(initialContent || "");
124+
// AppState.editor.setValue(initialContent || "");
125125
console.log("Initial content:\n", initialContent);
126126
console.log("Editor initialized");
127127
}

experimental/fasthtml/gpu_puzzles/evaluator.h

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ std::vector<float> runPuzzle2(Context &ctx, TestCase &testCase,
206206
CompilationInfo &compilationInfo) {
207207
size_t N = testCase.input.size() / 2;
208208

209-
std::vector<float> aVec(testCase.input.begin(), testCase.input.begin() + N);
210-
std::vector<float> bVec(testCase.input.begin() + N, testCase.input.end());
209+
std::vector<float> aVec(begin(testCase.input), begin(testCase.input) + N);
210+
std::vector<float> bVec(begin(testCase.input) + N, end(testCase.input));
211211

212212
Tensor a = createTensor(ctx, {N}, kf32, aVec.data());
213213
Tensor b = createTensor(ctx, {N}, kf32, bVec.data());
@@ -1050,7 +1050,7 @@ bool evaluate(Context &ctx, const std::string &kernelCode, int puzzleIndex) {
10501050
// wprintf("Time taken: %f s\n", elapsed.count());
10511051

10521052
bool compilePassed = true;
1053-
int ptr = 0;
1053+
size_t ptr = 0;
10541054
constexpr size_t kBufSize = 1024 * 10;
10551055
char buf[kBufSize];
10561056

@@ -1073,47 +1073,54 @@ bool evaluate(Context &ctx, const std::string &kernelCode, int puzzleIndex) {
10731073
}
10741074

10751075
// ptr = 0;
1076+
const char *successColor = compilePassed ? green : red;
10761077
if (compilePassed && checkOutput(output, testCase.expectedOutput)) {
10771078
ptr += snprintf(buf + ptr, kBufSize, "Test case %d %sPASSED%s\n\n\r",
10781079
caseIdx + 1, green, reset);
10791080
} else {
10801081
ptr += snprintf(buf + ptr, kBufSize, "Test case %d %sFAILED%s\n\n\r",
10811082
caseIdx + 1, red, reset);
10821083
allPassed = false;
1084+
successColor = red;
10831085
}
10841086

10851087
ptr += snprintf(buf + ptr, kBufSize,
1086-
"\033[1;30mWorkgroup Size ( %s )\n\r",
1087-
toString(testCase.workgroupSize).c_str());
1088+
"\033[1;30mWorkgroup Size (%s %s %s)\n\r",
1089+
reset, toString(testCase.workgroupSize).c_str(), grey);
10881090
ptr += snprintf(buf + ptr, kBufSize,
1089-
"Number of Workgroups ( %s )\n\033[0m\n\r",
1090-
toString(testCase.gridSize).c_str());
1091+
"Number of Workgroups (%s %s %s)\n\033[0m\n\r",
1092+
reset, toString(testCase.gridSize).c_str(), grey);
10911093

1092-
wprintf("%s", buf);
1094+
// wprintf("%s", buf);
10931095

1096+
char titleBuf[kBufSize];
10941097
if (testCase.nInputs > 1) {
10951098
for (size_t inp = 0; inp < testCase.nInputs; ++inp) {
10961099
size_t sz = testCase.input.size() / testCase.nInputs;
10971100
size_t offset = inp * sz;
1098-
snprintf(buf, sizeof(buf), "%sInput %zu%s", grey, inp, reset);
1099-
printVec({begin(testCase.input) + offset,
1101+
snprintf(titleBuf, sizeof(titleBuf), "%sInput %zu%s ", grey, inp, reset);
1102+
// Note this is using pointer initialization of a vector of floats
1103+
// by specifying start and end pointers
1104+
printVecBuf({begin(testCase.input) + offset,
11001105
begin(testCase.input) + offset + sz},
1101-
buf);
1106+
titleBuf, buf, ptr);
11021107
}
11031108
} else {
1104-
snprintf(buf, sizeof(buf), "%sInput %s", grey, reset);
1105-
printVec(testCase.input, buf);
1109+
snprintf(titleBuf, sizeof(titleBuf), "%sInput %s", grey, reset);
1110+
printVecBuf(testCase.input, titleBuf, buf, ptr);
11061111
}
1112+
ptr += snprintf(buf + ptr, kBufSize, "\n");
11071113
if (compilePassed) {
1108-
wprintf("");
1109-
snprintf(buf, sizeof(buf), "%sGot %s", grey, reset);
1110-
printVec(output, buf);
1111-
wprintf("");
1114+
// wprintf("");
1115+
snprintf(titleBuf, sizeof(titleBuf), "%sGot %s", successColor, reset);
1116+
printVecBuf(output, titleBuf, buf, ptr);
1117+
// wprintf("");
11121118
}
11131119

1114-
snprintf(buf, sizeof(buf), "%sExpected%s", grey, reset);
1115-
printVec(testCase.expectedOutput, buf);
1116-
wprintf("");
1120+
snprintf(titleBuf, sizeof(titleBuf), "%sExpected %s", successColor, reset);
1121+
printVecBuf(testCase.expectedOutput, titleBuf, buf, ptr);
1122+
wprintf("%s", buf);
1123+
// wprintf("");
11171124
}
11181125

11191126
return allPassed;

0 commit comments

Comments
 (0)