Hauptinhalt

parallel.pool.Constant

Build and use constant from data or function handle

    Description

    Use a Constant object to avoid unnecessarily copying data multiple times from your current MATLAB® session to workers in a parallel pool. You can build a Constant object once on a client and transfer the constant to the workers once. Access the Constant in multiple parfor-loops, spmd blocks, or parfeval calls. The value you access in a Constant object is constant. You can share your parallel code that uses Constant objects with MATLAB users who do not have Parallel Computing Toolbox™. For more information, see parallel.pool.Constant.

    Creation

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

    Syntax

    Description

    C = parallel.pool.Constant(X) copies the array X to each worker and returns a Constant object.

    Each worker can access the array X within a parallel language construct (parfor, spmd, parfeval) using the Value property to read the data.

    example

    C = parallel.pool.Constant(FH) evaluates the function handle FH on each worker and stores the results in the Constant object C. Use the Value property to access the result from running FH() with one output.

    Use this syntax to create and use any handle-type resources on a parallel pool, such as file handles and database connections. If you want to evaluate a function on each worker to set up workers before computations, use parfevalOnAll instead.

    example

    C = parallel.pool.Constant(FH,cleanupFH) evaluates cleanupFH(C.Value) on each worker when C is cleared.

    example

    C = parallel.pool.Constant(COMP) stores the values in the entries of the Composite object COMP in the Constant object C on each worker.

    Use this syntax when you want to construct data only on the workers, such as when the data is too large to conveniently fit in the client, or when you load the data from a file that only the workers can access. Access the values using the Value property.

    example

    Input Arguments

    expand all

    Input data, specified as any MATLAB variable that can be saved and loaded.

    Function handle for the build function, specified as a function handle.

    MATLAB evaluates the build function to get 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 stores the result from running fcn() in that environment as the Value property.

    • The function is run only once in your MATLAB session or on a parallel pool 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')

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

    MATLAB runs the cleanup function 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

    Composite object to build Constant object, specified as a Composite object.

    The COMP object must have a defined value on every worker otherwise you will receive an error.

    Example: spmd COMP = rand(3); end; C

    Properties

    expand all

    Independent copy of underlying data or handle-type resource, specified as any MATLAB variable that can be saved and loaded or a handle variable.

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

    Examples

    collapse all

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant