Book Image

Hands-On GPU Computing with Python

By : Avimanyu Bandyopadhyay
Book Image

Hands-On GPU Computing with Python

By: Avimanyu Bandyopadhyay

Overview of this book

GPUs are proving to be excellent general purpose-parallel computing solutions for high-performance tasks such as deep learning and scientific computing. This book will be your guide to getting started with GPU computing. It begins by introducing GPU computing and explaining the GPU architecture and programming models. You will learn, by example, how to perform GPU programming with Python, and look at using integrations such as PyCUDA, PyOpenCL, CuPy, and Numba with Anaconda for various tasks such as machine learning and data mining. In addition to this, you will get to grips with GPU workflows, management, and deployment using modern containerization solutions. Toward the end of the book, you will get familiar with the principles of distributed computing for training machine learning models and enhancing efficiency and performance. By the end of this book, you will be able to set up a GPU ecosystem for running complex applications and data models that demand great processing capabilities, and be able to efficiently manage memory to compute your application effectively and quickly.
Table of Contents (17 chapters)
Free Chapter
1
Section 1: Computing with GPUs Introduction, Fundamental Concepts, and Hardware
5
Section 2: Hands-On Development with GPU Programming
11
Section 3: Containerization and Machine Learning with GPU-Powered Python

Writing your first PyOpenCL programs to compute a general-purpose solution

As we discussed in the previous section, PyOpenCL has a clear advantage with respect to code length over OpenCL and HIP as the former is Python-based. Now, let's get our hands dirty and write our first PyOpenCL code. Follow these steps to get started:

  1. First, open a new file on PyCharm:
  1. Now, let's use the following code to implement OpenCL code within a single .py Python file. This is a regular and basic format in PyOpenCL:
import pyopencl as cl # Importing the OpenCL API
import numpy # Import Numpy for using numbers
from time import time # Import access to the current time

N = 500000000 # 500 Million Elements

a = numpy.zeros(N).astype(numpy.double) # Create a numpy array with all zeroes
b = numpy.zeros(N).astype(numpy.double) # Create a second numpy array with all zeroes

a.fill(23.0) # set all...