非常费解在visual studio下,cuda编程中会出现debug模式和release模式结果不同的情况。
我们知道Release会去掉很多编译和调用信息,但是出现结果误差真是让人费解。
查阅CUDA开发文档,发现use_fast_math。。。原来默认状态下release版本下的cuda为了速度快居然牺牲了精度
| Name | Description |
| use_fast_math | Make use of fast math library. --use_fast_math implies --ftz=true --prec-div=false --prec-sqrt=false --fmad=true. |
| --ftz {true|false} | This option controls single-precision denormals support. --ftz=true flushes denormal values to zero and --ftz=false preserves denormal values. --use_fast_math implies --ftz=true. Allowed values for this option: true, false. Default value: false |
| --prec-div {true|false} | This option controls single-precision floating-point division and reciprocals. --prec-div=true enables the IEEE round-to-nearest mode and --prec-div=false enables the fast approximation mode. --use_fast_math implies --prec-div=false. Allowed values for this option: true, false. Default value: true |
| --prec-sqrt {true|false} | This option controls single-precision floating-point squre root. --prec-sqrt=true enables the IEEE round-to-nearest mode and --prec-sqrt=false enables the fast approximation mode. --use_fast_math implies --prec-sqrt=false. Allowed values for this option: true, false. Default value: true |
| --fmad {true|false} | This option enables (disables) the contraction of floating-point multiplies and adds/subtracts into floating-point multiply-add operations (FMAD, FFMA, or DFMA). --use_fast_math implies --fmad=true. Allowed values for this option: true, false. Default value: true |
Read more at: http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#ixzz4txhhioSo
即cuda为了加快速度,会把加法和乘法合成。此时计算结果会有轻微的变化,导致浮点运算release模式运算结果有偏差。
浮点运算偏差一点没什么大不了的,当作的项目不允许这种误差时,我们可以nvcc开关设置关闭,拿fmad举例
所以设置nvcc开关--fmad=false。项目属性 -> 配置属性 -> cuda c/c++ -> Command Line,下面的其它选项里添加: -fmad=false
最后
以上就是英俊身影最近收集整理的关于Visual Studio debug模式和release模式 CUDA 结果不同的解决的全部内容,更多相关Visual内容请搜索靠谱客的其他文章。
发表评论 取消回复