CUDA + QT Creator + Win7的集成

42 篇文章 0 订阅
23 篇文章 0 订阅

最近看到brown大学一位同学做的demo简直是牛B,里面居然实现了物体的移动轴,旋转圈等,跟MAYA界面差不多,一直想找一个这样的GUI,找不到,没办法,自己做吧,所以决心学下QT,当然这位同学的程序中用到了CUDA,为了调通他的程序,我又只好硬着头皮去搞下CUDA,花了二天时间,终于把它带的CUDA helloworld程序调通了


注意这里QT creator里面的调试器用的VS2010里面的, 并且这个程序又跟CUDA结合, 所以PRO的写法我琢磨了很久,最终将近二天的时间将它搞出来了.看来要学的还是跑不掉,所以多学点吧,Linux也跑不掉的


好了,开始正题


1.下载CUDA

下载地址:https://developer.nvidia.com/cuda-downloads


自己结合需求选


我用的CUDA7.5


2.QT Creator的配置


已经配置好了的就跳过该步,没有配置好的见我的另一篇文章

http://blog.csdn.net/seamanj/article/details/49650101


3.开搞


main.cpp文件如下:

#include <QCoreApplication>

extern "C" // tell compiler that function is defined somewhere else

void runCudaPart();

#include <stdio.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    runCudaPart();
    return a.exec();
}
kernel.cu文件如下:

#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <glm/glm.hpp>
#include <stdio.h>


#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 200)
  # error printf is only supported on devices of compute capability 2.0 and higher, please compile with -arch=sm_20 or higher
#endif

extern "C"
void runCudaPart();

__global__ void helloCUDA(glm::vec3 v)
{
    int tid = blockIdx.x;
    printf("Hello block %d thread %d, x=%f\n",tid , threadIdx.x, v.x);
}
extern "C"
void runCudaPart()
{
    // all your cuda code here
    glm::vec3 v(0.1f, 0.2f, 0.3f);
    //helloCUDA<<<1, 5>>>(v); // 1 block, 5 GPU threads
    helloCUDA<<<5,1>>>(v); // 5 blocks, 1 GPU thread each
    cudaDeviceSynchronize();
}

重点是CUDA_helloworld.pro文件的配置

格式不对,请点plain↓

#-------------------------------------------------
#
# Project created by QtCreator 2014-04-03T18:12:01
#
#-------------------------------------------------

QT       += core
QT       -= gui

TARGET = CUDA_helloworld
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

# GLM
INCLUDEPATH += "D:/work_files/glm"


# CUDA

# Define output directories
DESTDIR = ../bin
CUDA_OBJECTS_DIR = OBJECTS_DIR/../cuda

# This makes the .cu files appear in your project
OTHER_FILES += \
    kernel.cu \
    CUDA_notes.txt
    
CUDA_SOURCES += \
    kernel.cu

#-------------------------------------------------

# 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"

# CUDA settings
CUDA_DIR = "D:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v7.5/"                # Path to cuda toolkit install
SYSTEM_NAME = Win32                 # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 32                    # '32' or '64', depending on your system
CUDA_ARCH = sm_30                   # Type of CUDA architecture
NVCC_OPTIONS = --use_fast_math

# include paths
INCLUDEPATH += $$CUDA_DIR/include \
               $$CUDA_DIR/common/inc \
               $$CUDA_DIR/../shared/inc

# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib/$$SYSTEM_NAME \
                $$CUDA_DIR/common/lib/$$SYSTEM_NAME \
                $$CUDA_DIR/../shared/lib/$$SYSTEM_NAME

# 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 = cudart_static kernel32 user32 gdi32 winspool comdlg32 \
                 advapi32 shell32 ole32 oleaut32 uuid odbc32 odbccp32 \
                 #freeglut glew32

for(lib, CUDA_LIB_NAMES) {
    CUDA_LIBS += -l$$lib
}
LIBS += $$CUDA_LIBS

# Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
    # Debug mode
    cuda_d.input = CUDA_SOURCES
    cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
    cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
                      --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                      --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
    cuda.input = CUDA_SOURCES
    cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
    cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
                    --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                    --compile -cudart static -DWIN32 -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
}


具体的含义我就不多解释了,运行结果如下:



最后,如果想学CUDA的话,推荐视频:

https://www.udacity.com/course/viewer#!/c-cs344/l-55120467/m-65830481







  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值