Technical: Hardware: G4
Advanced Search
Apple Developer Connection
Member Login Log In | Not a Member? Support

Using AltiVec

Getting Started in FORTRAN

For Panther (MacOS X.3) and earlier releases of MacOS X, Apple does not provide FORTRAN compilers, though high performance FORTRAN compilers are available from third party software developers, such as GNU (g77), Absoft, Numerical Algorithms Group (NAG), and the high performance FORTRAN compiler from IBM (xlf). If you choose to use the free GNU g77 compiler, please be aware that there is an incompatibility between the gcc 3.3 based assembler in the 2003 developer tools, and version 3.1 and earlier of g77. You will need to either install an older set of gcc-3.1 based developer tools (AltiVec performance is not as good with gcc-3.1 or lower), or install a gcc-3.4 or better based version of g77.

Writing AltiVec in FORTRAN

Compiler support for the high level AltiVec language extensions is common for C family languages, but may be missing for FORTRAN. There is no standard for how a AltiVec extensions should be added to a language outside of C/C++/Objective C. If you are interested in writing AltiVec code directly in FORTRAN, please contact your compiler vendor to find out how best to do this. It is likely that you will need to write C functions that adhere to FORTRAN calling conventions. (All arguments need to be passed by address rather than by value. Matrices need to be in the FORTRAN column-major storage order.)

Libraries

The BLAS and LAPACK vectorized libraries in vecLib.framework are directly callable from FORTRAN. Both the g77 style naming convention (all lowercase name, underscore suffix) and the "classical" naming scheme (all uppercase, no underscores) are supported. The header cblas.h provides prototypes for the standard C interface to the BLAS. The header, clapack.h, provides prototypes to the LAPACK entry points. These use the FORTRAN calling conventions since there is no industry standard C binding for LAPACK.

New for MacOS X.4, Tiger, vForce is callable from FORTRAN.

Linking to vecLib

To link against vecLib.framework, add the following compile line option:

-framework vecLib

Some compilers do not understand the -framework flag. A common workaround is to use the following modified compile line instead. This will usually pass -framework vecLib through to the linker unmodified. The linker should understand what a framework is even if the compiler doesn't: There is no space after the commas. The second character is a lowercase L.

-Wl,-framework -Wl,vecLib

As a last resort, you may try linking directly to the vecLib binary as a dynamically linked library. It is to be found here:

/System/Library/Frameworks/vecLib.framework/versions/A/vecLib

Example: Integrating AltiVec into a FORTRAN app

This example was prepared using the MacOS X public beta of xlf. The test application (contained in test.f90) does performance benchmarking of three different ways to apply exp() over a small array of single precision floating point values. It begins by using the standard math library exp() function call. It then repeats the benchmark using the vecLib vexpf() function. That function is written to take AltiVec vectors by value, which means it only works for C. So, for this case, we show how to write a small C wrapper function to make it suitable for use with FORTRAN. Finally, there is an example that uses the AltiVec vexptefp instruction to do a limited precision estimate of exp(). It is written entirely in C using the AltiVec C language extension and calls no outside code.

Both C functions are in a file, vector.c and are compiled with a standard C compiler. We used GCC-3.3. You simply compile that to make an object files, vector.o. There is another C file, timer.c, that contains the implementation for a small fast timer easily callable in FORTRAN, that we used for bechmarking. Compile that into a .o and then compile both into the FORTRAN app.

You may download the example here. If your browser does not automatically uncompress the tar.gz file, you may do so from the command line as follows:

gunzip fortran_sample.tar.gz
tar -xvf fortran_sample.tar

Next, compile the inputs:

cd fortran_sample
gcc -c -O3 -funroll-loops -faltivec vector.c
gcc -c -O3 timer.c
f90 vector.o timer.o test.f90 -Wl,-framework -Wl,vecLib -O3

To run, simply type:

./a.out

On a 800 MHz G4 Powerbook, running MacOS X.3, Panther, we saw the following output:

clock overhead: 0.600058961793585866E-01 microseconds.

Benchmarking....

Time to call exp() 4096 times: 1313.34904967762122 microseconds.

Time for vexpf to repeat this calculation: 280.407552846142664 microseconds.

Time for vexp_estimate to estimate exp() over that dataset: 8.49083430937924000 microseconds.

Table of ContentsNextPrevious