casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TempLattice.h
Go to the documentation of this file.
00001 //# TempLattice.h: A Lattice that can be used for temporary storage
00002 //# Copyright (C) 1997,1998,1999,2000,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: TempLattice.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
00028 
00029 #ifndef LATTICES_TEMPLATTICE_H
00030 #define LATTICES_TEMPLATTICE_H
00031 
00032 
00033 //# Includes
00034 #include <lattices/Lattices/TempLatticeImpl.h>
00035 #include <casa/Utilities/CountedPtr.h>
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 
00040 // <summary>
00041 // A Lattice that can be used for temporary storage
00042 // </summary>
00043 
00044 // <use visibility=export>
00045 
00046 // <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tTempLattice.cc" demos="">
00047 // </reviewed>
00048 
00049 // <prerequisite>
00050 //   <li> <linkto class="Lattice">Lattice</linkto>
00051 //   <li> <linkto class="ArrayLattice">ArrayLattice</linkto>
00052 //   <li> <linkto class="PagedArray">PagedArray</linkto>
00053 // </prerequisite>
00054 
00055 // <etymology>
00056 // A TempLattice disappears from both memory and disk when it goes out of
00057 // scope. Hence it is only useful for temporary storage of data.
00058 // </etymology>
00059 
00060 // <synopsis>
00061 // Lattice classes are designed to allow the memory-efficient handling of large
00062 // amounts of data. But they can also used with much smaller arrays. With
00063 // large amounts of data the <linkto class="PagedArray">PagedArray</linkto>
00064 // class should be used, as this will store the data on disk and efficiently
00065 // access specified portions of the data on request. With small amounts of
00066 // data the <linkto class="ArrayLattice">ArrayLattice</linkto> class should be
00067 // used as all the data is always in memory avoiding the I/O associated with
00068 // PagedArrays.
00069 // <p>
00070 // Applications often cannot predict until run time whether they will
00071 // be dealing with a large or small amount of data. So the use of a
00072 // PagedArray or an ArrayLattice cannot be made until the size of the arrays
00073 // are known. TempLattice makes this decision given the size of the Array. To
00074 // help in making a good choice the TempLattice class also examines how much
00075 // memory the operating system has (using an aipsrc variable) and compares
00076 // it with the size of the requested Array.
00077 // <p>
00078 // The algorithm currently used is: create an ArrayLattice if the size of the
00079 // array is less than a quarter of the total system memory; otherwise a
00080 // PagedArray is created. The PagedArray is stored in the current
00081 // working directory and given a unique name that contains the string
00082 // "pagedArray". This pagedArray will be deleted once the TempLattice goes out
00083 // of scope. So unlike PagedArrays which can be made to exist longer than the
00084 // time they are used by a process, the PagedArrays created by the
00085 // TempLattice class are always scratch arrays.
00086 // <p>
00087 // It is possible to temporarily close a TempLattice, which only takes effect
00088 // when it is created as a PagedArray. In this way it is possible to reduce
00089 // the number of open files in case a lot of TempLattice objects are used.
00090 // A temporarily closed TempLattice will be reopened automatically when needed.
00091 // It can also be reopened explicitly.
00092 // <p>
00093 // You can force the TempLattice to be disk based by setting the memory
00094 // argument in the constructors to 0
00095 // <p>
00096 // TempLattice is implemented using TempLatticeImpl for reasons explained
00097 // in that class.
00098 // </synopsis>
00099 
00100 // <example>
00101 // <srcblock>
00102 //  // Create a temporary lattice and initialize to 0.
00103 //  TempLattice<Float> myLat (IPosition(2,1024,1024));
00104 //  myLat.set (0.);
00105 //  // Temporarily close the lattice.
00106 //  myLat.tempClose();
00107 //  // Do an operation, which will automatically reopen the lattice.
00108 //  myLat.set (1.);
00109 //  // Note that the destructor deletes the table (if the TempLattice
00110 //  // was created on disk).
00111 // </srcblock>
00112 // </example>
00113 
00114 // <motivation>
00115 // I needed a temporary Lattice when converting the Convolver class to using
00116 // Lattices. This was to store the Transfer function.
00117 // </motivation>
00118 
00119 // <templating arg=T>
00120 //  <li> Any type that can be used by the Lattices can also be used by
00121 //       this class.
00122 // </templating>
00123 
00124 //# <todo asof="yyyy/mm/dd">
00125 //#   <li> add this feature
00126 //#   <li> fix this bug
00127 //#   <li> start discussion of this possible extension
00128 //# </todo>
00129 
00130 
00131 template<class T> class TempLattice : public Lattice<T>
00132 {
00133 public:
00134   // The default constructor creates a TempLattice containing a
00135   // default ArrayLattice object.
00136   TempLattice()
00137     : itsImpl (new TempLatticeImpl<T>()) {}
00138 
00139   // Create a TempLattice of the specified shape. You can specify how much
00140   // memory the Lattice can consume before it becomes disk based by giving a
00141   // non-negative value to the maxMemoryInMB argument. Otherwise it will assume
00142   // it can use up to 25% of the memory on your machine as defined in aipsrc
00143   // (this algorithm may change). Setting maxMemoryInMB to zero will force
00144   // the lattice to disk.
00145   // <group>
00146   explicit TempLattice (const TiledShape& shape, Int maxMemoryInMB=-1)
00147     : itsImpl (new TempLatticeImpl<T>(shape, maxMemoryInMB)) {}
00148   TempLattice (const TiledShape& shape, Double maxMemoryInMB)
00149     : itsImpl (new TempLatticeImpl<T>(shape, maxMemoryInMB)) {}
00150   // </group>
00151   
00152   // The copy constructor uses reference semantics. ie modifying data in the
00153   // copied TempLattice also modifies the data in the original TempLattice.
00154   // Passing by value doesn't make sense, because it may require the creation
00155   // of a temporary (but possibly huge) file on disk.
00156   TempLattice (const TempLattice<T>& other)
00157     : Lattice<T>(other), itsImpl (other.itsImpl) {}
00158     
00159   // The destructor removes the Lattice from memory and if necessary disk.
00160   virtual ~TempLattice();
00161 
00162   // The assignment operator with reference semantics. As with the copy
00163   // constructor assigning by value does not make sense.
00164   TempLattice<T>& operator= (const TempLattice<T>& other)
00165     { itsImpl = other.itsImpl; }
00166 
00167   // Make a copy of the object (reference semantics).
00168   virtual Lattice<T>* clone() const;
00169 
00170   // Is the TempLattice paged to disk?
00171   virtual Bool isPaged() const;
00172 
00173   // Can the lattice data be referenced as an array section?
00174   virtual Bool canReferenceArray() const;
00175 
00176   // Is the TempLattice writable? It should be.
00177   virtual Bool isWritable() const;
00178 
00179   // Flush the data.
00180   virtual void flush();
00181 
00182   // Close the Lattice temporarily (if it is paged to disk).
00183   // It'll be reopened automatically when needed or when
00184   // <src>reopen</src> is called explicitly.
00185   virtual void tempClose();
00186 
00187   // If needed, reopen a temporarily closed TempLattice.
00188   virtual void reopen();
00189 
00190   // Return the shape of the Lattice including all degenerate axes.
00191   // (ie. axes with a length of one)
00192   virtual IPosition shape() const;
00193   
00194   // Set all of the elements in the Lattice to the given value.
00195   virtual void set (const T& value);
00196 
00197   // Replace every element, x, of the Lattice with the result of f(x).  You
00198   // must pass in the address of the function -- so the function must be
00199   // declared and defined in the scope of your program.  All versions of
00200   // apply require a function that accepts a single argument of type T (the
00201   // Lattice template type) and return a result of the same type.  The first
00202   // apply expects a function with an argument passed by value; the second
00203   // expects the argument to be passed by const reference; the third
00204   // requires an instance of the class <src>Functional<T,T></src>.  The
00205   // first form ought to run faster for the built-in types, which may be an
00206   // issue for large Lattices stored in memory, where disk access is not an
00207   // issue.
00208   // <group>
00209   virtual void apply (T (*function)(T));
00210   virtual void apply (T (*function)(const T&));
00211   virtual void apply (const Functional<T,T>& function);
00212   // </group>
00213 
00214   // This function returns the recommended maximum number of pixels to
00215   // include in the cursor of an iterator.
00216   virtual uInt advisedMaxPixels() const;
00217 
00218   // Get the best cursor shape.
00219   virtual IPosition doNiceCursorShape (uInt maxPixels) const;
00220 
00221   // Maximum size - not necessarily all used. In pixels.
00222   virtual uInt maximumCacheSize() const;
00223 
00224   // Set the maximum (allowed) cache size as indicated.
00225   virtual void setMaximumCacheSize (uInt howManyPixels);
00226 
00227   // Set the cache size as to "fit" the indicated path.
00228   virtual void setCacheSizeFromPath (const IPosition& sliceShape,
00229                                      const IPosition& windowStart,
00230                                      const IPosition& windowLength,
00231                                      const IPosition& axisPath);
00232     
00233   // Set the actual cache size for this Array to be be big enough for the
00234   // indicated number of tiles. This cache is not shared with PagedArrays
00235   // in other rows and is always clipped to be less than the maximum value
00236   // set using the setMaximumCacheSize member function.
00237   // tiles. Tiles are cached using a first in first out algorithm. 
00238   virtual void setCacheSizeInTiles (uInt howManyTiles);
00239 
00240   // Clears and frees up the caches, but the maximum allowed cache size is 
00241   // unchanged from when setCacheSize was called
00242   virtual void clearCache();
00243 
00244   // Report on cache success.
00245   virtual void showCacheStatistics (ostream& os) const;
00246 
00247   // Get or put a single element in the lattice.
00248   // Note that Lattice::operator() can also be used to get a single element.
00249   // <group>
00250   virtual T getAt (const IPosition& where) const;
00251   virtual void putAt (const T& value, const IPosition& where);
00252   // </group>
00253   
00254   // Check class internals - used for debugging. Should always return True
00255   virtual Bool ok() const;
00256 
00257   // This function is used by the LatticeIterator class to generate an
00258   // iterator of the correct type for this Lattice. Not recommended
00259   // for general use. 
00260   virtual LatticeIterInterface<T>* makeIter (const LatticeNavigator& navigator,
00261                                              Bool useRef) const;
00262 
00263   // Do the actual getting of an array of values.
00264   virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section);
00265 
00266   // Do the actual getting of an array of values.
00267   virtual void doPutSlice (const Array<T>& sourceBuffer,
00268                            const IPosition& where,
00269                            const IPosition& stride);
00270   
00271 private:
00272   CountedPtr<TempLatticeImpl<T> > itsImpl;
00273 };
00274 
00275 
00276 
00277 } //# NAMESPACE CASA - END
00278 
00279 #ifndef CASACORE_NO_AUTO_TEMPLATES
00280 #include <lattices/Lattices/TempLattice.tcc>
00281 #endif //# CASACORE_NO_AUTO_TEMPLATES
00282 #endif