introduction to cudacompmed/seminars/lecture1.pdf · introduction to cuda! jonathan baca!...

29
Introduction to CUDA Jonathan Baca

Upload: others

Post on 20-Jan-2021

25 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Introduction to CUDA!

Jonathan Baca!

Page 2: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Installing CUDA!

 CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc is installed! Download the software! Install the drivers (NVIDIA)! Install CUDA!

Page 3: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

CUDA-Ready Systems!

 In linux, run the following command:! lpsci | grep -i nvidia!

 This will show the settings on your system.!

 A list of CUDA compliant GPUs can be found at:! nvidia.com/object/cuda_gpus.html

Page 4: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Make sure you have the right Linux! Only certain Linux distributions are supported.!

  These are very specific and unfortunately rigid standards.!

 The following command can be run:!  uname -m && cat /etc/*release!

 A list of Linux distributions can be found on the CUDA website.!

Page 5: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Make sure that gcc is installed!

 Pretty self-explanatory.!

Page 6: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Download the Software!

 The software can be found at:! nvidia.com/object/cuda_get.html!

 You must download:! The NVIDIA Drivers! The CUDA Toolkit! The GPU Computing SDK!

Page 7: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Install the NVIDIA Drivers!

 Make sure everything is downloaded and you know where it is.!

 Exit the GUI using the command:! Ctrl-Alt-Backspace (sometimes twice)! or “sudo /etc/init.d/gdm stop”! or “/sbin/init 3”!

 This will vary from system to system!

Page 8: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Install the NVIDIA Drivers (cont)!

 You must have access to your system as a superuser.!

 Run the installation script.! Make sure that the right version is installed:!

  cat /proc/driver/nvidia/version! Next, what to do if you never use a GUI

environment.!

Page 9: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Install the NVIDIA Drivers (cont.)! Make sure that “/dev/nvidia*” exist.!  #/bin/bash!  /sbin/modprobe nvidia!  if [ "$?" -eq 0 ]; then!  # Count the number of NVIDIA controllers found.!  NVDEVS=`lspci | grep -i NVIDIA`!  N3D=`echo "$NVDEVS" | grep "3D controller" | wc -l`!  NVGA=`echo "$NVDEVS" | grep "VGA compatible controller" | wc -l`!  N=`expr $N3D + $NVGA - 1`!  for i in `seq 0 $N`; do!  mknod -m 666 /dev/nvidia$i c 195 $i!  done!  mknod -m 666 /dev/nvidiactl c 195 25!  else!  exit 1!  fi!

 Is a startup script to load the driver Kernel at boot time. (Must be a superuser obvs)!

Page 10: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Install the NVIDIA Drivers (cont..)!

 Restart the GUI environment using either:!  startx!  sudo /etc/init.d/gdm start!

 This is different from system to system.!

Page 11: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Install CUDA!

 Uninstall any previous incarnations of cuda:!  Delete all files from!

  /usr/local/cuda!  ~/NVIDIA_GPU_Computing_SDK or!  ~/NVIDIA_CUDA_SDK (for older installs)!

 Use the given “.run” file to install the files you need for CUDA.!

 You must define environment variables:!  export PATH=/usr/local/cuda/bin:$PATH!  export LD_LIBRARY_PATH=/usr/local/cuda/lib:

$LD_LIBRARY_PATH!

Page 12: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Install SDK!

 The second “.run” file must be run as a regular user (not a superuser)!

 The SDK will include many coding samples.!

Page 13: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Verifying Installation!

 A quick way to verify installation is to go to the C directory of the SDK installation and run:!  “make”!

 This will compile all of the examples.! Run “deviceQuery” (in the C/bin/<system>/

release directory of the SDK installation)! <system> is linux on a linux system.!

Page 14: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Installing on a Mac!

 On a Mac, you download the .dmg files.! You must still set the environment

variables.! Compiling the SDK is still done the same

way.!

Page 15: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Programming with CUDA!

 Introduction! Pros! Cons! Paradigms!

Page 16: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Introduction!

 Complete Unified Device Architecture! NVIDIA!

Page 17: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Pros!

 Extremely fast! 8-Series GPUs 25-200+!

 Scalable! Shared Memory! SIMD!

Page 18: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Cons!

 Eschews recursion! Needs a lot of data! SIMD!

Page 19: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Paradigms!

 SIMD! Threading! Groups! Blocks!

Page 20: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

SIMD!

 Single Instruction Multiple Data! One function is run on all threads! Threads within the same block can

communicate with one another!

Page 21: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Grid!

 The top level of thread organization! Contains a 2D array of uniformly sized

blocks! Declared thusly:!

 dim3 dimGrid(64, 64)!

Page 22: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Block!

 Each block is a three dimensional array of threads!

 The threads within a block can communicate with one another!

 Declared thusly:! dim3 dimBlock(16, 16, 16)!

Page 23: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Shared Memory!!

 The amount of bytes of shared memory must also be declared.!

Page 24: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Calling a Function!

 CUDA Functions are called with a special syntax.!

 Suppose our function is:!  __global__ void Asbestos(args)!  DimGrid is our grid dimension!  DimBlock is our block dimension!  SharedMemBytes are the number of bytes of

shared memory! We would call this function using the

command:!  Asbestos <<< DimGrid, DimBlock,

SharedMemBytes >>> (args);!

Page 25: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

CUDA Function Declaration!

 __device__!  Executed on the device and called from the device!

 __host__!  Executed on the device and called from the host! May be used with __device__!

 __global__!  Executed on the host and called from the host!  Kernel function! Must return void!

Page 26: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Memory Functions!

 cudaMalloc(target, size)! cudaMemcpy(target, original, size, *)! * indicates the keywords:!

 cudaMemcpyHostToDevice! cudaMemcpyDeviceToHost!

 cudaFree(target)!

Page 27: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Thread Functions!

 Thread Identification! threadIdx!

  threadIdx.x, threadIdx.y, threadIdx.z! blockIdx!

 blockIdx.y, blockIdx.y!

 Synchronization! __syncthreads()!

Page 28: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Granulation!

 Depending on the GPU, blocks cannot take certain sizes (this must be checked every time)!

 Choosing too large of blocks will slow down the hardware immensely.!

 Similarily, too small of blocks will.! Assigned to SMs (Streaming Multiprocessors)!

Page 29: Introduction to CUDAcompmed/seminars/lecture1.pdf · Introduction to CUDA! Jonathan Baca! Installing CUDA! CUDA-Ready System! Make sure you have the right Linux! Make sure that gcc

Summary!

 Installation! Verification! Demo! Paradigms! Summary!