Agent skill · Design & Presentation

matlab-optimize-gpu-codegen

Optimize MATLAB design files for GPU Coder to generate faster CUDA code. Iteratively profiles, rewrites, and benchmarks until performance targets are met or diagnostics are resolved. Use when asked to: optimize for GPU Coder, improve GPU codegen performance, profile generated GPU/CUDA code, profile GPU MEX, fix gpuPerformanceAnalyzer diagnostics, speed up GPU MEX, reduce GPU memory transfers, improve kernel parallelism, rewrite MATLAB for CUDA, or run gpuPerformanceAnalyzer.

matlabgithub.com/matlabGitHub ↗
claude-codecodexcopilotNOASSERTION
Install
npx skills add matlab/matlab-agentic-toolkit --skill matlab-optimize-gpu-codegen --agent claude-code

Same command for any agent — swap --agent for codex, cursor, copilot.

Facts
Files in the skill folder: 5
SKILL.md size: 18 KB
Bundled scripts: none
Version: 1.0
Declared author: MathWorks
Path: skills-catalog/code-generation/matlab-optimize-gpu-codegen/SKILL.md
Open the folder on GitHub →
Where it comes from
Stars: 868
Language: MATLAB

Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.

Review
written from the skill's own SKILL.md · Aug 5, 2026

What it does

Iteratively optimize a MATLAB design file for GPU Coder: compile, benchmark, apply structural optimizations, profile with gpuPerformanceAnalyzer, fix diagnostics, and verify numerical equivalence at every step.

How it works

  • Setup: creates a single session directory; uses two helpers in a scripts/ folder: benchmarkMex and extractDiagnostics; adjusts MATLAB path to include these scripts and the design file directory; writes artifacts to the session directory only.
  • Step 1 — Codegen on Original: selects codegen config (default to mex), enables SIL for lib/dll targets if needed, resolves inputs, runs codegen -config cfg <designFile> -args {<inputs>}, saves the fixed file as <designFile>_v1.m in the session directory, and ensures a baseline MEX named <designFile>_v1_mex (or SIL-generated MEX with the same name for lib/dll).
  • Step 2 — Baseline Benchmark: uses benchmarkMex("<designFile>_v1_mex", {<inputs>}) to obtain a MedianTime; prints baseline time. Always use benchmarkMex (or gputimeit) for GPU timing; avoid tic/toc.
  • Step 3 — Structural Optimization Loop: iteratively creates <designFile>_v<N>.m with next optimization, verifies numerical equivalence against the original, runs codegen to generate a new MEX, benchmarks with benchmarkMex, and keeps the fastest passing version as the current best. It uses MATLAB-level restructuring patterns to reduce loop-invariant computations, replace explicit loops with implicit expansion, and reduce divergent branching; includes a rule-based exit when the performance target is met or no further structural optimizations are identifiable. An Exit criteria requires either the performance target is met or no further structural optimizations are found, limited to five iterations for this loop.
  • Step 3b — Verify Numerical Equivalence: runs original and optimized versions on up to 5 input sets, generates inputs matching types/sizes, compares outputs with type-appropriate tolerance (double 1e-6, single 1e-3, half 1e-2, integer/logical exact), handles NaN consistency, and ensures outputs match for all cases.
  • Step 4 — Profile with gpuPerformanceAnalyzer: runs PA on the current best version using the same cfg from Step 1, saves output to a dedicated paDir, and uses extractDiagnostics on the generated HTML report to obtain diagnostics. If no diagnostics are found, skips to Final Report. Diagnostics are given as a struct with id, message, file, and line.
  • Step 5 — Fix Diagnostics: for each diagnostic, creates a new <designFile>_v<N+1>.m, re-runs codegen, re-verifies equivalence, and benchmarks. Provides a mapping from diagnostic IDs to fixes, such as adding coder.gpu.kernelfun, simplifying per-thread memory usage, converting inputs to gpuArray when flagged, increasing parallelism, or keeping results on GPU to avoid CPU-GPU memory copies.

When to use it

  • User has a MATLAB function and wants faster GPU MEX or CUDA code; mentions GPU Coder, codegen, CUDA, gpuPerformanceAnalyzer; wants to profile generated GPU/CUDA code or GPU MEX; aims to reduce GPU memory transfers or improve kernel parallelism; rewrite MATLAB for CUDA; or run gpuPerformanceAnalyzer.

What it can touch

  • Uses MATLAB codegen workflow with codegen, profiling via gpuPerformanceAnalyzer, and benchmarking via benchmarkMex.
  • Interacts with inputs and outputs, potentially converts inputs to gpuArray when a diagnostic indicates GPU input usage; creates and writes <designFile>_v*.m variants in the session directory; relies on the scripts folder for helpers.

Caveats

  • Target is limited to GPU codegen workflows; executable targets (exe) are not supported for benchmarking or equivalence checking; instructions require switching to mex or lib/dll if exe is specified.
  • Activation depends on the user prompt mentioning GPU, codegen, CUDA, MEX, or profiling; otherwise, the skill remains inactive.
  • The license is NOASSERTION; the skill relies on MathWorks tooling and may require appropriate licenses for gpuPerformanceAnalyzer and SIL when using lib/dll targets.
From the SKILL.md

# Optimize MATLAB for GPU Code Generation Iteratively optimize a MATLAB design file for GPU Coder: compile, benchmark, apply structural optimizations, profile with gpuPerformanceAnalyzer, fix diagnostics, and verify numerical equivalence at every step. ## When to Use - User has a MATLAB function and wants faster GPU MEX or CUDA code - User mentions GPU Coder, codegen, CUDA, gpuPerformanceAnalyzer - User asks to profile generated GPU/CUDA code or GPU MEX (profiling generated GPU code is the entry point to this skill's diagnostic-fix workflow) - User wants to reduce GPU memory, improve kernel parallelism, or fix Performance Analyzer diagnostics - User has a `.m` design file and representative inputs ## When NOT to Use - Workflows with no codegen - `coder.gpuConfig("exe")` — standalone executables cannot be benchmarked or equivalence-checked from MATLAB. Suggest the user switch to `mex` or `lib`/`dll` and regenerate `exe` from the final optimized source. - Simulink GPU code generation - Writing new MATLAB functions from scratch (this skill optimizes existing code) - Optimizing helper functions called by the design file — this skill optimizes the main design file only - Hardware setup

What's inside
Steps it walks through
  1. When to Use
  2. When NOT to Use
  3. Workflow
  4. Setup — Create Session Directory
  5. Step 1 — Codegen on Original
  6. Step 2 — Baseline Benchmark
  7. Step 3 — Structural Optimization Loop
  8. Step 3b — Verify Numerical Equivalence
  9. Step 4 — Profile with gpuPerformanceAnalyzer
  10. Step 5 — Fix Diagnostics
  11. Step 6 — Final Report
  12. Conventions
Ships with 4 files
  • manifest.yaml
  • references/gpu-codegen-functions.md
  • scripts/benchmarkMex.m
  • scripts/extractDiagnostics.p
More from matlab-agentic-toolkit
All skills →
About this skill
What does the matlab-optimize-gpu-codegen skill do?

Optimize MATLAB design files for GPU Coder to generate faster CUDA code. Iteratively profiles, rewrites, and benchmarks until performance targets are met or diagnostics are resolved. Use when asked to: optimize for GPU Coder, improve GPU codegen performance, profile generated GPU/CUDA code, profile GPU MEX, fix gpuPerformanceAnalyzer diagnostics, speed up GPU MEX, reduce GPU memory transfers, improve kernel parallelism, rewrite MATLAB for CUDA, or run gpuPerformanceAnalyzer.

How do I install it?

Run `npx skills add matlab/matlab-agentic-toolkit --skill matlab-optimize-gpu-codegen --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.

Where does this skill come from?

From matlab/matlab-agentic-toolkit, a repository with 868 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.

Is a popular skill a good skill?

Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.

Keep going