gpt4 book ai didi

cuda - NVCC 在代码优化方面有多好?

转载 作者:行者123 更新时间:2023-12-04 17:56:44 25 4
gpt4 key购买 nike

NVCC 对设备代码的优化效果如何?它是否进行了诸如常量折叠和公共(public)子表达式消除之类的优化?

例如,它会减少以下内容:

float a = 1 / sqrtf(2 * M_PI);
float b = c / sqrtf(2 * M_PI);

对此:
float sqrt_2pi = sqrtf(2 * M_PI); // Compile time constant
float a = 1 / sqrt_2pi;
float b = c / sqrt_2pi;

更聪明的优化呢,包括了解数学函数的语义:
float a = 1 / sqrtf(c * M_PI);
float b = c / sqrtf(M_PI);

对此:
float sqrt_pi = sqrtf(M_PI); // Compile time constant
float a = 1 / (sqrt_pi * sqrtf(c));
float b = c / sqrt_pi;

最佳答案

编译器遥遥领先。在您的示例中:

float a = 1 / sqrtf(2 * M_PI);
float b = c / sqrtf(2 * M_PI);

nvopencc (Open64) 会发出这个:
    mov.f32         %f2, 0f40206c99;        // 2.50663
div.full.f32 %f3, %f1, %f2;
mov.f32 %f4, 0f3ecc422a; // 0.398942

这相当于
float b = c / 2.50663f;
float a = 0.398942f;

第二种情况编译为:
float a = 1 / sqrtf(c * 3.14159f); // 0f40490fdb
float b = c / 1.77245f; // 0f3fe2dfc5

我猜 a 的表达式编译器生成的应该比你的“优化”版本更准确,但速度差不多。

关于cuda - NVCC 在代码优化方面有多好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7527496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com