The announcement for Blitz++ 0.2 alpha (Arrays) went out yesterday.
You can find the announcement and online documentation
for the release at http://oonumerics.org/blitz/ (see the yellow
highlighted box near the top of the page).
The main addition in this release is Array objects. Most of the
features are predictable: reference counting, expression templates,
etc. But there are some neat features which I thought I'd describe
for the list.
The basic array object in Blitz++ is Array<T, N>. The parameter T is
the numeric type stored in the array (integral, floating point,
complex or a user type) and N is the rank or dimensionality (an integer).
* Flexible storage format
Arrays can have arbitrary index ranges. This code snippet creates
a 4x11 array with valid index ranges 5..8 and 10..20:
Array<int,2> A(Range(5,8), Range(10,20));
Arrays can be stored in row-major or column-major order; you can
also select a storage ordering based on any permutation of the
dimensions.
Each dimension can be stored in either ascending or descending
order in memory.
Combined, these two features mean that an N-dimensional array can
be stored in N! * 2^N possible ways (basically, the symmetry group
of an N-cube).
* Comma-delimited initialization syntax
This code makes B a 4x4 identity matrix:
Array<float,2> B(4,4);
B = 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
This is implemented by overloading the comma operator. I had
no idea you could do this until Axel Thimm pointed it out to me
recently. Isn't C++ a great language? The standard document
is like an overgrown jungle, every now and then some survivor
will stumble out of the wilds with a new discovery like
overloading comma.
In a future version you may see comma overloading for simultaneous
execution semantics like Fortran 90 (or is it HPF?) enjoys:
A = B + C, --> for (...)
D = A + E; {
A[i] = B[i] + C[i];
D[i] = A[i] + E[i];
}
* Using array indices as expression operands
Blitz++ provides a set of types called firstIndex, secondIndex,
thirdIndex, ... which act as placeholders for the array indices
in an expression:
firstIndex i;
secondIndex j;
Array<float,4> A(Range(1,4), Range(1,4));
A = 1. / (i+j);
That sets a_ij = 1/(i+j) for all (i,j).
* Tensor-like notation
You can also use index placeholders to write tensor-like
expressions. For example, suppose we had these declarations:
Array<float,2> A(4,4), B(4,4); // Two 4x4 arrays
Array<float,4> C(4,4,4,4); // A 4D array: 4x4x4x4
firstIndex i;
secondIndex j;
thirdIndex k;
fourthIndex l;
...and wanted to compute this tensor product:
ijkl lj ki
C = A B
Here's the Blitz++ code which does it:
C = A(l,j) * B(k,i);
The index placeholder arguments tell the arrays A and B how to map
their dimensions onto the dimensions of the destination array.
Where the notation departs from real tensors is that repeated
indices don't imply contraction (or summation). For example,
this tensor expression performs a matrix product of A and B:
ij ik kj
D = A * B the repeated "k" index means "sum over k"
In Blitz++, you have to explicitly indicate contractions using
the sum() function:
Array<float,2> D(4,4);
D = sum(A(i,k) * B(k,j), k);
* Array reductions
Similar to the sum() function above, there are other reductions
called mean, min, max, minIndex, maxIndex, product, count, any,
all. They transform any N-dimensional array or array expression
into a N-1 dimensional array expression.
* Cache-optimized 3D array stencilling
Here's a 3D array stencil:
int N = 64;
double c;
Array<float,3> A(N,N,N), B(N,N,N);
Range I(1,N-2), J(1,N-2), K(1,N-2);
B(I,J,K) = c * (A(I,J,K) + A(I+1,J,K) + A(I-1,J,K)
+ A(I,J+1,K) + A(I,J-1,K) + A(I,J,K+1) + A(I,J,K-1));
The library automatically detects 3D stencil expressions, and
uses a special traversal order to get better cache reuse. I've
benchmarked stencils like this on my RS/6000 workstation and
they run significantly faster than Fortran 77.
You can read more about the Array classes in the online
documentation, which is at
http://oonumerics.org/blitz/manual/arrays.html
This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:05 EST