## The AltiVec Difference

by Craig Hunter04/05/2002

In early February 2002, Apple announced an accelerated implementation of BLAST, a bioinformatics tool from the National Center for Biotechnology Information (NCBI). This software is used for protein and DNA searches in biomedical research and drug discovery. Co-developed with Genentech, this high-throughput version of BLAST takes advantage of AltiVec technology on the PowerMac G4 processor, running up to five times faster than the standard BLAST implementation.

Apple first introduced PowerMac G4 computer systems using AltiVec -- a high performance vector processing expansion to the PowerPC architecture -- in the fall of 1999. Architecturally, AltiVec adds 128-bit-wide vector execution units to the PowerPC architecture. Early versions of the G4 processor had a single AltiVec unit, while more recent versions have up to four units (simple, complex, floating, and permute). These vector units operate independently from the traditional integer and floating-point units.

For all it’s marketing hurrah, the concept of AltiVec is exceedingly simple: it processes data in multiples, working on a whole mouthful of data instead of tackling it one nibble at a time like the integer and floating-point units.

How big the mouthful is depends on the type of data. Within the AltiVec unit, data is held by 128-bit "short vector" registers, each capable of holding four single-precision (32-bit) floating-point words, four 32-bit integer words, eight 16-bit integer half-words, or 16 8-bit integer bytes. AltiVec operates on all data elements in a vector register simultaneously, with every instruction. This is known as SIMD -- single instruction, multiple data -- parallel processing.

For example, in computations involving single-precision floating-point numbers, AltiVec can offer 4-way parallelism by simultaneously operating on four elements of a vector with each instruction.

Consider a case where we have 20,000 pieces of floating-point data to process. In traditional "scalar" processing (with the floating-point unit), we would operate on each piece of data separately. This would require 20,000 cycles to perform each operation. With AltiVec, we could operate on four pieces of data at a time, requiring just 5,000 cycles for each operation. Right off the bat, that gives us a factor of four reduction in processor cycles.

To take advantage of the AltiVec unit, 162 new vector operations were added to the PowerPC instruction set, including functions like add, multiply-add, subtract, absolute value, truncate, round, sum, multiply-sum, average, and exponent. Simple combinations of these functions allow for the fast and easy computation of operations such as dot product.

All AltiVec instructions are fully pipelined with single-cycle throughput, and each can address up to three source-vector registers and one destination-vector register during execution. While the factor of four example given above is a good guideline for floating-point data, performance boosts range from less than a factor of four in operations involving lots of loads and stores and/or large data sets (like the floating-point and integer units, AltiVec still relies heavily on memory/cache access and bus performance) to much more than a factor of four in cases with multiple compound operations (like matrix multiplication).

One of the coolest aspects of AltiVec is that anyone can experiment with it quite easily; the C compiler included as part of Apple’s OS X Developer tools has AltiVec support built right in. This compiler can be used to step through a few benchmarks that illustrate some of AltiVec’s capabilities. We’ll consider the following four examples: basic vector addition, scaling and translation, 2D rotation, and matrix multiplication. These examples are typical in math, science, and engineering computations, but also applicable to things like image processing, encryption, and graphics manipulation. All cases will use floating-point data.

### Vector Addition

This is a very basic example, where we have two sets of data (x and y) that we want to add together to form a third set of data (z). The data sets x, y, and z are treated as vectors, each containing “n” elements of data:

```
x = (x1, x2, x3, x4, x5, . . . . , xn )
y = (y1, y2, y3, y4, y5, . . . . , yn )
z = (z1, z2, z3, z4, z5, . . . . , zn )
```

To implement the vector addition with traditional scalar code, we would do something like:

```
for (i=1; i<=n; i++) {
z[i]=x[i]+y[i];
}
```

where x, y, and z are defined as “float.” So, it will take n cycles to go through and add the n elements of data together. With AltiVec, we can use the `vec_add`

instruction as follows:

```
for (i=1; i<=n/4; i++) {
z[i]=vec_add(x[i],y[i]);
}
```

Here, x, y, and z are defined as "vector float." Note that the loop count is reduced by a factor of 4 (upper limit is changed from n to n/4) since the `vec_add`

function will actually operate on 4 pieces of data at a time. Thus, the total number of loop cycles required to add the n elements of data together is just n/4.

When Apple says that Mhz isn't the entire story, AltiVec has something to do with that. What are your thoughts? | |

Post your comments |

When benchmarked on a 400Mhz PowerBook G4 with n=1000, the scalar computation performed at 88 MFLOPS while the vector computation performed at 345 MFLOPS. This nets a factor of 3.9 increase in performance by using AltiVec. Part of this great performance is due to the fact that the vector length of 1,000 results in small vectors (about 4KB each) that fit into the processor’s L1 cache.

This performance advantage will decrease as the vector length gets longer and data spills out into the L2 cache and RAM. Since the vector add operation actually consists of two loads, an add and a store, three load/store operations are required for every add. This makes a vector add operation expensive for large amounts of data.

Pages: |