October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Blitz++: Fast, Accurate Numerical Computing in C++

  • January 26, 2007
  • By Victor Volkman
  • Send Email »
  • More Articles »

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.

So, how can you turbo charge C++ so that you can have your advanced language features but lose the poor performance? This is precisely the goal of the Blitz++ project: to develop techniques that will enable C++ to rival and even surpass the speed of Fortran for numerical computing, while preserving an object-oriented interface. The Blitz++ Numerical Library is being constructed as the solution under dual licenses—GNU GPL and Blitz++ Artistic License.

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>
using namespace 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>
using namespace 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));




Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel