Speeding up a Python loop with Numba JIT
Compiling a numerical function to machine code with a single decorator.
Goal: Running NumPy-style code on the GPU.
import numpy as np, cupy as cp
a = cp.asarray(np.random.rand(4096, 4096).astype(np.float32))
b = a @ a.T
cp.cuda.Stream.null.synchronize()
print("Result on:", b.device, "| shape:", b.shape)
print("First value:", float(cp.asnumpy(b[0, 0])))
cupy.asarray moves the array to GPU memory, the functions work under the same names, and cupy.asnumpy brings it back. It needs an NVIDIA GPU (or AMD with ROCm) and the CuPy package matching the CUDA version, so it was not run in a test environment without a GPU.
This example was not run in the test environment: needs a GPU and CUDA. Check it against the library documentation before relying on it.
Compiling a numerical function to machine code with a single decorator.
Writing a GPU kernel in Python instead of CUDA C++.
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.