• Advertisement
Sign in to follow this  

Vulkan Confusing performance with async compute

This topic is 435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started to do some testing about this with compute workloads only, so not the typical 'Do compute while rendering shadow maps' or similar.

Motivation is to keep the GPU busy in cases when a dispatch has little or zero work, which is often unavoidable.

 

I use Vulkan on FuryX, but i'm interested in any experience / opinions so i post it here.

 

Test shader uses 64 threads. Each Thread reads one float, then build prefix sum in LDS, writes back.

On API side do 100 dispatches, but each time only one thread group (so one wavefront), memory barrier between each dispatch to simulate dependcies.

Do all of this 3 times on 3 different memory buffers, so we could do each time async.

 

 

Here are the results:

 

All in a single queue takes 1.41 ms

Using 3 queues i would expect to get one third of that, assuming my GPU can process 3 wavefronts in parallel :) what i really get is 0.64 ms, which is not that bad.

 

Now i remove the memory barriers. Getting wrong results but just to look how costly they are:

1 q.: 0.12 ms

3 q.: 0.16 ms ...now, starting to get bad

 

Next test is with barriers but zero work dispatches, so doing nothing.

1 q.: 0.43 ms   wtf?

3 q.: 0.50 ms   i saw it coming

 

Next is zero work dispatches without barriers, doing absolutely nothing

1 q.: 0.12 ms

3 q.: 0.18 ms

 

Next, empty command buffers without any dispatch for reference

1 q.: 0 ms         what's that? driver optimizations?

3 q.: 0.14 ms    should we subtract this from all async timings? makes sense...

 

 

Some confusing numbers. (Ok - more at the unpractical cases but worth to mention).

 

Probably i should explain how i measure time:

Reading a GPU timestamp at start and end of each queue does not work here - they act like barriers and seem to disturb async compute.

So i read time with Windows timeGetTime(), vkQueueSubmit() * q, vkQueueWaitIdle() * q, read time again and accumulate for 100 frames, print average

So this includes API overhead.

 

Maybe i should increase the workload, but it's all about small workloads, and 100 dispatches is already more i'll use in practice.

The reasons behind my small / zero workloads problem are dependencies between processing a tree by level. (But the same issue arises on pretty much anything beyond per pixel brute force)

I use prebuild command buffers with one indirect dispatch per tree level.

An alternative would be to pop the work from a queue and busy waiting until a higher tree level has been processed: One dispatch for the whole tree, but busy waiting on GPU? Sounds very bad to me.

 

 

EDIT:

I get resonable timings after making dispatches indirect.

I may be even illegal to dispatch zero work directly, but that's what i've done with above numbers.

Edited by JoeJ

Share this post


Link to post
Share on other sites
Advertisement

I think you should actually use this:

 

https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkCmdWriteTimestamp.html

 

In any API when you want to work with GPU and time it, you need to use timer queries (basically request the device to capture timestamp at some point of execution in some sort of a buffer) - and read them back to operate with them, otherwise the timing result will be wrong and imprecise. So basically what you should do is - before adding your execution commands into command buffer, add timestamp write command. And do the same after you add commands into your command buffer. You can ten read those values on host - using vkQueryPoolResults or copy them into VkBuffer using vkCmdCopyQueryPoolResults

 

Note, there is actually some further description directly in specs:

 

https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html#queries-timestamps 

Edited by Vilem Otte

Share this post


Link to post
Share on other sites

Yep, seems an issue of measuring time and doing pointless tests.

I'm too lazy to mess around with QueryPerformanceTimer right now. Summing up hundrets of 1's and 0's and dividing by framecount should give the same result - it just takes longer.

 

But enabling my GPU timestamp profiler (which already uses the thing Vilem mentioned) suddenly seems to work properly.

Don't ask me why. I definitively have had issues before that async took langer than not and disabling timestamps fixed it.

 

 

 

Repeating the first test i get now:

 

 

3 queues async: 0.71ms from my timeGetTime approach including overhead (timestamps are expensive, so it's more now)

 

And here the timings for each queue from GPU timestamps at start and end of each queue:

queue0, 1, 2:   0.42ms,  0.44ms,  0.43ms

 

And the differnece between the lowest and highest timestamp ist: 0.48ms

 

 

 

1 queue, no async:

 

timeGetTime: 1.42 ms

queue from GPU: 1.30 ms

 

 

 

I don't dare to repeat the meaningless tests, but lets change dispacht count from 100,100,100 to 200,100,50 and focus on GPU timestamps only:

1q.: 1.49ms

3q.: 0.86ms (0.83, 0.44, 0.23)

 

Now, additionaly change dispatch parameters from (1,1,1) to (20,1,1) wavefronts

1q.: 1.38ms

3q.: 0.80ms (0.79, 0.39, 0.20)

 

Confused again that 3x20 parallel wavefronts are even faster than 3x1, but beside that numbers look very good :)

Note that single queue takes only 2 timestamps but 3 queues take 6, so there is some additional overhead.

Seems very close to optimal. Nice :)

Share this post


Link to post
Share on other sites

One more thing i've found out: Atomics on global memory do not work across differnt queues.

Having each workgroup increasing the same value, i get fluctuating numbers between 4000 and 5000. The correct value would be 6000.

Seems each queue gets its own cache, even if all of them operate on the same memory buffer.

Maybe this is the reason i still can't get an advantage from async compute beyond syntetic tests...

Edited by JoeJ

Share this post


Link to post
Share on other sites

I found the reason for my problem. To go async with Vulkan you need to divide your command buffer into multiple command buffers to make synchronization by semaphores possible (There is no other way to sync 2 command buffers. Or am i wrong?)

I have division like this:

 

A (0.1ms - 34 invocations, mostly zero or tiny workloads)

B (0.5ms - 16 invocations, starting with tiny, ending with heavy workloads)

C (0.5ms - final work, at this point i need results from both A and B)

 

So i can do A and B simultaneously. My goal is to hide runtime of A behind B, and this totally works.

 

Option 1:

queue1: process A

queue2: process B, wait on A, process C

 

Option 2:

queue1: process A

queue2: process B

queue3: wait on A and B, process C

 

And here is the problem:

No matter what option i use, after successfully doing A and B in the desired time of 0.5ms,

The GPU does nothing for about 0.15ms, and only after this gap it starts processing C.

 

0.15ms - that's a lot. Do you think that indicates a driver issue?

Do you see something else i could try?

 

I may prepare a small project for AMD to show them...

 

 

EDIT:

 

Maybe the timestamp measurements cause the gap.

Looking at the CPU timer, if i turn timestamps on / off the difference is 0.3 ms.

 

Unfortunately there is no way to be sure.

Hopefully AMDs upcoming profiling tool will clarify...

Edited by JoeJ

Share this post


Link to post
Share on other sites

Ok, assuming both queues start at the same time (difference usually is about 0.01ms) and using only 2 timestamps instead of 6, i finally get a win of 0.05ms.

Share this post


Link to post
Share on other sites

I found the reason for my problem. To go async with Vulkan you need to divide your command buffer into multiple command buffers to make synchronization by semaphores possible

So is that cause you get

One more thing i've found out: Atomics on global memory do not work across differnt queues. Having each workgroup increasing the same value, i get fluctuating numbers between 4000 and 5000. The correct value would be 6000. Seems each queue gets its own cache, even if all of them operate on the same memory buffer.
  

 

If different queue gets' its own cache, my assumption of GPU cache strategy will be totally wrong, and I have to rewrite a lot of my code..... :(  

Share this post


Link to post
Share on other sites

 

I found the reason for my problem. To go async with Vulkan you need to divide your command buffer into multiple command buffers to make synchronization by semaphores possible

So is that cause you get

 

 

One more thing i've found out: Atomics on global memory do not work across differnt queues. Having each workgroup increasing the same value, i get fluctuating numbers between 4000 and 5000. The correct value would be 6000. Seems each queue gets its own cache, even if all of them operate on the same memory buffer.
  

 

If different queue gets' its own cache, my assumption of GPU cache strategy will be totally wrong, and I have to rewrite a lot of my code..... :(  

 

 

I'm not sure if you get the right conclusions of this and if it affects your made decissions at all. What's your example?

The atomic inconsistentcy hints atomics are implemented on cache (which is good - otherwise they would be probably very slow), but i don't know if this inconsistentcy is hardware / API related and how the API specs handle it. Could be different for VK / DX12.

But anyways it should not lead to serious limitations in practice: Either put all dispatches that do atomics to the same memory in one queue, or if you really want to use async compute sync multiple queue to ensure visible results.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By turanszkij
      Hi, right now building my engine in visual studio involves a shader compiling step to build hlsl 5.0 shaders. I have a separate project which only includes shader sources and the compiler is the visual studio integrated fxc compiler. I like this method because on any PC that has visual studio installed, I can just download the solution from GitHub and everything just builds without additional dependencies and using the latest version of the compiler. I also like it because the shaders are included in the solution explorer and easy to browse, and double-click to open (opening files can be really a pain in the ass in visual studio run in admin mode). Also it's nice that VS displays the build output/errors in the output window.
      But now I have the HLSL 6 compiler and want to build hlsl 6 shaders as well (and as I understand I can also compile vulkan compatible shaders with it later). Any idea how to do this nicely? I want only a single project containing shader sources, like it is now, but build them for different targets. I guess adding different building projects would be the way to go that reference the shader source project? But how would they differentiate from shader type of the sources (eg. pixel shader, compute shader,etc.)? Now the shader building project contains for each shader the shader type, how can other building projects reference that?
      Anyone with some experience in this?
    • By mark_braga
      I am working on a compute shader in Vulkan which does some image processing and has 1024 * 5=5120 loop iterations (5 outer and 1024 inner)
      If I do this, I get a device lost error after the succeeding call to queueSubmit after the image processing queueSubmit
      // Image processing dispatch submit(); waitForFence(); // All calls to submit after this will give the device lost error If I lower the number of loops from 1024 to 256 => 5 * 256 = 1280 loop iterations, it works fine. The shader does some pretty heavy arithmetic operations but the number of resources bound is 3 (one SRV, one UAV, and one sampler). The thread group size is x=16 ,y=16,z=1
      So my question - Is there a hardware limit to the number of loop executions/number of instructions per shader?
    • By AxeGuywithanAxe
      I wanted to see how others are currently handling descriptor heap updates and management.
      I've read a few articles and there tends to be three major strategies :
      1 ) You split up descriptor heaps per shader stage ( i.e one for vertex shader , pixel , hull, etc)
      2) You have one descriptor heap for an entire pipeline
      3) You split up descriptor heaps for update each update frequency (i.e EResourceSet_PerInstance , EResourceSet_PerPass , EResourceSet_PerMaterial, etc)
      The benefits of the first two approaches is that it makes it easier to port current code, and descriptor / resource descriptor management and updating tends to be easier to manage, but it seems to be not as efficient.
      The benefits of the third approach seems to be that it's the most efficient because you only manage and update objects when they change.
    • By khawk
      CRYENGINE has released their latest version with support for Vulkan, Substance integration, and more. Learn more from their announcement and check out the highlights below.
      Substance Integration
      CRYENGINE uses Substance internally in their workflow and have released a direct integration.
       
      Vulkan API
      A beta version of the Vulkan renderer to accompany the DX12 implementation. Vulkan is a cross-platform 3D graphics and compute API that enables developers to have high-performance real-time 3D graphics applications with balanced CPU/GPU usage. 

       
      Entity Components
      CRYENGINE has addressed a longstanding issue with game code managing entities within the level. The Entity Component System adds a modular and intuitive method to construct games.
      And More
      View the full release details at the CRYENGINE announcement here.

      View full story
    • By khawk
      The AMD GPU Open website has posted a brief tutorial providing an overview of objects in the Vulkan API. From the article:
      Read more at http://gpuopen.com/understanding-vulkan-objects/.


      View full story
  • Advertisement