Skip to content

Commit

Permalink
Remove usages of printf-based outputting
Browse files Browse the repository at this point in the history
There were a few places (and there still are, just outside the important
code paths) where printf was being used to output data. This commit
removes the important ones replacing them with logging. I also took the
chance to centralise some minor duplicated code, plus removing code that
isn't used at all.

Signed-off-by: Rodrigo Tobar <[email protected]>
  • Loading branch information
rtobar committed Nov 26, 2020
1 parent 957fc22 commit cb4336d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 63 deletions.
78 changes: 23 additions & 55 deletions src/hdfitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#ifndef HDFITEMS_H
#define HDFITEMS_H

#include "hdf5.h"
#include <string>

#include <hdf5.h>

#include "logging.h"


///\name ILLUSTRIS specific constants
Expand Down Expand Up @@ -325,39 +329,6 @@ static inline hid_t HDF5OpenDataSpace(const hid_t &id){
}


static inline int whatisopen(hid_t fid) {
ssize_t cnt;
int howmany;
int i;
H5I_type_t ot;
hid_t anobj;
hid_t *objs;
char name[1024];
herr_t status;

cnt = H5Fget_obj_count(fid, H5F_OBJ_ALL);

if (cnt <= 0) return cnt;

printf("%zd object(s) open\n", cnt);

objs = new hid_t[cnt];

howmany = H5Fget_obj_ids(fid, H5F_OBJ_ALL, cnt, objs);

printf("open objects:\n");

for (i = 0; i < howmany; i++ ) {
anobj = objs[i];
ot = H5Iget_type(anobj);
status = H5Iget_name(anobj, name, 1024);
printf(" %d: type %d, name %s\n",i,ot,name);
}
delete[] objs;
return howmany;
}


static inline void HDF5CloseFile(hid_t &id){
if (id>=0) H5Fclose(id);
id = -1;
Expand Down Expand Up @@ -1481,19 +1452,25 @@ inline void HDFSetUsedParticleTypes(Options &opt, int &nusetypes, int &nbusetype
}
//@}

static std::string find_hdf5_file(const char *prefix)
{
for (auto *suffix : {".0.hdf5", ".hdf5"}) {
std::string filename(prefix);
filename += suffix;
LOG(debug) << "Looking for " << filename;
if (FileExists(filename.c_str())) {
return filename;
}
}
LOG(error) << "Can't find HDF5 file with prefix " << prefix;
exit(9);
}

/// \name Get the number of particles in the hdf files
//@{
inline Int_t HDF_get_nbodies(char *fname, int ptype, Options &opt)
{
char buf[2000],buf1[2000],buf2[2000];
sprintf(buf1,"%s.0.hdf5",fname);
sprintf(buf2,"%s.hdf5",fname);
if (FileExists(buf1)) sprintf(buf,"%s",buf1);
else if (FileExists(buf2)) sprintf(buf,"%s",buf2);
else {
printf("Error. Can't find snapshot!\nneither as `%s'\nnor as `%s'\n\n", buf1, buf2);
exit(9);
}
auto buf = find_hdf5_file(fname);

//H5File Fhdf;
hid_t Fhdf;
Expand Down Expand Up @@ -1529,7 +1506,7 @@ inline Int_t HDF_get_nbodies(char *fname, int ptype, Options &opt)

//Open the specified file and the specified dataset in the file.
//Fhdf.openFile(buf, H5F_ACC_RDONLY);
Fhdf = H5Fopen(buf, H5F_ACC_RDONLY, H5P_DEFAULT);
Fhdf = H5Fopen(buf.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
LOG(info) << "Loading HDF header info in header group: " << hdf_gnames.Header_name;

if(opt.ihdfnameconvention == HDFSWIFTEAGLENAMES || opt.ihdfnameconvention == HDFOLDSWIFTEAGLENAMES) {
Expand Down Expand Up @@ -1701,20 +1678,11 @@ inline Int_t HDF_get_nbodies(char *fname, int ptype, Options &opt)
//@}



/// \name Get the number of hdf files per snapshot
//@{
inline Int_t HDF_get_nfiles(char *fname, int ptype)
{
char buf[2000],buf1[2000],buf2[2000];
sprintf(buf1,"%s.0.hdf5",fname);
sprintf(buf2,"%s.hdf5",fname);
if (FileExists(buf1)) sprintf(buf,"%s",buf1);
else if (FileExists(buf2)) sprintf(buf,"%s",buf2);
else {
printf("Error. Can't find snapshot!\nneither as `%s'\nnor as `%s'\n\n", buf1, buf2);
exit(9);
}
auto buf = find_hdf5_file(fname);

//H5File Fhdf;
hid_t Fhdf;
Expand All @@ -1739,7 +1707,7 @@ inline Int_t HDF_get_nfiles(char *fname, int ptype)

//Open the specified file and the specified dataset in the file.
//Fhdf.openFile(buf, H5F_ACC_RDONLY);
Fhdf = H5Fopen(buf, H5F_ACC_RDONLY, H5P_DEFAULT);
Fhdf = H5Fopen(buf.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
//get header group
hdf_header_info.num_files = read_attribute<int>(Fhdf, hdf_header_info.names[hdf_header_info.INumFiles]);
}
Expand Down
22 changes: 16 additions & 6 deletions src/localbgcomp.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ if (nbodies > ompsubsearchnum)
}


#define LOG_STATS(meanr, sdlow, sdhigh) \
LOG(trace) << "Using meanr=" << std::scientific << meanr \
<< " sdlow=" << std::scientific << sdlow \
<< " sdhigh=" << std::scientific << sdhigh;

void DetermineDenVRatioDistribution(Options &opt,const Int_t nbodies, Particle *Part, Double_t &meanr,Double_t &sdlow,Double_t &sdhigh, int sublevel)
{
Int_t i,nbins,iprob,jprob;
Expand Down Expand Up @@ -221,7 +226,7 @@ void DetermineDenVRatioDistribution(Options &opt,const Int_t nbodies, Particle *

//if object is small or bg search (ie sublevel==-1, then to keep statistics high, use preliminary determination of the variance and mean.
if (nbodies<2*MINSUBSIZE) {
if (opt.iverbose>=2) printf("Using meanr=%e sdlow=%e sdhigh=%e\n",meanr,sdlow,sdhigh);
LOG_STATS(meanr, sdlow, sdhigh);
return;
}
//now rebin around most probable over sl in either direction to be used to estimate dispersion
Expand Down Expand Up @@ -303,7 +308,7 @@ void DetermineDenVRatioDistribution(Options &opt,const Int_t nbodies, Particle *
sdhigh=sdlow;
//again, if number of particles is low (and so bin statisitics is poor) use initial estimate
if (nbodies<16*MINSUBSIZE||sublevel==-1) {
if (opt.iverbose>=2) printf("Using meanr=%e sdlow=%e sdhigh=%e\n",meanr,sdlow,sdhigh);
LOG_STATS(meanr, sdlow, sdhigh);
return;
}

Expand Down Expand Up @@ -343,7 +348,8 @@ void DetermineDenVRatioDistribution(Options &opt,const Int_t nbodies, Particle *
fixp[itemp][0]=1;fixp[itemp][1]=0;fixp[itemp][2]=0;fixp[itemp][3]=0;itemp++;
fixp[itemp][0]=0;fixp[itemp][1]=0;fixp[itemp][2]=0;fixp[itemp][3]=0;itemp++;

if (opt.iverbose>=2) printf("Initial estimate: mu=%e var=%e \n",params[1],sqrt(params[2]));
LOG(trace) << "Initial estimate: mu=" << std::scientific << params[1]
<< " var=" << std::scientific << std::sqrt(params[2]);
nfits=8;
oldchi2=MAXVALUE;
for (int i=0;i<nfits;i++) {
Expand All @@ -356,16 +362,20 @@ void DetermineDenVRatioDistribution(Options &opt,const Int_t nbodies, Particle *
meanr=params[1];sdlow=sqrt(params[2]*params[3]);sdhigh=sqrt(params[2]);
nfix=0;for (int j=0;j<nparams;j++) nfix+=(fixp[i][j]==1);
oldchi2=chi2;
if(opt.iverbose>2) printf("chi2/dof=%e/%lld, A=%e mu=%e var=%e s=%e\n",chi2,nbins-(nparams-nfix)-1,params[0],params[1],sqrt(params[2]),sqrt(params[3]));
LOG(trace) << "chi2/dof=" << std::scientific << chi2 << "/" << nbins - (nparams - nfix) - 1
<< ", A=" << std::scientific << params[0]
<< " mu=" << std::scientific << params[1]
<< " var=" << std::scientific << std::sqrt(params[2])
<< " s=" << std::scientific << std::sqrt(params[3]);
}
//else if (oldchi2<chi2 && i>0) break;
else {
if (opt.iverbose>2)printf("fit failed, using previous values\n");
LOG(trace) << "Fit failed, using previous values";
params[0]=maxprob;params[1]=meanr;params[2]=sdhigh*sdhigh;params[3]=(sdlow*sdlow)/(sdhigh*sdhigh);
}
}
delete[] difffuncs;
if (opt.iverbose>=2) printf("Using meanr=%e sdlow=%e sdhigh=%e\n",meanr,sdlow,sdhigh);
LOG_STATS(meanr, sdlow, sdhigh);
}

/*! Calculates the normalized deviations from the mean of the dominated population.
Expand Down
14 changes: 12 additions & 2 deletions src/localfield.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ private(i,tid)
}

fracdone[tid]++;
if (opt.iverbose) if (fracdone[tid]>fraclim[tid]) {printf("Task %d done %e of its share \n",tid,fracdone[tid]/((double)nbodies/(double)nthreads));fraclim[tid]+=addamount;}
if (fracdone[tid] > fraclim[tid]) {
LOG(debug) << "Task " << tid << " has done " << std::scientific
<< fracdone[tid] / (double(nbodies) / nthreads)
<< " of its share";
fraclim[tid] += addamount;
}
#ifdef STRUCDEN
}
#endif
Expand Down Expand Up @@ -397,7 +402,12 @@ private(i,tid)
#endif
Part[i].SetDensity(tree->CalcVelDensityParticle(i,opt.Nvel,opt.Nsearch,1,pqx[tid],pqv[tid],&nnids[tid*opt.Nsearch],&nnr2[tid*opt.Nsearch]));
fracdone[tid]++;
if (opt.iverbose) if (fracdone[tid]>fraclim[tid]) {printf("Task %d done %e of its share \n",tid,fracdone[tid]/((double)nbodies/(double)nthreads));fraclim[tid]+=addamount;}
if (fracdone[tid] > fraclim[tid]) {
LOG(debug) << "Task " << tid << " has done " << std::scientific
<< fracdone[tid] / (double(nbodies) / nthreads)
<< " of its share";
fraclim[tid] += addamount;
}
}
#ifdef USEOPENMP
}
Expand Down

0 comments on commit cb4336d

Please sign in to comment.