Sliced Variables - MATLAB & Simulink (original) (raw)

A sliced variable is one whose value can be broken up into segments, or slices, which are then operated on separately by different workers. Each iteration of the loop works on a different slice of the array. Using sliced variables can reduce communication between the client and workers.

In this example, the workers apply f to the elements ofA separately.

parfor i = 1:length(A) B(i) = f(A(i)); end

Characteristics of a Sliced Variable

If a variable in a parfor-loop has all the following characteristics, then the variable is sliced:

Type of First-Level Indexing

For a sliced variable, the first level of indexing is enclosed in either parentheses, (), or braces, {}.

Here are the forms for the first level of indexing for arrays that are sliced and not sliced.

Not Sliced Sliced
A.x A(...)
A.(...) A{...}

After the first level, you can use any type of valid MATLAB® indexing in the second and subsequent levels.

The variable A shown here on the left is not sliced; that shown on the right is sliced.

Fixed Index Listing

Within the first-level indexing of a sliced variable, the list of indices is the same for all occurrences of a given variable.

The variable A on the left is not sliced becauseA is indexed by i andi+1 in different places. In the code on the right, variable A is sliced correctly.

Not sliced Sliced
parfor i = 1:k B(:) = h(A(i), A(i+1)); end parfor i = 1:k B(i) = f(A(i)); C(i) = g(A{i}); end

The example on the right shows occurrences of first-level indexing using both parentheses and braces in the same loop, which is acceptable.

The following example on the left does not slice A because the indexing of A is not the same in all places. The example on the right slices both A and B. The indexing of A is not the same as the indexing ofB. However, the indexing of both A andB are individually consistent.

Not sliced Sliced
parfor i=1:10 b = A(1,i) + A(2,i) end A = [ 1 2 3 4 5 6 7 8 9 10; 10 20 30 40 50 60 70 80 90 100]; B = zeros(1,10); parfor i=1:10 for n=1:2 B(i) = B(i)+A(n,i) end end

Form of Indexing

Within the first-level of indexing for a sliced variable, exactly one indexing expression is of the form i, i+k,i-k, or k+i. The indexi is the loop variable and k is a scalar integer constant or a simple (non-indexed) broadcast variable. Every other indexing expression is a positive integer constant, a simple (non-indexed) broadcast variable, a nested for-loop index variable,colon, a colon expression involving simple broadcast variables or scalar integer constants, or end.

With i as the loop variable, the A variables shown on the left are not sliced, while the A variables on the right are sliced.

Not sliced Sliced
A(i+f(k),j,:,3) % f(k) invalid for slicing A(i,:,s.field1) % s.field1 not simple broadcast var A(i,[20,21,30],end) % array literal not supported Before R2024b: A(i,20:30,end) A(i+k,j,:,3) A(i,:,end) A(i,:,k) A(i,20:30,end) (since R2024b)

You can work around the unsupported indexing expressions by declaring them as broadcast variables. In this example, the code on the left does not work because it directly uses unsupported indexing expressions to index the slice variables. The code on the right provides a workaround by declaring the expressions as broadcast variables before the loop body.

Not Sliced Sliced
parfor i = 1:n A(i, [false,true,true]) = 7; % not supported B(i, [20,21,30]) = 7; % also not supported end b1 = [false,true,true]; b2 = [20,21,30]; parfor i = 1:n A(i,b1) = 7; % works B(i,b2) = 7; % also works end

When you use other variables along with the loop variable to index an array, you cannot set these variables inside the loop. In effect, such variables are constant over the execution of the entire parfor statement. You cannot combine the loop variable with itself to form an index expression.

Shape of Array

A sliced variable must maintain a constant shape. The variableA shown here is not sliced:

A is not sliced because changing the shape of a sliced array would violate assumptions governing communication between the client and workers.

Sliced Input and Output Variables

A sliced variable can be an input variable, an output variable, or both. MATLAB transmits sliced input variables from the client to the workers, and sliced output variables from workers back to the client. If a variable is both input and output, it is transmitted in both directions.

In this parfor-loop, A is a sliced input variable and B is a sliced output variable.

A = rand(1,10); parfor ii = 1:10 B(ii) = A(ii); end

However, if MATLAB determines that, in each iteration, the sliced variable elements are set before any use, then MATLAB does not transmit the variable to the workers. In this example, all elements of A are set before any use.

parfor ii = 1:n if someCondition A(ii) = 32; else A(ii) = 17; end % loop code that uses A(ii) end

Sliced-output variables can grow dynamically through indexed assignments with default values inserted at intermediate indices. In this example, you can see that the default value of 0 has been inserted at several places inA.

A = []; parfor idx = 1:10 if rand < 0.5 A(idx) = idx; end end

disp(A);

Even if a sliced variable is not explicitly referenced as an input, implicit usage can make it so. In the following example, not all elements of A are necessarily set inside the parfor-loop. Therefore the original values of the array are received, held, and then returned from the loop.

A = 1:10; parfor ii = 1:10 if rand < 0.5 A(ii) = 0; end end

Under some circumstances, parfor-loops must assume that a worker may need all segments of a sliced variable. In this example, it is not possible to determine which elements of the sliced variable will be read before execution, so parfor sends all possible segments.

A = 1:10; parfor ii=1:11 if ii <= randi([10 11]) A(ii) = A(ii) + 1; end end

Note that in these circumstances, the code can attempt to index a sliced variable outside of the array bounds and generate an error.

Nested for-Loops with Sliced Variables

When you index a sliced variable with a nested for-loop variable, keep these requirements in mind:

Data Type Limitations