casacore
Loading...
Searching...
No Matches
TempLattice.h
Go to the documentation of this file.
1//# TempLattice.h: A Lattice that can be used for temporary storage
2//# Copyright (C) 1997,1998,1999,2000,2003
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef LATTICES_TEMPLATTICE_H
27#define LATTICES_TEMPLATTICE_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/lattices/Lattices/TempLatticeImpl.h>
33#include <memory>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37
38// <summary>
39// A Lattice that can be used for temporary storage
40// </summary>
41
42// <use visibility=export>
43
44// <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tTempLattice.cc" demos="">
45// </reviewed>
46
47// <prerequisite>
48// <li> <linkto class="Lattice">Lattice</linkto>
49// <li> <linkto class="ArrayLattice">ArrayLattice</linkto>
50// <li> <linkto class="PagedArray">PagedArray</linkto>
51// </prerequisite>
52
53// <etymology>
54// A TempLattice disappears from both memory and disk when it goes out of
55// scope. Hence it is only useful for temporary storage of data.
56// </etymology>
57
58// <synopsis>
59// Lattice classes are designed to allow the memory-efficient handling of large
60// amounts of data. But they can also used with much smaller arrays. With
61// large amounts of data the <linkto class="PagedArray">PagedArray</linkto>
62// class should be used, as this will store the data on disk and efficiently
63// access specified portions of the data on request. With small amounts of
64// data the <linkto class="ArrayLattice">ArrayLattice</linkto> class should be
65// used as all the data is always in memory avoiding the I/O associated with
66// PagedArrays.
67// <p>
68// Applications often cannot predict until run time whether they will
69// be dealing with a large or small amount of data. So the use of a
70// PagedArray or an ArrayLattice cannot be made until the size of the arrays
71// are known. TempLattice makes this decision given the size of the Array. To
72// help in making a good choice the TempLattice class also examines how much
73// memory the operating system has (using an aipsrc variable) and compares
74// it with the size of the requested Array.
75// <p>
76// The algorithm currently used is: create an ArrayLattice if the size of the
77// array is less than a quarter of the total system memory; otherwise a
78// PagedArray is created. The PagedArray is stored in the current
79// working directory and given a unique name that contains the string
80// "pagedArray". This pagedArray will be deleted once the TempLattice goes out
81// of scope. So unlike PagedArrays which can be made to exist longer than the
82// time they are used by a process, the PagedArrays created by the
83// TempLattice class are always scratch arrays.
84// <p>
85// It is possible to temporarily close a TempLattice, which only takes effect
86// when it is created as a PagedArray. In this way it is possible to reduce
87// the number of open files in case a lot of TempLattice objects are used.
88// A temporarily closed TempLattice will be reopened automatically when needed.
89// It can also be reopened explicitly.
90// <p>
91// You can force the TempLattice to be disk based by setting the memory
92// argument in the constructors to 0
93// <p>
94// TempLattice is implemented using TempLatticeImpl for reasons explained
95// in that class.
96// </synopsis>
97
98// <example>
99// <srcblock>
100// // Create a temporary lattice and initialize to 0.
101// TempLattice<Float> myLat (IPosition(2,1024,1024));
102// myLat.set (0.);
103// // Temporarily close the lattice.
104// myLat.tempClose();
105// // Do an operation, which will automatically reopen the lattice.
106// myLat.set (1.);
107// // Note that the destructor deletes the table (if the TempLattice
108// // was created on disk).
109// </srcblock>
110// </example>
111
112// <motivation>
113// I needed a temporary Lattice when converting the Convolver class to using
114// Lattices. This was to store the Transfer function.
115// </motivation>
116
117// <templating arg=T>
118// <li> Any type that can be used by the Lattices can also be used by
119// this class.
120// </templating>
121
122//# <todo asof="yyyy/mm/dd">
123//# <li> add this feature
124//# <li> fix this bug
125//# <li> start discussion of this possible extension
126//# </todo>
127
128
129template<class T> class TempLattice : public Lattice<T>
130{
131public:
132 // The default constructor creates a TempLattice containing a
133 // default ArrayLattice object.
135 : itsImpl (new TempLatticeImpl<T>()) {}
136
137 // Create a TempLattice of the specified shape. You can specify how much
138 // memory the Lattice can consume before it becomes disk based by giving a
139 // non-negative value to the maxMemoryInMB argument. Otherwise it will assume
140 // it can use up to 25% of the memory on your machine as defined in aipsrc
141 // (this algorithm may change). Setting maxMemoryInMB to zero will force
142 // the lattice to disk.
143 // <group>
144 explicit TempLattice (const TiledShape& shape, Int maxMemoryInMB=-1)
145 : itsImpl (new TempLatticeImpl<T>(shape, maxMemoryInMB)) {}
146 TempLattice (const TiledShape& shape, Double maxMemoryInMB)
147 : itsImpl (new TempLatticeImpl<T>(shape, maxMemoryInMB)) {}
148 // </group>
149
150 // The copy constructor uses reference semantics. ie modifying data in the
151 // copied TempLattice also modifies the data in the original TempLattice.
152 // Passing by value doesn't make sense, because it may require the creation
153 // of a temporary (but possibly huge) file on disk.
155 : Lattice<T>(other), itsImpl (other.itsImpl) {}
156
157 // The destructor removes the Lattice from memory and if necessary disk.
158 virtual ~TempLattice();
159
160 // The assignment operator with reference semantics. As with the copy
161 // constructor assigning by value does not make sense.
163 { itsImpl = other.itsImpl; }
164
165 // Make a copy of the object (reference semantics).
166 virtual Lattice<T>* clone() const;
167
168 // Is the TempLattice paged to disk?
169 virtual Bool isPaged() const;
170
171 // Can the lattice data be referenced as an array section?
172 virtual Bool canReferenceArray() const;
173
174 // Is the TempLattice writable? It should be.
175 virtual Bool isWritable() const;
176
177 // Flush the data.
178 virtual void flush();
179
180 // Close the Lattice temporarily (if it is paged to disk).
181 // It'll be reopened automatically when needed or when
182 // <src>reopen</src> is called explicitly.
183 virtual void tempClose();
184
185 // If needed, reopen a temporarily closed TempLattice.
186 virtual void reopen();
187
188 // Return the shape of the Lattice including all degenerate axes.
189 // (ie. axes with a length of one)
190 virtual IPosition shape() const;
191
192 // Set all of the elements in the Lattice to the given value.
193 virtual void set (const T& value);
194
195 // Replace every element, x, of the Lattice with the result of f(x). You
196 // must pass in the address of the function -- so the function must be
197 // declared and defined in the scope of your program. All versions of
198 // apply require a function that accepts a single argument of type T (the
199 // Lattice template type) and return a result of the same type. The first
200 // apply expects a function with an argument passed by value; the second
201 // expects the argument to be passed by const reference; the third
202 // requires an instance of the class <src>Functional<T,T></src>. The
203 // first form ought to run faster for the built-in types, which may be an
204 // issue for large Lattices stored in memory, where disk access is not an
205 // issue.
206 // <group>
207 virtual void apply (T (*function)(T));
208 virtual void apply (T (*function)(const T&));
209 virtual void apply (const Functional<T,T>& function);
210 // </group>
211
212 // This function returns the recommended maximum number of pixels to
213 // include in the cursor of an iterator.
214 virtual uInt advisedMaxPixels() const;
215
216 // Get the best cursor shape.
217 virtual IPosition doNiceCursorShape (uInt maxPixels) const;
218
219 // Maximum size - not necessarily all used. In pixels.
220 virtual uInt maximumCacheSize() const;
221
222 // Set the maximum (allowed) cache size as indicated.
223 virtual void setMaximumCacheSize (uInt howManyPixels);
224
225 // Set the cache size as to "fit" the indicated path.
226 virtual void setCacheSizeFromPath (const IPosition& sliceShape,
227 const IPosition& windowStart,
228 const IPosition& windowLength,
229 const IPosition& axisPath);
230
231 // Set the actual cache size for this Array to be be big enough for the
232 // indicated number of tiles. This cache is not shared with PagedArrays
233 // in other rows and is always clipped to be less than the maximum value
234 // set using the setMaximumCacheSize member function.
235 // tiles. Tiles are cached using a first in first out algorithm.
236 virtual void setCacheSizeInTiles (uInt howManyTiles);
237
238 // Clears and frees up the caches, but the maximum allowed cache size is
239 // unchanged from when setCacheSize was called
240 virtual void clearCache();
241
242 // Report on cache success.
243 virtual void showCacheStatistics (ostream& os) const;
244
245 // Get or put a single element in the lattice.
246 // Note that Lattice::operator() can also be used to get a single element.
247 // <group>
248 virtual T getAt (const IPosition& where) const;
249 virtual void putAt (const T& value, const IPosition& where);
250 // </group>
251
252 // Check class internals - used for debugging. Should always return True
253 virtual Bool ok() const;
254
255 // This function is used by the LatticeIterator class to generate an
256 // iterator of the correct type for this Lattice. Not recommended
257 // for general use.
259 Bool useRef) const;
260
261 // Do the actual getting of an array of values.
262 virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section);
263
264 // Do the actual getting of an array of values.
265 virtual void doPutSlice (const Array<T>& sourceBuffer,
266 const IPosition& where,
267 const IPosition& stride);
268
269private:
270 std::shared_ptr<TempLatticeImpl<T>> itsImpl;
271};
272
273
274
275} //# NAMESPACE CASACORE - END
276
277#ifndef CASACORE_NO_AUTO_TEMPLATES
278#include <casacore/lattices/Lattices/TempLattice.tcc>
279#endif //# CASACORE_NO_AUTO_TEMPLATES
280#endif
virtual void clearCache()
Clears and frees up the caches, but the maximum allowed cache size is unchanged from when setCacheSiz...
virtual Bool isWritable() const
Is the TempLattice writable? It should be.
virtual Lattice< T > * clone() const
Make a copy of the object (reference semantics).
virtual T getAt(const IPosition &where) const
Get or put a single element in the lattice.
virtual uInt advisedMaxPixels() const
This function returns the recommended maximum number of pixels to include in the cursor of an iterato...
virtual void reopen()
If needed, reopen a temporarily closed TempLattice.
virtual LatticeIterInterface< T > * makeIter(const LatticeNavigator &navigator, Bool useRef) const
This function is used by the LatticeIterator class to generate an iterator of the correct type for th...
virtual void setMaximumCacheSize(uInt howManyPixels)
Set the maximum (allowed) cache size as indicated.
virtual Bool ok() const
Check class internals - used for debugging.
virtual void apply(T(*function)(const T &))
virtual void putAt(const T &value, const IPosition &where)
Put the value of a single element.
virtual void apply(const Functional< T, T > &function)
virtual void tempClose()
Close the Lattice temporarily (if it is paged to disk).
virtual IPosition shape() const
Return the shape of the Lattice including all degenerate axes.
TempLattice(const TempLattice< T > &other)
The copy constructor uses reference semantics.
TempLattice(const TiledShape &shape, Int maxMemoryInMB=-1)
Create a TempLattice of the specified shape.
virtual void doPutSlice(const Array< T > &sourceBuffer, const IPosition &where, const IPosition &stride)
Do the actual getting of an array of values.
TempLattice< T > & operator=(const TempLattice< T > &other)
The assignment operator with reference semantics.
virtual Bool isPaged() const
Is the TempLattice paged to disk?
std::shared_ptr< TempLatticeImpl< T > > itsImpl
virtual uInt maximumCacheSize() const
Maximum size - not necessarily all used.
virtual void setCacheSizeInTiles(uInt howManyTiles)
Set the actual cache size for this Array to be be big enough for the indicated number of tiles.
virtual void apply(T(*function)(T))
Replace every element, x, of the Lattice with the result of f(x).
virtual ~TempLattice()
The destructor removes the Lattice from memory and if necessary disk.
TempLattice(const TiledShape &shape, Double maxMemoryInMB)
virtual void showCacheStatistics(ostream &os) const
Report on cache success.
virtual void setCacheSizeFromPath(const IPosition &sliceShape, const IPosition &windowStart, const IPosition &windowLength, const IPosition &axisPath)
Set the cache size as to "fit" the indicated path.
virtual void set(const T &value)
Set all of the elements in the Lattice to the given value.
virtual void flush()
Flush the data.
virtual Bool doGetSlice(Array< T > &buffer, const Slicer &section)
Do the actual getting of an array of values.
virtual IPosition doNiceCursorShape(uInt maxPixels) const
Get the best cursor shape.
virtual Bool canReferenceArray() const
Can the lattice data be referenced as an array section?
TempLattice()
The default constructor creates a TempLattice containing a default ArrayLattice object.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
double Double
Definition aipstype.h:53