Matlab Use Thread Pool
MathWorksOfficialCreated by MathWorks
- Skill
- Tool integration
- Science and research
- BSD-2-Clause
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides guidance on improving the execution speed of local MATLAB parallel tasks like parfor, parfeval, and spmd. By utilizing thread-based parallel pools instead of the default process-based pools, users can significantly reduce serialization and data transfer overhead. The approach emphasizes empirical testing over static code analysis, as thread-based support for functions has expanded significantly in recent releases.
What it does
- Recommends initializing a thread-based pool using parpool('Threads').
- Provides a development-time helper script to conditionally switch to a thread pool.
- Explains how to set a persistent default profile for thread-based execution.
- Clarifies the performance benefits regarding broadcast variables versus sliced inputs.
- Advises on how to interpret error diagnostics when thread-based support is missing.
When to use it
- When local parfor or parfeval performance is slower than expected.
- When large arrays are being broadcast across workers, causing high serialization overhead.
- When the user wants to determine if specific code is compatible with thread-based execution.
- When the user experiences slow startup times for parallel pools on a local machine.
Advantages
- Eliminates serialization and inter-process communication costs for many operations.
- Broadcast variables become zero-copy, providing immediate performance gains.
- Drop-in replacement that requires no changes to existing parallel code blocks.
- Reduces the overhead associated with opening process-based pools for short workloads.
Usage examples
- “My parfor loop on my laptop is barely faster than a standard for loop. How can I speed it up?”
- The agent suggests checking if the current pool is process-based and switching to a thread-based pool using parpool('Threads') to reduce data transfer overhead.
- “Can I use thread pools for my existing parallel code?”
- The agent explains that thread pools are a drop-in replacement and provides a code snippet to safely initialize a thread pool if one is not already active.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/matlab/matlab-use-thread-pool/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip matlab-use-thread-pool.zip -d .agents/skills
Documentation by MathWorks
Use a Thread-Based Parallel Pool
Recommend starting a thread pool using parpool("Threads") instead of
using the default process-based pool to speed up local parfor /
parfeval / spmd code.
When to Use
User asks to improve performance of
parfor,parfeval, orspmdon a local machine.Code passes large arrays into iterations or broadcasts them across workers; user mentions slow start-up, serialization, or data transfer.
User asks "Processes or Threads?" or wants Threads as the default.
When NOT to Use
User is running on a cluster -- thread pools are local-only.
Code uses
parsimor other Simulink parallel features -- those require a process-based pool.Code uses
distributedorcodistributedarrays.User wants
backgroundPool-- that's for running work asynchronously (e.g. keeping an app responsive while a computation runs), not for parallel speed-up. All MATLAB users have 1 worker in the background pool; users with a Parallel Computing Toolbox license have multiple workers.
For general MATLAB performance tuning (vectorisation, preallocation,
profiling), see matlab-optimize-performance (if available).
Workflow
Try it on a thread pool. Don't scan the code first -- just run it. Only create a thread pool if the current pool isn't already a thread pool. Avoid unconditionally deleting whichever pool the user has open. The conditional snippet below is a development convenience -- not something to bake into shipped code:
% Development-time helper: ensure a thread pool is active pool = gcp("nocreate"); if isempty(pool) || ~isa(pool, "parallel.ThreadPool") delete(pool); parpool("Threads"); endThe existing
parfor/parfeval/spmdblock does not need to change. Thread pools are a drop-in replacement, not a refactor.Read the diagnostic if it errors. Unsupported features fail loudly -- MATLAB tells you what isn't allowed and to use a process-based pool instead. Recommend
parpool("Processes"). No silent wrong answers, no guessing.Offer the persistent option if the user wants to use a thread pool "always" or "every time": suggest setting Threads as the default profile via
parallel.defaultProfile("Threads")(R2022b+). Persists across sessions; no startup script needed.
What threads actually save
Be precise. Broadcast variables are zero-copy on threads (the win).
Sliced inputs/outputs (X(:,:,i)) still allocate and copy --
threads only skip the serialize/IPC cost, not the slice itself.
Don't say "zero-copy" or "shared memory eliminates the cost
entirely" without qualifying it.
Convention
Always run it, never guess. This is the single most important rule in this skill.
LLM training data is incomplete and stale. Thread-based workers gained support for many functions across releases, and new support is added regularly. Do not assert a function is unsupported on threads from memory, by analogy to similar functions, or because it's not on the skill's short blocker list. Reasoning about thread support without running is the biggest failure mode for this skill -- it has pushed users to a process pool unnecessarily.
The default action is to run the code on a thread pool and read
the diagnostic if it errors. Don't pre-scan the code looking for
blockers. Most functions you'd reach for (load, FFT, imgaussfilt,
numeric and basic I/O primitives) work on threads.
Only if you genuinely cannot run the code (planning context, code review, user can't execute right now) should you fall back to documentation. Use an appropriate documentation skill (if available) to fetch the function's reference page and read its Extended Capabilities -> Thread-Based Environment section. Even then: an absent or older entry is not proof the function won't run -- some thread support is undocumented. When in doubt, try it.
Copyright 2026 The MathWorks, Inc.
Files
- ATTRIBUTION.mdAdded by AutoGineer1.0 KiB
- LICENSELicence file added by AutoGineer1.4 KiB
- SKILL.md5.0 KiB
- manifest.yaml432 B
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
The author says: >=R2020a
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- MathWorks
- Publisher
- MathWorks (Official)
- Source
- matlab/matlab-agentic-toolkit · skills-catalog/parallel-computing/matlab-use-thread-pool · commit 2a7f215
- Licence
- BSD 2-Clause License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE.md)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by MathWorks or MathWorks.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-use-thread-pool.zip