准备
安装QT,参考教程:
https://blog.csdn.net/zhizhengguan/article/details/107567449
安装CUDA11.3,较为简单,可参考:
https://blog.csdn.net/weixin_43848614/article/details/117221384
配置环境
与在VS2017中的配置不同,VS2017可以在选项卡中对CUDA的编译调试环境进行配置,而在QT中,这都需要在工程文件.pro中,通过代码来实现。下面介绍如何新建一个可运行CUDA代码的QT工程。
(1)新建一个QT Console Application工程,kit selection选择 MSVC2017 64bit
(2)右键工程文件夹->Add new->general->empty file,新建一个hello.cu文件,并新建对应的头文件hello.h
(3)新建的hello.cu文件会被自动放入other files文件夹下,此时的.cu文件是不参与编译调试过程的,因此接下来需要在.pro文件中添加代码,我放出添加之前的.pro中的代码供大家参考:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES += \
hello.cu
HEADERS += \
hello.h
我们在上述代码后加入以下代码:
#-------------------------------------------------
# CUDA settings
CUDA_SOURCES += hello.cu
CUDA_DIR = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.3/" # Path to cuda toolkit install
SYSTEM_NAME = x64 # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 64 # '32' or '64', depending on your system
CUDA_ARCH = compute_75 # Type of CUDA architecture
CUDA_CODE = sm_75
NVCC_OPTIONS = --use_fast_math
# include paths
INCLUDEPATH += "$$CUDA_DIR/include" \
"C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.3\common\inc"
# library directories
QMAKE_LIBDIR += "$$CUDA_DIR/lib/x64"
# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
# Add the necessary libraries
CUDA_LIB_NAMES += cublas \cuda \cudadevrt \cudart \cudart_static \cufft \cufftw \curand \
cusolver \cusparse \nppc \nppial \nppicc \nppidei \nppif \nppig \nppim \nppist \nppisu \nppitc \
npps \nvblas \nvml \nvrtc \OpenCL \kernel32 \user32 \gdi32 \winspool \comdlg32 \advapi32 \shell32 \ole32 \oleaut32 \uuid \odbc32 \odbccp32 \ucrt \MSVCRT
for(lib, CUDA_LIB_NAMES) {
CUDA_LIBS += $$lib.lib
}
for(lib, CUDA_LIB_NAMES) {
NVCC_LIBS += -l$$lib
}
LIBS += $$NVCC_LIBS
# The following library conflicts with something in Cuda
QMAKE_LFLAGS_RELEASE = /NODEFAULTLIB:msvcrt.lib
QMAKE_LFLAGS_DEBUG = /NODEFAULTLIB:msvcrtd.lib
# MSVCRT link option (static or dynamic, it must be the same with your Qt SDK link option)
MSVCRT_LINK_FLAG_DEBUG = "/MDd"
MSVCRT_LINK_FLAG_RELEASE = "/MD"
# Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
# Debug mode
DESTDIR = debug
OBJECTS_DIR = debug/obj
CUDA_OBJECTS_DIR = debug/cuda
cuda_d.input = CUDA_SOURCES
cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
--machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -code=$$CUDA_CODE \
--compile -cudart static -g -DWIN32 -D_MBCS \
-Xcompiler "/wd4819,/EHsc,/W3,/nologo,/Od,/Zi,/RTC1" \
-Xcompiler $$MSVCRT_LINK_FLAG_DEBUG \
-c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
cuda_d.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
# Release mode
DESTDIR = release
OBJECTS_DIR = release/obj
CUDA_OBJECTS_DIR = release/cuda
cuda.input = CUDA_SOURCES
cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
--machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -code=$$CUDA_CODE \
--compile -cudart static -D_MBCS \
-Xcompiler "/wd4819,/EHsc,/W3,/nologo,/O2,/Zi" \
-Xcompiler $$MSVCRT_LINK_FLAG_RELEASE \
-c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
cuda.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += cuda
}
这样就完成了对.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
并用记事本打开,其中的内容如下,只有这一行:
@call "%~dp0vcvarsall.bat" x64 %*
之后我在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中的内容为:
@if not "%VSCMD_DEBUG%" GEQ "3" echo off
@REM -------------------------------------------------------------------------
:parse_args
set __VCVARSALL_STORE=
set __VCVARSALL_WINSDK=
set __VCVARSALL_PARSE_ERROR=
set __VCVARSALL_TARGET_ARCH=
set __VCVARSALL_HOST_ARCH=
set __VCVARSALL_VER=
@REM Parse command line arguments. This implementation does not care about
@REM argument order.
if "%VSCMD_DEBUG%" GEQ "1" (
@echo [DEBUG:%~nx0] init with arguments '%*'
)
set __VCVARSALL_PARSE_ERROR=0
set "__VCVARSALL_ARGS_LIST=%*"
call :parse_loop
set __VCVARSALL_ARGS_LIST=
if "%VSCMD_DEBUG%" GEQ "1" (
@echo [DEBUG:%~nx0] Command line parse completed with values:
@echo [DEBUG:%~nx0] __VCVARSALL_TARGET_ARCH='%__VCVARSALL_TARGET_ARCH%'
@echo [DEBUG:%~nx0] __VCVARSALL_HOST_ARCH='%__VCVARSALL_HOST_ARCH%'
@echo [DEBUG:%~nx0] __VCVARSALL_WINSDK='%__VCVARSALL_WINSDK%'
@echo [DEBUG:%~nx0] __VCVARSALL_STORE='%__VCVARSALL_STORE%'
@echo [DEBUG:%~nx0] __VCVARSALL_HELP='%__VCVARSALL_HELP%'
@echo [DEBUG:%~nx0] __VCVARSALL_PARSE_ERROR='%__VCVARSALL_PARSE_ERROR%'
)
if "%__VCVARSALL_CLEAN_ENV%" NEQ "" goto :call_vsdevcmd
if "%__VCVARSALL_PARSE_ERROR%" NEQ "0" goto :usage_error
if "%__VCVARSALL_HELP%" NEQ "" goto :usage
@REM -------------------------------------------------------------------------
:check_platform
@REM This script is installed to ...\VC\Auxiliary\Build.
@REM vsdevcmd is installed to ...\Common7\Tools.
if not exist "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" goto missing
@REM Assemble the arguments to pass to vsdevcmd.bat
if "%__VCVARSALL_TARGET_ARCH%" == "" goto :usage_error
if "%__VCVARSALL_HOST_ARCH%" == "" goto :usage_error
set "__VCVARSALL_VSDEVCMD_ARGS=-arch=%__VCVARSALL_TARGET_ARCH% -host_arch=%__VCVARSALL_HOST_ARCH%"
if "%__VCVARSALL_WINSDK%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -winsdk=%__VCVARSALL_WINSDK%"
)
if "%__VCVARSALL_STORE%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -app_platform=UWP"
)
if "%__VCVARSALL_VER%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_ver=%__VCVARSALL_VER%"
)
if "%__VCVARSALL_SPECTRE%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_spectre_libs=%__VCVARSALL_SPECTRE%"
)
goto :call_vsdevcmd
@REM -------------------------------------------------------------------------
@REM Call vsdevcmd.bat to setup the command prompt environment
:call_vsdevcmd
@REM This temporary environment variable is used to control setting of VC++
@REM command prompt-specific environment variables that should not be set
@REM by the VS Developer Command prompt (specifically vsdevcmd\ext\vcvars.bat).
@REM The known case this effects is the Platform environment variable, which
@REM will override platform target for .NET builds.
set VSCMD_VCVARSALL_INIT=1
@REM Special handling for the /clean_env argument
if "%__VCVARSALL_CLEAN_ENV%" NEQ "" (
call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" /clean_env
goto :end
)
call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" %__VCVARSALL_VSDEVCMD_ARGS%
if "%ERRORLEVEL%"=="0" (
@REM Print the target command prompt architecture...
if "%__VCVARSALL_HOST_ARCH%" NEQ "%__VCVARSALL_TARGET_ARCH%" (
echo [%~nx0] Environment initialized for: '%__VCVARSALL_HOST_ARCH%_%__VCVARSALL_TARGET_ARCH%'
) else (
echo [%~nx0] Environment initialized for: '%__VCVARSALL_TARGET_ARCH%'
)
)
goto :end
:parse_loop
for /F "tokens=1,* delims= " %%a in ("%__VCVARSALL_ARGS_LIST%") do (
if "%VSCMD_DEBUG%" GEQ "2" (
@echo [DEBUG:%~nx0] inner argument {%%a}
)
call :parse_argument %%a
set "__VCVARSALL_ARGS_LIST=%%b"
goto :parse_loop
)
exit /B 0
:parse_argument
@REM called by :parse_loop and expects the arguments to either be:
@REM 1. a single argument in %1
@REM 2. an argument pair from the command line specified as '%1=%2'
set __local_ARG_FOUND=
@REM Architecture
if /I "%1"=="x86" (
set __VCVARSALL_TARGET_ARCH=x86
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_amd64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_x64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_arm" (
set __VCVARSALL_TARGET_ARCH=arm
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_arm64" (
set __VCVARSALL_TARGET_ARCH=arm64
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_x86" (
set __VCVARSALL_TARGET_ARCH=x86
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64_x86" (
set __VCVARSALL_TARGET_ARCH=x86
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_arm" (
set __VCVARSALL_TARGET_ARCH=arm
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64_arm" (
set __VCVARSALL_TARGET_ARCH=arm
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_arm64" (
set __VCVARSALL_TARGET_ARCH=arm64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64_arm64" (
set __VCVARSALL_TARGET_ARCH=arm64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="-vcvars_ver" (
set "__VCVARSALL_VER=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="/vcvars_ver" (
set "__VCVARSALL_VER=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="--vcvars_ver" (
set "__vcvarsall_ver=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="-vcvars_spectre_libs" (
set "__VCVARSALL_SPECTRE=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="/vcvars_spectre_libs" (
set "__VCVARSALL_SPECTRE=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="--vcvars_spectre_libs" (
set "__vcvarsall_SPECTRE=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="help" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="/help" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="-help" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="/?" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="-?" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
@REM -- /clean_env --
@REM Mostly used for internal testing to restore the state of
@REM the command line environment to its state prior to vcvarsall.bat
@REM being executed.
if /I "%1"=="/clean_env" (
set __VCVARSALL_CLEAN_ENV=/clean_env
set __local_ARG_FOUND=1
)
if /I "%1"=="-clean_env" (
set __VCVARSALL_CLEAN_ENV=/clean_env
set __local_ARG_FOUND=1
)
@REM Windows SDK Version
if /I "%1"=="8.1" (
set "__VCVARSALL_WINSDK=8.1"
set __local_ARG_FOUND=1
)
set __temp1=%1
if /I "%__temp1:~0,3%"=="10." (
set "__VCVARSALL_WINSDK=%1"
set __local_ARG_FOUND=1
)
set __temp1=
@REM Store/UWP
if /I "%1"=="store" (
set "__VCVARSALL_STORE=-app_platform=UWP"
set __local_ARG_FOUND=1
)
if /I "%1"=="uwp" (
set "__VCVARSALL_STORE=-app_platform=UWP"
set __local_ARG_FOUND=1
)
if "%__local_ARG_FOUND%" NEQ "1" (
set /A __VCVARSALL_PARSE_ERROR=__VCVARSALL_PARSE_ERROR+1
if "%2"=="" (
@echo [ERROR:%~nx0] Invalid argument found : %1
) else (
@echo [ERROR:%~nx0] Invalid argument found : %1=%2
)
)
set __local_ARG_FOUND=
exit /B 0
:usage_error
echo [ERROR:%~nx0] Error in script usage. The correct usage is:
goto :usage
:usage
echo Syntax:
echo %~nx0 [arch] [platform_type] [winsdk_version] [-vcvars_ver=vc_version] [-vcvars_spectre_libs=spectre_mode]
echo where :
echo [arch]: x86 ^| amd64 ^| x86_amd64 ^| x86_arm ^| x86_arm64 ^| amd64_x86 ^| amd64_arm ^| amd64_arm64
echo [platform_type]: {empty} ^| store ^| uwp
echo [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK.
echo [vc_version] : {none} for default VS 2017 VC++ compiler toolset ^|
echo "14.0" for VC++ 2015 Compiler Toolset ^|
echo "14.1x" for the latest 14.1x.yyyyy toolset installed (e.g. "14.11") ^|
echo "14.1x.yyyyy" for a specific full version number (e.g. 14.11.25503)
echo [spectre_mode] : {none} for default VS 2017 libraries without spectre mitigations ^|
echo "spectre" for VS 2017 libraries with spectre mitigations
echo:
echo The store parameter sets environment variables to support Universal Windows Platform application
echo development and is an alias for 'uwp'.
echo:
echo For example:
echo %~nx0 x86_amd64
echo %~nx0 x86_amd64 10.0.10240.0
echo %~nx0 x86_arm uwp 10.0.10240.0
echo %~nx0 x86_arm onecore 10.0.10240.0 -vcvars_ver=14.0
echo %~nx0 x64 8.1
echo %~nx0 x64 store 8.1
echo:
echo Please make sure either Visual Studio or C++ Build SKU is installed.
goto :end
:missing
echo The specified configuration type is missing. The tools for the
echo configuration might not be installed.
goto :end
:end
set __VCVARSALL_TARGET_ARCH=
set __VCVARSALL_HOST_ARCH=
set __VCVARSALL_STORE=
set __VCVARSALL_WINSDK=
set __VCVARSALL_PARSE_ERROR=
set __VCVARSALL_CLEAN_ENV=
set VSCMD_VCVARSALL_INIT=
set __VCVARSALL_VSDEVCMD_ARGS=
set __VCVARSALL_HELP=
set __VCVARSALL_VER=
set __VCVARSALL_SPECTRE=
(6)再次debug程序,编译成功
(7)下面给出hello.cu 和hello.h、main.cpp中的代码
hello.cu
#include "hello.h"
extern "C"
__global__ void hellofromGPU(void)
{
printf("GPU:hello world\n");
}
void showhello(void)
{
hellofromGPU <<<1,10>>>();
cudaDeviceSynchronize();
}
hello.h
#ifndef HELLO_H
#define HELLO_H
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include "malloc.h"
#define WIDTH 11
#define HEIGHT 10
#define X_INTER 3
#define Y_INTER 3
#define BLOCK_SIZE 8
void showhello(void);
#endif // HELLO_H
main.cpp
#include<stdio.h>
#include "hello.h"
int main(void)
{
showhello();
}
(8)运行代码,输出结果如图所示: