CuPy

From ScientificComputing
Jump to: navigation, search

< Examples

CuPy implements NumPy-compatible arrays and mimics NumPy and SciPy functions for CUDA-backed GPUs. It can be a quick drop-in replacement into your Python code and help accelerate your computation.

Load modules

Switch to the new software stack

$ env2lmod

or, set your default software stack to the new software stack

$ set_software_stack.sh new

Load the Python module for GPU

$ module load gcc/6.3.0 python_gpu/3.8.5

CuPy code

Create a working directory on $SCRATCH

[jarunanp@eu-login-14 ~]$ cd $SCRATCH
[jarunanp@eu-login-14 jarunanp]$ mkdir l2_cupy
[jarunanp@eu-login-14 jarunanp]$ cd l2_cupy
[jarunanp@eu-login-14 l2_cupy]$

Open a new script l2_cupy.py with a text editor and add the code:

import numpy as np 
import cupy as cp 
import sys

points = np.random.rand(10_000)

# Move data to GPU
x_gpu = cp.array(points)    
 
# Compute on the GPU
l2_gpu = cp.linalg.norm(x_gpu)  
   
# Return the array to host memory     
result = cp.asnumpy(l2_gpu)

print(result)

Request an interactive session on a compute node

[jarunanp@eu-login-14 l2_cupy]$ bsub -n 1 -R "rusage[ngpus_excl_p=1]" -Is bash
Generic job.
Job <175624777> is submitted to queue <gpu.4h>.
<<Waiting for dispatch ...>>
<<Starting on eu-g3-004>>
FILE: /sys/fs/cgroup/cpuset/lsf/euler/job.175624777.1850.1623842420/tasks
[jarunanp@eu-g3-004 l2_cupy]$ 

Execute the script on GPU

[jarunanp@eu-g3-004 l2_cupy]$ python l2_cupy.py
57.54006418943398


< Examples