Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
SearchIteration.c
Go to the documentation of this file.
1//
2// Copyright (C) 2017 Karl Wette
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with with program; see the file COPYING. If not, write to the
16// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17// MA 02110-1301 USA
18//
19
20///
21/// \file
22/// \ingroup lalpulsar_bin_Weave
23///
24
25#include "SearchIteration.h"
26
27///
28/// Iterator over a search parameter space
29///
31 /// Number of parameter-space dimensions
32 size_t ndim;
33 /// Iterator over semicoherent parameter space
34 LatticeTilingIterator *semi_itr;
35 /// Current lattice tiling point
36 gsl_vector *semi_rssky;
37 /// Dimension in which to partition iterator output
39 /// Maximum number of points in the partitioned dimension
41 /// Number of points in each partition
43 /// Number of partitions of iterator output
45 /// Index of the current iterator output partition
47 /// Number of times to repeat iteration over semicoherent parameter space
49 /// Index of the current repetition
51 /// Progress count for iteration
53 /// Progress index for iteration
55};
56
57///
58/// Create iterator over the main loop search parameter space
59///
61 const LatticeTiling *semi_tiling,
62 const UINT4 freq_partitions,
63 const UINT4 f1dot_partitions
64)
65{
66
67 // Check input
68 XLAL_CHECK_NULL( semi_tiling != NULL, XLAL_EINVAL );
69 XLAL_CHECK_NULL( freq_partitions > 0, XLAL_EINVAL );
70
71 // Allocate memory
72 WeaveSearchIterator *itr = XLALCalloc( 1, sizeof( *itr ) );
73 XLAL_CHECK_NULL( itr != NULL, XLAL_ENOMEM );
74
75 // Get number of parameter-space dimensions
76 itr->ndim = XLALTotalLatticeTilingDimensions( semi_tiling );
77
78 // Create iterator over semicoherent tiling
79 // - The last parameter-space dimension is always frequency and is not iterated over, since we
80 // always operate over a block of frequencies at once. Since the frequency spacing is always
81 // equal over all tilings due to XLALEqualizeReducedSuperskyMetricsFreqSpacing(), operations
82 // such as nearest point finding can be performed once per frequency block instead of per bin.
83 itr->semi_itr = XLALCreateLatticeTilingIterator( semi_tiling, itr->ndim - 1 );
84
85 // Allocate current lattice tiling point
86 GAVEC_NULL( itr->semi_rssky, itr->ndim );
87
88 // Partition iterator output in reduced supersky spindown dimension
89 {
90 itr->partition_count = f1dot_partitions;
91 itr->partition_index = 0;
92 itr->partition_dim = XLALLatticeTilingDimensionByName( semi_tiling, "nu1dot" );
93 XLAL_CHECK_NULL( itr->partition_dim >= 0, XLAL_EFUNC );
94 const LatticeTilingStats *stats = XLALLatticeTilingStatistics( semi_tiling, itr->partition_dim );
96 XLAL_CHECK_NULL( stats->max_points > 0, XLAL_ESIZE );
97 itr->partition_max = stats->max_points;
98 itr->partition_size = 1 + ( itr->partition_max - 1 ) / itr->partition_count;
99 }
100
101 // Set repetition count and index
102 itr->repetition_count = freq_partitions;
103 itr->repetition_index = 0;
104
105 // Set progress count and index for iteration
106 itr->prog_count = itr->repetition_count * XLALTotalLatticeTilingPoints( itr->semi_itr );
107 XLAL_CHECK_NULL( itr->prog_count > 0, XLAL_EFUNC );
108 itr->prog_index = 0;
109
110 return itr;
111
112}
113
114///
115/// Destroy iterator
116///
118 WeaveSearchIterator *itr
119)
120{
121 if ( itr != NULL ) {
122 XLALDestroyLatticeTilingIterator( itr->semi_itr );
123 GFVEC( itr->semi_rssky );
124 XLALFree( itr );
125 }
126}
127
128///
129/// Save state of iterator to a FITS file
130///
132 const WeaveSearchIterator *itr,
134)
135{
136
137 // Check input
138 XLAL_CHECK( itr != NULL, XLAL_EFAULT );
139 XLAL_CHECK( file != NULL, XLAL_EFAULT );
140
141 // Write state of iterator over semicoherent parameter space
142 XLAL_CHECK( XLALSaveLatticeTilingIterator( itr->semi_itr, file, "itrstate" ) == XLAL_SUCCESS, XLAL_EFUNC );
143
144 // Write partition index
145 XLAL_CHECK( XLALFITSHeaderWriteUINT4( file, "partidx", itr->partition_index, "partition index" ) == XLAL_SUCCESS, XLAL_EFUNC );
146
147 // Write repetition index
148 XLAL_CHECK( XLALFITSHeaderWriteUINT4( file, "reptidx", itr->repetition_index, "repetition index" ) == XLAL_SUCCESS, XLAL_EFUNC );
149
150 // Write progress index
151 XLAL_CHECK( XLALFITSHeaderWriteUINT8( file, "progidx", itr->prog_index, "progress index" ) == XLAL_SUCCESS, XLAL_EFUNC );
152
153 return XLAL_SUCCESS;
154
155}
156
157///
158/// Restore state of iterator from a FITS file
159///
161 WeaveSearchIterator *itr,
163)
164{
165
166 // Check input
167 XLAL_CHECK( itr != NULL, XLAL_EFAULT );
168 XLAL_CHECK( file != NULL, XLAL_EFAULT );
169
170 // Read state of iterator over semicoherent parameter space
171 XLAL_CHECK( XLALRestoreLatticeTilingIterator( itr->semi_itr, file, "itrstate" ) == XLAL_SUCCESS, XLAL_EFUNC );
172
173 // Read partition index
174 XLAL_CHECK( XLALFITSHeaderReadUINT4( file, "partidx", &itr->partition_index ) == XLAL_SUCCESS, XLAL_EFUNC );
175 XLAL_CHECK( itr->partition_index < itr->partition_count, XLAL_EIO );
176
177 // Read repetition index
178 XLAL_CHECK( XLALFITSHeaderReadUINT4( file, "reptidx", &itr->repetition_index ) == XLAL_SUCCESS, XLAL_EFUNC );
179 XLAL_CHECK( itr->repetition_index < itr->repetition_count, XLAL_EIO );
180
181 // Read progress index
182 XLAL_CHECK( XLALFITSHeaderReadUINT8( file, "progidx", &itr->prog_index ) == XLAL_SUCCESS, XLAL_EFUNC );
183 XLAL_CHECK( itr->prog_index < itr->prog_count, XLAL_EIO );
184
185 return XLAL_SUCCESS;
186
187}
188
189///
190/// Advance to next state of iterator
191///
193 WeaveSearchIterator *itr,
194 BOOLEAN *iteration_complete,
195 BOOLEAN *expire_cache,
196 UINT8 *semi_index,
197 const gsl_vector **semi_rssky,
198 INT4 *semi_left,
199 INT4 *semi_right,
200 UINT4 *repetition_index
201)
202{
203
204 // Check input
205 XLAL_CHECK( itr != NULL, XLAL_EFAULT );
206 XLAL_CHECK( iteration_complete != NULL, XLAL_EFAULT );
207 XLAL_CHECK( expire_cache != NULL, XLAL_EFAULT );
208
209 // Initialise output flags
210 *iteration_complete = 0;
211 *expire_cache = 0;
212
213 while ( 1 ) {
214
215 // Get the next frequency block in the semicoherent tiling iterator
216 // - XLALNextLatticeTilingPoint() returns mid-point in non-iterated dimensions
217 const int itr_retn = XLALNextLatticeTilingPoint( itr->semi_itr, itr->semi_rssky );
218 XLAL_CHECK( itr_retn >= 0, XLAL_EFUNC );
219 if ( itr_retn == 0 ) {
220
221 // Move to the next partition
222 ++itr->partition_index;
223 if ( itr->partition_index == itr->partition_count ) {
224 itr->partition_index = 0;
225
226 // Move to the next repetition
227 ++itr->repetition_index;
228 if ( itr->repetition_index == itr->repetition_count ) {
229
230 // Iteration is complete
231 *iteration_complete = 1;
232 return XLAL_SUCCESS;
233
234 }
235
236 }
237
238 // Expire cache items from previous partitions/repetitions
239 *expire_cache = 1;
240
241 // Reset iterator over semicoherent tiling
243 XLAL_CHECK( XLALNextLatticeTilingPoint( itr->semi_itr, itr->semi_rssky ) > 0, XLAL_EFUNC );
244
245 }
246
247 // Partition iterator output, if requested
248 if ( itr->partition_count > 1 ) {
249 INT4 current_left = 0, current_right = 0;
250 XLAL_CHECK( XLALCurrentLatticeTilingBlock( itr->semi_itr, itr->partition_dim, &current_left, &current_right ) == XLAL_SUCCESS, XLAL_EINVAL );
251 const UINT4 current_max = current_right - current_left + 1;
252 XLAL_CHECK( current_max <= itr->partition_max, XLAL_ESIZE );
253 const UINT4 current_index = -current_left / itr->partition_size;
254 XLAL_CHECK( current_index < itr->partition_count, XLAL_ESIZE );
255 if ( current_index != itr->partition_index ) {
256 continue;
257 }
258 }
259
260 // Next frequency block has been found
261 break;
262
263 }
264
265 // Update iteration progress
266 ++itr->prog_index;
267
268 // Return iterator state
269 *semi_index = XLALCurrentLatticeTilingIndex( itr->semi_itr );
270 *semi_rssky = itr->semi_rssky;
271 XLAL_CHECK( XLALCurrentLatticeTilingBlock( itr->semi_itr, itr->ndim - 1, semi_left, semi_right ) == XLAL_SUCCESS, XLAL_EFUNC );
272 *repetition_index = itr->repetition_index;
273
274 return XLAL_SUCCESS;
275
276}
277
278///
279/// Return progress of iterator as a percentage
280///
282 const WeaveSearchIterator *itr
283)
284{
285
286 // Check input
287 XLAL_CHECK_REAL8( itr != NULL, XLAL_EFAULT );
288
289 // Return iteration progress
290 return GSL_MAX( 0.0, GSL_MIN( ( ( double ) itr->prog_index ) / itr->prog_count, 1.0 ) ) * 100.0;
291
292}
293
294///
295/// Return estimate of time remaining for iteration to complete,
296/// assuming a equal dstribution in computation cost over time
297///
299 const WeaveSearchIterator *itr,
300 const REAL8 elapsed_time
301)
302{
303
304 // Check input
305 XLAL_CHECK_REAL8( itr != NULL, XLAL_EFAULT );
306
307 // Return estimate of time remaining for iteration to complete,
308 return elapsed_time * ( itr->prog_count - itr->prog_index ) / itr->prog_index;
309
310}
311
312// Local Variables:
313// c-file-style: "linux"
314// c-basic-offset: 2
315// End:
int XLALFITSHeaderWriteUINT4(FITSFile UNUSED *file, const CHAR UNUSED *key, const UINT4 UNUSED value, const CHAR UNUSED *comment)
Definition: FITSFileIO.c:762
int XLALFITSHeaderWriteUINT8(FITSFile UNUSED *file, const CHAR UNUSED *key, const UINT8 UNUSED value, const CHAR UNUSED *comment)
Definition: FITSFileIO.c:827
int XLALFITSHeaderReadUINT8(FITSFile UNUSED *file, const CHAR UNUSED *key, UINT8 UNUSED *value)
Definition: FITSFileIO.c:863
int XLALFITSHeaderReadUINT4(FITSFile UNUSED *file, const CHAR UNUSED *key, UINT4 UNUSED *value)
Definition: FITSFileIO.c:797
int XLALWeaveSearchIteratorNext(WeaveSearchIterator *itr, BOOLEAN *iteration_complete, BOOLEAN *expire_cache, UINT8 *semi_index, const gsl_vector **semi_rssky, INT4 *semi_left, INT4 *semi_right, UINT4 *repetition_index)
Advance to next state of iterator.
REAL8 XLALWeaveSearchIteratorProgress(const WeaveSearchIterator *itr)
Return progress of iterator as a percentage.
void XLALWeaveSearchIteratorDestroy(WeaveSearchIterator *itr)
Destroy iterator.
int XLALWeaveSearchIteratorSave(const WeaveSearchIterator *itr, FITSFile *file)
Save state of iterator to a FITS file.
WeaveSearchIterator * XLALWeaveMainLoopSearchIteratorCreate(const LatticeTiling *semi_tiling, const UINT4 freq_partitions, const UINT4 f1dot_partitions)
Create iterator over the main loop search parameter space.
REAL8 XLALWeaveSearchIteratorRemainingTime(const WeaveSearchIterator *itr, const REAL8 elapsed_time)
Return estimate of time remaining for iteration to complete, assuming a equal dstribution in computat...
int XLALWeaveSearchIteratorRestore(WeaveSearchIterator *itr, FITSFile *file)
Restore state of iterator from a FITS file.
Module which implements iterators over search parameter spaces.
struct tagFITSFile FITSFile
Representation of a FITS file.
Definition: FITSFileIO.h:54
unsigned char BOOLEAN
uint64_t UINT8
double REAL8
uint32_t UINT4
int32_t INT4
void * XLALCalloc(size_t m, size_t n)
void XLALFree(void *p)
size_t XLALTotalLatticeTilingDimensions(const LatticeTiling *tiling)
Return the total number of dimensions of the lattice tiling.
UINT8 XLALTotalLatticeTilingPoints(const LatticeTilingIterator *itr)
Return the total number of points covered by the lattice tiling iterator.
int XLALSaveLatticeTilingIterator(const LatticeTilingIterator *itr, FITSFile *file, const char *name)
Save the state of a lattice tiling iterator to a FITS file.
int XLALLatticeTilingDimensionByName(const LatticeTiling *tiling, const char *bound_name)
Return the index of the lattice tiling dimension which has the given name.
UINT8 XLALCurrentLatticeTilingIndex(const LatticeTilingIterator *itr)
Return the index of the current point in the lattice tiling iterator.
int XLALRestoreLatticeTilingIterator(LatticeTilingIterator *itr, FITSFile *file, const char *name)
Restore the state of a lattice tiling iterator from a FITS file.
LatticeTilingIterator * XLALCreateLatticeTilingIterator(const LatticeTiling *tiling, const size_t itr_ndim)
Create a new lattice tiling iterator.
void XLALDestroyLatticeTilingIterator(LatticeTilingIterator *itr)
Destroy a lattice tiling iterator.
int XLALNextLatticeTilingPoint(LatticeTilingIterator *itr, gsl_vector *point)
Advance lattice tiling iterator, and optionally return the next point in point.
int XLALCurrentLatticeTilingBlock(const LatticeTilingIterator *itr, const size_t dim, INT4 *left, INT4 *right)
Return indexes of the left-most and right-most points in the current block of points in the given dim...
const LatticeTilingStats * XLALLatticeTilingStatistics(const LatticeTiling *tiling, const size_t dim)
Return statistics related to the number/value of lattice tiling points in a dimension.
int XLALResetLatticeTilingIterator(LatticeTilingIterator *itr)
Reset an iterator to the beginning of a lattice tiling.
#define XLAL_CHECK(assertion,...)
#define XLAL_CHECK_REAL8(assertion,...)
#define XLAL_CHECK_NULL(assertion,...)
XLAL_ENOMEM
XLAL_SUCCESS
XLAL_EFAULT
XLAL_EFUNC
XLAL_ESIZE
XLAL_EIO
XLAL_EINVAL
Statistics related to the number/value of lattice tiling points in a dimension.
Iterator over a search parameter space.
UINT4 partition_size
Number of points in each partition.
UINT4 partition_count
Number of partitions of iterator output.
UINT4 partition_max
Maximum number of points in the partitioned dimension.
UINT4 partition_index
Index of the current iterator output partition.
UINT4 repetition_count
Number of times to repeat iteration over semicoherent parameter space.
UINT8 prog_index
Progress index for iteration.
gsl_vector * semi_rssky
Current lattice tiling point.
UINT8 prog_count
Progress count for iteration.
LatticeTilingIterator * semi_itr
Iterator over semicoherent parameter space.
UINT4 repetition_index
Index of the current repetition.
size_t ndim
Number of parameter-space dimensions.
int partition_dim
Dimension in which to partition iterator output.