CUDA 配置环境(二):Windows10+QT5.14+CUDA11.3+MSVC2017
准备
安装QT,参考教程:
安装CUDA11.3
配置环境
与在VS2017中的配置不同,VS2017可以在选项卡中对CUDA的编译调试环境进行配置,而在QT中,这都需要在工程文件.pro中,通过代码来实现。下面介绍如何新建一个可运行CUDA代码的QT工程。
(1)新建一个QT Console Application工程,kit selection选择 MSVC2017 64bit
(2)右键工程文件夹>Add new>general>empty file,新建一个hellou文件,并新建对应的头文件hello.h
(3)新建的hellou文件会被自动放入other files文件夹下,此时的u文件是不参与编译调试过程的,因此接下来需要在.pro文件中添加代码,我放出添加之前的.pro中的代码供大家参考:
1 QT = gui 2 3 CONFIG += c++11 console 4 CONFIG = app_bundle 5 6 # The following define makes your cpiler emit warnings if you use 7 # any Qt feature that has been marked deprecated (the exact warnings 8 # depend on your cpiler). Please consult the documentation of the 9 # deprecated API in order to know how to port your code away fr it. 10 DEFINES += QT_DEPRECATED_WARNINGS 11 12 # You can also make your code fail to cpile if it uses deprecated APIs. 13 # In order to do so, uncment the following line. 14 # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 17 SOURCES += \ 18 mainpp 19 20 # Default rules for deployment. 21 qnx: target.path = /tmp/$$/bin 22 else: unix:!android: target.path = /opt/$$/bin 23 !isEmpty(target.path): INSTALLS += target 24 25 DISTFILES += \ 26 hellou 27 28 HEADERS += \ 29 hello.h我们在上述代码后加入以下代码:
1 # 2 # CUDA settings 3 CUDA_SOURCES += hellou 4 CUDA_DIR = "C:/Program Files/NVIDIA GPU Cputing Toolkit/CUDA/v11.3/" # Path to cuda toolkit install 5 SYSTEM_NAME = x64 # Depending on your system either 'Win32', 'x64', or 'Win64' 6 SYSTEM_TYPE = 64 # '32' or '64', depending on your system 7 CUDA_ARCH = cpute_75 # Type of CUDA architecture 8 CUDA_CODE = sm_75 9 NVCC_OPTIONS = use_fast_math 10 # include paths 11 INCLUDEPATH += "$$CUDA_DIR/include" \ 12 "C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.3\cmon\inc" 13 # library directories 14 QMAKE_LIBDIR += "$$CUDA_DIR/lib/x64" 15 # The following makes sure all path names (which often include spaces) are put between quotation marks 16 CUDA_INC = $$join(INCLUDEPATH,'" I"','I"','"') 17 # Add the necessary libraries 18 CUDA_LIB_NAMES += cublas \cuda \cudadevrt \cudart \cudart_static \cufft \cufftw \curand \ 19 cusolver \cusparse \nppc \nppial \nppicc \nppidei \nppif \nppig \nppim \nppist \nppisu \nppitc \ 20 npps \nvblas \nvml \nvrtc \OpenCL \kernel32 \user32 \gdi32 \winspool \cdlg32 \advapi32 \shell32 \ole32 \oleaut32 \uuid \odbc32 \odbccp32 \ucrt \MSVCRT 21 22 for(lib, CUDA_LIB_NAMES) 25 for(lib, CUDA_LIB_NAMES) 28 LIBS += $$NVCC_LIBS 29 # The following library conflicts with sething in Cuda 30 QMAKE_LFLAGS_RELEASE = /NODEFAULTLIB:msvcrt.lib 31 QMAKE_LFLAGS_DEBUG = /NODEFAULTLIB:msvcrtd.lib 32 # MSVCRT link option (static or dynamic, it must be the same with your Qt SDK link option) 33 MSVCRT_LINK_FLAG_DEBUG = "/MDd" 34 MSVCRT_LINK_FLAG_RELEASE = "/MD" 35 # Configuration of the Cuda cpiler 36 CONFIG(debug, debug|release) _cuda.o 43 cuda_dmands = $$CUDA_DIR/bin/nvcc.exe D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$LIBS \ 44 machine $$SYSTEM_TYPE arch=$$CUDA_ARCH code=$$CUDA_CODE \ 45 cpile cudart static g DWIN32 D_MBCS \ 46 Xcpiler "/wd4819,/EHsc,/W3,/nologo,/Od,/Zi,/RTC1" \ 47 Xcpiler $$MSVCRT_LINK_FLAG_DEBUG \ 48 c o $ $ 49 cuda_d.dependency_type = TYPE_C 50 QMAKE_EXTRA_CPILERS += cuda_d 51 } 52 else _cuda.o 59 cudamands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS \ 60 machine $$SYSTEM_TYPE arch=$$CUDA_ARCH code=$$CUDA_CODE \ 61 cpile cudart static D_MBCS \ 62 Xcpiler "/wd4819,/EHsc,/W3,/nologo,/O2,/Zi" \ Xcpiler $$MSVCRT_LINK_FLAG_RELEASE \ 64 c o $ $ 65 cuda.dependency_type = TYPE_C 66 QMAKE_EXTRA_CPILERS += cuda 67 }这样就完成了对.pro文件的配置。下面介绍可能出现的问题。
Debug出现问题
在一开始debug的时候我遇到了以下问题:Could not set up the environment for Microsoft Visual Studio using
nvcc fatal : Could not set up the environment for Microsoft Visual Studio using 'C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/../../../../../../../VC/Auxiliary/Build/vcvars64.bat'我在网上找了好久的解决办法,大致的方法包括检查系统的环境变量,CUDA的路径不能有空格,nvcc的输入太长等,但这些都不能解决这个问题。
在尝试了很长时间之后,我还是决定根据报错信息入手,我找到了出现问题的vcvars64.bat这个文件,默认路径在:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build
并用记事本打开,其中的内容如下,只有这一行:
之后我在QT的工具>选项>Kits>编译器,发现QT中的编译器使用都是与上述vcvars64.bat同一路径下的vcvarsall.bat文件。
因此我首先想要改变我的.pro文件调用vcvars文件的代码,有关的代码如下:
SYSTEM_TYPE = 64改变等号右边的值,可以改变调试时使用的vavars文件的后缀,但是他只能接收数字输入,所以也无法使其调用vcvarsall.bat,因此只能另辟蹊径,考虑将vcvarsall.bat的内容复制到vcvars64.bat,达到调用vcvarsall.bat的效果。
(5)最终解决方案,用记事本打开vcvarsall.bat,复制其中的代码到vcvars64.bat,最终vcvars64.bat中的内容为:
1 @if not "%VSCMD_DEBUG%" GEQ "3" echo off 2 3 @REM 4 :parse_args 5 set __VCVARSALL_STORE= 6 set __VCVARSALL_WINSDK= 7 set __VCVARSALL_PARSE_ERROR= 8 set __VCVARSALL_TARGET_ARCH= 9 set __VCVARSALL_HOST_ARCH= 10 set __VCVARSALL_VER= 11 12 @REM Parse cmand line arguments. This implementation does not care about 13 @REM argument order. 14 15 if "%VSCMD_DEBUG%" GEQ "1" ( 16 @echo [DEBUG:%~nx0] init with arguments '%*' 17 ) 18 19 set __VCVARSALL_PARSE_ERROR=0 20 set "__VCVARSALL_ARGS_LIST=%*" 21 call :parse_loop 22 set __VCVARSALL_ARGS_LIST= 23 24 if "%VSCMD_DEBUG%" GEQ "1" ( 25 @echo [DEBUG:%~nx0] Cmand line parse cpleted with values: 26 @echo [DEBUG:%~nx0] __VCVARSALL_TARGET_ARCH='%__VCVARSALL_TARGET_ARCH%' 27 @echo [DEBUG:%~nx0] __VCVARSALL_HOST_ARCH='%__VCVARSALL_HOST_ARCH%' 28 @echo [DEBUG:%~nx0] __VCVARSALL_WINSDK='%__VCVARSALL_WINSDK%' 29 @echo [DEBUG:%~nx0] __VCVARSALL_STORE='%__VCVARSALL_STORE%' 30 @echo [DEBUG:%~nx0] __VCVARSALL_HELP='%__VCVARSALL_HELP%' 31 @echo [DEBUG:%~nx0] __VCVARSALL_PARSE_ERROR='%__VCVARSALL_PARSE_ERROR%' 32 ) 33 34 if "%__VCVARSALL_CLEAN_ENV%" NEQ "" goto :call_vsdevcmd 35 if "%__VCVARSALL_PARSE_ERROR%" NEQ "0" goto :usage_error 36 if "%__VCVARSALL_HELP%" NEQ "" goto :usage 37 38 @REM 39 :check_platform 40 @REM This script is installed to ...\VC\Auxiliary\Build. 41 @REM vsdevcmd is installed to ...\Cmon7\Tools. 42 if not exist "%~dp0..\..\..\Cmon7\Tools\vsdevcmd.bat" goto missing 43 44 @REM Assemble the arguments to pass to vsdevcmd.bat 45 if "%__VCVARSALL_TARGET_ARCH%" == "" goto :usage_error 46 if "%__VCVARSALL_HOST_ARCH%" == "" goto :usage_error 47 48 set "__VCVARSALL_VSDEVCMD_ARGS=arch=%__VCVARSALL_TARGET_ARCH% host_arch=%__VCVARSALL_HOST_ARCH%" 49 if "%__VCVARSALL_WINSDK%" NEQ "" ( 50 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% winsdk=%__VCVARSALL_WINSDK%" 51 ) 52 if "%__VCVARSALL_STORE%" NEQ "" ( 53 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% app_platform=UWP" 54 ) 55 if "%__VCVARSALL_VER%" NEQ "" ( 56 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% vcvars_ver=%__VCVARSALL_VER%" 57 ) 58 if "%__VCVARSALL_SPECTRE%" NEQ "" ( 59 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% vcvars_spectre_libs=%__VCVARSALL_SPECTRE%" 60 ) 61 62 goto :call_vsdevcmd 64 @REM 65 @REM Call vsdevcmd.bat to setup the cmand prpt environment 66 67 :call_vsdevcmd 68 69 @REM This temporary environment variable is used to control setting of VC++ 70 @REM cmand prptspecific environment variables that should not be set 71 @REM by the VS Developer Cmand prpt (specifically vsdevcmd\ext\vcvars.bat). 72 @REM The known case this effects is the Platform environment variable, which 73 @REM will override platform target for .NET builds. 74 set VSCMD_VCVARSALL_INIT=1 75 76 @REM Special handling for the /clean_env argument 77 if "%__VCVARSALL_CLEAN_ENV%" NEQ "" ( 78 call "%~dp0..\..\..\Cmon7\Tools\vsdevcmd.bat" /clean_env 79 goto :end 80 ) 81 82 call "%~dp0..\..\..\Cmon7\Tools\vsdevcmd.bat" %__VCVARSALL_VSDEVCMD_ARGS% 83 84 if "%ERRORLEVEL%"=="0" ( 85 @REM Print the target cmand prpt architecture... 86 if "%__VCVARSALL_HOST_ARCH%" NEQ "%__VCVARSALL_TARGET_ARCH%" ( 87 echo [%~nx0] Environment initialized for: '%__VCVARSALL_HOST_ARCH%_%__VCVARSALL_TARGET_ARCH%' 88 ) else ( 89 echo [%~nx0] Environment initialized for: '%__VCVARSALL_TARGET_ARCH%' 90 ) 91 ) 92 goto :end 93 94 :parse_loop 95 for /F "tokens=1,* delims= " %%a in ("%__VCVARSALL_ARGS_LIST%") do ( 96 if "%VSCMD_DEBUG%" GEQ "2" ( 97 @echo [DEBUG:%~nx0] inner argument 98 ) 99 call :parse_argument %%a 100 set "__VCVARSALL_ARGS_LIST=%%b" 101 goto :parse_loop 102 ) 103 104 exit /B 0 105 106 :parse_argument 107 108 @REM called by :parse_loop and expects the arguments to either be: 109 @REM 1. a single argument in %1 110 @REM 2. an argument pair fr the cmand line specified as '%1=%2' 111 112 set __local_ARG_FOUND= 113 @REM Architecture 114 if /I "%1"=="x86" ( 115 set __VCVARSALL_TARGET_ARCH=x86 116 set __VCVARSALL_HOST_ARCH=x86 117 set __local_ARG_FOUND=1 118 ) 119 if /I "%1"=="x86_amd64" ( 120 set __VCVARSALL_TARGET_ARCH=x64 121 set __VCVARSALL_HOST_ARCH=x86 122 set __local_ARG_FOUND=1 123 ) 124 if /I "%1"=="x86_x64" ( 125 set __VCVARSALL_TARGET_ARCH=x64 126 set __VCVARSALL_HOST_ARCH=x86 127 set __local_ARG_FOUND=1 128 ) 129 if /I "%1"=="x86_arm" ( 130 set __VCVARSALL_TARGET_ARCH=arm 131 set __VCVARSALL_HOST_ARCH=x86 132 set __local_ARG_FOUND=1 133 ) 134 if /I "%1"=="x86_arm64" ( 135 set __VCVARSALL_TARGET_ARCH=arm64 136 set __VCVARSALL_HOST_ARCH=x86 137 set __local_ARG_FOUND=1 138 ) 139 if /I "%1"=="amd64" ( 140 set __VCVARSALL_TARGET_ARCH=x64 141 set __VCVARSALL_HOST_ARCH=x64 142 set __local_ARG_FOUND=1 143 ) 144 if /I "%1"=="x64" ( 145 set __VCVARSALL_TARGET_ARCH=x64 146 set __VCVARSALL_HOST_ARCH=x64 147 set __local_ARG_FOUND=1 148 ) 149 if /I "%1"=="amd64_x86" ( 150 set __VCVARSALL_TARGET_ARCH=x86 151 set __VCVARSALL_HOST_ARCH=x64 152 set __local_ARG_FOUND=1 153 ) 154 if /I "%1"=="x64_x86" ( 155 set __VCVARSALL_TARGET_ARCH=x86 156 set __VCVARSALL_HOST_ARCH=x64 157 set __local_ARG_FOUND=1 158 ) 159 if /I "%1"=="amd64_arm" ( 160 set __VCVARSALL_TARGET_ARCH=arm 161 set __VCVARSALL_HOST_ARCH=x64 162 set __local_ARG_FOUND=1 1 ) 164 if /I "%1"=="x64_arm" ( 165 set __VCVARSALL_TARGET_ARCH=arm 166 set __VCVARSALL_HOST_ARCH=x64 167 set __local_ARG_FOUND=1 168 ) 169 if /I "%1"=="amd64_arm64" ( 170 set __VCVARSALL_TARGET_ARCH=arm64 171 set __VCVARSALL_HOST_ARCH=x64 172 set __local_ARG_FOUND=1 173 ) 174 if /I "%1"=="x64_arm64" ( 175 set __VCVARSALL_TARGET_ARCH=arm64 176 set __VCVARSALL_HOST_ARCH=x64 177 set __local_ARG_FOUND=1 178 ) 179 if /I "%1"=="vcvars_ver" ( 180 set "__VCVARSALL_VER=%2" 181 set __local_ARG_FOUND=1 182 ) 183 if /I "%1"=="/vcvars_ver" ( 184 set "__VCVARSALL_VER=%2" 185 set __local_ARG_FOUND=1 186 ) 187 if /I "%1"=="vcvars_ver" ( 188 set "__vcvarsall_ver=%2" 189 set __local_ARG_FOUND=1 190 ) 191 if /I "%1"=="vcvars_spectre_libs" ( 192 set "__VCVARSALL_SPECTRE=%2" 193 set __local_ARG_FOUND=1 194 ) 195 if /I "%1"=="/vcvars_spectre_libs" ( 196 set "__VCVARSALL_SPECTRE=%2" 197 set __local_ARG_FOUND=1 198 ) 199 if /I "%1"=="vcvars_spectre_libs" ( 200 set "__vcvarsall_SPECTRE=%2" 201 set __local_ARG_FOUND=1 202 ) 203 if /I "%1"=="help" ( 204 set __VCVARSALL_HELP=1 205 set __local_ARG_FOUND=1 206 ) 207 if /I "%1"=="/help" ( 208 set __VCVARSALL_HELP=1 209 set __local_ARG_FOUND=1 210 ) 211 if /I "%1"=="help" ( 212 set __VCVARSALL_HELP=1 213 set __local_ARG_FOUND=1 214 ) 215 if /I "%1"=="/?" ( 216 set __VCVARSALL_HELP=1 217 set __local_ARG_FOUND=1 218 ) 219 if /I "%1"=="?" ( 220 set __VCVARSALL_HELP=1 221 set __local_ARG_FOUND=1 222 ) 223 224 @REM /clean_env 225 @REM Mostly used for internal testing to restore the state of 226 @REM the cmand line environment to its state prior to vcvarsall.bat 227 @REM being executed. 228 if /I "%1"=="/clean_env" ( 229 set __VCVARSALL_CLEAN_ENV=/clean_env 230 set __local_ARG_FOUND=1 231 ) 232 if /I "%1"=="clean_env" ( 233 set __VCVARSALL_CLEAN_ENV=/clean_env 234 set __local_ARG_FOUND=1 235 ) 236 237 @REM Windows SDK Version 238 if /I "%1"=="8.1" ( 239 set "__VCVARSALL_WINSDK=8.1" 240 set __local_ARG_FOUND=1 241 ) 242 243 set __temp1=%1 244 if /I "%__temp1:~0,3%"=="10." ( 245 set "__VCVARSALL_WINSDK=%1" 246 set __local_ARG_FOUND=1 247 ) 248 set __temp1= 249 250 @REM Store/UWP 251 if /I "%1"=="store" ( 252 set "__VCVARSALL_STORE=app_platform=UWP" 253 set __local_ARG_FOUND=1 254 ) 255 if /I "%1"=="uwp" ( 256 set "__VCVARSALL_STORE=app_platform=UWP" 257 set __local_ARG_FOUND=1 258 ) 259 260 if "%__local_ARG_FOUND%" NEQ "1" ( 261 set /A __VCVARSALL_PARSE_ERROR=__VCVARSALL_PARSE_ERROR+1 262 if "%2"=="" ( 2 @echo [ERROR:%~nx0] Invalid argument found : %1 264 ) else ( 265 @echo [ERROR:%~nx0] Invalid argument found : %1=%2 266 ) 267 ) 268 set __local_ARG_FOUND= 269 exit /B 0 270 271 :usage_error 272 echo [ERROR:%~nx0] Error in script usage. The correct usage is: 273 goto :usage 274 275 :usage 276 echo Syntax: 277 echo %~nx0 [arch] [platform_type] [winsdk_version] [vcvars_ver=vc_version] [vcvars_spectre_libs=spectre_mode] 278 echo where : 279 echo [arch]: x86 ^| amd64 ^| x86_amd64 ^| x86_arm ^| x86_arm64 ^| amd64_x86 ^| amd64_arm ^| amd64_arm64 280 echo [platform_type]: ^| store ^| uwp 281 echo [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK. 282 echo [vc_version] : for default VS 2017 VC++ cpiler toolset ^| 283 echo "14.0" for VC++ 2015 Cpiler Toolset ^| 284 echo "14.1x" for the latest 14.1x.yyyyy toolset installed (e.g. "14.11") ^| 285 echo "14.1x.yyyyy" for a specific full version number (e.g. 14.11.25503) 286 echo [spectre_mode] : for default VS 2017 libraries without spectre mitigations ^| 287 echo "spectre" for VS 2017 libraries with spectre mitigations 288 echo: 289 echo The store parameter sets environment variables to support Universal Windows Platform application 290 echo development and is an alias for 'uwp'. 291 echo: 292 echo For example: 293 echo %~nx0 x86_amd64 294 echo %~nx0 x86_amd64 10.0.10240.0 295 echo %~nx0 x86_arm uwp 10.0.10240.0 296 echo %~nx0 x86_arm onecore 10.0.10240.0 vcvars_ver=14.0 297 echo %~nx0 x64 8.1 298 echo %~nx0 x64 store 8.1 299 echo: 300 echo Please make sure either Visual Studio or C++ Build SKU is installed. 301 goto :end 302 303 :missing 304 echo The specified configuration type is missing. The tools for the 305 echo configuration might not be installed. 306 goto :end 307 308 :end 309 set __VCVARSALL_TARGET_ARCH= 310 set __VCVARSALL_HOST_ARCH= 311 set __VCVARSALL_STORE= 312 set __VCVARSALL_WINSDK= 313 set __VCVARSALL_PARSE_ERROR= 314 set __VCVARSALL_CLEAN_ENV= 315 set VSCMD_VCVARSALL_INIT= 316 set __VCVARSALL_VSDEVCMD_ARGS= 317 set __VCVARSALL_HELP= 318 set __VCVARSALL_VER= 319 set __VCVARSALL_SPECTRE=(6)再次debug程序,编译成功
(7)下面给出hellou 和hello.h、mainpp中的代码
hellou
1 #include "hello.h" 2 3 extern "C" 4 __global__ void hellofrGPU(void) 5 8 9 void showhello(void) 10hello.h
1 #ifndef HELLO_H 2 #define HELLO_H 3 #include "cuda_runtime.h" 4 #include "device_launch_parameters.h" 5 #include <stdio.h> 6 #include "malloc.h" 7 #define WIDTH 11 8 9 #define HEIGHT 10 10 #define X_INTER 3 11 #define Y_INTER 3 12 #define BLOCK_SIZE 8 13 14 void showhello(void); 15 #endif // HELLO_Hmainpp
1 #include<stdio.h> 2 #include "hello.h" 3 4 int main(void) 5(8)运行代码,输出结果如图所示:
上一篇:C++ 字符串拼接技巧(stringstream、字符串迭代器、字符串的加法运算符、std::accumulate、boost库join)
下一篇:C++ 初学者如何正确使用指针
CUDA