Zero To GPU
This is a work-in-progress free and open-source online book, teaching beginner programmers how to write performant GPU kernels that can be called from Python. Subscribe to the RSS feed to stay up to date.
What is Mojo?
Mojo is a performance-focussed language that gives you ergonomic access to low-level primitives like SIMD and GPU intrinsics. Syntactically and ergonomically it's similar to Python, as it's goal is to unify the AI ecosystem. It gives Python developers a smooth path to understanding and writing safe performant CPU code and GPU kernels, without having to learn an onerous language like C++.
The language has a package manager to make integration with the Python ecosystem seamless:
Install the Magic CLI
Simply run:
curl -ssL https://magic.modular.com | bashInstalling the latest version of Magic...
Done. The 'magic' binary is in '/Users/jack/.modular/bin'Restart your terminal and initialize a new project, then cd into the project:
magic init --format mojoproject hello
cd hello✔ Created /private/var/folders/z4/syf53w0d243ft3bpzdj55s9w0000gn/T/mdl/hello/mojoproject.toml
✔ Added max >=25.1.0.dev2025020905,<26Your First Mojo Program
Create a file named hello.mojo:
def main():
print("Hello Mojo🔥")And run it with:
magic run mojo hello.mojoHello Mojo🔥Adding a Task
Create a task that accepts standard bash-like commands:
magic task add hello "magic run mojo hello.mojo"✔ Added task `hello`: magic run mojo hello.mojoAnd run it with:
magic run helloHello Mojo🔥Adding Community Packages
MAX is the official Modular package that allows you to do write GPU kernels with Mojo and call them from Python. Let's modify the version to be a wildcard so it'll use the latest compatible version with any community packages we add:
magic add "max==*"✔ Added max==*And add a community package to the project:
magic add emberjson✔ Added emberjson >=0.1.1,<0.2Update the hello.mojo file to use the community package:
from emberjson import parse
def main():
json = parse('{"Hello": "Mojo🔥"}')
print(json["Hello"])And run it again with:
magic run hello"Mojo🔥"You can find curated Mojo community packages at builds.modular.com.
The mojoproject.toml File
This file has been modified automatically while we've been running commands to update our project:
[project]
...
channels = [
"https://conda.modular.com/max-nightly",
"https://repo.prefix.dev/modular-community",
"conda-forge"
]
name = "hello"
platforms = ["osx-arm64"]
version = "0.1.0"
[tasks]
hello = "mojo run hello.mojo"
[dependencies]
max = "*"
emberjson = ">=0.1.1,<0.2"You can modify this file directly to add more dependencies and tasks.
Activate the Virtual Environment
If you'd prefer to put Mojo and other installed binaries on path so you can call them directly, you can activate the virtual environment:
magic shell
mojo run hello.mojoInstall Mojo globally
If you prefer to traditional experience of a globally installed binary, you can run:
magic global install max
# On Linux
magic global expose add -e max $(find "$HOME/.modular/envs/max/bin" -type f -executable -exec basename {} \;)
# On MacOS
magic global expose add -e max $(find "$HOME/.modular/envs/max/bin" -type f -exec basename {} \;)This will add the Mojo binary and utilities like lsp, debugger, formatter etc to your global PATH so you can call it directly from the terminal:
mojo --versionmojo 25.1.0.dev2025020905Running Mojo in a notebook
Next Steps
CS Fundamentals
If you have only used garbage collected languages like Python, you can run through the Systems Programming guide to get a primer on concepts like memory management, pointers, stack and heap memory.
GPU programming basics are coming soon...