matlab-set-up-worker-state
Set up worker environment and per-worker state for parallel pools
Install / Use
npx skills add matlab/matlab-agentic-toolkit --skill matlab-set-up-worker-stateInstalls into whichever agent you are using.
SKILL.md
Installable skill definition
Quality Score
Category
Data & AnalyticsSupported Platforms
Our assessment of matlab-set-up-worker-state
matlab-set-up-worker-state scores 90/100 on our quality scale, 323rd of 591 Data & Analytics skills we index.
Its SKILL.md is 13 KB long, well organised into 17 sections with 8 code examples: a thorough specification that gives an agent plenty to work with.
With 1,098 GitHub stars, it is one of the more widely adopted skills in the catalogue.
Maintenance, license and trust
- The repository was last updated 21 days ago, so matlab-set-up-worker-state is actively maintained.
- No license is declared. By default that means all rights are reserved: you can read it, but reusing or redistributing it is not clearly permitted. Ask the author before building on it commercially.
- Its trust signals score 88/100, with 1 caution from licensing, adoption, age or documentation. These come from repository metadata, not a code audit — read the skill file before letting an agent act on it.
matlab-set-up-worker-state compared with similar skills
All 4 of these similar skills score higher than matlab-set-up-worker-state; compare them before choosing.
| Skill | Score | Stars | Updated | Format |
|---|---|---|---|---|
| matlab-set-up-worker-state (this skill)by matlab | 90 | 1.1k | 21d ago | SKILL.md |
| algorithmic-artby anthropics | 100 | 177.9k | 14d ago | SKILL.md |
| pptxby anthropics | 100 | 177.9k | 14d ago | SKILL.md |
| designby nextlevelbuilder | 100 | 133.6k | 3d ago | SKILL.md |
| ui-ux-pro-maxby nextlevelbuilder | 100 | 133.6k | 3d ago | SKILL.md |
Frequently asked questions
- How do I install matlab-set-up-worker-state?
- Run
npx skills add matlab/matlab-agentic-toolkit --skill matlab-set-up-worker-state. The install tabs above show the steps for each supported agent. - Which AI agents does matlab-set-up-worker-state work with?
- It is written for Universal, as a SKILL.md file. Other agents that read the same format can often use it too.
- Is matlab-set-up-worker-state safe to use?
- It declares no license and scores 88/100 on trust signals. Skills are instructions an agent will follow, so read the file before installing it and do not approve commands you do not understand.
- Is matlab-set-up-worker-state still maintained?
- The repository was last updated 21 days ago, so matlab-set-up-worker-state is actively maintained.
Skill content
View source on GitHubname: matlab-set-up-worker-state description: > Set up worker environment and per-worker state for parallel pools. Use when code needs paths, environment variables, database connections, loaded libraries, or expensive objects available on workers before parfor/parfeval runs. Teaches parallel.pool.Constant, parfevalOnAll, and parpool name-value pairs. Also use when refactoring existing code that uses spmd for side-effect setup (an anti-pattern). Triggers: worker setup, pool constant, per-worker state, non-serializable, loadlibrary on workers, database connection parfor, addpath workers, spmd before parfor, worker environment, reduce parfor overhead, parfor setup, resource creation in parallel loop, cannot serialize error, undefined function or variable on workers error, load data per worker, reduce data transfer, parallelize setup, improve parallel code. license: https://www.mathworks.com/content/dam/mathworks/license/pmrl/license.md metadata: author: MathWorks version: "2.0"
Set Up Worker State for Parallel Pools
By default, process workers in a parallel pool inherit MATLAB path state from their controlling client, but they do not inherit loaded libraries, open connections, or expensive pre-computed objects. Code that relies on any of these needs explicit setup. This skill teaches the correct APIs for each scenario. Most patterns target process-based pools; thread pool applicability is noted where relevant.
When to Use
- Code needs a non-serializable resource on workers (database connections, COM objects, loaded shared libraries, file handles)
- Code needs expensive one-time setup per worker (large object construction, data loading) that should not repeat every parfor iteration
- Code needs paths or environment variables set on workers
- User has existing code using spmd for side-effect setup before parfor (anti-pattern — help them modernise)
- User sees errors about objects not being serializable when passed to parfor
When NOT to Use
- Data already in client memory used in a single parfor loop (MATLAB broadcasts it automatically — Constant adds complexity for no benefit in this case)
- Choosing between process and thread pools (out of scope — this skill assumes a pool type is already chosen)
Decision Framework
| Scenario | Correct API | Why |
|----------|-------------|-----|
| Non-serializable resource needing cleanup (connections, libraries) | parallel.pool.Constant(@buildFcn, @cleanupFcn) | Constructs on each worker; automatic cleanup on delete |
| Data from a file needed on workers | parallel.pool.Constant(@() load(file).var) | Default for file-based data. Each worker loads from disk; client never holds the dataset. Always prefer this when the source is a file. |
| Data already in client memory, used in multiple parfor loops | parallel.pool.Constant(data) | Transfers once for pool lifetime; without Constant, broadcast re-sends for every parfor loop. Not needed for a single parfor — let MATLAB broadcast. |
| One-shot side effect, no return value needed | parfevalOnAll(pool, @fcn, 0) | Runs once on all workers; use fetchOutputs to surface errors |
| Paths needed on workers — parpool call is in your code | parpool(..., AdditionalPaths=paths) | Cleanest option when you can modify the parpool call; client path entries are inherited automatically |
| Paths needed on workers — pool opened elsewhere (can't modify call) | parfevalOnAll(pool, @addpath, 0, p) | Never delete and recreate a pool just to add paths — use parfevalOnAll on the existing pool |
| Environment variables on workers — parpool call is in your code | parpool(..., EnvironmentVariables=vars) | Forwards named env vars from client to workers at startup; set values with setenv on the client before this call |
| Environment variables on workers — pool opened elsewhere (can't modify call) | parfevalOnAll(pool, @setenv, 0, k, v) | Never delete and recreate a pool just to set env vars — use parfevalOnAll on the existing pool |
Thread pool notes: parallel.pool.Constant and parfevalOnAll also work on
thread pools. However, thread workers share the client's process, so path and
environment variable changes on the client are visible to threads automatically —
AdditionalPaths and EnvironmentVariables do not apply. To modify a thread
worker's environment, alter the client environment before the parfor.
The spmd Anti-Pattern
This applies equally to process pools and thread pools — prefer
parallel.pool.Constant over spmd for worker state setup regardless of pool
type.
What it looks like
% ANTI-PATTERN: Do not do this
spmd
loadlibrary("mylib", "mylib.h");
end
parfor i = 1:100
result(i) = calllib("mylib", "compute", data(i));
end
spmd
unloadlibrary("mylib");
end
Why it's fragile
- No cleanup guarantee — if the parfor errors, the second spmd never runs
- Makes the pool fragile to worker disconnection — once a pool has run an
spmdblock, a single worker losing connection tears down the entire pool. Pools that never usespmdcontinue operating with fewer workers if one disconnects (no replacement occurs — the pool simply shrinks). - Composite variables cannot be used inside
parfor— values created inspmdare Composite objects that require indexing on the client; they cannot be referenced directly from within aparforbody
How to modernise
Replace with parallel.pool.Constant:
C = parallel.pool.Constant( ...
@() loadAndReturn(), ...
@(~) unloadlibrary("mylib")); %#ok<NASGU> — kept alive for cleanup
result = zeros(1, 100);
parfor i = 1:100
result(i) = calllib("mylib", "compute", data(i));
end
% Cleanup happens automatically when C goes out of scope
Or with parfevalOnAll for simple side effects:
f = parfevalOnAll(pool, @setupWorker, 0);
fetchOutputs(f);
result = zeros(1, 100);
parfor i = 1:100
result(i) = doWork(data(i));
end
% No automatic cleanup
Prefer parallel.pool.Constant when cleanup is needed. Use parfevalOnAll
only for operations where cleanup is not required.
Patterns
Pattern 1: Loading data from a file for use in parfor
Always use the build-function form when the source is a file. This avoids loading the data into client memory entirely — each worker loads directly from disk. This is critical for large files but is also the correct default for any file-based data because it scales without code changes as file size grows.
c = parallel.pool.Constant(@() load("costSurface.mat").costSurface);
results = zeros(1, 10000);
parfor i = 1:10000
results(i) = processWithLookup(input(i), c.Value);
end
Each worker calls load() once. The client never holds the full dataset.
Do NOT do this — loading on the client then wrapping defeats the purpose:
% WRONG: loads entire file into client memory, then copies to each worker
data = load("costSurface.mat").costSurface;
c = parallel.pool.Constant(data); % client already paid the memory cost
The only time parallel.pool.Constant(data) (without a build function) is
appropriate is when the data is already in client memory and used across
multiple parfor loops (avoids re-broadcasting each loop). For a single parfor
loop, just let MATLAB broadcast — Constant adds no benefit.
Pattern 2: Shared library (non-serializable, needs cleanup)
c = parallel.pool.Constant( ...
@() loadLibraryAndReturn("mylib", "mylib.h"), ...
@unloadlibrary);
results = zeros(1, 100);
parfor i = 1:100
results(i) = calllib(c.Value, "compute", data(i));
end
% Cleanup runs automatically when c goes out of scope
function libName = loadLibraryAndReturn(libName, headerFile)
loadlibrary(libName, headerFile);
end
The build function returns the library name so that c.Value is naturally
referenced in the parfor body — this triggers lazy initialisation. The cleanup
function receives the same value and calls unloadlibrary on it.
Pattern 3: One-shot setup with parfevalOnAll (no cleanup needed)
pool = gcp;
f = parfevalOnAll(pool, @setupWorker, 0);
fetchOutputs(f);
Pattern 4: Database connection (use createConnectionForPool)
Database connections are a common specific case that requires Database
Toolbox. Prefer the purpose-built helper
createConnectionForPool
over a hand-rolled parallel.pool.Constant. It returns a
parallel.pool.Constant whose value is a per-worker connection to the
configured data source, and it handles worker-side initialisation that a naive
database() call inside a Constant build function does not.
pool = gcp;
c = createConnectionForPool(pool, "MyDataSource", "", "");
results = cell(1, 1000);
parfor i = 1:1000
conn = c.Value;
results{i} = fetch(conn, sprintf("SELECT val FROM t WHERE id=%d", i));
end
The returned Constant is the standard parallel.pool.Constant type, so it
participates in the normal lifetime model: connections are torn down when the
Constant goes out of scope or the pool shuts down.
Recognising When pool.Constant Is Needed
Look for these signals in user code:
| Signal | Indicates |
|--------|-----------|
| Object created and destroyed every parfor iteration | Per-worker resource needed |
| "Cannot serialize" or "not valid on workers" errors | Non-serializable resource |
| loadlibrary/unloadlibrary inside parfor | Library should persist across iterations |
| database()/close() inside parfor | Connection should persist |
| fopen/fclose inside parfor (same file) | File handle should persist |
| Large variable used read-only in multiple parfor loops (>100 MB) | Send-once with Constant |
| spmd block doing setup before parfor | Modernise to Constant or parfevalOnAll |
Common Mistakes
| What the agent does wrong | Why it's wrong | Correct approach |
|---------------------------|---------------|------------------|
| Uses spmd before parfor for setup | Fragile, no cleanup, pool tears down if any worker disconnects | parallel.pool.Constant with build + cleanup functions |
| Creates resource every iteration | Massive overhead (N connections for N iterations) | parallel.pool.Constant — one per worker |
| Uses persistent variables on workers | No cleanup, hard to reason about lifecycle | parallel.pool.Constant — explicit lifecycle |
| Tries to send non-serializable object from client | Will error — can't serialize connections, libraries | Use build function form: parallel.pool.Constant(@buildFcn) |
| Build function calls a function not on the worker path | Workers can't find it — "Unable to create parallel.pool.Constant on the workers" | Ensure build function helpers are on the path (or use AdditionalPaths). Local functions in the same file work — MATLAB captures them with the handle. |
| Uses addpath inside parfor on process pool | Runs every iteration unnecessarily | parpool(..., AdditionalPaths=...) or parfevalOnAll |
| Deletes and recreates pool to change paths or env vars | Extremely expensive — pool startup can take 30+ seconds and may queue on a scheduler | Use parfevalOnAll on the existing pool instead |
Key Functions
| Function | Product | Available From | Purpose |
|----------|---------|----------------|---------|
| parallel.pool.Constant | Parallel Computing Toolbox | R2015b | Per-worker state with lifecycle management |
| parfevalOnAll | Parallel Computing Toolbox | R2013b | Run a function once on all workers |
| parpool(..., AdditionalPaths=) | Parallel Computing Toolbox | R2025a | Set worker paths at pool creation |
| parpool(..., EnvironmentVariables=) | Parallel Computing Toolbox | R2017b | Forward env vars to workers at pool creation |
| createConnectionForPool | Database Toolbox | R2019a | Per-worker database connections (optional) |
Conventions
Truncated for display — read the full file on GitHub.
Related Skills
algorithmic-art
177.9kCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code, generative art, algorithmic art, flow fields, or particle systems.
pptx
177.9kUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an em…
design
133.6kComprehensive design skill: brand identity, design tokens, UI styling, logo generation (55 styles, Gemini, Atlas Cloud, or MuAPI AI), corporate identity program (50 deliverables, CIP mockups), HTML presentations (Chart.js), banner design (22 styles, social/ads/web/print), icon design (15 styles, SVG…
ui-ux-pro-max
133.6kUI/UX design intelligence for web, mobile, and desktop. This skill should be used when designing, building, reviewing, or fixing interfaces, including pages, components, design systems, accessibility, interaction, responsive layout, typography, color, charts, and stack-specific UI implementation.
Languages
Trust signals
From repository metadata: license, adoption, age and documentation. Not a code audit — see the Safety scan above for what the skill file itself contains.
