Skip to content

Async Control Flow Refactor#1

Open
MichealReed wants to merge 17 commits intoorigin/devfrom
improve-async
Open

Async Control Flow Refactor#1
MichealReed wants to merge 17 commits intoorigin/devfrom
improve-async

Conversation

@MichealReed
Copy link
Copy Markdown
Owner

• Refactored key GPU operations (createContext, toCPU, dispatchKernel, and createKernel) to return std::future instead of blocking with busy-wait loops.
• Replaced inline lambda callbacks with free‑standing functions that use promise/future for asynchronous completion.
• Updated createKernel (and its templated overload) to report shader module compilation asynchronously (instead of synchronously busy‑waiting), and dispatchKernel now returns a future that is fulfilled when the kernel dispatch is complete.
• Replaced wait with WaitForFuture(ctx.instance, future) as a template to return future objects after webgpu processing is complete.

Example usage:

  // Create kernel asynchronously
  std::future<Kernel> kernelFuture = createKernel(ctx, code, bindings, totalWorkgroups);
  Kernel kernel = waitForFuture(ctx.instance, kernelFuture);

  // Dispatch the kernel and wait for completion asynchronously
  std::future<void> dispatchFuture = dispatchKernel(ctx, kernel);
  waitForFuture(ctx.instance, dispatchFuture);

Why This Is Better:
• No blocking loops or synchronous waits – all async work is chained via futures, letting the event loop run naturally.
• Reduced risk of reentrancy issues and resource errors due to improper lifetime management.
• Cleaner separation between asynchronous callbacks and higher‐level logic, making the code easier to maintain and integrate (especially in WebAssembly/Emscripten contexts).

@MichealReed MichealReed changed the title refactors async Async Control Flow Refactor Feb 19, 2025
@MichealReed
Copy link
Copy Markdown
Owner Author

MichealReed commented Feb 20, 2025

Only breaking change with latest commit is dispatchKernel and toCPU no longer take a promise, toCPUAsync and dispatchKernelAsync should be used instead to get futures. Manually waiting should no longer be necessary with the sync call though, even in a emscripten context.

@MichealReed
Copy link
Copy Markdown
Owner Author

Added an optional param with this refactor to set a offset for the toCPU buffer read. This is tested in the new test/test_gpu.cpp. I think the toCPU with copydata is now redundant. Will leave it alone if you prefer to keep it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant