windows 10 安装CUDA 9.1和PyCUDA

搭建环境之前自己曾在网上搜索过相关博客,能找到的中文资料确实不敢恭维,主要存在的问题是版本过低。因此参考了别人的很多经验,才有了这篇相对版本较新的中文资料,供大家参考交流。


1.    安装Visual Studio 2013


VS2013的安装破解比较简单,在网盘搜索引擎里很容易就能找到安装包和密钥。

安装完成后,记得在系统变量path(system PATH)中添加以下两项:

C:\ProgramFiles (x86)\Microsoft Visual Studio 12.0\VC\bin\;C:\Program Files(x86)\Microsoft Visual Studio 12.0\Common7\IDE

(PS: 如上你需要根据自己的VS的安装路径做出更改,如果只是针对Theano的话,其实可以不用在这里指明cl.exe的位置,因为接下来我们会在.theanorc文件中再定义一次,为了方便其他应用,最好还是写进去吧)。


2.    安装CUDA toolkit


进入NVIDA的官网,下载完CUDA  toolkit之后,一键傻瓜式安装。但是,安装之前还是要提醒你检查一下自己机器的显卡是不是NVIDA的产品。详细安装步骤参考:http://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html#axzz410A2xbq6


3.    安装Python


可以尝试使用Anaconda(下载地址:https://www.continuum.io/downloads),自动的就会帮助你安装完成Python基本环境,并配置好了诸如numpy,scipy等等python科学计算的常用工具包。如果想安装别的包也很方便,在Anaconda Prompt中键入命令:“conda install [工具包名]”即可。

安装完成Anaconda之后,需要安装依赖项。

[html]  view plain  copy
  1. conda install mingw libpython  



5.    安装PyCUDA


下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pycuda

我的Python版本是2.7,所以我下载的是pycuda‑2015.1.3+cuda7518‑cp27‑none‑win_amd64.whl

安装

[plain]  view plain  copy
  1. pip install pycuda‑2015.1.3+cuda7518‑cp27‑none‑win_amd64.whl  


6.    测试Theano和PyCUDA


最简单的import测试:

[python]  view plain  copy
  1. import theano  

输出:

Using gpu device0: GeForce GT 640M (CNMeM is disabled)

根据theano的文档(文档地址:http://deeplearning.net/software/theano/tutorial/using_gpu.html)示例snippet测试:

[python]  view plain  copy
  1. from theano import function, config, shared, sandbox  
  2. import theano.tensor as T  
  3. import numpy  
  4. import time  
  5.    
  6. vlen = 10 * 30 *768  # 10 x #cores x # threads per core  
  7. iters = 1000  
  8.    
  9. rng = numpy.random.RandomState(22)  
  10. x =shared(numpy.asarray(rng.rand(vlen), config.floatX))  
  11. f = function([],T.exp(x))  
  12. printf.maker.fgraph.toposort()  
  13. t0 = time.time()  
  14. for i inxrange(iters):  
  15.     r = f()  
  16. t1 = time.time()  
  17. print 'Looping%d times took' % iters, t1 - t0, 'seconds'  
  18. print 'Resultis', r  
  19. ifnumpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):  
  20.     print 'Used the cpu'  
  21. else:  
  22.     print 'Used the gpu'  

得到的结果如下:

Using gpu device0: GeForce GT 630M (CNMeM is disabled)

[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32,vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]

Looping 1000times took 1.42199993134 seconds

Result is [1.23178029  1.61879349  1.52278066 ...,  2.20771813 2.29967761

  1.62323296]

Used the gpu

 

测试PyCUDA(文档地址: http://documen.tician.de/pycuda/index.html

[python]  view plain  copy
  1. import pycuda.autoinit  
  2. import pycuda.driver as drv  
  3. import numpy  
  4.    
  5. from pycuda.compiler import SourceModule  
  6. mod = SourceModule(""" 
  7. __global__  void multiply_them(float *dest, float *a, float *b) 
  8. { 
  9.   const int i = threadIdx.x; 
  10.   dest[i] = a[i] * b[i]; 
  11. } 
  12. """)  
  13.    
  14. multiply_them =mod.get_function("multiply_them")  
  15.    
  16. a =numpy.random.randn(400).astype(numpy.float32)  
  17. b =numpy.random.randn(400).astype(numpy.float32)  
  18.    
  19. dest = numpy.zeros_like(a)  
  20. multiply_them(  
  21.         drv.Out(dest), drv.In(a), drv.In(b),  
  22.         block=(400,1,1), grid=(1,1))  
  23.    
  24. print (dest-a*b )


测试结果:如果你看到了一堆0那么就要恭喜你了。


个人原创.转载请注明出处

 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Parasol5/article/details/50677483


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值