11

GPU高性能编程CUDA实战(二)

 3 years ago
source link: https://mp.weixin.qq.com/s/JOo0UFd72dT7IG4mMCso7w
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

GPU高性能编程CUDA实战(二)

Original 齐小凡 3D视觉工坊 2019-04-01
收录于话题
#学习资源&求职&编程

视觉IMAX的第45篇文章

在上一篇文章中:CUDA8.0+VS2015+Win10开发环境搭建教程中已经介绍了CUDA工程的配置与安装。本篇文章是对CUDA工程的配置作进一步介绍与补充说明。

CUDA工程的建立(两种方法)

第一种方法:

这种方法在上一篇文章中作了简单介绍,但是尚有不足,此处补全。
按照常规方式,新建一个win32控制台程序,新建一个main.cpp源文件。
右键点击工程——>「生成依赖项」——>「生成自定义」(选择cuda生成)。

Image
640

接下来实施「三步走战略」配置「附加包含目录」、「附加库目录」以及「附加依赖项」。
第一步:配置「附加包含目录」。

640

注:对于CUDA Samples,有的电脑安装后的路径会隐藏,我们可以在「开始」按钮处查找,见下图。

640

第二步:配置「附加库目录」。

640

第三步:配置「附加依赖项」

640

最后,验证一下配置是否成功。
在新建的工程main.cpp中输入下述测试代码:

#include "stdafx.h"

#include<iostream>

#include<stdio.h>

#include "cuda_runtime.h"

#include"device_launch_parameters.h"

bool InitCUDA(){ 

   int count;

cudaGetDeviceCount(&count);   

 if (count == 0)

 fprintf(stderr, "There is no device.\n");  

return false;

 int i;  

for (i = 0; i < count; i++)

cudaDeviceProp prop; 

if(cudaGetDeviceProperties(&prop,i)== cudaSuccess)  {     

   if (prop.major >= 1)

       break;

if (i == count)

  fprintf(stderr, "There is no device supporting CUDA 1.x.\n");   

     return false;

cudaSetDevice(i); 

return true;
}

int main(){ 

 if (!InitCUDA())

 return 0;

printf("HelloWorld,CUDA has beeninitialized.\n"); 

system("pause");  

return 0; 

运行结果如图所示。

640

第二种方法

这种方法,与第一种方法仅在创建工程时存在差异,其他配置方式皆一致。如图:

640

经过上述步骤,便完成了CUDA项目工程的搭建,只要在这个框架下实现你的项目需求即可。关于CUDA加速的更多细节,后期将会在知识星球【3D视觉工坊】与公众号【视觉IMAX】中持续总结并与大家分享,感兴趣的小伙伴可以关注。

在公众号【视觉IMAX】后台回复「GPU高性能编程CUDA实战」,即可领取该书籍的中文版pdf。

上述内容,如有侵犯版权,请联系作者,会自行删文。

CUDA8.0+VS2015+Win10开发环境搭建教程

回复关键词——知识星球,前往【3D视觉工坊】知识星球

640?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK