Set default host compiler for nvcc - cuda

I have just installed Debian Stretch (9) and Cuda 8 on a new GPU server. Stretch does not come with older versions of gcc, so I need to use clang as the host compiler (nvcc does not support gcc-6). I can do this invoking nvcc as:
nvcc -ccbin clang-3.8
Is there any way to acheive this system wide - e.g. in cuda config or an environment variable?

Documentation of nvcc does not list any env variable to change ccbin, only the option:
http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html
--compiler-bindir directory, -ccbin Specify the directory in which the compiler executable resides. The host compiler executable name can be also specified to ensure that the correct host compiler is selected.
Linux guide have no such info too: http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
You may try creating some nvcc wrapper script and putting it earlier in the PATH env var like:
mkdir ~/supernvcc
echo '#!/bin/sh' > ~/supernvcc/nvcc
echo `which nvcc` -ccbin clang-3.8 '$#' >> ~/supernvcc/nvcc
chmod +x ~/supernvcc/nvcc
export PATH=/home/`id -un`/supernvcc:$PATH
(repeat last line with export in every new shell before using nvcc or add it to your .bashrc or other shell init script)
PS: and nvcc is bash script too, you can just copy it and edit:
cat `which nvcc`
UPDATE: People recommend to link correct gcc version to the internal dir /usr/local/cuda/bin/ of cuda:
sudo ln -s /usr/bin/gcc-4.4 /usr/local/cuda/bin/gcc

you can use NVCC_PREPEND_FLAGS and NVCC_APPEND_FLAGS as described in the official docs to inject -ccbin into all calls of nvcc.
For example, I have the following in my ~/.bash_profile:
export NVCC_PREPEND_FLAGS='-ccbin /home/linuxbrew/.linuxbrew/bin/g++-11'

Related

riscv64 linux kernel compilation issue

I am trying to compile the linux kernel for riscv64 using the following link -
https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html
While building linux with the command make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig
the following error shows up -
scripts/kconfig.include:35 compiler riscv64-unknown-linux-gnu-gcc not found in PATH
scripts/kconfig/Makefile:82:recipe for target 'defconfig' failed
I have included path of tool chain. Still not working. Attached the screenshot of folder structure and error.
I would suggest to provide the full prefix for your toolchain in the make command, for example:
wget https://toolchains.bootlin.com/downloads/releases/toolchains/riscv64/tarballs/riscv64--glibc--bleeding-edge-2020.02-2.tar.bz2
mkdir -p /opt/bootlin
tar jxf riscv64--glibc--bleeding-edge-2020.02-2.tar.bz2 -C /opt/bootlin
make ARCH=riscv CROSS_COMPILE=/opt/bootlin/riscv64--glibc--bleeding-edge-2020.02-2/bin/riscv64-buildroot-linux-gnu- mrproper defconfig Image
Compilation should complete without errors - using linux 5.7.11 here:
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
YACC scripts/kconfig/parser.tab.[ch]
HOSTCC scripts/kconfig/lexer.lex.o
.../...
LD vmlinux.o
MODPOST vmlinux.o
MODINFO modules.builtin.modinfo
GEN modules.builtin
LD .tmp_vmlinux.kallsyms1
KSYM .tmp_vmlinux.kallsyms1.o
LD .tmp_vmlinux.kallsyms2
KSYM .tmp_vmlinux.kallsyms2.o
LD vmlinux
SYSMAP System.map
OBJCOPY arch/riscv/boot/Image
Kernel: arch/riscv/boot/Image is ready
It is saying compiler riscv64-unknown-linux-gnu not found which means either riscv gnu toolchain is not installed on your machine or it is not found by the shell instance in which you are trying to compile linux kernel.
To check if RISC-V GNU toolchain is installed, create a simple C file and try to compile it with RISC-V gnu toolchain with following command:
riscv64-unknown-linux-gnu-gcc <filename.c> -o <binaryname>
1. If RISC-V GNU Toolchain is not installed:
Follow the instruction here to install riscv gnu toolchain. And keep in mind to compile it with make linux instead of make.
2. If RISC-V GNU Toolchain is installed or you are done installing
Add it to the $PATH variable inside .bashrc file located on home directory.
Then try compiling your kernel again.

linker not working in Xcode -CUDA error [duplicate]

I'm currently trying to build a Cuda project with Cmake on MacOS 10.9. My C and C++ compiler are gcc, but it seems that since Mavericks gcc and g++ links to clang, which is not supported by CUDA.
Has anyone found a good solution to use the real gcc, or to make clang work without "dumpspecs"?
The issue with 10.9 is that gcc is actually clang. Please try latest CUDA toolkit and explicitely point NVCC to use /usr/bin/clang (nvcc -ccbin /usr/bin/clang). This way NVCC will know it's dealing with clang.
This is an extension of the answer provided by Eugene:
The CUDA toolkit download for Mac OSX 10.9 has been posted to the CUDA download page
It supports XCode 5 on 10.9, and will automatically use clang instead of gcc, FYI.
IF you are using XCode 5 on 10.8, please see the release notes:
ยท On Mac OS X 10.8, if you install XCode 5, including the command-line tools, the gcc compiler will get replaced with clang. You can continue to successfully compile with nvcc from the command-line by using the --ccbin /usr/bin/clang option, which instructs nvcc to use the clang compiler instead of gcc to compile any host code passed to it. However, this solution will not work when building with NSight Eclipse Edition. An alternative solution that will work from the command-line and with NSight Eclipse Edition is to download an older version of the command-line tools package from the Apple Developer website after installing XCode 5, which will re-install gcc to /usr/bin. In order to do this, go to
http://developer.apple.com/downloads/index.action
sign in with your Apple ID, and search for command-line tools using the search pane on the left side of the screen.
The problem actually was there before OS X Mavericks, I had the same problem with Moutain Lion and the answer from Eugene is 100% correct.
However, it seems that my Geforce card is not recognized as a CUDA capable device since the upgrade to Mavericks and it seems that people using softwares that use CUDA are having the same problems.
So better not update right now
I have just downloaded CUDA 5.5, installed under Mac OSX 10.8.5, with Xcode 5.0.2, and updated command line tools in Xcode.
But I couldn't get the CUDA sample "nbody" to compile.
I got all kinds of funny error messages, like clang error: unsupported option '-dumpspecs'
I thought I had solved that one by the help of some other web pages, but then other problems kept creeping up (e.g., GLEW not found, CUDA samples path undefined, ...).
(And the provided makefiles and cmake files seemed just too contrived, so that I couldn't find the bug.)
So I rolled my own makefile. I'm posting it here, in the hope that it might help others save some hours.
#!/usr/bin/make -R
# Simple Makefile for a CUDA sample program
# (because the provided ones don't work! :-( )
#
# We assume that all files in this directory produce one executable.
# Set your GPU version in variable NVCC below
#
# Developed and tested under Mac OS X 10.8.5;
# under Linux, you probably need to adjust a few paths, compiler options, and #ifdef's.
#
# Prerequisites:
# - CUDA 5.5
# - The "Command Line Tools" installed via XCode 5
# - DYLD_FALLBACK_LIBRARY_PATH or DYLD_LIBRARY_PATH must include
# /Developer/NVIDIA/CUDA-5.5/lib:/Developer/NVIDIA/CUDA-5.5/samples/common/lib/darwin
#
# GZ Dec 2013
# -------- variables and settings ---------
#
CUDA := /Developer/NVIDIA/CUDA-5.5
NVCC := nvcc -ccbin /usr/bin/clang -arch=sm_30 --compiler-options -Wall,-ansi,-Wno-extra-tokens
# -ccbin /usr/bin/clang is needed with XCode 5 under OSX 10.8
# -arch=sm_30 is needed for my laptop (it does not provide sm_35)
INCFLAGS := -I $(CUDA)/samples/common/inc
TARGET := nbody
OBJDIR := obj
MAKEFLAGS := kR
.SUFFIXES: .cu .cpp .h
ALLSOURCES := $(wildcard *.cu *.cpp)
ALLFILES := $(basename $(ALLSOURCES))
ALLOBJS := $(addsuffix .o,$(addprefix $(OBJDIR)/,$(ALLFILES)))
DEPDIR := depend
# --------- automatic targets --------------
.PHONY: all
all: $(OBJDIR) $(DEPDIR) $(TARGET)
#true
$(OBJDIR):
mkdir $#
# --------- generic rules --------------
UNAME = $(shell uname)
ifeq ($(UNAME), Darwin) # Mac OS X
# OpenGL and GLUT are frameworks
LDFLAGS = -Xlinker -framework,OpenGL,-framework,GLUT,-L,$(CUDA)/samples/common/lib/darwin,-lGLEW
endif
$(TARGET): $(ALLOBJS)
$(NVCC) $^ $(LDFLAGS) -o $#
$(OBJDIR)/%.o: %.cu
$(NVCC) -c $(INCFLAGS) $ $#
$(DEPDIR)/%.d : %.cpp $(DEPDIR)
#echo creating dependencies for $ $#
$(DEPDIR):
mkdir $#
# ------ administrative stuff -------
.PHONY: clean
clean:
rm -f *.o $(TARGET)
rm -rf $(DEPDIR) $(OBJDIR)
echo:
#echo $(ALLSOURCES)
#echo $(ALLFILES)
#echo $(ALLOBJS)
Clang become the default compiler with OsX Maverick release, gcc and g++ commands are redirected to the clang compiler. You can download gcc compiler via homebrew and link gcc and g++ commands back to the gcc compiler via following the steps below.
$ brew install gcc48
[...]
$ sudo mkdir /usr/bin/backup && sudo mv /usr/bin/gcc /usr/bin/g++ /usr/bin/backup
$ sudo ln -s /usr/local/bin/gcc-4.8 /usr/bin/gcc
$ sudo ln -s /usr/local/bin/g++-4.8 /usr/bin/g++
I have found the solution on this article:
http://gvsmirnov.ru/blog/tech/2014/02/07/building-openjdk-8-on-osx-maverick.html#all-you-had-to-do-was-follow-the-damn-train-cj
Here's a Homebrew recipe that works for me on Lion.
$ brew cat memtestG80
require "formula"
# Documentation: https://github.com/Homebrew/homebrew/wiki/Formula-Cookbook
# /usr/local/Library/Contributions/example-formula.rb
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
# Requires at compile time nvcc from https://developer.nvidia.com/cuda-downloads
# Requires popt at compile time
class Memtestg80 < Formula
homepage ""
url "https://github.com/ihaque/memtestG80/archive/master.zip"
sha1 ""
version "c4336a69fff07945c322d6c7fc40b0b12341cc4c"
# depends_on "cmake" => :build
depends_on :x11 # if your formula requires any X11/XQuartz components
def install
# ENV.deparallelize # if your formula fails when building in parallel
system "make", "-f", "Makefiles/Makefile.osx", "NVCC=/usr/local/cuda/bin/nvcc -ccbin /usr/bin/clang", "POPT_DIR=/usr/local/Cellar/popt/1.16/lib"
system "cp", "-a", "memtestG80", "/usr/local/bin"
end
test do
# `test do` will create, run in and delete a temporary directory.
#
# This test will fail and we won't accept that! It's enough to just replace
# "false" with the main program this formula installs, but it'd be nice if you
# were more thorough. Run the test with `brew test memtestG80`.
#
# The installed folder is not in the path, so use the entire path to any
# executables being tested: `system "#{bin}/program", "do", "something"`.
system "false"
end
end

nvcc compiled object on windows not a valid file format

Since I did not have access to a nVIDIA card, I was using GPUOcelot to compile and run my programs. Since I had separated out my cuda kernel and the C++ programs in two separate files (since I was using C++11 features) I was doing the following to run my program.
nvcc -c my_kernel.cu -arch=sm_20
g++ -std=c++0x -c my_main.cpp
g++ my_kernel.o my_main.o -o latest_output.o 'OcelotConfig -l'
I have recently been given access to a Windows box which has a nVIDIA card. I downloaded the CUDA toolkit for windows and mingw g++. Now I run
nvcc -c my_kernel.cu -arch=sm_20
g++ -std=c++0x -c my_main.cpp
The nvcc call now instead of producing my_kernel.o produces my_kernel.obj. And when I try to link them and run using g++ as I did before
g++ my_kernel.obj my_main.o -o m
I get the following error:
my_kernel.obj: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 status
Could you please resolve the problem? Thanks.
nvcc is a compiler wrapper that invokes the device compiler and the host compiler under the hood (it can also invoke the host linker, but you're using -c so not doing linking). On Windows, the supported host compiler is cl.exe from Visual Studio.
Linking two object files created with two different C++ compilers is typically not possible, even if you are just using CPU only. This is because the ABI is different. The error message you are seeing is simply telling you that the object file format from cl.exe (via nvcc) is incompatible with g++.
You need to compile my_main.cpp with cl.exe, if that's producing errors then that's a different question!

Converting Octave to Use CuBLAS

I'd like to convert Octave to use CuBLAS for matrix multiplication. This video seems to indicate this is as simple as typing 28 characters:
Using CUDA Library to Accelerate Applications
In practice it's a bit more complex than this. Does anyone know what additional work must be done to make the modifications made in this video compile?
UPDATE
Here's the method I'm trying
in dMatrix.cc add
#include <cublas.h>
in dMatrix.cc change all occurences of (preserving case)
dgemm
to
cublas_dgemm
in my build terminal set
export CC=nvcc
export CFLAGS="-lcublas -lcudart"
export CPPFLAGS="-I/usr/local/cuda/include"
export LDFLAGS="-L/usr/local/cuda/lib64"
the error I receive is:
libtool: link: g++ -I/usr/include/freetype2 -Wall -W -Wshadow -Wold-style-cast
-Wformat -Wpointer-arith -Wwrite-strings -Wcast-align -Wcast-qual -g -O2
-o .libs/octave octave-main.o -L/usr/local/cuda/lib64
../libgui/.libs/liboctgui.so ../libinterp/.libs/liboctinterp.so
../liboctave/.libs/liboctave.so -lutil -lm -lpthread -Wl,-rpath
-Wl,/usr/local/lib/octave/3.7.5
../liboctave/.libs/liboctave.so: undefined reference to `cublas_dgemm_'
EDIT2:
The method described in this video requires the use of the fortran "thunking library" bindings for cublas.
These steps worked for me:
Download octave 3.6.3 from here:
wget ftp://ftp.gnu.org/gnu/octave/octave-3.6.3.tar.gz
extract all files from the archive:
tar -xzvf octave-3.6.3.tar.gz
change into the octave directory just created:
cd octave-3.6.3
make a directory for your "thunking cublas library"
mkdir mycublas
change into that directory
cd mycublas
build the "thunking cublas library"
g++ -c -fPIC -I/usr/local/cuda/include -I/usr/local/cuda/src -DCUBLAS_GFORTRAN -o fortran_thunking.o /usr/local/cuda/src/fortran_thunking.c
ar rvs libmycublas.a fortran_thunking.o
switch back to the main build directory
cd ..
run octave's configure with additional options:
./configure --disable-docs LDFLAGS="-L/usr/local/cuda/lib64 -lcublas -lcudart -L/home/user2/octave/octave-3.6.3/mycublas -lmycublas"
Note that in the above command line, you will need to change the directory for the second -L switch to that which matches the path to your mycublas directory that you created in step 4
Now edit octave-3.6.3/liboctave/dMatrix.cc according to the instructions given in the video. It should be sufficient to replace every instance of dgemm with cublas_dgemm and every instance of DGEMM with CUBLAS_DGEMM. In the octave 3.6.3 version I used, there were 3 such instances of each (lower case and upper case).
Now you can build octave:
make
(make sure you are in the octave-3.6.3 directory)
At this point, for me, Octave built successfully. I did not pursue make install although I assume that would work. I simply ran octave using the ./run-octave script in the octave-3.6.3 directory.
The above steps assume a proper and standard CUDA 5.0 install. I will try to respond to CUDA-specific questions or issues, but there are any number of problems that may arise with a general Octave install on your platform. I'm not an octave expert and I won't be able to respond to those. I used CentOS 6.2 for this test.
This method, as indicated, involves modification of the C source files of octave.
Another method was covered in some detail in the S3527 session at the GTC 2013 GPU Tech Conference. This session was actually a hands-on laboratory exercise. Unfortunately the materials on that are not conveniently available. However the method there did not involve any modification of GNU Octave source, but instead uses the LD_PRELOAD capability of Linux to intercept the BLAS library calls and re-direct (the appropriate ones) to the cublas library.
A newer, better method (using the NVBLAS intercept library) is discussed in this blog article
I was able to produce a compiled executable using the information supplied. It's a horrible hack, but it works.
The process looks like this:
First produce an object file for fortran_thunking.c
sudo /usr/local/cuda-5.0/bin/nvcc -O3 -c -DCUBLAS_GFORTRAN fortran_thunking.c
Then move that object file to the src subdirectory in octave
cp /usr/local/cuda-5.0/src/fortran_thunking.o ./octave/src
run make. The compile will fail on the last step. Change to the src directory.
cd src
Then execute the failing final line with the addition of ./fortran_thunking.o -lcudart -lcublas just after octave-main.o. This produces the following command
g++ -I/usr/include/freetype2 -Wall -W -Wshadow -Wold-style-cast -Wformat
-Wpointer-arith -Wwrite-strings -Wcast-align -Wcast-qual
-I/usr/local/cuda/include -o .libs/octave octave-main.o
./fortran_thunking.o -lcudart -lcublas -L/usr/local/cuda/lib64
../libgui/.libs/liboctgui.so ../libinterp/.libs/liboctinterp.so
../liboctave/.libs/liboctave.so -lutil -lm -lpthread -Wl,-rpath
-Wl,/usr/local/lib/octave/3.7.5
An octave binary will be created in the src/.libs directory. This is your octave executable.
In a most recent version of CUDA you don't have to recompile anything. At least as I found in Debian. First, create a config file for NVBLAS (a cuBLAS wrapper). It won't work without it, at all.
tee nvblas.conf <<EOF
NVBLAS_CPU_BLAS_LIB $(dpkg -L libopenblas-base | grep libblas)
NVBLAS_GPU_LIST ALL
EOF
Then use Octave as you would usually do running it with:
LD_PRELOAD=libnvblas.so octave
NVBLAS will do what it can on a GPU while relaying everything else to OpenBLAS.
Further reading:
Benchmark for Octave.
Relevant slides for NVBLAS presentation.
Manual for nvblas.conf
Worth noting that you may not enjoy all the benefits of GPU computing depending on used CPU/GPU: OpenBLAS is quite fast with current multi-core processors. So fast that time spend copying data to GPU, working on it, and copying back could come close to time needed to do the job right on CPU. Check for yourself. Though GPUs are usually more energy efficient.

How to get the CUDA version?

Is there any quick command or script to check for the version of CUDA installed?
I found the manual of 4.0 under the installation directory but I'm not sure whether it is of the actual installed version or not.
As Jared mentions in a comment, from the command line:
nvcc --version
(or /usr/local/cuda/bin/nvcc --version) gives the CUDA compiler version (which matches the toolkit version).
From application code, you can query the runtime API version with
cudaRuntimeGetVersion()
or the driver API version with
cudaDriverGetVersion()
As Daniel points out, deviceQuery is an SDK sample app that queries the above, along with device capabilities.
As others note, you can also check the contents of the version.txt using (e.g., on Mac or Linux)
cat /usr/local/cuda/version.txt
However, if there is another version of the CUDA toolkit installed other than the one symlinked from /usr/local/cuda, this may report an inaccurate version if another version is earlier in your PATH than the above, so use with caution.
On Ubuntu Cuda V8:
$ cat /usr/local/cuda/version.txt
You can also get some insights into which CUDA versions are installed with:
$ ls -l /usr/local | grep cuda
which will give you something like this:
lrwxrwxrwx 1 root root 9 Mar 5 2020 cuda -> cuda-10.2
drwxr-xr-x 16 root root 4096 Mar 5 2020 cuda-10.2
drwxr-xr-x 16 root root 4096 Mar 5 2020 cuda-8.0.61
Given a sane PATH, the version cuda points to should be the active one (10.2 in this case).
NOTE: This only works if you are willing to assume CUDA is installed under /usr/local/cuda (which is true for the independent installer with the default location, but not true e.g. for distributions with CUDA integrated as a package). Ref: comment from #einpoklum.
[Edited answer. Thanks for everyone who corrected it]
If you run
nvidia-smi
You should find the CUDA Version highest CUDA version the installed driver supports on the top right corner of the comand's output. At least I found that output for CUDA version 10.0 e.g.,
For CUDA version:
nvcc --version
Or use,
nvidia-smi
For cuDNN version:
For Linux:
Use following to find path for cuDNN:
$ whereis cuda
cuda: /usr/local/cuda
Then use this to get version from header file,
$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
For Windows,
Use following to find path for cuDNN:
C:\>where cudnn*
C:\Program Files\cuDNN7\cuda\bin\cudnn64_7.dll
Then use this to dump version from header file,
type "%PROGRAMFILES%\cuDNN7\cuda\include\cudnn.h" | findstr CUDNN_MAJOR
If you're getting two different versions for CUDA on Windows -
Different CUDA versions shown by nvcc and NVIDIA-smi
Use the following command to check CUDA installation by Conda:
conda list cudatoolkit
And the following command to check CUDNN version installed by conda:
conda list cudnn
If you want to install/update CUDA and CUDNN through CONDA, please use the following commands:
conda install -c anaconda cudatoolkit
conda install -c anaconda cudnn
Alternatively you can use following commands to check CUDA installation:
nvidia-smi
OR
nvcc --version
If you are using tensorflow-gpu through Anaconda package (You can verify this by simply opening Python in console and check if the default python shows Anaconda, Inc. when it starts, or you can run which python and check the location), then manually installing CUDA and CUDNN will most probably not work. You will have to update through conda instead.
If you want to install CUDA, CUDNN, or tensorflow-gpu manually, you can check out the instructions here https://www.tensorflow.org/install/gpu
Other respondents have already described which commands can be used to check the CUDA version. Here, I'll describe how to turn the output of those commands into an environment variable of the form "10.2", "11.0", etc.
To recap, you can use
nvcc --version
to find out the CUDA version.
I think this should be your first port of call.
If you have multiple versions of CUDA installed, this command should print out the version for the copy which is highest on your PATH.
The output looks like this:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Thu_Jun_11_22:26:38_PDT_2020
Cuda compilation tools, release 11.0, V11.0.194
Build cuda_11.0_bu.TC445_37.28540450_0
We can pass this output through sed to pick out just the MAJOR.MINOR release version number.
CUDA_VERSION=$(nvcc --version | sed -n 's/^.*release \([0-9]\+\.[0-9]\+\).*$/\1/p')
If nvcc isn't on your path, you should be able to run it by specifying the full path to the default location of nvcc instead.
/usr/local/cuda/bin/nvcc --version
The output of which is the same as above, and it can be parsed in the same way.
Alternatively, you can find the CUDA version from the version.txt file.
cat /usr/local/cuda/version.txt
The output of which
CUDA Version 10.1.243
can be parsed using sed to pick out just the MAJOR.MINOR release version number.
CUDA_VERSION=$(cat /usr/local/cuda/version.txt | sed 's/.* \([0-9]\+\.[0-9]\+\).*/\1/')
Note that sometimes the version.txt file refers to a different CUDA installation than the nvcc --version. In this scenario, the nvcc version should be the version you're actually using.
We can combine these three methods together in order to robustly get the CUDA version as follows:
if nvcc --version 2&> /dev/null; then
# Determine CUDA version using default nvcc binary
CUDA_VERSION=$(nvcc --version | sed -n 's/^.*release \([0-9]\+\.[0-9]\+\).*$/\1/p');
elif /usr/local/cuda/bin/nvcc --version 2&> /dev/null; then
# Determine CUDA version using /usr/local/cuda/bin/nvcc binary
CUDA_VERSION=$(/usr/local/cuda/bin/nvcc --version | sed -n 's/^.*release \([0-9]\+\.[0-9]\+\).*$/\1/p');
elif [ -f "/usr/local/cuda/version.txt" ]; then
# Determine CUDA version using /usr/local/cuda/version.txt file
CUDA_VERSION=$(cat /usr/local/cuda/version.txt | sed 's/.* \([0-9]\+\.[0-9]\+\).*/\1/')
else
CUDA_VERSION=""
fi
This environment variable is useful for downstream installations, such as when pip installing a copy of pytorch that was compiled for the correct CUDA version.
python -m pip install \
"torch==1.9.0+cu${CUDA_VERSION/./}" \
"torchvision==0.10.0+cu${CUDA_VERSION/./}" \
-f https://download.pytorch.org/whl/torch_stable.html
Similarly, you could install the CPU version of pytorch when CUDA is not installed.
if [ "$CUDA_VERSION" = "" ]; then
MOD="+cpu";
echo "Warning: Installing CPU-only version of pytorch"
else
MOD="+cu${CUDA_VERSION/./}";
echo "Installing pytorch with $MOD"
fi
python -m pip install \
"torch==1.9.0${MOD}" \
"torchvision==0.10.0${MOD}" \
-f https://download.pytorch.org/whl/torch_stable.html
But be careful with this because you can accidentally install a CPU-only version when you meant to have GPU support.
For example, if you run the install script on a server's login node which doesn't have GPUs and your jobs will be deployed onto nodes which do have GPUs. In this case, the login node will typically not have CUDA installed.
On Ubuntu :
Try
$ cat /usr/local/cuda/version.txt
or
$ cat /usr/local/cuda-8.0/version.txt
Sometimes the folder is named "Cuda-version".
If none of above works, try going to
$ /usr/local/
And find the correct name of your Cuda folder.
Output should be similar to:
CUDA Version 8.0.61
If you have installed CUDA SDK, you can run "deviceQuery" to see the version of CUDA
If you have PyTorch installed, you can simply run the following code in your IDE:
import torch
print(torch.version.cuda)
On Windows 10, I found nvidia-smi.exe in 'C:\Program Files\NVIDIA Corporation\NVSMI'; after cd into that folder (was not in the PATH in my case) and '.\nvidia-smi.exe' it showed
You might find CUDA-Z useful, here is a quote from their Site:
"This program was born as a parody of another Z-utilities such as CPU-Z and GPU-Z. CUDA-Z shows some basic information about CUDA-enabled GPUs and GPGPUs. It works with nVIDIA Geforce, Quadro and Tesla cards, ION chipsets."
http://cuda-z.sourceforge.net/
On the Support Tab there is the URL for the Source Code: http://sourceforge.net/p/cuda-z/code/ and the download is not actually an Installer but the Executable itself (no installation, so this is "quick").
This Utility provides lots of information and if you need to know how it was derived there is the Source to look at. There are other Utilities similar to this that you might search for.
One can get the cuda version by typing the following in the terminal:
$ nvcc -V
# below is the result
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85
Alternatively, one can manually check for the version by first finding out the installation directory using:
$ whereis -b cuda
cuda: /usr/local/cuda
And then cd into that directory and check for the CUDA version.
We have three ways to check Version:
In my case below is the output:-
Way 1:-
cat /usr/local/cuda/version.txt
Output:-
CUDA Version 10.1.243
Way2:-
nvcc --version
Output:-
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85
Way3:-
/usr/local/cuda/bin/nvcc --version
Output:-
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
Way4:-
nvidia-smi
NVIDIA-SMI 450.36.06 Driver Version: 450.36.06 CUDA Version: 11.0
Outputs are not same. Don't know why it's happening.
First you should find where Cuda installed.
If it's a default installation like here the location should be:
for ubuntu:
/usr/local/cuda
in this folder you should have a file
version.txt
open this file with any text editor or run:
cat version.txt
from the folder
OR
cat /usr/local/cuda/version.txt
On Windows 11 with CUDA 11.6.1, this worked for me:
cat "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\version.json"
if nvcc --version is not working for you then use cat /usr/local/cuda/version.txt
After installing CUDA one can check the versions by: nvcc -V
I have installed both 5.0 and 5.5 so it gives
Cuda Compilation Tools,release 5.5,V5.5,0
This command works for both Windows and Ubuntu.
Apart from the ones mentioned above, your CUDA installations path (if not changed during setup) typically contains the version number
doing a which nvcc should give the path and that will give you the version
PS: This is a quick and dirty way, the above answers are more elegant and will result in the right version with considerable effort
If you are running on linux:
dpkg -l | grep cuda
If you have multiple CUDA installed, the one loaded in your system is CUDA associated with "nvcc". Therefore, "nvcc --version" shows what you want.
Open a terminal and run these commands:
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
You can get the information of CUDA Driver version, CUDA Runtime Version, and also detailed information for GPU(s). An image example of the output from my end is as below.
You can find the image here.
i get /usr/local - no such file or directory. Though nvcc -V gives
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Sep__4_22:14:01_CDT_2016
Cuda compilation tools, release 8.0, V8.0.44
Found mine after:
whereis cuda
at
cuda: /usr/lib/cuda /usr/include/cuda.h
with
nvcc --version
CUDA Version 9.1.85
Using tensorflow:
import tensorflow as tf
from tensorflow.python.platform import build_info as build
print(f"tensorflow version: {tf.__version__}")
print(f"Cuda Version: {build.build_info['cuda_version']}")
print(f"Cudnn version: {build.build_info['cudnn_version']}")
tensorflow version: 2.4.0
Cuda Version: 11.0
Cudnn version: 8
Programmatically with the CUDA Runtime API C++ wrappers (caveat: I'm the author):
auto v1 = cuda::version::maximum_supported_by_driver();
auto v2 = cuda::version::runtime();
This gives you a cuda::version_t structure, which you can compare and also print/stream e.g.:
if (v2 < cuda::version_t{ 8, 0 } ) {
std::cerr << "CUDA version " << v2 << " is insufficient." std::endl;
}
You can check the version of CUDA using
nvcc -V
or you can use
nvcc --version
or You can check the location of where the CUDA is using
whereis cuda
and then do
cat location/of/cuda/you/got/from/above/command
On my cuda-11.6.0 installation, the information can be found in /usr/local/cuda/version.json. It contains the full version number (11.6.0 instead of 11.6 as shown by nvidia-smi.
The information can be retrieved as follows:
python -c 'import json; print(json.load(open("/usr/local/cuda/version.json"))["cuda"]["version"])'
If there is a version mismatch between nvcc and nvidia-smi then different versions of cuda are used as driver and run time environemtn.
To ensure same version of CUDA drivers are used what you need to do is to get CUDA on system path.
First run whereis cuda and find the location of cuda driver.
Then go to .bashrc and modify the path variable and set the directory precedence order of search using variable 'LD_LIBRARY_PATH'.
for instance
$ whereis cuda
cuda: /usr/lib/cuda /usr/include/cuda.h /usr/local/cuda
CUDA is installed at /usr/local/cuda, now we need to to .bashrc and add the path variable as:
vim ~/.bashrc
export PATH="/usr/local/cuda/bin:${PATH}"
and after this line set the directory search path as:
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:${LD_LIBRARY_PATH}"
Then save the .bashrc file. And refresh it as:
$ source ~/.bashrc
This will ensure you have nvcc -V and nvidia-smi to use the same version of drivers.