Starting in 1996, Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the Wayback Machine after an embargo period.
The C++ programming language offers many useful features for tackling complex scientific computing problems. Inheritance, polymorphism, generic programming, and operator overloading are of primary importance. Unfortunately, most of these advanced features carry a hefty performance cost. Until recently, C++ performance lagged behind Fortran's performance by anywhere from 20 percent to orders of magnitude. As a result, the adoption of C++ for scientific computing has been slower than it should be, given the platform's popularity.
Blitz++ provides a single array class called Array<T_numtype, N_rank>. This class provides a dynamically allocated N-dimensional array with reference counting, arbitrary storage ordering, subarrays and slicing, flexible expression handling, and many other useful features.
Can C++ Compete with Fortran?
Recent benchmarks show C++ encroaching steadily on Fortran's high-performance monopoly. For some benchmarks, C++ is even faster than Fortran! No, this is not a result of better optimizing compilers, preprocessors, or even language extensions. It's achieved simply through the use of template techniques. Through the magic of template metaprogramming, optimizations such as loop fusion, unrolling, tiling, and algorithm specialization can be performed automatically at compile time. Another successful package that uses this technique is, of course, the Boost C++ Libraries.
Another goal of Blitz++ is to extend the conventional dense array model (in other words, not sparse) with new and useful features. Some examples of such extensions are flexible storage formats, tensor notation, and index placeholders. Blitz++ is more than just cool arrays; you also get matrix math, random number generation, and a variety of supporting functionality.
Blitz++ Platform Support
As is true of any template metaprogramming tool, Blitz++ is only as good as the ISO/ANSI C++ compliance of the compiler you choose. Specifically, your compiler of choice must have rock-solid member templates, partial specialization, and partial ordering of templates. The good news is that Visual Studio .NET 2003 and Visual Studio 2005 are supported directly by way of project files. Additionally, a whole raft of compilers are supported, including Intel C++ for Windows, gcc, IBM XL C/C++ for AIX, and Sun Studio 10 for Solaris. Older versions of Borland C++ and Visual Studio C++ (6.0) definitely will not work. Of course, Blitz++ comes with a complete self-test suite so you can be confident that your OS and compiler are 100 percent compliant.
Getting Started
To use the Array class, you must include the header <blitz/array.h> and use the namespace blitz:
#include <blitz/array.h>
usingnamespace blitz;
The array class, called Array<T_numtype, N_rank>, has two templated parameters:
T_numtype is the numeric type to be stored in the array. T_numtype can be an integral type (scalars, including int and long double), complex type, or any user-defined type with appropriate numeric semantics.
N_rank is the dimensionality of the array (in other words, three for a three-dimensional array).
Array<int,1> x; // A one-dimensional array of int
Array<double,2> y; // A two-dimensional array of double
Array<complex<float>, 12> z; // A twelve-dimensional array of// complex<float>
When no constructor arguments are provided, the array is empty and no memory is allocated. To create an array that contains some data, simply provide the size of the array as constructor arguments:
Array<double,2> y(4,4); // A 4x4 array of double
The contents of a newly created array are garbage. To initialize the array to all zeroes, you can write the following:
y = 0;
Putting it all together, you can write a "hello world"-type program to add two 2x2 matrices together:
#include <blitz/array.h>
usingnamespace blitz;
int main() {
Array<float,2> A(2,2), B(2,2), C(2,2);
A = 1, 0, 2, 2;
B = 0, 0, 7, 0;
C = A + B;
cout << "C = " << C << endl;
return 0;
}
and the output:
C = 2 x 2
[ 1 0]
9 ]
The Array constructor can be manipulated in a lot of ways to achieve a variety of results: computation during initialization, setting the extent (length), setting the base of an array to be something other than zero, and building an array as the reference of another array. Here's a cool example of creating an array "B" that contains the square roots of elements in "A":
Array<float,2> A(N,N);
Array<float,2> B(sqrt(A));
About the Author
Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed
RATE THIS ARTICLE:
Excellent Very Good Average Below Average Poor
(You must be signed in to rank an article. Not a member? Click here to register)
Latest Comments:
No Comments Posted.
Add a Comment:
Title:
Comment:
Pre-Formatted:
Check this if you want the text to display with the formatting as typed (good for source code)
(You must be signed in to comment on an article. Not a member? Click here to register)