如何在VS2010 + QT中调用CUDA

如何在VS2010 + QT中调用CUDA

环境:     Windows 7 SP1 x64

    Microsoft Visual Studio 2010 

    qt-win-opensource-4.7.4-vs2008.exe  + qt-vs-addin-1.1.9.exe

    CUDA 4.0


    VS2010 + qt 和CUDA 4.0的安装及配置请参考网上资料


步骤:

1.      创建一个QMainWindow的Qt程序

 


个人喜欢类名为mainwindow,然后点击Finish


 

2.然后注意将Debug改成Release(本人只配置了Release Win32版的)

 

3.创建 CUDA 的.cu文件

(1)Source Files ->添加->新建项


 

 

(2) cudatest.cu文件:


(3)

(4) cudatest.cu代码:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}


// Helper function for using CUDA to add vectors in parallel.
void addWithCuda(int *c, const int *a, const int *b, size_t size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaSetDevice(0);  //设置目标GPU
  
    // Allocate GPU buffers for three vectors (two input, one output)//
	//在显存上开辟内存空间
    cudaMalloc((void**)&dev_c, size * sizeof(int));
    
    cudaMalloc((void**)&dev_a, size * sizeof(int));
   
    cudaMalloc((void**)&dev_b, size * sizeof(int));
   

    // Copy input vectors from host memory to GPU buffers.
	//将内存的数据复制到显存
	cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
  
    cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
  

	//调用GPU上的函数
    // Launch a kernel on the GPU with one thread for each element.
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaDeviceSynchronize();  //GPU的线程同步
 

	//将结果复制到内存上
    // Copy output vector from GPU buffer to host memory.
    cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
   

	//释放显存空间
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);

	// cudaDeviceReset must be called before exiting in order for profiling and
	// tracing tools such as Parallel Nsight and Visual Profiler to show complete traces.
    cudaDeviceReset(); //
}

extern "C" int* test()
{
	const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
	int *c = new int[5];

    // Add vectors in parallel.
    addWithCuda(c, a, b, arraySize);

	return c;
}


/ENDCU

 

 

(4) 然后到main.cpp:

关于cu文件里的test函数,我们用extern的方式在main.cpp文件里调用(这里是参考 \NVIDIA GPU Computing SDK 4.0\C\src\cppIntegration这个example的...所以不用将cudatest.cu文件include到main.cpp里///

因为c++中调用C的lib,需要用extern "C"来修饰用到的c的头文件。


(5)

 

代码为:

#include "mainwindow.h"
#include <QtGui/QApplication>

#include <QtGui\qmessagebox.h>  //

extern "C" int* test();  //

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	mainWindow w;
	w.show();
	
	int *c = test(); //
	QMessageBox::about(&w,"CUDA",
		QObject::tr("{1,2,3,4,5} + {10,20,30,40,50} = {%1,%2,%3,%4,%5}")
		.arg(c[0]).arg(c[1]).arg(c[2]).arg(c[3]).arg(c[4]));
	
	return a.exec();
}


  

//

 

 

 

 

(6)在cudatest.cu点击右键->属性:

将不参与生成,改为:自定义生成工具:


 

 

点击"应用"后,修改为:


 

 

命令行:

"$(CUDA_BIN_PATH)\nvcc.exe"--use-local-env --cl-version 2010 -ccbin "C:\Program Files (x86)\MicrosoftVisual Studio 10.0\VC\bin" --keep-dir "Release" -maxrregcount=0  --machine 32 --compile   -Xcompiler "/EHsc /nologo /Od /Zi  /MD " -o$(ConfigurationName)\cudatest.cu.obj  cudatest.cu

输出:

$(ConfigurationName)\cudatest.cu.obj

 

//参数中的文件名自己改:

 

这里的参数,因为在不同的环境可能不同(这里我看了网上的好多例子,在它的例子可能可以,但去到我的机器就有问题,所以我都试了好久)

 

可以打开\NVIDIA GPU Computing SDK 4.0\C\src\cppIntegration这个example,然后参考cppIntegration.cu的属性页:



可以将以上这一段改改试试

 

点击test_qt_cuda项目的属性页:


 

//以下为一些QT CUDA 配置时应该做好的设置:

VC++目录->包含目录:  (添加以下)

C:\Program Files\NVIDIA GPUComputing Toolkit\CUDA\v4.0\include;

D:\NVIDIA GPU Computing SDK4.0\C\common\inc;

D:\NVIDIA GPU Computing SDK4.0\shared\inc;

D:\Qt\4.7.4\include;

 

VC++目录->库目录:

C:\Program Files\NVIDIA GPUComputing Toolkit\CUDA\v4.0\lib\Win32;

D:\NVIDIA GPU Computing SDK4.0\C\common\lib\Win32;

D:\NVIDIA GPU Computing SDK4.0\shared\lib\Win32;

D:\Qt\4.7.4\lib

 

链接器->常规->附加库目录:

$(QTDIR)\lib;

$(CudaToolkitLibDir);

 

链接器->输入->附加依赖项:

qtmain.lib;QtCore.lib;QtGui.lib;

cudart.lib;cublas.lib;cutil32.lib;shrUtils32.lib;

 

上面的目录,大家也可以用环境变量那里设置好的

 

 

生成的输出:

1>  Performing Custom Build Tools

1>  cudatest.cu

1> tmpxft_00000a70_00000000-0_cudatest.cudafe1.gpu

1> tmpxft_00000a70_00000000-5_cudatest.cudafe2.gpu

1>  cudatest.cu

1>  tmpxft_00000a70_00000000-0_cudatest.cudafe1.cpp

1>  tmpxft_00000a70_00000000-11_cudatest.ii

会生成一个cudatest.cu.obj文件:

 

 

 

以上在我的机器是正常运行的:

 

 

大家在调试的过程中要善于看生成的输出,看看是*.cu文件有没有成功生成为*.cu.obj文件,如果失败,就很可能是"自定义生成工具"那里的命令行的参数有问题,如果成功的话,我之前遇到1>Link:

的时候出问题,这里我们就要看链接器那里有没有设置错,有里会出现无法找到*.dll之类的,这里就要看VC++目录->库目录: / 链接器->常规->附加库目录:/链接器->输入->附加依赖项:

里有没能少了什么库,不断地摸索,尤其参考一下SDK里的cppIntegration那个example,然后总会成功的...


之前一直想直接找个可以运行的例子,一直找不到,现在我这个工程的下载地址:

http://www.rayfile.com/zh-cn/files/ed735400-f7a2-11e0-81ca-0015c55db73d/

 

参考文件:

http://wenku.baidu.com/view/203b72d133d4b14e852468b2.html

http://wenku.baidu.com/view/b109f202bed5b9f3f90f1c10.html

 

---------------------------------------------------------------------------华丽的分割线---------------------------------------------------------------------------------------------------------


或者大家都会遇到一个问题: 怎样可以静态编译出QT+CUDA的软件呢??都不用依赖QT或CUDA的DLL和VC的DLL的呢??

首先配置好静态的QT的环境: 

1: 安装 qt-win-opensource-4.7.4-vs2008.exe (我安装到 D:\Qt\4.7.4)

2:进入D:\Qt\4.7.4\mkspecs\win32-msvc2010 打开 qmake.conf

    并修改: 将 QMAKE_CFLAGS_RELEASE 和 QMAKE_CFLAGS_DEBUG 中的 -MD -MDd 分别修改为 -MT 和 -MTd 

    这里设定为静态的C/C++ 运行库


3:用VS2010的命令行 进入 D:\Qt\4.7.4\ 目录,

  然后 输入:

configure -platform win32-msvc2010 -debug-and-release -opensource -static -fast -qt-sql-sqlite -plugin-sql-sqlite -no-qt3support -qt-zlib -qt-gif -qt-libpng -qt-libmng -qt-libtiff -qt-libjpeg -no-webkit -nomake examples -nomake docs -nomake demos

-static 表示表态的QT库


4: 1个小时后就得到 不依赖 QtCore4.dll、QtGui4.dll 等 Qt 的静态库 且  
不依赖 msvcr90.dll、msvcp90.dll 等 C、C++ 的运行库 的QT 环境


  • 详细请参考:http://hi.baidu.com/cyclone/blog/item/25b262d9337a172310df9b78.html


    5: 然后安装 CUDA 的环境: devdriver_4.0_270.81   +   cudatoolkit_4.0.17    +     cudatools_4.0.17  + GPUcomputingsdk_4.0.17 + Parallel_Nsight 2.0


    然后其它的参考上面...........................................



    默认为 /MDd的,/Md时会出现如下错误:


    1>LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson 已经在 MSVCRT.lib(MSVCR100.dll) 中定义
    1>LINK : warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
    1>E:\test_qt_cuda\\test_qt_cuda.exe : fatal error LNK1169: 找到一个或多个多重定义的符号


所以要设置为MT:




这样设置后,上面的错误还是会存在,然后我们再设置.cu文件的自定义生成工具的命令行 :


上面的是:

命令行:

"$(CUDA_BIN_PATH)\nvcc.exe"--use-local-env --cl-version 2010 -ccbin "C:\Program Files (x86)\MicrosoftVisual Studio 10.0\VC\bin" --keep-dir "Release" -maxrregcount=0  --machine 32 --compile   -Xcompiler "/EHsc /nologo /Od /Zi  /MD " -o$(ConfigurationName)\cudatest.cu.obj  cudatest.cu


将其中的 /MD 改为 /MT:

命令行:

"$(CUDA_BIN_PATH)\nvcc.exe"--use-local-env --cl-version 2010 -ccbin "C:\Program Files (x86)\MicrosoftVisual Studio 10.0\VC\bin" --keep-dir "Release" -maxrregcount=0  --machine 32 --compile   -Xcompiler "/EHsc /nologo /Od /Zi  /MT " -o$(ConfigurationName)\cudatest.cu.obj  cudatest.cu


 然后再运行看看:

1>Link:
1>  test_qt_cuda.vcxproj -> E:\test_qt_cuda\test_qt_cuda.exe
1>FinalizeBuildStatus:
1>  正在删除文件“Release\test_qt_cuda.unsuccessfulbuild”。
1>  正在对“Release\test_qt_cuda.lastbuildstate”执行 Touch 任务。
1>
1>生成成功。

成功生成.............现在就是可以得到绿色的软件了......


附上这次成功的工程: http://download.csdn.net/detail/irelandken/3709655


梁志峰

广东工业大学irelandken@163.com


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值