linux安装CUDA cudnn anaconda3-python3.6 tensorflow-gpu

友情链接:

https://blog.csdn.net/taoqick/article/details/79171199

https://blog.csdn.net/masa_fish/article/details/51882183

https://blog.csdn.net/xianglao1935/article/details/80510494

准备:

  • 一块计算性能大于等于3.0的NVIDIA的显卡
    (不知道自己的NVIDIA GPU的计算性能的可以在这里查到: https://developer.nvidia.com/cuda-gpus )
  • 装有Linux系统的电脑
    (本文以Ubuntu 16.04.2 LTS Gnome 64位系统为例展开,其他的也是大同小异)
  • Python 3.6
    (建议使用Python3版本,而不是2.7版,部分Linux系统默认可能没有安装Python3)

开始:

安装NVIDIA Driver

首先检查你的NVIDIA VGA card model,检查你的显卡型号

  1. $ sudo lshw -numeric -C display

我的电脑上可以看到其中的nvidia显卡有这样一条信息

product: GM107M [GeForce GTX 850M] [10DE:1391]

根据刚才显示的信息内容,到官网上选择合适的选项,检查你的显卡所适合的驱动版本:

http://www.nvidia.com/Download/index.aspx

我的电脑上的显卡所适合的版本是375.66。

同样,你还可以在终端上输入命令来看所需的版本:

  1. $ ubuntu-drivers devices

我的电脑上出现有nividia-375字样,跟在官网上查的一样。所以我们可以在终端上使用apt-get来安装它,安装前建议先update一下源,或者更换一个速度快的源。

  1. $ sudo apt-get install nvidia-375

其中,nvidia-375这个需要根据自己显卡的型号所支持的驱动版本来修改后面的数字。

装完之后,你可以在dashboard上搜索nvidia,看到有 NVIDIA X Server Settings的东西,那么驱动就安装成功了,如果没有,那就重启一次电脑看看。

安装Cuda 8:

首先是去官网下载 cuda toolkit 8.0 ,选择你电脑对应的选项,下载文件一定要选择runfile。

https://developer.nvidia.com/cuda-downloads

下载完成之后,执行

  1. $ sudo sh cuda_8.0.61_375.26_linux.run

就进入安装了,开始的一大堆文字都是End User License Agreement,可以CTRL +C 跳过,然后输入accept表示接受协议

接下来就是安装的交互界面,根据提示一步一步进行就好。

在开始的

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?

选择n,因为已经安装驱动了。

 

  1. Using more to view the EULA.
  2.  
  3. End User License Agreement
  4.  
  5. --------------------------
  6.  
  7. Preface
  8.  
  9. -------
  10.  
  11. The following contains specific license terms and conditions
  12.  
  13. for four separate NVIDIA products. By accepting this
  14.  
  15. agreement, you agree to comply with all the terms and
  16.  
  17. conditions applicable to the specific product(s) included
  18.  
  19. herein.
  20.  
  21.  
  22. NVIDIA CUDA Toolkit
  23.  
  24.  
  25. Description
  26.  
  27.  
  28.  
  29. The NVIDIA CUDA Toolkit provides command-line and graphical
  30.  
  31. tools for building, debugging and optimizing the performance
  32.  
  33. of applications accelerated by NVIDIA GPUs, runtime and math
  34.  
  35. libraries, and documentation including programming guides,
  36.  
  37. user manuals, and API references. The NVIDIA CUDA Toolkit
  38.  
  39. License Agreement is available in Chapter 1.
  40.  
  41.  
  42.  
  43. Default Install Location of CUDA Toolkit
  44.  
  45.  
  46.  
  47. Windows platform:
  48.  
  49.  
  50.  
  51. Do you accept the previously read EULA?
  52.  
  53. accept/decline/quit: accept
  54.  
  55.  
  56.  
  57. Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?
  58.  
  59. (y)es/(n)o/(q)uit: n
  60.  
  61.  
  62.  
  63. Install the CUDA 8.0 Toolkit?
  64.  
  65. (y)es/(n)o/(q)uit: y
  66.  
  67.  
  68.  
  69. Enter Toolkit Location
  70.  
  71. [ default is /usr/local/cuda-8.0 ]:
  72.  
  73.  
  74.  
  75. Do you want to install a symbolic link at /usr/local/cuda?
  76.  
  77. (y)es/(n)o/(q)uit: y
  78.  
  79.  
  80.  
  81. Install the CUDA 8.0 Samples?
  82.  
  83. (y)es/(n)o/(q)uit: y
  84.  
  85.  
  86.  
  87. Enter CUDA Samples Location
  88.  
  89. [ default is /home/kinny ]:
  90.  
  91. Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...
  92.  
  93. Missing recommended library: libXmu.so
  94.  
  95. Installing the CUDA Samples in /home/kinny ...
  96.  
  97. Copying samples to /home/kinny/NVIDIA_CUDA-8.0_Samples now...
  98.  
  99. Finished copying samples.
  100.  
  101. ===========
  102.  
  103. = Summary =
  104.  
  105. ===========
  106.  
  107. Driver: Not Selected
  108.  
  109. Toolkit: Installed in /usr/local/cuda-8.0
  110.  
  111. Samples: Installed in /home/kinny, but missing recommended libraries
  112.  
  113. Please make sure that
  114.  
  115. - PATH includes /usr/local/cuda-8.0/bin
  116.  
  117. - LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root
  118.  
  119. To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin
  120.  
  121. Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.
  122.  
  123. ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.
  124.  
  125. To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
  126.  
  127. sudo <CudaInstaller>.run -silent -driver
  128.  
  129. Logfile is /tmp/cuda_install_17494.log

 配置Cuda环境变量:

 

终端中输入 $ sudo gedit /etc/profile
g)  在打开的文件末尾,添加以下三行。

  1. export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
  2. export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  3. export CUDA_HOME=/usr/local/cuda

其中,

前 2 个(PATH, LD_LIBRARY_PATH) 是 CUDA 官网安装文档中建议的变量。

第 3 个(CUDA_HOME)是 tensorflow-GPU 版本要求的变量。

配置完环境变量之后,保存,一定要更新一下,否则不能立即生效。也可以通过重启电脑使得环境变量生效。

  1. $ source ~/.bashrc

这里有点与官方安装文档稍有不同,需要说明:

官方文档里说只需在终端中运行上述两条export语句即可,但如果不将它们不写入/etc/profile文件的话,这样的环境变量在你退出终端后就消失了,不起作用了,所以写入才是永久的做法。

h) 重启电脑,检查上述的环境变量是否设置成功。

终端中输入 : $ env
在输出的环境变量中检查有无上述 g) 中设置的变量,如果有则代表设置成功。
 

检查显卡驱动安装结果

  1. $ nvidia-smi
  2.  
  3. Wed Jun 7 04:27:39 2017
  4.  
  5. +-----------------------------------------------------------------------------+
  6.  
  7. | NVIDIA-SMI 375.66 Driver Version: 375.66 |
  8.  
  9. |-------------------------------+----------------------+----------------------+
  10.  
  11. | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
  12.  
  13. | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
  14.  
  15. |===============================+======================+======================|
  16.  
  17. | 0 GeForce GTX 850M Off | 0000:01:00.0 Off | N/A |
  18.  
  19. | N/A 50C P0 N/A / N/A | 316MiB / 2002MiB | 2% Default |
  20.  
  21. +-------------------------------+----------------------+----------------------+
  22.  
  23.  
  24.  
  25. +-----------------------------------------------------------------------------+
  26.  
  27. | Processes: GPU Memory |
  28.  
  29. | GPU PID Type Process name Usage |
  30.  
  31. |=============================================================================|
  32.  
  33. | 0 1221 G /usr/lib/xorg/Xorg 24MiB |
  34.  
  35. | 0 1609 G /usr/lib/xorg/Xorg 150MiB |
  36.  
  37. | 0 1930 G /usr/bin/gnome-shell 90MiB |
  38.  
  39. | 0 2424 G ...el-token=3A74D277B6B5EFD197119F9AEDDC5740 24MiB |

 安装深度学习库cuDNN

首先到官网下载cuDNN5.1 ( https://developer.nvidia.com/cudnn ),需要注册为开发者才能下载,而且直接下载可能速度非常慢,如果有别人已经下载过的,应该是可以直接拿过来用,不过前提是版本是5.1的。

文件名为:cudnn-8.0-linux-x64-v5.1.tgz

然后解压

  1. $ tar xvzf cudnn-8.0-linux-x64-v5.1.tgz

下载完成后解压,得到一个 cudn 文件夹,该文件夹下include 和 lib64 两个文件夹,命令行进入 cudn/include 路径下,然后进行以下操作:

  1. $ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
  2.  
  3. $ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64

接下来就是修改文件访问权限:

  1. $ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

3、Anaconda 的安装

这里贴出来anaconda的安装链接

https://blog.csdn.net/u012318074/article/details/77074665

https://blog.csdn.net/qq_35614920/article/details/79116103

 https://blog.csdn.net/qq_37659064/article/details/79329156

参考以上三条anaconda的安装教程 有错误解析。

 

最后一步:安装TensorFlow的GPU版本

终端命令行输入

  1. $ sudo pip3 install tensorflow-gpu

如果没有pip3则运行以下代码

sudo apt-get install python3-pip  在运行 $ sudo pip3 install tensorflow-gpu

等待一会儿,就安装好了。然后可以使用我刚才提到的那篇文章中的测试代码验证是否安装成功,以及GPU是否可用。

  1. $ python3
  2. >>> import tensorflow as tf
  3. >>> hello = tf.constant('Hello, TensorFlow!')
  4. >>> sess = tf.Session()
  5. >>> print(sess.run(hello))
  6. Hello, TensorFlow!
  7. >>> a = tf.constant(10)
  8. >>> b = tf.constant(32)
  9. >>> print(sess.run(a + b))
  10. 42
  11. >>>

对于GPU版本,可以使用下面的代码来测试TF是否可以使用GPU来加速计算,如果输出False,那就是不能使用,否则是可以使用,会输出GPU信息。

  1. >>> tf.test.is_gpu_available()
  2. 2017-06-06 05:36:41.972817: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
  3. 2017-06-06 05:36:41.972852: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
  4. 2017-06-06 05:36:41.972863: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
  5. 2017-06-06 05:36:41.972871: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
  6. 2017-06-06 05:36:41.972879: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
  7. 2017-06-06 05:36:42.306655: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
  8. 2017-06-06 05:36:42.306887: I tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with properties:
  9. name: GeForce GTX 850M
  10. major: 5 minor: 0 memoryClockRate (GHz) 0.9015
  11. pciBusID 0000:01:00.0
  12. Total memory: 1.96GiB
  13. Free memory: 1.44GiB
  14. 2017-06-06 05:36:42.306905: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0
  15. 2017-06-06 05:36:42.306911: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0: Y
  16. 2017-06-06 05:36:42.306927: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, pci bus id: 0000:01:00.0)
  17. True

 

至此,如果你看到了跟我一样的输出的话,那么恭喜你,tensorflow-gpu可以正常使用GPU来计算了。

From https://blog.ailemon.me/2017/06/06/install-tensorflow-gpu-on-ubuntu-linux/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以通过修改内核的驱动程序来实现屏幕旋转方向的修改。具体步骤如下: 1. 首先需要确定屏幕的旋转方向,以及屏幕使用的显示控制器。根据不同的显示控制器,可能需要修改不同的驱动程序。 2. 打开Linux内核源码,在驱动程序的目录下找到相关的文件,例如对于使用DRM进行显示的情况,可以在`drivers/gpu/drm`目录下找到相关的驱动程序。 3. 找到需要修改的驱动程序文件,例如`drm_panel.c`,在该文件中查找与屏幕旋转相关的代码。 4. 根据需要修改的旋转方向,修改相关的代码。例如,如果需要将屏幕旋转90度,可以修改代码如下: ```c static const struct drm_display_mode panel_mode = { .clock = 74250, .hdisplay = 1080, .hsync_start = 1080 + 88, .hsync_end = 1080 + 88 + 44, .htotal = 1080 + 88 + 44 + 148, .vdisplay = 1920, .vsync_start = 1920 + 4, .vsync_end = 1920 + 4 + 5, .vtotal = 1920 + 4 + 5 + 36, .vrefresh = 60, .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC, }; ``` 可以看到,需要修改的是`hdisplay`和`vdisplay`,将它们分别改为1920和1080即可实现屏幕旋转90度。 5. 修改完成后,重新编译内核,并加载新的内核模块即可。 关于Linux内核驱动的编写,可以参考以下步骤: 1. 首先需要确定需要编写的驱动类型,例如字符设备驱动、块设备驱动、网络设备驱动等。 2. 打开Linux内核源码,在相关的驱动程序目录下创建一个新的文件,例如`my_driver.c`。 3. 在文件中定义驱动程序的数据结构和相关的操作函数,例如: ```c struct my_driver_data { struct device *dev; int data; }; static int my_driver_open(struct inode *inode, struct file *file) { struct my_driver_data *data; data = kmalloc(sizeof(struct my_driver_data), GFP_KERNEL); data->dev = file->f_path.dentry->d_inode; file->private_data = data; return 0; } static int my_driver_release(struct inode *inode, struct file *file) { struct my_driver_data *data = file->private_data; kfree(data); return 0; } static ssize_t my_driver_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { struct my_driver_data *data = file->private_data; int ret; ret = copy_to_user(buf, &data->data, sizeof(int)); return sizeof(int) - ret; } static ssize_t my_driver_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { struct my_driver_data *data = file->private_data; int ret; ret = copy_from_user(&data->data, buf, sizeof(int)); return sizeof(int) - ret; } static const struct file_operations my_driver_fops = { .owner = THIS_MODULE, .open = my_driver_open, .release = my_driver_release, .read = my_driver_read, .write = my_driver_write, }; ``` 可以看到,定义了一个`my_driver_data`结构体,包含设备指针和一个整数型数据;以及对应的设备操作函数`my_driver_open`、`my_driver_release`、`my_driver_read`和`my_driver_write`。 4. 在文件中定义驱动程序的初始化函数和销毁函数,例如: ```c static int __init my_driver_init(void) { int ret; ret = register_chrdev(0, "my_driver", &my_driver_fops); if (ret < 0) { printk(KERN_ERR "my_driver: failed to register driver\n"); return ret; } return 0; } static void __exit my_driver_exit(void) { unregister_chrdev(0, "my_driver"); } ``` 可以看到,初始化函数中调用了`register_chrdev`函数来注册设备,并指定了设备操作函数;销毁函数中调用了`unregister_chrdev`函数来注销设备。 5. 在驱动程序的顶部添加必要的宏定义和头文件包含,例如: ```c #include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/device.h> #include <linux/uaccess.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("My Driver"); MODULE_VERSION("0.1"); static int __init my_driver_init(void); static void __exit my_driver_exit(void); ``` 6. 编写Makefile文件,将驱动程序编译为内核模块,例如: ``` obj-m += my_driver.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ``` 7. 在Linux内核源码目录下,执行`make modules`命令编译内核模块,然后执行`insmod my_driver.ko`命令加载新的内核模块。 注意,在编写和修改内核驱动程序时,需要谨慎操作,建议在虚拟机环境下进行测试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值