27 #include <lal/StringInput.h>
28 #include <lal/LALConstants.h>
29 #include <lal/LALStdio.h>
30 #include <lal/LogPrintf.h>
31 #include <lal/HeapToplist.h>
44 #define snprintf _snprintf
48 #define fileno _fileno
52 #define finite _finite
71 #define min(a,b) ((a)<(b)?(a):(b))
75 #define SYNC_FAIL_LIMIT 5
507 return ( snprintf( buf, buflen,
514 "%.13g %.13g %.13g %.5g %.7g %.7g %.7g\n",
527 return ( snprintf( buf, buflen,
530 "%.10f %.10f %.10g %.10f %.10g %.5f %.10g %.10g %.10g\n",
553 if ( length >
sizeof( linebuf ) - 1 ) {
558 for (
i = 0;
i < length;
i++ ) {
559 *checksum += linebuf[
i];
575 if ( length >
sizeof( linebuf ) - 1 ) {
580 for (
i = 0;
i < length;
i++ ) {
581 *checksum += linebuf[
i];
657 errno, strerror( errno ) );
679 errno, strerror( errno ) );
715 #define TEMP_EXT ".tmp"
717 tempname = (
char * )malloc(
s );
725 fpnew = fopen( tempname,
"wb" );
728 tempname, errno, strerror( errno ) );
737 if ( ( write_done ) && ( length >= 0 ) ) {
739 ret =
fprintf( fpnew,
"%%DONE\n" );
751 tempname, errno, strerror( errno ) );
759 if ( rename( tempname,
filename ) ) {
761 filename, errno, strerror( errno ) );
781 #define TEMP_EXT ".tmp"
783 tempname = (
char * )malloc(
s );
791 fpnew = fopen( tempname,
"wb" );
794 tempname, errno, strerror( errno ) );
803 if ( ( write_done ) && ( length >= 0 ) ) {
805 ret =
fprintf( fpnew,
"%%DONE\n" );
817 tempname, errno, strerror( errno ) );
825 if ( rename( tempname,
filename ) ) {
827 filename, errno, strerror( errno ) );
870 #define __func__ __FUNCTION__
877 #define LOGIOERROR(mess,filename) \
878 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): doserr:%d, errno:%d: %s\n",\
879 mess,filename,__func__,__FILE__,__LINE__,_doserrno,errno,strerror(errno))
881 #define LOGIOERROR(mess,filename) \
882 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): errno:%d: %s\n",\
883 mess,filename,__func__,__FILE__,__LINE__,errno,strerror(errno))
889 #define TMP_EXT ".tmp"
894 static UINT4 sync_fail_counter = 0;
898 tmpfilename =
LALCalloc( len,
sizeof(
char ) );
899 if ( !tmpfilename ) {
904 strcat( tmpfilename,
TMP_EXT );
908 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
909 checksum += *( ( (
char * ) & ( tl->
elems ) ) + len );
911 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
912 checksum += *( ( (
char * )tl->
data ) + len );
914 for ( len = 0; len <
sizeof( counter ); len++ ) {
915 checksum += *( ( (
char * )&counter ) + len );
919 fp = fopen( tmpfilename,
"wb" );
926 len = fwrite( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
928 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
930 if ( fclose(
fp ) ) {
931 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
938 if ( len != tl->
elems ) {
939 LOGIOERROR(
"Couldn't write data to", tmpfilename );
941 if ( fclose(
fp ) ) {
942 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
948 len = fwrite( &counter,
sizeof( counter ), 1,
fp );
950 LOGIOERROR(
"Couldn't write counter to", tmpfilename );
952 if ( fclose(
fp ) ) {
953 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
959 len = fwrite( &checksum,
sizeof( checksum ), 1,
fp );
961 LOGIOERROR(
"Couldn't write checksum to", tmpfilename );
963 if ( fclose(
fp ) ) {
964 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
971 if ( fsync( fileno(
fp ) ) ) {
978 sync_fail_counter = 0;
983 if ( fclose(
fp ) ) {
989 if ( rename( tmpfilename,
filename ) ) {
990 LOGIOERROR(
"Couldn't rename\n", tmpfilename );
1011 if ( errno == ENOENT ) {
1023 len = fread( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
1027 if ( fclose(
fp ) ) {
1035 "Number of elements read larger than length of toplist: %zu > %zu\n",
1037 if ( fclose(
fp ) ) {
1045 if ( len != tl->
elems ) {
1048 if ( fclose(
fp ) ) {
1056 len = fread( counter,
sizeof( *counter ), 1,
fp );
1060 if ( fclose(
fp ) ) {
1068 len = fread( &checksum,
sizeof( checksum ), 1,
fp );
1072 if ( fclose(
fp ) ) {
1080 if ( fclose(
fp ) ) {
1087 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
1088 checksum -= *( ( (
char * ) & ( tl->
elems ) ) + len );
1090 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
1091 checksum -= *( ( (
char * )tl->
data ) + len );
1093 for ( len = 0; len <
sizeof( *counter ); len++ ) {
1094 checksum -= *( ( (
char * )counter ) + len );
1103 for ( len = 0; len < tl->
elems; len++ ) {
void sort_crossCorrBinary_toplist(toplist_t *l)
static int crossCorr_toplist_qsort_function(const void *a, const void *b)
int write_crossCorr_toplist_item_to_fp(CrossCorrOutputEntry fline, FILE *fp, UINT4 *checksum)
File IO.
static void reduce_crossCorrline_precision(void *line)
static void reduce_crossCorrBinary_toplist_precision(toplist_t *l)
int final_write_crossCorr_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
meant for the final writing of the toplist
static int crossCorrBinary_toplist_qsort_function(const void *a, const void *b)
static void reduce_crossCorr_toplist_precision(toplist_t *l)
int write_cc_checkpoint(const char *filename, toplist_t *tl, UINT4 counter, BOOLEAN do_sync)
writes a checkpoint:
int atomic_write_crossCorr_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
writes the given toplitst to a temporary file, then renames the temporary file to filename.
void sort_crossCorr_toplist(toplist_t *l)
reads a (created!) toplist from an open filepointer sets the checksum if non-NULL reads maximum maxby...
int atomic_write_crossCorrBinary_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
int write_crossCorrBinary_toplist_item_to_fp(CrossCorrBinaryOutputEntry fline, FILE *fp, UINT4 *checksum)
int write_crossCorr_toplist_to_fp(toplist_t *tl, FILE *fp, UINT4 *checksum)
Writes the toplist to an (already open) filepointer Returns the number of written charactes sets the ...
static int _atomic_write_crossCorrBinary_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum, int write_done)
void free_crossCorr_toplist(toplist_t **l)
frees the space occupied by the toplist
static int print_crossCorrBinaryline_to_str(CrossCorrBinaryOutputEntry fline, char *buf, int buflen)
int final_write_crossCorrBinary_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
int create_crossCorr_toplist(toplist_t **tl, UINT8 length)
creates a toplist with length elements, returns -1 on error (usually out of memory),...
int create_crossCorrBinary_toplist(toplist_t **tl, UINT8 length)
static int crossCorrBinary_smaller(const void *a, const void *b)
static void reduce_crossCorrBinaryline_precision(void *line)
static int _atomic_write_crossCorr_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum, int write_done)
int write_cc_output(const char *filename, toplist_t *tl)
write the final output file:
int insert_into_crossCorrBinary_toplist(toplist_t *tl, CrossCorrBinaryOutputEntry elem)
static int crossCorr_smaller(const void *a, const void *b)
static int print_crossCorrline_to_str(CrossCorrOutputEntry fline, char *buf, int buflen)
int read_cc_checkpoint(const char *filename, toplist_t *tl, UINT4 *counter)
tries to read a checkpoint
int insert_into_crossCorr_toplist(toplist_t *tl, CrossCorrOutputEntry elem)
Inserts an element in to the toplist either if there is space left or the element is larger than the ...
#define LOGIOERROR(mess, filename)
int write_crossCorrBinary_toplist_to_fp(toplist_t *tl, FILE *fp, UINT4 *checksum)
int create_toplist(toplist_t **list, size_t length, size_t size, int(*smaller)(const void *, const void *))
void free_toplist(toplist_t **list)
void qsort_toplist(toplist_t *list, int(*compare)(const void *, const void *))
void qsort_toplist_r(toplist_t *list, int(*compare)(const void *, const void *))
int insert_into_toplist(toplist_t *list, void *element)
void go_through_toplist(toplist_t *list, void(*handle)(void *))
void clear_toplist(toplist_t *list)
#define XLAL_LAST_ELEM(x)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
Type to hold the fields that will be kept in a "toplist" – for a directed binary search.
REAL8 argp
argument of periapse
REAL8 estSens
average template E[rho]/h0^2)^2
REAL8 evSquared
E[rho]/h0^2)^2.
REAL8 tp
time of periapse passage
REAL8 rho
Crosscorr statistic.
REAL8 asini
projected semi-major axis
Type to hold the fields that will be kept in a "toplist"
REAL8 BrakingIndex
braking index
REAL8 Rho
Crosscorr statistic.
REAL8 Delta
skyposition: latitude
REAL8 Alpha
Skyposition: longitude in equatorial coords, radians.