28#include <lal/StringInput.h>
29#include <lal/LALConstants.h>
30#include <lal/LALStdio.h>
31#include <lal/LogPrintf.h>
32#include <lal/HeapToplist.h>
45#define snprintf _snprintf
64#define min(a,b) ((a)<(b)?(a):(b))
68#define SYNC_FAIL_LIMIT 5
500 return ( snprintf( buf, buflen,
507 "%.13g %.13g %.13g %.5g %.7g %.7g %.7g\n",
520 return ( snprintf( buf, buflen,
523 "%.10f %.10f %.10g %.10f %.10g %.5f %.10g %.10g %.10g\n",
546 if ( length >
sizeof( linebuf ) - 1 ) {
551 for (
i = 0;
i < length;
i++ ) {
552 *checksum += linebuf[
i];
568 if ( length >
sizeof( linebuf ) - 1 ) {
573 for (
i = 0;
i < length;
i++ ) {
574 *checksum += linebuf[
i];
650 errno, strerror( errno ) );
672 errno, strerror( errno ) );
708#define TEMP_EXT ".tmp"
710 tempname = (
char * )malloc(
s );
718 fpnew = fopen( tempname,
"wb" );
721 tempname, errno, strerror( errno ) );
730 if ( ( write_done ) && ( length >= 0 ) ) {
732 ret =
fprintf( fpnew,
"%%DONE\n" );
744 tempname, errno, strerror( errno ) );
752 if ( rename( tempname,
filename ) ) {
754 filename, errno, strerror( errno ) );
774#define TEMP_EXT ".tmp"
776 tempname = (
char * )malloc(
s );
784 fpnew = fopen( tempname,
"wb" );
787 tempname, errno, strerror( errno ) );
796 if ( ( write_done ) && ( length >= 0 ) ) {
798 ret =
fprintf( fpnew,
"%%DONE\n" );
810 tempname, errno, strerror( errno ) );
818 if ( rename( tempname,
filename ) ) {
820 filename, errno, strerror( errno ) );
863#define __func__ __FUNCTION__
870#define LOGIOERROR(mess,filename) \
871 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): doserr:%d, errno:%d: %s\n",\
872 mess,filename,__func__,__FILE__,__LINE__,_doserrno,errno,strerror(errno))
874#define LOGIOERROR(mess,filename) \
875 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): errno:%d: %s\n",\
876 mess,filename,__func__,__FILE__,__LINE__,errno,strerror(errno))
882#define TMP_EXT ".tmp"
887 static UINT4 sync_fail_counter = 0;
891 tmpfilename =
LALCalloc( len,
sizeof(
char ) );
892 if ( !tmpfilename ) {
897 strcat( tmpfilename,
TMP_EXT );
901 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
902 checksum += *( ( (
char * ) & ( tl->
elems ) ) + len );
904 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
905 checksum += *( ( (
char * )tl->
data ) + len );
907 for ( len = 0; len <
sizeof( counter ); len++ ) {
908 checksum += *( ( (
char * )&counter ) + len );
912 fp = fopen( tmpfilename,
"wb" );
919 len = fwrite( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
921 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
923 if ( fclose(
fp ) ) {
924 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
931 if ( len != tl->
elems ) {
932 LOGIOERROR(
"Couldn't write data to", tmpfilename );
934 if ( fclose(
fp ) ) {
935 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
941 len = fwrite( &counter,
sizeof( counter ), 1,
fp );
943 LOGIOERROR(
"Couldn't write counter to", tmpfilename );
945 if ( fclose(
fp ) ) {
946 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
952 len = fwrite( &checksum,
sizeof( checksum ), 1,
fp );
954 LOGIOERROR(
"Couldn't write checksum to", tmpfilename );
956 if ( fclose(
fp ) ) {
957 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
964 if ( fsync( fileno(
fp ) ) ) {
971 sync_fail_counter = 0;
976 if ( fclose(
fp ) ) {
982 if ( rename( tmpfilename,
filename ) ) {
983 LOGIOERROR(
"Couldn't rename\n", tmpfilename );
1004 if ( errno == ENOENT ) {
1016 len = fread( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
1020 if ( fclose(
fp ) ) {
1028 "Number of elements read larger than length of toplist: %zu > %zu\n",
1030 if ( fclose(
fp ) ) {
1038 if ( len != tl->
elems ) {
1041 if ( fclose(
fp ) ) {
1049 len = fread( counter,
sizeof( *counter ), 1,
fp );
1053 if ( fclose(
fp ) ) {
1061 len = fread( &checksum,
sizeof( checksum ), 1,
fp );
1065 if ( fclose(
fp ) ) {
1073 if ( fclose(
fp ) ) {
1080 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
1081 checksum -= *( ( (
char * ) & ( tl->
elems ) ) + len );
1083 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
1084 checksum -= *( ( (
char * )tl->
data ) + len );
1086 for ( len = 0; len <
sizeof( *counter ); len++ ) {
1087 checksum -= *( ( (
char * )counter ) + len );
1096 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.