Followers 0

# Many dispatch calls vs. higher ThreadGroupCount

## 8 posts in this topic

I wonder if it is performance wise much worse to do more dispatch calls instead of a higher ThreadGroupCount.

To explain this in a stupid example:

ID3D11DeviceContext::Dispatch(10,1,1);

vs

for(int i = 0; i < 10; i++)
ID3D11DeviceContext::Dispatch(1,1,1);

Has anyone tested this already or has some theoretical knowledge?

0

##### Share on other sites

My understanding is dispatch(1,1,1) is a bad idea.  I think the most recent directcompute performance advice from both vendors is in this presentation:

It's toward the end of the slides.  I believe ideally you want your dispatch threadgroups to be a multiple of 64 to ensure full thread occupancy on the hardware, since the GPUs run threads in groups of 32/64 (ie. their warp size), though the ideal thread group size depends on the numthreads in the actual hlsl compute shader, i would assume.

I guess technically dispatch(1,1,1) would be fine if you wanted to process a tiny 16x16 texture with [numthreads(16,16,1)] in your hlsl, since that'd give you a single thread group of 256 threads.

However for processing more realistic data sizes, I think it's a bad idea to manually call multiple dispatches on a single threadgroup rather than call a single dispatch with multiple threadgroups.  Multiple dispatch calls would certainly cause more API overhead, and I would expect the driver/hardware scheduling will handle the single call better also.

Perhaps a good analogy would be drawing a 10 triangle object with a single drawindexed(30,0,0) versus looping 10 times with drawindexed(3, i*3, 0), you'd never do the latter.

1

##### Share on other sites

Yep I know about the thread group size. But for example if your shader is configured so that you have [numthreads(64, 1, 1)] as thread group size you could dispatch(1,1,1) (one) of that group and still have a full 64 threads running. As far as I know you should always run a multiple of 64.

So, if we take my first example again:

ID3D11DeviceContext::Dispatch(10,1,1); // dispatches 10 x 64 threads -> 640
for(int i = 0; i < 10; i++)
ID3D11DeviceContext::Dispatch(1,1,1); // dispatches 10 x 64 threads -> 640

Lets say I am forced to use the second "solution" for some complicated reason. I wonder how much worse it will be. Or what other disadvantage I would have.

Edited by me_12
0

##### Share on other sites

Yep I know about the thread group size. But for example if your shader is configured so that you have [numthreads(64, 1, 1)] as thread group size you could dispatch(1,1,1) (one) of that group and still have a full 64 threads running. As far as I know you should always run a multiple of 64.

So, if we take my first example again:

ID3D11DeviceContext::Dispatch(10,1,1); // dispatches 10 x 64 threads -> 640
for(int i = 0; i < 10; i++)
ID3D11DeviceContext::Dispatch(1,1,1); // dispatches 10 x 64 threads -> 640

Lets say I am forced to use the second "solution" for some complicated reason. I wonder how much worse it will be. Or what other disadvantage I would have.

if you only have one thread group, whatever it's size, you will probably run into issues when the GPU stalls it's execution waiting for mem fetches / whatever. Typically it would then just start working on another warp/wavefront, and get back to the original one once the data was ready and it's current group stalled for some reason.

If you dispatch many groups one by one, there is no guarantee that they will run in parallel and allow the GPU to jump in between. ( In fact, I am pretty sure than on current PC gpus you are guaranteed that they won't ).

1

##### Share on other sites

I'm no expert in directcompute, really just starting out with it, but I would think the difference in your example would come down to how the API command queue is handled by the driver.  I mean your hardware probably has more than 64 threads available, but will it share them amongst multiple small dispatches?

Assuming each loop is operating on different data and there are no collisions, it could run the multiple dispatches in parallel the same as the single dispatch.  I don't know what level of support GPUs have for concurrently running multiple compute kernels, and that's what it'd come down to.

Unless anyone else here is more knowledgeable on how compute command queues are handled once they leave the API, my advice would be to profile it.  You can use http://graphics.stanford.edu/~mdfisher/GPUView.html (part of the SDK now) to check the command queue in the API and see how the driver/gpu schedules your calls to see if the looped version runs in series or parallel.   You could also grab a vendor profiling tool like nsight for nvidia or perfstudio for amd to check that also, and get some feedback on the thread occupancy as well.

Edit: Just noticed ATEfred's reply and he put it more succintly, you're asking the GPU to run multiple dispatches concurrently and it sounds like that isn't supported.

Edited by backstep
1

##### Share on other sites

Ah thank you for that tip with GPUView. Before I try getting this to work maybe ATEFred knows the answer about the question if they run in parallel or series.

Because actually I would need them to run in series. I am doing something like this:

for(int i = 0; i < x; i++) {
// 1. update a small constant buffer which holds data for the upcoming particle spawns processed in the dispatch call
// 2. Dispatch(1,1,1);
}

So if this constant buffer gets overwritten by the second loop while the first dispatch is not done it would access wrong data. :/

I could also do it like this:

// 1. bind 250kb data to hold the full spawn over time graph processed in this dispatch call
// 2. Dispatch(x,1,1);


The thing is, that the x is variable. Its random based on time passed. It can be 1 or it can be 30000. But mostly, like 99% of the time it will be 1.

Thats why I thought it might be better to just pass the data actually needed in small junks and process only that bit. And if it happens that I actually need more than 1 pass, I would just use a loop.

Maybe there is even another solution i haven't noticed yet.

If it was only possible to allocate memory dynamically... but no I have to allocate as much memory as is needed in the worst case.

Edited by me_12
0

##### Share on other sites

If you've called dispatch before mapping the constant buffer, then I believe it won't get overwritten.  Depending on the type of constant buffer (usage and cpu access) and how you're mapping it, it will either stall the pipeline or use an alias of the buffer for the map/unmap.  That's to say, it'll either wait to update the buffer until it's not in use, or if you're mapping with discard it'll just update to an alias of it without making your context cpu thread wait.  That's my understanding at least.

I am still a beginner like you, but a better approach might be to use a SRV of a structured buffer instead of a constant buffer.  That would allow you to update with a single map (write_discard to prevent a stall), then use a single dispatch with multiple threadgroups when required, and also avoid constant waterfalling performance problems.

Oh also if you don't need to update the whole 100KB every frame, you could simply make two structured buffers - a smaller one for typical use cases, and a 100KB one for your worst case scenario.  Then simply map/update/unmap/bind whichever one's SRV is required for that frame.  It's not quite dynamic allocation but it'd allow you the benefit of the smaller buffer 99% of the time.

Also from what I can tell from a quick google, directcompute does only support a single dispatch being processed on the GPU at once (slide 43 in http://www.slideshare.net/NVIDIA/1015-gtc09-5013444 ).  CUDA appears to have some kernel concurrency support, but that's something else entirely.

Edited by backstep
1

##### Share on other sites

Thank you guys!

Oh also if you don't need to update the whole 100KB every frame, you could simply make two structured buffers - a smaller one for typical use cases, and a 100KB one for your worst case scenario.  Then simply map/update/unmap/bind whichever one's SRV is required for that frame.  It's not quite dynamic allocation but it'd allow you the benefit of the smaller buffer 99% of the time.

This is kind of what I am doing now.

I have a very small buffer and one overflow buffer. 99% of the time the small one will be used. Only if the system lags or if I have more data to process, the overflow buffer will be used as well. And in some very extrem situation or when doing a prewarm I run the overflow buffer in a loop.

0

## Create an account

Register a new account