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

Hands-On GPU Programming with Python and CUDA: Explore high-performance parallel computing with CUDA

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Table of content icon View table of contents Preview book icon Preview Book

Hands-On GPU Programming with Python and CUDA

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?
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Expand your background in GPU programming—PyCUDA, scikit-cuda, and Nsight
  • Effectively use CUDA libraries such as cuBLAS, cuFFT, and cuSolver
  • Apply GPU programming to modern data science applications

Description

Hands-On GPU Programming with Python and CUDA hits the ground running: you’ll start by learning how to apply Amdahl’s Law, use a code profiler to identify bottlenecks in your Python code, and set up an appropriate GPU programming environment. You’ll then see how to “query” the GPU’s features and copy arrays of data to and from the GPU’s own memory. As you make your way through the book, you’ll launch code directly onto the GPU and write full blown GPU kernels and device functions in CUDA C. You’ll get to grips with profiling GPU code effectively and fully test and debug your code using Nsight IDE. Next, you’ll explore some of the more well-known NVIDIA libraries, such as cuFFT and cuBLAS. With a solid background in place, you will now apply your new-found knowledge to develop your very own GPU-based deep neural network from scratch. You’ll then explore advanced topics, such as warp shuffling, dynamic parallelism, and PTX assembly. In the final chapter, you’ll see some topics and applications related to GPU programming that you may wish to pursue, including AI, graphics, and blockchain. By the end of this book, you will be able to apply GPU programming to problems related to data science and high-performance computing.

Who is this book for?

Hands-On GPU Programming with Python and CUDA is for developers and data scientists who want to learn the basics of effective GPU programming to improve performance using Python code. You should have an understanding of first-year college or university-level engineering mathematics and physics, and have some experience with Python as well as in any C-based programming language such as C, C++, Go, or Java.

What you will learn

  • Launch GPU code directly from Python
  • Write effective and efficient GPU kernels and device functions
  • Use libraries such as cuFFT, cuBLAS, and cuSolver
  • Debug and profile your code with Nsight and Visual Profiler
  • Apply GPU programming to datascience problems
  • Build a GPU-based deep neuralnetwork from scratch
  • Explore advanced GPU hardware features, such as warp shuffling
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 27, 2018
Length: 310 pages
Edition : 1st
Language : English
ISBN-13 : 9781788993913
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Publication date : Nov 27, 2018
Length: 310 pages
Edition : 1st
Language : English
ISBN-13 : 9781788993913
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 111.97
Hands-On GPU Programming with Python and CUDA
€36.99
Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA
€41.99
Hands-On GPU Computing with Python
€32.99
Total 111.97 Stars icon

Table of Contents

14 Chapters
Why GPU Programming? Chevron down icon Chevron up icon
Setting Up Your GPU Programming Environment Chevron down icon Chevron up icon
Getting Started with PyCUDA Chevron down icon Chevron up icon
Kernels, Threads, Blocks, and Grids Chevron down icon Chevron up icon
Streams, Events, Contexts, and Concurrency Chevron down icon Chevron up icon
Debugging and Profiling Your CUDA Code Chevron down icon Chevron up icon
Using the CUDA Libraries with Scikit-CUDA Chevron down icon Chevron up icon
The CUDA Device Function Libraries and Thrust Chevron down icon Chevron up icon
Implementation of a Deep Neural Network Chevron down icon Chevron up icon
Working with Compiled GPU Code Chevron down icon Chevron up icon
Performance Optimization in CUDA Chevron down icon Chevron up icon
Where to Go from Here Chevron down icon Chevron up icon
Assessment Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Most Recent
Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(7 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Most Recent

Filter reviews by




Alexander Shnaiderman Feb 27, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good book, and came fast
Amazon Verified review Amazon
Joseph Picone Aug 25, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is an excellent introduction on how to program a GPU. I use it in my split-level course on parallel processing and GPU programming. It explains key concepts very clearly.
Amazon Verified review Amazon
Ahmad Junaid Nov 27, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book has given tremendous practical value to my projects as a researcher and engineer. A few words could never do it justice, but it’s for anyone seeking 100x speed improvements without having to give up the ease and comfort of Python’s development environment. It goes step by step through implementations of highly performant heterogenous computing programs right within Python, with readily reusable kernels—but it also treats the theoretical aspects in depth, covering core concepts in both CUDA C and general massively parallelized systems design.About to start on another ML project, I waited impatiently for the second edition to implement the changes moving from Python 2.x to 3. It’s unfortunate that its release has been delayed so, but when I reached out to the author directly I was shocked to have him offer to help and share his updated materials and notes from the upcoming second edition. I’m truly honoured, forever grateful and looking forward to more titles from him.
Amazon Verified review Amazon
Mark Ettinger Jan 09, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is clear, thorough, and comprehensive. Because it leverages PyCUDA, a python interface to the NVIDIA compiler, you can work your way through the book using free Colab notebooks with GPU runtimes. This is helpful if you don't already have a NVIDIA GPU, for example if you own a Mac as I do. The code uses Python 2 which is being phased out on Colab so you may need to convert the code to Python 3. There are websites and scripts that do this automatically. This book also makes a good predecessor to another good book "Professional CUDA C Programming" or the two can be read in parallel (pun intended). Highly recommended!
Amazon Verified review Amazon
Yading Yue May 06, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I followed the guides in the book and adapted the codes from the book in my own kernel which is running correctly now. The author was recommending that Python 2 is more stable than 3, which is very true -- with 3, I got many strange nvcc errors, even for the sample codes of the book when only a blank space or a blank line was added. I would recommend the book anyone who needs to save their time.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela