Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On GPU Programming with Python and CUDA

You're reading from  Hands-On GPU Programming with Python and CUDA

Product type Book
Published in Nov 2018
Publisher Packt
ISBN-13 9781788993913
Pages 310 pages
Edition 1st Edition
Languages
Author (1):
Dr. Brian Tuomanen Dr. Brian Tuomanen
Profile icon Dr. Brian Tuomanen

Table of Contents (15) Chapters

Preface 1. Why GPU Programming? 2. Setting Up Your GPU Programming Environment 3. Getting Started with PyCUDA 4. Kernels, Threads, Blocks, and Grids 5. Streams, Events, Contexts, and Concurrency 6. Debugging and Profiling Your CUDA Code 7. Using the CUDA Libraries with Scikit-CUDA 8. The CUDA Device Function Libraries and Thrust 9. Implementation of a Deep Neural Network 10. Working with Compiled GPU Code 11. Performance Optimization in CUDA 12. Where to Go from Here 13. Assessment 14. Other Books You May Enjoy

Setting Up Your GPU Programming Environment

We will now see how to set up an appropriate environment for GPU programming under both Windows and Linux. In both cases, there are several steps we will have to take. We will proceed through these steps one-by-one, noting any differences between Linux and Windows as we proceed. You should, of course, feel free to skip or ignore any sections or comments that don't apply to your choice of operating system.

The reader should note that we will only cover two platforms for 64-bit Intel/AMD-based PCs in this chapter—Ubuntu LTS (long-term support) releases and Windows 10. Note that any Ubuntu LTS-based Linux operating systems (such as Xubuntu, Kubuntu, or Linux Mint) are also equally appropriate to the generic Unity/GNOME-based Ubuntu releases.

We suggest the use of Python 2.7 over Python 3.x. Python 2.7 has stable support across...

Technical requirements

Ensuring that we have the right hardware

For this book, we recommend that you have the following hardware as a minimum:

  • 64-bit Intel/AMD-based PC
  • 4 gigabytes (GB) of RAM
  • NVIDIA GeForce GTX 1050 GPU (or higher)

This configuration will ensure that you can comfortably learn GPU programming, run all of the examples in this book, and also run some of the other newer and interesting GPU-based software, such as Google's TensorFlow (a machine learning framework) or the Vulkan SDK (a cutting-edge graphics API).

Note that you must have an NVIDIA brand GPU to make use of this book! The CUDA Toolkit is proprietary for NVIDIA cards, so it won't work for programming Intel HD or Radeon GPUs.

As stated, we will be assuming that you are using either the Windows 10 or Ubuntu LTS (long-term support) release.

Ubuntu LTS releases generally have version numbers of the form 14.04, 16.04...

Installing the GPU drivers

If you already have drivers for your GPU installed, you may possibly skip this step; moreover, some versions of CUDA are pre-packaged with the latest drivers. Quite often, CUDA is very particular about which driver you have installed and may not even work with the CUDA Toolkit driver, so you may have to experiment with several different drivers before you find one that works.

Generally speaking, Windows has better CUDA driver compatibility and a more user-friendly installation than Linux. Windows users may consider skipping this step and just use the driver that is packaged with the CUDA Toolkit, which we will install a little later in this chapter. We would strongly suggest that Linux users (particularly Linux laptop users), however, closely follow all the steps in this section before proceeding.

...

Setting up a C++ programming environment

Now that we have our drivers installed, we have to set up our C/C++ programming environment; both Python and CUDA are particular about what compilers and IDEs they may integrate with, so you may have to be careful. In the case of Ubuntu Linux users, the standard repository compilers and IDEs generally work and integrate perfectly with the CUDA Toolkit, while Windows users might have to exercise a little more caution.

Setting up GCC, Eclipse IDE, and graphical dependencies (Linux)

Open up a Terminal from the Ubuntu desktop (Ctrl + Alt + T). We first update the apt repository as follows:

sudo apt-get update

Now we can install everything we need for CUDA with one additional line:

sudo...

Setting up our Python environment for GPU programming

With our compilers, IDEs, and the CUDA Toolkit properly installed on our system, we now can set up an appropriate Python environment for GPU programming. There are many options here, but we explicitly recommend that you work with the Anaconda Python Distribution. Anaconda Python is a self-contained and user-friendly distribution that can be installed directly in your user directory, and which does not require any administrator or sudo level system access to install, use, or update.

Keep in mind that Anaconda Python comes in two flavors—Python 2.7, and Python 3. Since Python 3 is currently not as well-supported for some of the libraries we will be using, we will be using Python 2.7 in this book, which still has a broad mainstream usage.

You can install Anaconda Python by going to https://www.anaconda.com/download, choosing...

Summary

Setting up your Python environment for GPU programming can be a very delicate process. The Anaconda Python 2.7 distribution is suggested for both Windows and Linux users for the purposes of this text. First, we should ensure that we have the correct hardware for GPU programming; generally speaking, a 64-bit Windows or Linux PC with 4 gigabytes of RAM and any entry-level NVIDIA GPU from 2016 or later will be sufficient for our ends. Windows users should be careful in using a version of Visual Studio that works well with both the CUDA Toolkit and Anaconda (such as VS 2015), while Linux users should be particularly careful in the installation of their GPU drivers, and set up the appropriate environment variables in their .bashrc file. Furthermore, Windows users should create an appropriate launch script that will set up their environment for GPU programming and should use...

Questions

  1. Can we run CUDA on our main processor's built-in Intel HD GPU? What about on a discrete AMD Radeon GPU?
  2. Does this book use Python 2.7 or Python 3.7 for examples?
  3. What program do we use in Windows to see what GPU hardware we have installed?
  4. What command-line program do we use in Linux to see what GPU hardware we have installed?
  5. What is the command we use in Linux to determine how much memory our system has?
  6. If we don't want to alter our Linux system's APT repository, should we use the run or deb installer for CUDA?
lock icon The rest of the chapter is locked
You have been reading a chapter from
Hands-On GPU Programming with Python and CUDA
Published in: Nov 2018 Publisher: Packt ISBN-13: 9781788993913
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}