Skip to content

finish hw04 #35

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

finish hw04 #35

wants to merge 1 commit into from

Conversation

QifanWang
Copy link

优化效果

环境:Win10 WLS/Ubuntu 20.04 + GCC9.3.0_x86_64

优化手段 耗时 加速比
initial 6275 ms 1
AOS -> SOA 4338 ms 1.45
AOS -> SOA -O3 1695 ms 3.70
AOS -> SOA -O3 -ffast-math -march=native 182 ms 34.48

说明

  • AOS -> SOA
  1. 修改 stars 为 Struct of Array 的类型,修改相应的计算代码。这里用 C array 而不是 std::array<float, N> 因为感觉没有必要上模板(计算逻辑不是十分依赖STL与模板,有时间可以比较两种数组的优化)。
  2. 并提取部分公共子表达式,如 G * dt 。将部分常量用 const 修饰以期 compiler 优化。值得注意的是,不是所有公共表达式都需要提取,如 1 / RAND_MAX * 2 就不能。因为 1 / RAND_MAX 就是 0.0f ,这会导致计算逻辑错误。
  • -O3 -ffast-math -march=native
    开启编译器优化前后,对比汇编代码,在优化后 step 函数原来的标量计算指令,许多都被替换成 mulpsaddpsshufps 等 SIMD 指令的结合操作,而且有循环的展开,使得计算速度大幅提升。

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant