【问题标题】:Using GPU for number-crunching and rendering at the same time in parallel使用 GPU 同时并行进行数字运算和渲染
【发布时间】:2013-10-02 19:59:54
【问题描述】:

渲染作业和数字运算作业(例如 OpenCL 上的)能否在同一个 GPU 上有效共享?例如,

  1. 线程 A 运行 OpenCL 任务生成图像
  2. 然后,当图像准备好时,线程 A 通知另一个线程 B(图像准备好)并继续进行新的图像计算
  3. 线程 B 在给定图像上启动一些预显示活动(例如使用 GDI 进行叠加计算),组合最终图像并将其渲染以显示

这种 GPU 资源共享能否提高性能,或者相反,会导致计算和渲染任务的整体速度变慢?

谢谢

【问题讨论】:

    标签: multithreading parallel-processing rendering opencl gdi


    【解决方案1】:

    这里涉及到许多因素,但一般来说,您不应该看到放缓。

    直接回答问题的问题:

    • OpenCL 也可能正在使用您的 CPU,具体取决于您的设置方式
    • 您的 gfx 内容可以主要在 CPU 或 GPU 的不同部分完成,具体取决于您显示的内容;例如,许多 GDI 实现使用 CPU 进行渲染,并且只在 GPU 上使用非常简单的 2D 加速技术,主要用于对最终合成图像进行 blit。
    • 这可能取决于您使用的 GPU、GPU 驱动程序、图形堆栈等。

    在大多数情况下,您将通过尝试或至少对不同部分进行基准测试来获得最佳答案。毕竟,如果您的计算太简单或图像渲染部分太简单,您将不会真正获得太多好处。

    您还可以尝试更进一步并使用着色器等渲染结果 - 在这种情况下,您可以避免将数据从 gpu 内存移回主内存,这可能 - 根据您的情况 - 也会给出你的速度提升。

    【讨论】:

      【解决方案2】:

      如果数据/运算比率很大,并且您必须将数据从 cpu 发送到 gpu:

      紧缩 ---> 紧缩 ----> 渲染

      GPU  th0        :  crunch for (t-1)     crunch for (t)         rendering
      CPU  th1        :  send data for t      send data for t+1      send data for t+2
      CPU  th2        :  get data of t-2      get  data of t-1       get data of t
      CPU th3-th7     :  Other things independent of crunching or rendering.
      At the same time:  crunching&comm.      crunching&comm.        rendering&communication
                         and other things     and other things       and other things
      

      如果数据/运算比率很大,并且您不必将数据从 cpu 发送到 gpu:

       use interoperatability of CL (example: cl-gl interop)
      

      如果数据/处理比率很小

       should not see any slowdown.
      

      中等数据/加工比例:紧缩 --> 渲染 --->紧缩 ---> 渲染

      GPU th0         :  crunch for  (t)      rendering              crunch for (t+1)         render again! and continue cycling like this
      CPU th1         :  get data of (t-1)    send data for t+1      get data of (t)
      CPU th2-th7     :  Other things independent of crunching or rendering.
      At the same time:  crunching&getting.   rendering&sending.     crunching&getting
                         and other things     and other things       and other things
      

      【讨论】:

      • 如果我猜对了,1) 对于非繁重或简单的计算,在一个线程中运行基于 GPU 的计算并在另一个线程中渲染 - 应该不会出现任何减速,2) 对于 GPU 上的繁重计算- 最好使用相同的线程进行计算和渲染。对吗?
      • 不,渲染和处理都在同一个线程中:thread-0 :) 所以它们之间没有等待。但是线程 0 和其他线程之间需要同步。对于重度版本,仅更改了处理/渲染的比例,并且 getter/sender 位于不同的线程中。