CG Hardware - GPU

Date
Mar 14, 2021
Tag
MasterApplication

半导体

半导体Semiconductor是一种电导率在绝缘体至导体之间的物质。

二极管 Diode

notion image
只能从 A 端流到 C 端

与门 (And Gate)

通过二极管构造与门逻辑

notion image
  • 当 A, B 都为高电压,二极管不导通,所有电流流向 Y,Y 为高电压
  • 当 A 为 0,B 为高电压,A 支路导通,B 支路断路,几乎所有电流流向 A,Y 为 0
  • 当 A,B 均为 0,同上,Y 为 0
通过类似的原理,我们可以构造或门,非门,与非门,或非门,异或门

运算

有了各种门,我们就能设计二进制加法器来进行运算了。
notion image
有了加法器,通过移位和加算来实现乘法,除法会有些复杂,但同理也可以实现。
再加以flip-flop触发器或 latch 锁存器来在电路中存储数据。通过并联可以得到[Register](https://en.wikipedia.org/wiki/Shift_register#:~:text=A shift register is a,one location to the next.)寄存器
以及多进一出的选择器 MUX Multiplexer,并利用它来控制选择流入哪一个计算模块。

编程

至此我们可以用机器码来实现不同的运算和数据存储。通过编译原理的相关知识,我们可以通过人类简单易懂的编程语言更快更简单地写出机器码。

CPU, GPU

中央处理器 CPU 整合了以上的所有运算功能和控制功能,以特定的时钟频率一条条执行机器码。通过多核心或者不断在程式中切换以实现并行。
传统的 CPU 是专门设计来用于通用计算的,而图形处理器 GPU GPU 是特殊类型的处理器,具有大量的内核(Core)来并行地进行大量的运算。
notion image
CPU
GPU
Central Processing Unit
Graphics Processing Unit
Several cores
Many cores
Low latency
High throughput
Good for serial processing
Good for parallel processing
Can do a handful of operations at once
Can do thousands of operations at once

Performance Test

CPU version

  • Rendering a 1200\*800 image with 10 samples per pixel
  • Time taken: 8m44.008s
chauncey@chauncey-OMEN:~/Workspace/ray-tracing$ time ./main > main.ppm real 8m44.008s user 8m41.179s sys 0m2.124s
notion image

GPU version

  • Rendering a 1200\800 image with 10 samples per pixelin 88 blocks.
  • Time taken: 0m8.654s
chauncey@chauncey-OMEN:~/Workspace/ray-tracing$ time ./main > main-cuda.ppm Rendering a 1200*800 image with 10 samples per pixelin 8*8 blocks. took 6.83485 seconds. real 0m8.654s user 0m4.893s sys 0m3.497s
notion image

结果

通过使用GPU,渲染一张光线追踪的图像所花费的时间从524秒缩减到了短短8.65秒。6000%的加速令人印象深刻。

Reference