Main Content

Constant

Copy or create data only once on workers in parallel pool

    Description

    Create a Constant object to avoid unnecessarily copying data multiple times from your current MATLAB® session to workers in a parallel pool. The value you access in a Constant object is constant.

    • If you have Parallel Computing Toolbox™, use a Constant object to avoid unnecessarily copying data to workers in a parallel pool. For more information, see parallel.pool.Constant (Parallel Computing Toolbox).

    • If you do not have Parallel Computing Toolbox, you can still use Constant objects without a parallel pool. The Constant is only created in your current MATLAB session. Use Constant when you run portable parallel code designed to work with or without Parallel Computing Toolbox.

    For more information about running parallel code without Parallel Computing Toolbox, see Run Parallel Language in MATLAB.

    Creation

    Use parallel.pool.Constant to create a Constant object from an array or a function handle. Use the Value property to access underlying data.

    Description

    C = parallel.pool.Constant(X) uses the array X to create a Constant. Use the Value property to read the data.

    C = parallel.pool.Constant(fcn) uses the function handle fcn to create a Constant object C. Use the Value property to get the result from running fcn() with one output.

    Note

    Use this function handle syntax to create a Constant object when you want to have the same variable name for a handle-type resource in your current MATLAB session and on a parallel pool. For example, use the function handle syntax to set up a database connection both in your current MATLAB and on a parallel pool. If you want to evaluate a function on each worker to set up workers before computations, use parfevalOnAll (Parallel Computing Toolbox) instead.

    C = parallel.pool.Constant(fcn,cleanupFcn) runs cleanupFcn(C.Value) when C is cleared.

    Input Arguments

    expand all

    Input data, specified as an array.

    Build function, specified as a function handle.

    MATLAB uses the build function to initialize the Value property of the Constant object. The function must take no input arguments and must return one output argument. When you read the Value property for the first time in your MATLAB session or on a parallel pool worker, MATLAB returns the result from running fcn() in that environment.

    • When you read the Value property in your MATLAB session, MATLAB returns the result from running fcn() in your MATLAB session. The first time you read the Value property, the result is stored.

      The function is run only once in your MATLAB session. The first time you read the When you read the Value property after the first time, you read the stored result.

    • When you read the Value property for the first time on a parallel pool worker, MATLAB returns the result from running fcn() on that worker.

      When you read the Value property for the first time on the worker, the result is stored as the Value property. The function is run only once on that worker. When you read the Value property after the first time, you read the stored result.

      If you read the Value property on a different parallel pool worker, MATLAB returns the result from running fcn() on that worker.

    Example: @() fopen(tempname(pwd),'wt')

    Cleanup function, specified as a function handle. The function must take one input argument, the Value property of the Constant object.

    The cleanup function is run when C is cleared. The Constant object C is cleared when you:

    • Create C in a function and do not return C from that function.

    • Clear the Constant object from your workspace.

    Example: @fclose

    Properties

    expand all

    Independent copy of underlying data, specified as an array.

    Use the Value property of a Constant to access underlying data.

    See Also

    (Parallel Computing Toolbox) | (Parallel Computing Toolbox) | (Parallel Computing Toolbox) |