casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Private Member Functions | Private Attributes
casa::TiledLineStepper Class Reference

Step a Vector cursor optimally through a tiled Lattice. More...

#include <TiledLineStepper.h>

Inheritance diagram for casa::TiledLineStepper:
casa::LatticeNavigator

List of all members.

Public Member Functions

 TiledLineStepper (const IPosition &latticeShape, const IPosition &tileShape, const uInt axis)
 Construct a TiledLineStepper by specifying the Lattice shape, a tile shape and the axis along which the Vector cursor will lie (0 means the x-axis).
 TiledLineStepper (const TiledLineStepper &other)
 The copy constructor uses copy semantics.
 ~TiledLineStepper ()
TiledLineStepperoperator= (const TiledLineStepper &other)
 The assignment operator uses copy semantics.
virtual Bool operator++ (int)
 Increment operator (postfix or prefix version) - move the cursor forward one step.
virtual Bool operator-- (int)
 Decrement operator (postfix or prefix version) - move the cursor backwards one step.
virtual void reset ()
 Function to move the cursor to the beginning of the Lattice.
virtual Bool atStart () const
 Function which returns "True" if the cursor is at the beginning of the Lattice, otherwise, returns "False".
virtual Bool atEnd () const
 Function which returns "True" if an attempt has been made to increment the cursor beyond the end of the Lattice.
virtual uInt nsteps () const
 Function to return the number of steps (increments & decrements) taken since construction (or since last reset).
virtual IPosition position () const
 Function which returns the current position of the beginning of the cursor.
virtual IPosition endPosition () const
 Function which returns the current position of the end of the cursor.
virtual IPosition latticeShape () const
 Functions which returns the shape of the Lattice being iterated through.
virtual IPosition subLatticeShape () const
virtual IPosition cursorShape () const
 Function which returns the shape of the cursor.
virtual IPosition cursorAxes () const
 Function which returns the axes of the cursor.
IPosition tileShape () const
 Function which returns the shape of the "tile" the cursor will iterate through before moving onto the next tile.
virtual Bool hangOver () const
 Function which returns "True" if the increment/decrement operators have moved the cursor position such that part of the cursor beginning or end is hanging over the edge of the Lattice.
virtual void subSection (const IPosition &blc, const IPosition &trc)
 Functions to specify a "section" of the Lattice to step over.
virtual void subSection (const IPosition &blc, const IPosition &trc, const IPosition &inc)
virtual IPosition blc () const
 Return the bottom left hand corner (blc), top right corner (trc) or step size (increment) used by the current sub-Lattice.
virtual IPosition trc () const
virtual IPosition increment () const
virtual const IPositionaxisPath () const
 Return the axis path.
virtual LatticeNavigatorclone () const
 Function which returns a pointer to dynamic memory of an exact copy of this instance.
virtual Bool ok () const
 Function which checks the internal data of this class for correct dimensionality and consistant values.
virtual uInt calcCacheSize (const IPosition &cubeShape, const IPosition &tileShape, uInt maxCacheSize, uInt bucketSize) const
 Calculate the cache size (in tiles) for this type of access to a lattice in the given row of the tiled hypercube.

Private Member Functions

 TiledLineStepper ()
 Prevent the default constructor from being used.

Private Attributes

IPosition itsBlc
IPosition itsTrc
IPosition itsInc
LatticeIndexer itsSubSection
LatticeIndexer itsIndexer
LatticeIndexer itsTiler
IPosition itsIndexerCursorPos
IPosition itsTilerCursorPos
IPosition itsCursorShape
IPosition itsTileShape
IPosition itsAxisPath
uInt itsNsteps
uInt itsAxis
Bool itsEnd
Bool itsStart

Detailed Description

Step a Vector cursor optimally through a tiled Lattice.

Intended use:

Public interface

 <h3>Review Status</h3><dl><dt>Reviewed By:<dd>Peter Barnes<dt>Date Reviewed:<dd>1999/10/30<dt>Test programs:<dd>tTiledLineStepper</dl> 

Prerequisite

Etymology

TiledLineStepper is used to step a Vector cursor optimally through a Lattice that is tiled.

Synopsis

When you wish to traverse a Lattice (say, a PagedArray or an Image) you will usually create a LatticeIterator. Once created, you may attach a LatticeNavigator to the iterator. A TiledLineStepper, is a concrete class derived from the abstract LatticeNavigator that allows you to move a Vector cursor through the Lattice in a way that will minimize the amount of cache memory consumed.

Some Lattices (in particular PagedArrays) are stored (on disk) in tiles. For an N-dimensional Lattice a tile is an N-dimensional subsection with fewer elements along each axis. For example a Lattice of shape [512,512,4,32] may have a tile shape of [32,16,4,16], and there will be 16*32*1*2 (=1024) tiles in the entire Lattice. To allow efficient access of the data in a Lattice some tiles are cached in memory. As each tile may consume a fair bit of memory (in this example 128kBytes, assuming each element consumes 4 bytes), it is desirable to minimise the number of tiles held in the cache. But it is also desirable to minimise the number of times a tiles must be read into or written from the cache as this may require a time consuming operation like disk I/O.

Now suppose you wanted to traverse a Lattice with a Vector cursor of length 512 pixel aligned along the x-axis. Using a LatticeStepper , each Vector is retrieved from the Lattice sequentially and without any consideration of the underlying tile shape. What is the optimal cache size for the above example?

Suppose we have a cache size of 16 ie., the number of tiles along the x-axis. Then Vectors beginning at positions [0,0,0,0] to [0,15,0,0] will be stored in the cache. But the next Vector beginning at position [0,16,0,0] will flush the cache and read in another 16 tiles. This I/O takes time and will occur 16 times for each plane in the four dimensional Lattice. Further when the cursor moves to position [0,0,1,0] the 16 tiles that where initially in the cache will need to be read again. To avoid all this cache I/O it is better to have a bigger cache.

Suppose the cache size is 16*32 (=512) ie., enough tiles to contain an (x,y)-plane. Then the cache size will not be flushed until the cursor is moved to position [0,0,0,16]. Further the cache will never need to read back into memory tiles that had previously been stored in there. The cache is big enough to store tiles until they have been completely used. But this cache is 64MBytes in size, and consumes too much memory for many computers.

This where a TiledLineStepper is useful. Because it knows the shape of the tiles in the underlying Lattice it moves the cursor to return all the Vectors in the smallest possible cache of tiles before moving on to the next set of tiles. Using the above example again, the TiledLineStepper will move the beginning of the Vector cursor in the following pattern.

    [0,0,0,0], [0,1,0,0], [0,2,0,0], ... [0,15,0,0]
    [0,0,1,0], [0,1,1,0],            ... [0,15,1,0], 
                                     ... [0,15,3,0],
    [0,0,0,1], ...                       [0,15,3,15]

Moving the Vector cursor through all 16*4*16 (=1024 positions) can be done by caching only 16 tiles in memory (those along the x-axis). Hence the cache size need only be 2MBytes in size. Further once all 1024 vectors have been returned it is not necessary to read these 16 tiles back into memory. All the data in those tiles has already been accessed. Using a TiledLineStepper rather than a LatticeStepper has, in this example, resulted in a drop in the required cache size from 64MBytes down to 2MBytes.

In constructing a TiledLineStepper, you specify the Lattice shape, the tile shape and the axis the Vector cursor will be aligned with. Specifying an axis=0 will align the cursor with the x-axis and axis=2 will produce a cursor that is along the z-axis. The length of the cursor is always the same as the number of elements in the Lattice along the axis the cursor is aligned with.
It is possible to use the function subSection to traverse only a subsection of the lattice.

The cursor position can be incremented or decremented to retrieve the next or previous Vector in the Lattice. The position of the next Vector in the Lattice will depend on the tile shape, and is described above. Within a tile the Vector cursor will move first through the x-axis and then the y-axis (assuming we have a cursor oriented along the z-axis). In general the lower dimensions will be exhausted (within a tile) before moving the cursor through higher dimensions. This intra-tile behaviour for cursor movement extends to the inter-tile movement of the cursor between tiles.

Example

This example is of a global function that will do a 2-D inplace complex Fourier transform of an arbitrary large Lattice (which must have at least two dimensions).

A two dimensional transform is done by successive one dimensional transforms along all the rows and then all the columns in the lattice. Scoping is used to destroy iterators once they have been used. This frees up the cache memory associated with the cursor in each iterator.

    void FFT2DComplex (Lattice<Complex>& cArray,
                      const Bool direction)
    {
      const uInt ndim = cArray.ndim();
      AlwaysAssert(ndim > 1, AipsError);
      const IPosition latticeShape = cArray.shape();
      const uInt nx=latticeShape(0);
      const uInt ny=latticeShape(1);
      const IPosition tileShape = cArray.niceCursorShape();
   
      {
        TiledLineStepper tsx(latticeShape, tileShape, 0);
        LatticeIterator<Complex> lix(cArray, tsx);
        FFTServer<Float,Complex> fftx(IPosition(1, nx));
        for (lix.reset();!lix.atEnd();lix++) {
          fftx.fft(lix.rwVectorCursor(), direction);
        }
      }
      {
        TiledLineStepper tsy(latticeShape, tileShape, 1);
        LatticeIterator<Complex> liy(cArray, tsy);
        FFTServer<Float,Complex> ffty(IPosition(1, ny));
        for (liy.reset();!liy.atEnd();liy++) {
          ffty.fft(liy.rwVectorCursor(), direction);
        }
      }
    }

Motivation

Moving through a Lattice by equal sized chunks, and without regard to the nature of the data, is a basic and common procedure.

To Do

Definition at line 192 of file TiledLineStepper.h.


Constructor & Destructor Documentation

casa::TiledLineStepper::TiledLineStepper ( const IPosition latticeShape,
const IPosition tileShape,
const uInt  axis 
)

Construct a TiledLineStepper by specifying the Lattice shape, a tile shape and the axis along which the Vector cursor will lie (0 means the x-axis).

Is is nearly always advisable to make the tileShape identical to the Lattice tileShape. This can be obtained by lat.niceCursorShape(lat.advisedMaxPixels()) where lat is a Lattice object.

The copy constructor uses copy semantics.

Prevent the default constructor from being used.


Member Function Documentation

virtual Bool casa::TiledLineStepper::atEnd ( ) const [virtual]

Function which returns "True" if an attempt has been made to increment the cursor beyond the end of the Lattice.

Implements casa::LatticeNavigator.

virtual Bool casa::TiledLineStepper::atStart ( ) const [virtual]

Function which returns "True" if the cursor is at the beginning of the Lattice, otherwise, returns "False".

Implements casa::LatticeNavigator.

virtual const IPosition& casa::TiledLineStepper::axisPath ( ) const [virtual]

Return the axis path.

See LatticeStepper for a description and examples.

Implements casa::LatticeNavigator.

virtual IPosition casa::TiledLineStepper::blc ( ) const [virtual]

Return the bottom left hand corner (blc), top right corner (trc) or step size (increment) used by the current sub-Lattice.

If no sub-Lattice has been defined (with the subSection function) these functions return blc=0, trc=latticeShape-1, increment=1, ie. the entire Lattice.

Reimplemented from casa::LatticeNavigator.

virtual uInt casa::TiledLineStepper::calcCacheSize ( const IPosition cubeShape,
const IPosition tileShape,
uInt  maxCacheSize,
uInt  bucketSize 
) const [virtual]

Calculate the cache size (in tiles) for this type of access to a lattice in the given row of the tiled hypercube.

Implements casa::LatticeNavigator.

virtual LatticeNavigator* casa::TiledLineStepper::clone ( ) const [virtual]

Function which returns a pointer to dynamic memory of an exact copy of this instance.

The pointer returned by this function must be deleted externally.

Implements casa::LatticeNavigator.

virtual IPosition casa::TiledLineStepper::cursorAxes ( ) const [virtual]

Function which returns the axes of the cursor.

Implements casa::LatticeNavigator.

virtual IPosition casa::TiledLineStepper::cursorShape ( ) const [virtual]

Function which returns the shape of the cursor.

This always includes all axes (ie. it includes degenerates axes)

Implements casa::LatticeNavigator.

virtual IPosition casa::TiledLineStepper::endPosition ( ) const [virtual]

Function which returns the current position of the end of the cursor.

The endPosition function is relative to the origin in the main Lattice.

Implements casa::LatticeNavigator.

virtual Bool casa::TiledLineStepper::hangOver ( ) const [virtual]

Function which returns "True" if the increment/decrement operators have moved the cursor position such that part of the cursor beginning or end is hanging over the edge of the Lattice.

This always returns False.

Implements casa::LatticeNavigator.

virtual IPosition casa::TiledLineStepper::increment ( ) const [virtual]

Reimplemented from casa::LatticeNavigator.

virtual IPosition casa::TiledLineStepper::latticeShape ( ) const [virtual]

Functions which returns the shape of the Lattice being iterated through.

latticeShape always returns the shape of the main Lattice while subLatticeShape returns the shape of any sub-Lattice defined using the subSection function.

Implements casa::LatticeNavigator.

virtual uInt casa::TiledLineStepper::nsteps ( ) const [virtual]

Function to return the number of steps (increments & decrements) taken since construction (or since last reset).

This is a running count of all cursor movement (operator++ or operator--), even though N-increments followed by N-decrements will always leave the cursor in the original position.

Implements casa::LatticeNavigator.

virtual Bool casa::TiledLineStepper::ok ( ) const [virtual]

Function which checks the internal data of this class for correct dimensionality and consistant values.

Returns True if everything is fine otherwise returns False

Reimplemented from casa::LatticeNavigator.

virtual Bool casa::TiledLineStepper::operator++ ( int  ) [virtual]

Increment operator (postfix or prefix version) - move the cursor forward one step.

Returns True if the cursor was moved.

Implements casa::LatticeNavigator.

virtual Bool casa::TiledLineStepper::operator-- ( int  ) [virtual]

Decrement operator (postfix or prefix version) - move the cursor backwards one step.

Returns True if the cursor was moved.

Implements casa::LatticeNavigator.

TiledLineStepper& casa::TiledLineStepper::operator= ( const TiledLineStepper other)

The assignment operator uses copy semantics.

virtual IPosition casa::TiledLineStepper::position ( ) const [virtual]

Function which returns the current position of the beginning of the cursor.

The position function is relative to the origin in the main Lattice.

Implements casa::LatticeNavigator.

virtual void casa::TiledLineStepper::reset ( ) [virtual]

Function to move the cursor to the beginning of the Lattice.

Also resets the number of steps (nsteps function) to zero.

Implements casa::LatticeNavigator.

Reimplemented from casa::LatticeNavigator.

virtual void casa::TiledLineStepper::subSection ( const IPosition blc,
const IPosition trc 
) [virtual]

Functions to specify a "section" of the Lattice to step over.

A section is defined in terms of the Bottom Left Corner (blc), Top Right Corner (trc), and step size (inc), on ALL of its axes, including degenerate axes. The step size defaults to one if not specified.

Reimplemented from casa::LatticeNavigator.

virtual void casa::TiledLineStepper::subSection ( const IPosition blc,
const IPosition trc,
const IPosition inc 
) [virtual]

Reimplemented from casa::LatticeNavigator.

Function which returns the shape of the "tile" the cursor will iterate through before moving onto the next tile.

THIS IS NOT THE SAME AS THE TILE SHAPE USED BY THE LATTICE. It is nearly the same except that the axis the cursor is aligned with is replaced by the shape of the Lattice on that axis. eg., If a Lattice has a shape of [512,512,4,32] and a tile shape of [32,16,4,16] then tileShape() will return [512,16,4,16] if the cursor is along the x-axis and [32,512,4,16] if the cursor is along the y-axis.

virtual IPosition casa::TiledLineStepper::trc ( ) const [virtual]

Reimplemented from casa::LatticeNavigator.


Member Data Documentation

Definition at line 345 of file TiledLineStepper.h.

Definition at line 343 of file TiledLineStepper.h.

Definition at line 333 of file TiledLineStepper.h.

Definition at line 341 of file TiledLineStepper.h.

Definition at line 346 of file TiledLineStepper.h.

Definition at line 335 of file TiledLineStepper.h.

Definition at line 337 of file TiledLineStepper.h.

Definition at line 339 of file TiledLineStepper.h.

Definition at line 344 of file TiledLineStepper.h.

Definition at line 347 of file TiledLineStepper.h.

Definition at line 336 of file TiledLineStepper.h.

Definition at line 338 of file TiledLineStepper.h.

Definition at line 340 of file TiledLineStepper.h.

Definition at line 342 of file TiledLineStepper.h.

Definition at line 334 of file TiledLineStepper.h.


The documentation for this class was generated from the following file: