Skip to content

WaitAny on OnSubmittedWorkDone does not wait for submitted works' callbacks #513

Description

@zcbenz

Considering following code:

buffer.MapAsync(wgpu::CallbackMode::WaitAnyOnly, ...);
auto future = queue.OnSubmittedWorkDone(wgpu::CallbackMode::WaitAnyOnly, ...);
wgpu::FutureWaitInfo info{future};
instance.WaitOnly(1, &info, UINT64_MAX);

Intuitively I would assume the callback of MapAsync would be called when WaitOnly returns, but it does not because the future of MapAsync is not passed to WaitOnly.

However collecting all futures and then pass them all to WaitOnly is also not practical because number of futures WaitOnly can take is quite small and even a linear regression program can exceed it.

The options I have are:

  1. Still use WaitOnly but instead of one single call I split the futures into multiple WaitOnly calls.
  2. Use other callback mode in MapAsync and then call ProcessEvents after calling WaitOnly. (Note that even with AllowSpontaneous I still need a ProcessEvents to make the callback get invoked, I don't know if it is intended or a bug.)

But:

  1. The WaitOnly approach is overkill and hard-to-use when I only want to wait for submitted work done.
  2. Using other callbacks mode in MapAsync would make OnSubmittedWorkDone's callback get called before MapAsync's callbacks.

Activity

  1. lokokung commented on Feb 18, 2025

    @lokokung
    Collaborator

    Hello, unfortunately, this is currently by design. The intention here is that if you need the result of the MapAsync, you should be WaitAny-ing the wgpu::Future returned from it, not any other wgpu::Futures. Your idea to use a different mode, i.e AllowSpontaneous or ProcessEvents in the MapAsync and then WaitAny-ing the OnSubmittedWorkDone would make sense, but that isn't implemented in Dawn at the moment. We can investigate that solution in the meantime. Note that AllowSpontaneous is still very much a work-in-progress in Dawn because it isn't a high priority and comes with many technical difficulties... (looking at you Vulkan...).

  2. zcbenz commented on Feb 19, 2025

    @zcbenz
    Author

    Thanks for answering! I'll stick with WaitAny for now.

    As for AllowSpontaneous, I think it would be more useful if the API actually specifies the timing when callback is called. Using my above example, it would be great if the callback is guaranteed to be called in the thread where WaitOnly is called for OnSubmittedWorkDone, however the documents give me a feeling that the callback could even be called in device threads on some platforms.

  3. eliemichel commented on Apr 27, 2025

    @eliemichel
    Collaborator

    it would be great if the callback is guaranteed to be called in the thread where WaitOnly is called for OnSubmittedWorkDone.

    If you want this garranty, I think you can simply not use AllowSpontaneous. Then of course your (only) thread needs to hand the execution to the WebGPU instance from time to time to let it run the callbacks, which is what ProcessEvents is for.

    Collecting all futures and then pass them all to WaitOnly is also not practical because number of futures WaitOnly can take is quite small and even a linear regression program can exceed it.

    I'm curious about the details of your regression program, if I may. The way I imagine it, you would mostly have to wait for 1 future, namely the one that maps the result back on the CPU.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions