Programming and Data Types

Program 2 -- Relaxation Algorithm

This program starts by creating a sharply contrasted graphics display in a figure window. When you press any key after starting the program, it runs a relaxation algorithm on the figure, gradually blurring the color boundaries.

When run on a MATLAB version that doesn't support performance acceleration, you can see the algorithm taking effect in distinct steps that are spaced over time. When run with acceleration, the display changes smoothly over a much shorter time period.

This table shows comparative execution times for 300 iterations of the program. (The version of this program used in these measurements is slightly different from what is shown below. The final image handling functions (`set` and `drawnow`) were moved to the outside of the three nested for loops, and thus refresh the image only once, at the very end.)

 Operating System MATLAB 6.1 MATLAB 6.5 Performance Gain Windows 1 min., 25.9 sec. 1.1 sec. ` x` 78.1 Linux 3 min., 13.8 sec. 1.7 sec. ` x` 114.0 Solaris 8 min., 51.0 sec. 23.5 sec. ` x` 22.6

Here is the program.

• ```function relax(iterations)

sz = 102;

plate = magic(sz) * 64 / (sz * sz);
newPlate = plate;

im = image(plate);
axis off
set(gcf, 'DoubleBuffer', 'on')

% Wait for user to press a key to kick off the image processing
pause

for i = 1:iterations
for j = 2:(sz-1)
jm1 = j - 1;
jp1 = j + 1;
for k = 2:(sz-1)
km1 = k - 1;
kp1 = k + 1;
newPlate(j,k) = (plate(jm1,km1)/2 + plate(jm1,k) + ...
plate(jm1,kp1)/2 + plate(j,km1) + plate(j,kp1) + ...
plate(jp1,km1)/2 + plate(jp1,k) + plate(jp1,kp1)/2)/6;
end
end
plate = newPlate;

% Refresh the image once every 5 times through the loop.
if (0 == rem(i,5))
set(im, 'cdata', plate)
drawnow
end

end
```

You can see the visual effect of the faster execution by running the program yourself. Put the code into an M-file named `relax.m`, and run it for 300 iterations by typing

• ```relax(300);
```

A new window is created showing the initial image. Once you see this, reselect the MATLAB Command Window, and then press any key to start processing the image.

What Makes It Faster

The program spends most of its time in a nested `for` loop that modifies the image data. The `newPlate` calculation in the inner loop executes 3 million times when `iterations` is set to 300, as it is in the above test.

MATLAB accelerates the two inner `for` loops for the reasons explained below. The outer loop does not accelerate (see The Outer for Loop), yet that has little effect on the overall execution time, as nearly all of the time spent is in the inner loops.

Scalar loop indices.   The `for` loops operate on a range of scalar values. For example,

• ```for j = 2:(sz-1)
```

Supported Data Types and Array Shapes.   All of the statements within the inner loops use a `double` data type with either a scalar value or two-dimensional matrix. These are among the data types and array shapes that MATLAB accelerates.

Higher Complexity Operations.   MATLAB usually shows a noticeable performance gain for statements containing multiple operators and/or functions. The `plate` computation is an example of this.

• ```newPlate(j,k) = (plate(jm1,km1)/2 + plate(jm1,k) + ...
plate(jm1,kp1)/2 + plate(j,km1) + plate(j,kp1) + ...
plate(jp1,km1)/2 + plate(jp1,k) + plate(jp1,kp1)/2)/6;
```

Function Calls and Overloading.   One factor that enables the acceleration of the two inner loops is that the only function calls made in this code are to built-in functions. The program performs a number of mathematical operations, but as long as none of the math operators used (`+`, `-`, and `/`) is overloaded for the data type being operated on (`double`), these math operations execute quickly, not having to make M-file calls.

The Outer for Loop

MATLAB does not accelerate the outer `for` loop. One reason is that the leading `for` statement relies on an ambiguous data type for the maximum index value. The value for `iterations` is passed into the program and thus may not necessarily be one of the data types or array shapes supported for acceleration.

• ```for i = 1:iterations
```

If you include the final `set` and `drawnow` calls in the loop, MATLAB does not accelerate these either, because they operate on Handle Graphics® objects, which are not among the data types supported for performance acceleration.

• ```      set(im,'cdata',plate)
drawnow
```

The fact that the outer loop does not accelerate is not that important in this case, as nearly all of the time spent is in the inner loops.

 Sample Accelerated Programs Program 3a -- Vector Comparison, with Loop