![]() |
Blitz Support : |
From: Julian Cummings (cummings_at_[hidden])
Date: 2005-06-17 20:39:51
Peter,
On Fri, 2005-06-17 at 12:12 +0200, Peter Kümmel wrote:
> Julian Cummings wrote:
> > This can be hard to verify
> > unless you have a compiler that preserves intermediate C code (e.g., KCC) or
> > prints out the templates it instantiates (e.g., SGI CC).
>
> Supports gcc this too?
No. But you can look at the assembly code (yuck!) or just try an
experiment comparing with some hand-coded C with for loops.
> > By the way, you do
> > not need the index placeholders i and j on the right-hand side of your
> > expression unless there is an offset or restricted range with respect to the
> > original Array domain. So you can just say
> >
> > A = c2.f(c1.f(A));
>
> Thanks, using blitz more intensively in the last days I've also
> realized it.
>
> But I've also realized that I need calculations on the indices,
> e.g. A = A exp(sqrt(i*i+j*j)). But I can't find a way to move
> the calculations from the call to the function, A = f(A,i,j)),
> seems that this is impossible.
Hmmm... this started from defining a struct that had a scalar member
function and then converting that member function into a member function
that would operate on an Array. I guess my question would be how would
the original scalar function have known anything about Array indices. I
think that by design, these are not functions that operate on Array
indices. Perhaps your original scalar function looks like this:
double f(double x, int i, int j) { return x*exp(sqrt(i*i+j*j)); }
There is no nifty macro for converting this sort of function into
something that takes an Array and assumes i and j to be the indices.
But if the function is so simple, why not just write it out with blitz
tensor notation and Arrays?
>
> Is the usage of the indices the best way to implement such
> calculations? I've written some benchmark code to compare with
> C-code and C is in all cases the fasted one.
>
Really? I just tried an example and got exactly the same performance
from blitz and C. I am attaching my example codes. I compiled them
with gcc 4.0.0 and full optimization (-O3) and each one took about 10
seconds to run on my Opteron. The example is taken from your original
example code.
> It is also possible with Array iterators?
> I thought this is not possible within blitz, I've nothing found about it.
> Could you please give me a hint or a short example.
The blitz Arrays have iterators which are now more or less STL
compliant. This allows you to write a generic loop over Array elements
and do whatever calculation you want to each element. The iterator type
also has a special position() member that returns the current Array
indices in a TinyVector of ints. So you could say
#define DIM 2
Array<double,DIM>::iterator it, iend = A.end();
for (it = A.begin(); it != iend; ++it) {
TinyVector<int,DIM> pos = it.position();
*it = *it * exp(sqrt(dot(pos,pos)));
}
Hope this helps,
Julian C.
-- Dr. Julian C. Cummings E-mail: cummings_at_[hidden] California Institute of Technology Phone: 626-395-2543 1200 E. California Blvd., Mail Code 158-79 Fax: 626-584-5917 Pasadena, CA 91125 Office: 125 Powell-Booth