Skip to content

Commit 61b92da

Browse files
committed
resource deallocation (debugging WIP)
1 parent 8639720 commit 61b92da

File tree

11 files changed

+416
-183
lines changed

11 files changed

+416
-183
lines changed

.github/workflows/build.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: build
2+
3+
on:
4+
push:
5+
pull_request:
6+
types: [opened, reopened, labeled, unlabeled, synchronize]
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
- name: Build
12+
run:
13+
echo "Hello, ${{ github.actor }}"
14+

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ clean-build:
6666

6767
clean:
6868
read -r -p "This will delete the contents of build/* and third_party/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* third_party/fetchcontent/*
69+
70+
all: build
71+
cd examples/gpu_puzzles && make
72+
cd examples/hello_world && make
73+
cd eamples/raymarch && make
74+
cd examples/webgpu_intro && make

README.md

+3-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ gpu.cpp provides a minimal set of composable functions and types that make
2323
WebGPU compute simple and concise to work with for GPU compute R&D use cases,
2424
while keeping abstractions minimal and transparent.
2525

26-
# Hello World: A GELU Kernel
26+
## Hello World: A GELU Kernel
2727

2828
Here's an GELU kernel implemented (based on the CUDA implementation of
2929
[llm.c](https://github.com/karpathy/llm.c)) as an on-device WGSL shader and
@@ -80,7 +80,7 @@ For those curious about what happens under the hood with the raw WebGPU API,
8080
the equivalent functionality is implemented using the WebGPU C API in
8181
`examples/webgpu_intro/run.cpp`.
8282

83-
## Quick Start
83+
## Quick Start: Building and Running
8484

8585
The only dependency of this library is a WebGPU implementation. Currently we
8686
recommend using the Dawn backend until further testing, but we plan to support
@@ -115,18 +115,8 @@ Welcome!
115115
116116
This program is a brief intro to the gpu.cpp library.
117117
118-
You can use the library by simply including the gpu.h header, starting with a
119-
build template (see examples/hello_gpu/ for a template project that builds the
120-
library).
121-
122-
#include "gpu.h"
123-
124-
See `examples/hello_world/` for an examle of build scripts to run a standalone
125-
program that uses this library.
118+
...
126119
127-
┌──────────────────────────────┐
128-
│ Press Enter to Continue... │
129-
└──────────────────────────────┘
130120
```
131121

132122
The first time you build and run the project, it will download the WebGPU

examples/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Examples (Work-in-Progress)
22

3-
NOTE: These examples are a work-in-progress - do not use them yet.
4-
53
Here are some standalone projects exemplifying how to use this library as well
6-
as what's under the hood.
4+
as what's under the hood. NOTE: These examples are a work-in-progress.
5+
76

87
In order of expertise (beginner to advanced):
98

109
- hello_world - Minimal template project to get started with the gpu.cpp library.
11-
- raymarch (TODO) - Using the webgpu computation to implement a toy raymarcher
10+
- gpu_puzzles - (WIP) Implementation of Sasha Rush's GPU puzzles https://github.com/srush/GPU-Puzzles
11+
- raymarch - (WIP) Using the webgpu computation to implement a toy raymarcher
1212
as an example of non-ML general purpose computation.
1313
- webgpu_intro - A minimal from-scratch example of how to use WebGPU directly
1414
without this library. This is useful to understand the code internals of

examples/gpu_puzzles/run.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void puzzle3(GPUContext& ctx) {
120120
// ...
121121

122122
int main(int argc, char **argv) {
123-
GPUContext ctx = CreateGPUContext();
123+
GPUContext ctx = CreateContext();
124124
puzzle1(ctx);
125125
puzzle2(ctx);
126126
puzzle3(ctx);

examples/hello_world/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ FASTBUILD_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=ON
88
run:
99
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && make -j$(NUM_JOBS) $(TARGET) && ./$(TARGET)
1010

11+
watch:
12+
@command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; }
13+
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET) && make -j$(NUM_JOBS) $(TARGET) && ./$(TARGET)"
14+
1115
clean:
1216
read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/*
1317

examples/hello_world/run.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "gpu.h"
22
#include "nn/shaders.h"
3+
#include "utils/logging.h"
34
#include <array>
45
#include <cstdio>
56

@@ -23,7 +24,8 @@ fn main(
2324
)";
2425

2526
int main(int argc, char **argv) {
26-
GPUContext ctx = CreateGPUContext();
27+
log(kDefLog, kInfo, "Hello, gpu.cpp!");
28+
GPUContext ctx = CreateContext();
2729
fprintf(stdout, "\nHello, gpu.cpp\n\n");
2830
static constexpr size_t N = 3072;
2931
std::array<float, N> inputArr;
@@ -35,7 +37,7 @@ int main(int argc, char **argv) {
3537
GPUTensor output = CreateTensor(ctx, {N}, kf32, outputArr.data());
3638

3739
Kernel op = CreateKernel(ctx, CreateShader(kGelu, 256, kf32),
38-
std::array{input}, output);
40+
input, output);
3941
DispatchKernel(ctx, op);
4042
Wait(ctx, op.future);
4143
ToCPU(ctx, output, outputArr.data(), sizeof(outputArr));

examples/raymarch/run.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int main(int argc, char **argv) {
7676
float sphereCenterZ;
7777
} params = {NCOLS, NROWS, 0.5, 0.0, 0.0, 0.0};
7878

79-
GPUContext ctx = CreateGPUContext();
79+
GPUContext ctx = CreateContext();
8080
GPUTensor devScreen = CreateTensor(ctx, {NROWS, NCOLS}, kf32, screen.data());
8181
Kernel render = CreateKernel(ctx, ShaderCode{kSDF, 64}, {}, 0, devScreen, params);
8282
DispatchKernel(ctx, render);

0 commit comments

Comments
 (0)