Information Technology Reference
In-Depth Information
We store this function in an M-file f1 vec.m . The main program used to test the
efficiency of the scalar version of the trapezoidal rule implementation in Matlab
can be reused in the vectorized case if the name of the integration routine and the
function to be integrated are changed:
function y = f1_vec(x)
y = exp(-x . * x) . * log(1 + x . * sin(x));
The vectorized version above reduced the CPU time to 1/42nd of the CPU time
required by the loop-based Matlab scripts from Sect. 6.3.5 . The CPU times of the
vectorized versions of the Python and Matlab codes were approximately equal,
running at half the speed of the Fortran 77 and C/C CC implementations.
Discussion
It might seem complicated to use languages that require vectorization for compu-
tational efficiency. Straightforward implementations in terms of loops are indeed
simpler, but vectorization has one great advantage: The vector operations can utilize
a library particularly tuned for the hardware in question. For example, smart tricks to
speed up array operations can be implemented in the library, but more importantly,
the library can implement parallel versions of the array operations such that one can
utilize parallel computers with only minor competence in parallel programming. At
least in principle, it may be hard to write a straightforward loop that can compete
with highly tuned basic array operations in libraries. The present example is suffi-
ciently simple such that straightforward loops inside a library yield high (and close
to optimal) efficiency.
Vectorization became an important programming technique with the advent of
the first supercomputers in the late 1970s. We just argued that in a compiled lan-
guage like Fortran there is no need to transform loops into calls to vector operations.
The situation was a bit different for the first supercomputers. The point then was to
break up more complicated nested loops into a series of simpler loops performing
basic vector operations. These simpler loops could easier be translated by the com-
piler to a suitable form for very efficient computations on the super computers. This
type of vectorization can still be beneficial on modern RISC architectures found in
PCs and workstations. More importantly, basic vector operations have been imple-
mented with maximum efficiency, tuned to the hardware, and made available in
libraries (BLAS and ATLAS are examples). To get the ultimate speed out of code, it
may be advantageous to rewrite the algorithms and implementations such that they
can utilize very efficient libraries for vector operations.
In the 1980s, supercomputers were based on parallel computations, and algo-
rithms had to be recast again, this time from serial into parallel versions. The
importance of vectorization disappeared, but due to the popularity of interpreted
environments such as Matlab and Python, RISC-based computers, and standardized
libraries for vector operations, the vectorized programming style remains highly
relevant.
Search WWH ::




Custom Search