Main Content

Composite

Create Composite object

Description

example

Note

Composite variables are typically created on the client when values are returned from the body of an spmd statement. Therefore, you rarely need to create Composite objects directly. For more information about working with Composite arrays, see Composites.

C = Composite() creates a Composite object on the client using workers from the parallel pool.

A Composite object contains references to arrays stored on parallel workers and can contain a different value on each worker. A Composite object has one entry for each worker; initially each entry contains no data. Entries can contain different values on each worker. Values can be retrieved using cell-array indexing. Use either indexing or an spmd block to define values for the entries. The actual data on the workers remains available on the workers for subsequent spmd execution, so long as the Composite exists on the client and the parallel pool remains.

The actual number of workers referenced by the Composite object depends on the size of the pool and any existing Composite objects.

To construct a Composite object manually, you must do so outside of any spmd statements.

C = Composite(nworkers) creates a Composite object on the number of workers in the parallel pool that matches the specified constraint. nworkers must be a vector of length 1 or 2, containing integers or Inf. If nworkers is of length 1, it specifies the exact number of workers to use. If nworkers is of length 2, it specifies the minimum and maximum number of workers to use. The actual number of workers used is the maximum number of workers compatible with the size of the parallel pool, and with any other existing Composite objects. MATLAB® produces an error if the constraints on the number of workers cannot be met.

Examples

collapse all

This example shows how to create a Composite object with no defined elements, then assign values using a for-loop in the client.

p = parpool('Processes',4);
c = Composite();  % One element per worker in the pool
for w = 1:length(c)
    c{w} = 0;    % Value stored on each worker
end

This example shows how to assign Composite elements in an spmd block.

p = parpool('Processes',4);
c = Composite();
spmd
    c = 0;    % Value stored on each worker
end

This example shows how to assign the elements of a Composite with a value from each worker.

p = parpool('Processes',4);
c = Composite();
spmd
    c = spmdIndex;
end
c{:}
     1


     2


     3


     4

This example shows how to use a distributed array vector to set the values of a Composite.

p = parpool('Processes',4);
d = distributed([3 1 4 2]); % One integer per worker
spmd
    c = getLocalPart(d);    % Unique value on each worker
end
c{:}
     3


     1


     4


     2

Input Arguments

collapse all

Constraint on the number of workers, specified as a vector. nworkers must be a vector of length 1 or 2, containing integers or Inf. If nworkers is of length 1, it specifies the exact number of workers to use. If nworkers is of size 2, it specifies the minimum and maximum number of workers to use.

Output Arguments

collapse all

Composite array on the client using workers from the parallel pool, returned as a Composite object.

Tips

  • A Composite object is created on the workers of the existing parallel pool. If no pool exists, the Composite function starts a new parallel pool, unless the automatic starting of pools is disabled in your parallel preferences. If there is no parallel pool and Composite cannot start one, the result is a 1-by-1 Composite object in the client workspace.

Version History

Introduced in R2008a