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