怎么查看cuda的版本_来自CUDA的hello world

fc5f908243acf295cf44e3d8301d74f9.png

首先,你要有个N卡,如果没有的话。。。。。。。。

安装

安装环境: Ubuntu 18.04

查看显卡

lspci | grep -i nvidia

将nouveau添加黑名单

# 查看
lsmod | grep -i nouveau

# 创建黑名单
sudo vi /etc/modprobe.d/blacklist-nouveau.conf

# 添加黑名单内容
blacklist nouveau
options nouveau modeset=0

# 生效
sudo update-initramfs -u

#重启
sudo reboot

安装依赖

sudo apt-get install build-essential

sudo apt-get install linux-headers-$(uname -r)

下载CUDA SDK

  • 下载地址
  • 历史版本下载

推荐方式--runfile(local)

wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
  • 缺点: 安装包大,下载时间长
  • 优点: 可直接一次安装驱动和SDK,避免驱动版本和SDK不兼容的情况(相信我,不兼容这个情况是个坑,深不见底)。

安装

sudo sh cuda_10.2.89_440.33.01_linux.run

检查

检查驱动

nvidia-smi

会输出当前显卡信息

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.39       Driver Version: 418.39       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:04:00.0 Off |                  N/A |
| 24%   42C    P0    54W / 250W |      0MiB / 11178MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

检查SDK

进入安装目录cd /usr/local/cuda/bin(默认安装目录)。

./nvcc --version

输出版本信息:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130

配置环境变量

系统级

sudo vim /etc/environment

添加CUDA信息

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/cuda/bin"

用户级

添加到当前用户 - bash vim ~/.bashrc - zsh vim ~/.zshrc

添加CUDA信息

export CUDA_BIN=/usr/local/cuda/bin
export CUDA_LIB=/usr/local/cuda/lib64
export PATH=${PATH}:${CUDA_BIN}
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUDA_LIB}

别忘了source

检查

nvcc --version

创建一个CUDA的CMake项目

代码

// @file main.cu

#include <iostream>

__global__ void hello_world(){
    unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
    printf("[ %d ] hello worldn", idx);
}

int main() {
    hello_world<<< 1, 1 >>>();
    cudaDeviceSynchronize();
    return 0;
}

CMakeList.txt

cmake_minimum_required(VERSION 3.17)
project(cuda_01_hello_world CUDA)

set(CMAKE_CUDA_STANDARD 14)

add_executable(cuda_01_hello_world main.cu)

set_target_properties(
        cuda_01_hello_world
        PROPERTIES
        CUDA_SEPARABLE_COMPILATION ON)

输出

9e5aa9f698b3a7e7be32cf023fbcaa43.png

小朋友你是否有很多的问号?

  • __global__是什么?
  • idx是怎么算出来的?
  • <<<1, 1>>是什么?
  • cudaDeviceSynchronize()是什么?
  • 是谁打印的“hello world”?

且听下回分解

拓展阅读

  • 《Learn CUDA Programming》:chapter 1 - 4
  • An Even Easier Introduction to CUDA
  • Building Cross-Platform CUDA Applications with CMake
  • CUDA Quick Start Guide,各平台安装。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CUDA programming: a developer's guide to parallel computing with GPUs. by Shane Cook. Over the past five years there has been a revolution in computing brought about by a company that for successive years has emerged as one of the premier gaming hardware manufacturersdNVIDIA. With the introduction of the CUDA (Compute Unified Device Architecture) programming language, for the first time these hugely powerful graphics coprocessors could be used by everyday C programmers to offload computationally expensive work. From the embedded device industry, to home users, to supercomputers, everything has changed as a result of this. One of the major changes in the computer software industry has been the move from serial programming to parallel programming. Here, CUDA has produced great advances. The graphics processor unit (GPU) by its very nature is designed for high-speed graphics, which are inherently parallel. CUDA takes a simple model of data parallelism and incorporates it into a programming model without the need for graphics primitives. In fact, CUDA, unlike its predecessors, does not require any understanding or knowledge of graphics or graphics primitives. You do not have to be a games programmer either. The CUDA language makes the GPU look just like another programmable device. Throughout this book I will assume readers have no prior knowledge of CUDA, or of parallel programming. I assume they have only an existing knowledge of the C/C++ programming language. As we progress and you become more competent with CUDA, we’ll cover more advanced topics, taking you from a parallel unaware programmer to one who can exploit the full potential of CUDA. For programmers already familiar with parallel programming concepts and CUDA, we’ll be discussing in detail the architecture of the GPUs and how to get the most from each, including the latest Fermi and Kepler hardware. Literally anyone who can program in C or C++ can program with CUDA in a few hours given a little training. Getting from novice CUDA programmer, with a several times speedup to 10 times–plus speedup is what you should be capable of by the end of this book. The book is very much aimed at learning CUDA, but with a focus on performance, having first achieved correctness. Your level of skill and understanding of writing high-performance code, especially for GPUs, will hugely benefit from this text. This book is a practical guide to using CUDA in real applications, by real practitioners. At the same time, however, we cover the necessary theory and background so everyone, no matter what their background, can follow along and learn how to program in CUDA, making this book ideal for both professionals and those studying GPUs or parallel programming.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值