www.codeguru.com/cpp/cpp/algorithms/math/article.php/c13199/

Back to Article

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

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

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)



Web Devs:
Moonlight as a Game Developer and Win Cool Prizes by Accepting the RIA Run Challenge

Now, your mission--should you choose to accept: Take your shot at gaming stardom if you think you might have what it takes to build a cool RIA game and you could win an Xbox 360 or other fabulous prizes. Hurry! You only have until May 15, 2008 to enter. »

 
Article:
Leveraging Your Flash Development with Silverlight

You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight. Learn more here. »

 
Article:
What Does it Take to Build the Best RIA?

With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application. »

 

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

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES