matlab-optimize-gpu-codegen

Optimizes MATLAB design files for GPU Coder to generate faster CUDA code.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-optimize-gpu-codegen
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-optimize-gpu-codegen
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/code-generation/matlab-optimize-gpu-codegen
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-optimize-gpu-codegen

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

MATLAB code compiled to CUDA with GPU Coder often underperforms due to redundant computation, memory copies between CPU and GPU, and insufficient kernel parallelism. This Skill iteratively profiles, rewrites, and benchmarks a MATLAB design file until performance targets are met or diagnostics are resolved.

Core Features & Use Cases

  • Iterative Optimization Loop: Compiles with codegen, benchmarks with convergence-based timing, applies structural rewrites, and verifies numerical equivalence against the original function at every step.
  • Diagnostic-Driven Fixes: Profiles generated code with gpuPerformanceAnalyzer and maps each diagnostic (e.g., UseGpuInput, KernelLaunchOverheadLargeInLoop) to a targeted source-level fix.
  • GPU Coder Primitives Reference: Guides use of kernel pragmas, parallel reductions, atomics, stencils, and memory placement from a curated reference document.
  • Use Case: You have a MATLAB function that compiles to a slow GPU MEX. This Skill benchmarks the baseline, restructures loops for kernel fusion, eliminates CPU-GPU memory copies flagged by the profiler, and delivers a verified faster version with a full performance report.

Quick Start

Optimize my MATLAB function myKernel.m for GPU Coder and profile it with gpuPerformanceAnalyzer until the generated CUDA code is as fast as possible.

Frequently Asked Questions about matlab-optimize-gpu-codegen

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize MATLAB code for GPU Coder?

Compile the design file with codegen, benchmark the generated MEX, then iteratively apply structural rewrites such as hoisting loop-invariant expressions, replacing loops with implicit expansion, and removing divergent branches. Profile with gpuPerformanceAnalyzer and fix each reported diagnostic, verifying numerical equivalence after every change.

How do I fix gpuPerformanceAnalyzer diagnostics in MATLAB?

Each diagnostic maps to a targeted fix: UseGpuInput means converting the flagged input to gpuArray, KernelLaunchOverheadLargeInLoop means parallelizing the outer loop instead of inner loops, and NoKernelFunPragma means adding coder.gpu.kernelfun. Re-run codegen and benchmark after each fix.

Which MATLAB products are required for GPU code generation optimization?

This workflow requires MATLAB Coder, GPU Coder, and Parallel Computing Toolbox, with MATLAB R2024b or later. Embedded Coder is optional but needed for SIL-based benchmarking of lib and dll targets.

Can I optimize a coder.gpuConfig exe target with this workflow?

No, standalone executables cannot be benchmarked or equivalence-checked from MATLAB. Switch to a mex config for the fastest iteration, or lib/dll with SIL enabled, then regenerate the exe from the final optimized source.

Why should I avoid tic/toc when timing GPU MEX functions?

GPU operations execute asynchronously, so tic/toc measures launch time rather than actual kernel completion. Use gputimeit or a convergence-based benchmarking helper that handles warmup and synchronization automatically.

When should I use gpucoder.reduce instead of atomic operations?

Use gpucoder.reduce for associative and commutative accumulations like sums or maximums, since parallel reductions avoid contention and are significantly faster. Reserve atomics such as gpucoder.atomicAdd for irregular patterns like histogram binning or scatter-adds.