我是靠谱客的博主 英俊身影,这篇文章主要介绍Visual Studio debug模式和release模式 CUDA 结果不同的解决,现在分享给大家,希望可以做个参考。

非常费解在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: truefalse.

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: truefalse.

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: truefalse.

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: truefalse.

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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(170)

评论列表共有 0 条评论

立即
投稿
返回
顶部