The Wayback Machine - https://web.archive.org/web/20090801081553/http://www.codeguru.com:80/cpp/cpp/algorithms/math/article.php/c13199/

CodeGuru
Earthweb Search
Login Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> C++ >> Algorithms & Formulas >> Mathematics


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

Victor Volkman (view profile)
January 26, 2007

Go to page: 1  2  Next

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.


(continued)




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

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.

Go to page: 1  2  Next

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive 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)