matlab-set-up-worker-state

Configure per-worker state and environment for MATLAB parallel pools using parallel.pool.Constant and parfevalOnAll.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-set-up-worker-state
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-set-up-worker-state
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/parallel-computing/matlab-set-up-worker-state
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-set-up-worker-state

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Parallel MATLAB code often fails or runs inefficiently because workers lack loaded libraries, database connections, paths, or expensive pre-computed objects that exist only on the client. This Skill teaches the correct APIs for setting up per-worker state so parfor and parfeval loops run without serialization errors or repeated per-iteration overhead.

Core Features & Use Cases

  • Per-Worker Resources: Create non-serializable resources like database connections and shared libraries on each worker using parallel.pool.Constant with build and cleanup functions.
  • Pool Configuration: Set worker paths and environment variables via parpool name-value pairs or parfevalOnAll on existing pools without recreating them.
  • Anti-Pattern Modernization: Refactor fragile spmd-based setup code into robust Constant-based patterns with automatic cleanup.
  • Use Case: A user needs to run a parfor loop that queries a database. Instead of opening a connection every iteration, the Skill guides them to use createConnectionForPool so each worker holds one persistent connection with automatic teardown.

Quick Start

Ask the agent to set up a parallel.pool.Constant so each worker loads data from a MAT-file once before the parfor loop runs.

Frequently Asked Questions about matlab-set-up-worker-state

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

FAQPage Schema
How do I share data with parfor workers in MATLAB?

Use parallel.pool.Constant to send data to workers once for the pool lifetime instead of re-broadcasting every loop. For file-based data, use the build-function form parallel.pool.Constant(@() load(file).var) so each worker loads directly from disk.

How do I set up a database connection for parfor loops?

Use createConnectionForPool from Database Toolbox, which returns a parallel.pool.Constant holding a per-worker connection. Connections are torn down automatically when the Constant goes out of scope or the pool shuts down.

Why does parfor fail with cannot serialize errors?

Objects like database connections, loaded libraries, and file handles cannot be serialized and sent from the client to workers. Instead, use parallel.pool.Constant with a build function so each worker constructs the resource locally.

Should I use spmd or parallel.pool.Constant for worker setup?

Prefer parallel.pool.Constant over spmd for worker state setup. spmd setup has no cleanup guarantee if parfor errors, makes the pool fragile to worker disconnection, and produces Composite variables unusable inside parfor.

How do I add paths to workers in an existing parallel pool?

Use parfevalOnAll(pool, @addpath, 0, p) to add paths on all workers of an existing pool. Never delete and recreate the pool just to change paths, since pool startup can take 30 seconds or more.

Does parallel.pool.Constant work with thread pools?

Yes, parallel.pool.Constant and parfevalOnAll work on thread pools. However, thread workers share the client process, so AdditionalPaths and EnvironmentVariables do not apply; modify the client environment before the parfor instead.