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.
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.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
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:
benchmarkMexandextractDiagnostics; 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, runscodegen -config cfg <designFile> -args {<inputs>}, saves the fixed file as<designFile>_v1.min 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 usebenchmarkMex(orgputimeit) for GPU timing; avoidtic/toc. - Step 3 — Structural Optimization Loop: iteratively creates
<designFile>_v<N>.mwith next optimization, verifies numerical equivalence against the original, runs codegen to generate a new MEX, benchmarks withbenchmarkMex, 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
cfgfrom Step 1, saves output to a dedicated paDir, and usesextractDiagnosticson 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 addingcoder.gpu.kernelfun, simplifying per-thread memory usage, converting inputs togpuArraywhen 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 viagpuPerformanceAnalyzer, and benchmarking viabenchmarkMex. - Interacts with inputs and outputs, potentially converts inputs to
gpuArraywhen a diagnostic indicates GPU input usage; creates and writes<designFile>_v*.mvariants in the session directory; relies on thescriptsfolder for helpers.
Caveats
- Target is limited to GPU codegen workflows; executable targets (
exe) are not supported for benchmarking or equivalence checking; instructions require switching tomexorlib/dllifexeis 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.
# 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
- When to Use
- When NOT to Use
- Workflow
- Setup — Create Session Directory
- Step 1 — Codegen on Original
- Step 2 — Baseline Benchmark
- Step 3 — Structural Optimization Loop
- Step 3b — Verify Numerical Equivalence
- Step 4 — Profile with gpuPerformanceAnalyzer
- Step 5 — Fix Diagnostics
- Step 6 — Final Report
- Conventions
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.
