Python CuPy - GeeksforGeeks

05 Dec, 2019
NumPy surely is the most compatible library for any pythonic code to process large amounts of data in the form of numpy.ndarray(n-dimensional matrix array). However, as soon as we increase the size of data processed to a significant amount the multicore CPU cannot process data as efficiently as required because Numpy is only running on CPU (mostly having 4-8 cores on consumer end) having the limited parallel processing power.
This is where a new nice python library comes in CuPy. CuPy is a NumPy compatible library for GPU.
CuPy is an open-source matrix library accelerated with NVIDIA CUDA. It also uses CUDA-related libraries including cuBLAS, cuDNN, cuRand, cuSolver, cuSPARSE, cuFFT, and NCCL to make full use of the GPU architecture.
It is an implementation of a NumPy-compatible multi-dimensional array on CUDA. CuPy consists of cupy.ndarray, the core multi-dimensional array class, and many functions on it. It supports a subset of numpy.ndarray interface. And it can also accelerate the existing NumPy code through GPU and CUDA libraries.
pip
python 3
Anaconda(optional)
CUDA X.0 (depending upon the hardware)
CPU: 2x Intel Xeon E5–2698 v4 @ 2.20GHz
Main memory: 1 TB
GPU: NVIDIA Tesla V100 32 GB
OS – Windows/Linux
InstallationDownload the compatible version of the CUDA setup on your device and install it. To install it open the terminal and enter
pip install cupy-cuda(version)
where version will be the version of CUDA installed on your device. For example-(For CUDA 10.0)
pip install cupy-cuda100
If your device does not support CUDA then you can install CuPy in Anaconda and use it for CPU based computing. Alternatively, Anaconda works fine with CUDA too.
To install it on Anaconda –
Open the Anaconda prompt and enterconda install -c anaconda cupyOrUse Anaconda navigator(GUI) to directly install cupy library.
Open the Anaconda prompt and enterconda install -c anaconda cupyOr
conda install -c anaconda cupy
Or
Use Anaconda navigator(GUI) to directly install cupy library.
Basics of cupy.ndarray
Importing – In the following code, cp is an abbreviation of cupy, as np is numpy as is customarily done.import numpy as np
import cupy as cp
Just like Numpy, CuPy also have a ndarray class cupy.ndarray which is compatible GPU alternative of numpy.ndarray.x_gpu = cp.array([1, 2, 3])
x_gpu in the above example is an instance of cupy.ndarray. You can see its creation of identical to NumPy’s one, except that numpy is replaced with cupy.
Importing – In the following code, cp is an abbreviation of cupy, as np is numpy as is customarily done.import numpy as np
import cupy as cp

import numpy as np
import cupy as cp

Just like Numpy, CuPy also have a ndarray class cupy.ndarray which is compatible GPU alternative of numpy.ndarray.x_gpu = cp.array([1, 2, 3])
x_gpu in the above example is an instance of cupy.ndarray. You can see its creation of identical to NumPy’s one, except that numpy is replaced with cupy.
x_gpu = cp.array([1, 2, 3])

x_gpu in the above example is an instance of cupy.ndarray. You can see its creation of identical to NumPy’s one, except that numpy is replaced with cupy.
Example – Take the Euclidean norm (a.k.a L2 norm).
import cupy as cpimport numpy as np  x_cpu = np.array([1, 2, 3])x_gpu = cp.array([1, 2, 3])  l2_cpu = np.linalg.norm(x_cpu)l2_gpu = cp.linalg.norm(x_gpu)  print("Using Numpy: ", l2_cpu)print("\nUsing Cupy: ", l2_gpu)
Using Numpy: 3.7416573867739413

Using Cupy: array(3.74165739)
CuPy is a NumPy compatible library for GPU. It is more efficient as compared to numpy because array operations with NVIDIA GPUs can provide considerable speedups over CPU computing.
Note- The configurations used here are for CPU is intel i7-7700 HQ and GPU is Geforce GTX 1050 4GB using CUDA 9.0.
# Python program to # demonstrate speed comparison# between cupy and numpy  # Importing modulesimport cupy as cpimport numpy as npimport time  # NumPy and CPU Runtimes = time.time()x_cpu = np.ones((1000, 1000, 100))e = time.time()print("Time consumed by numpy: ", e - s)  # CuPy and GPU Runtimes = time.time()x_gpu = cp.ones((1000, 1000, 100))e = time.time()print("\nTime consumed by cupy: "e - s)
Time consumed by numpy: 0.4238910675048828
Time consumed by cupy: 0.0010099411010742188

Here, we can see that CuPy could work way faster than NumPy.
CuPy implements many functions on cupy.ndarray objects. See the reference for the supported subset of NumPy API. Understanding NumPy might help utilizing most features of CuPy. So, it is recommend you to read the NumPy documentation.
References – https://cupy.chainer.org/
sid779
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists