Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory tracking and garbage collection to CLASS v3.2.0 #583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ endif
%.o: %.c .base $(HEADERFILES)
cd $(WRKDIR);$(CC) $(OPTFLAG) $(OMPFLAG) $(CCFLAG) $(INCLUDES) -c ../$< -o $*.o

TOOLS = growTable.o dei_rkck.o sparse.o evolver_rkck.o evolver_ndf15.o arrays.o parser.o quadrature.o hyperspherical.o common.o trigonometric_integrals.o
TOOLS = growTable.o dei_rkck.o sparse.o evolver_rkck.o evolver_ndf15.o arrays.o parser.o quadrature.o hyperspherical.o common.o trigonometric_integrals.o alloc_track.o

SOURCE = input.o background.o thermodynamics.o perturbations.o primordial.o fourier.o transfer.o harmonic.o lensing.o distortions.o

Expand Down
2 changes: 1 addition & 1 deletion default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ sd_detector_name = PIXIE # Name of the detector
overwrite_root = no # Overwrite the output files?
write_background = no # Write background parameter table
write_thermodynamics = no # Write thermodynamics parameter table
#k_output_values = 1e-3,1e-2 # Write perturbations parameter table (at given k)
k_output_values = 1e-3,1e-2 # Write perturbations parameter table (at given k)
write_primordial = no # Write primordial parameter table
write_parameters = yeap # Write used/unused parameter files
write_warnings = yes # Warn about forgotten/wrong inputs
Expand Down
33 changes: 17 additions & 16 deletions external/HyRec2020/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "history.h"
#include "helium.h"
#include "alloc_track.h"

/*************************************************************************************
Hubble expansion rate in sec^-1.
Expand Down Expand Up @@ -74,7 +75,7 @@ void hyrec_init() {
rec_data.path_to_hyrec = "";
hyrec_allocate(&rec_data, zmax, zmin);
chdir(buffer);
free(buffer);
tracked_free(buffer);
}

void rec_build_history_camb_(const double* OmegaC, const double* OmegaB, const double* h0inp, const double* tcmb,
Expand Down Expand Up @@ -160,7 +161,7 @@ double hyrec_xe_(double* a, double *xe){
double loga = log(*a);
int error; /* error and error_message are meaningless here */
char *error_message;
error_message = malloc(SIZE_ErrorM);
error_message = tracked_malloc(SIZE_ErrorM);
if (loga < logstart) return xe[0];
return rec_interp1d(logstart, dlna, xe, Nz, loga, &error, error_message);
}
Expand Down Expand Up @@ -934,23 +935,23 @@ void hyrec_allocate(HYREC_DATA *data, double zmax, double zmin) {
else DLNA = DLNA_HYREC;

data->error = 0;
data->error_message=malloc(SIZE_ErrorM);
data->error_message=tracked_malloc(SIZE_ErrorM);
sprintf(data->error_message, "\n**** ERROR HAS OCCURRED in HYREC-2 ****\n");

data->zmax = (zmax > 3000.? zmax : 3000.);
data->zmin = zmin;

data->atomic = (HYREC_ATOMIC *) malloc(sizeof(HYREC_ATOMIC));
data->atomic = (HYREC_ATOMIC *) tracked_malloc(sizeof(HYREC_ATOMIC));
allocate_and_read_atomic(data->atomic, &data->error, data->path_to_hyrec, data->error_message);

data->fit = (FIT_FUNC *) malloc(sizeof(FIT_FUNC));
data->fit = (FIT_FUNC *) tracked_malloc(sizeof(FIT_FUNC));
allocate_and_read_fit(data->fit, &data->error, data->path_to_hyrec, data->error_message);

data->cosmo = (REC_COSMOPARAMS *) malloc(sizeof(REC_COSMOPARAMS));
data->cosmo->inj_params = (INJ_PARAMS *) malloc(sizeof(INJ_PARAMS));
data->cosmo = (REC_COSMOPARAMS *) tracked_malloc(sizeof(REC_COSMOPARAMS));
data->cosmo->inj_params = (INJ_PARAMS *) tracked_malloc(sizeof(INJ_PARAMS));

data->Nz = (long int) (log((1.+zmax)/(1.+zmin))/DLNA) + 2;
data->rad = (RADIATION *) malloc(sizeof(RADIATION));
data->rad = (RADIATION *) tracked_malloc(sizeof(RADIATION));

// For now assume that radiation field never needed over more than 1 decade in redshift
// (typically from z ~ 1700 to 800 for recombination history)
Expand All @@ -964,16 +965,16 @@ void hyrec_allocate(HYREC_DATA *data, double zmax, double zmin) {

void hyrec_free(HYREC_DATA *data) {
free_atomic(data->atomic);
free(data->cosmo->inj_params);
free(data->cosmo);
free(data->atomic);
free(data->xe_output);
free(data->Tm_output);
free(data->error_message);
tracked_free(data->cosmo->inj_params);
tracked_free(data->cosmo);
tracked_free(data->atomic);
tracked_free(data->xe_output);
tracked_free(data->Tm_output);
tracked_free(data->error_message);
if (MODEL == FULL) free_radiation(data->rad);
free(data->rad);
tracked_free(data->rad);
free_fit(data->fit);
free(data->fit);
tracked_free(data->fit);
}

/******************************************************************
Expand Down
38 changes: 19 additions & 19 deletions external/HyRec2020/hydrogen.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include "hyrectools.h"
#include "hydrogen.h"

#include "alloc_track.h"

/***********************************************************************************************************
Some constants appropriately rescaled for different values of the fine-structure constant and electron mass
Expand Down Expand Up @@ -120,7 +120,7 @@ void allocate_and_read_atomic(HYREC_ATOMIC *atomic, int *error, char *path_to_hy

char *alpha_file, *rr_file, *twog_file;
char sub_message[128];
alpha_file = malloc(SIZE_InputFile);
alpha_file = tracked_malloc(SIZE_InputFile);
alpha_file[0] = 0;
strcat(alpha_file, path_to_hyrec);
strcat(alpha_file, ALPHA_FILE);
Expand All @@ -133,7 +133,7 @@ void allocate_and_read_atomic(HYREC_ATOMIC *atomic, int *error, char *path_to_hy
return;
}

rr_file = malloc(SIZE_InputFile);
rr_file = tracked_malloc(SIZE_InputFile);
rr_file[0] = 0;
strcat(rr_file, path_to_hyrec);
strcat(rr_file, RR_FILE);
Expand Down Expand Up @@ -187,7 +187,7 @@ void allocate_and_read_atomic(HYREC_ATOMIC *atomic, int *error, char *path_to_hy
double L2s1s_current;
int fscanf_counter;

twog_file = malloc(SIZE_InputFile);
twog_file = tracked_malloc(SIZE_InputFile);
twog_file[0] = 0;
strcat(twog_file, path_to_hyrec);
strcat(twog_file, TWOG_FILE);
Expand Down Expand Up @@ -244,9 +244,9 @@ void allocate_and_read_atomic(HYREC_ATOMIC *atomic, int *error, char *path_to_hy
for (b = 0; b < NVIRT; b++) atomic->A1s_tab[b] = 0;
#endif

free(alpha_file);
free(rr_file);
free(twog_file);
tracked_free(alpha_file);
tracked_free(rr_file);
tracked_free(twog_file);

}

Expand All @@ -273,7 +273,7 @@ void allocate_and_read_fit(FIT_FUNC *fit, int *error, char *path_to_hyrec, char

/*********** Effective rates *************/
char *fit_file;
fit_file = malloc(SIZE_InputFile);
fit_file = tracked_malloc(SIZE_InputFile);
fit_file[0] = 0;
strcat(fit_file, path_to_hyrec);
strcat(fit_file, FIT_FILE);
Expand Down Expand Up @@ -305,7 +305,7 @@ void allocate_and_read_fit(FIT_FUNC *fit, int *error, char *path_to_hyrec, char
}
}
fclose(fA);
free(fit_file);
tracked_free(fit_file);
}


Expand All @@ -315,7 +315,7 @@ Free the memory for rate tables.

void free_fit(FIT_FUNC *fit){
unsigned j;
for (j = 0; j < 5; j++) free(fit->swift_func[j]);
for (j = 0; j < 5; j++) tracked_free(fit->swift_func[j]);
}

/************************************************************************************************
Expand Down Expand Up @@ -728,8 +728,8 @@ void populateTS_2photon(double Trr[2][2], double *Trv[2], double *Tvr[2], double

}

free(Aup);
free(Adn);
tracked_free(Aup);
tracked_free(Adn);
}

/*********************************************************************
Expand Down Expand Up @@ -759,8 +759,8 @@ void solveTXeqB(double *diag, double *updiag, double *dndiag, double *X, double
X[N-1] = gamma[N-1];
for (i = N-2; i >= 0; i--) X[i] = gamma[i] - alpha[i] * X[i+1];

free(alpha);
free(gamma);
tracked_free(alpha);
tracked_free(gamma);
}

/**************************************************************************************************************
Expand Down Expand Up @@ -818,8 +818,8 @@ void solve_real_virt(double xr[2], double xv[NVIRT], double Trr[2][2], double *T
for (b = 0; b < NVIRT; b++) xv[b] = Tvv_inv_sv[b] - Tvv_inv_Tvr[0][b]*xr[0] - Tvv_inv_Tvr[1][b]*xr[1];

/** Free memory **/
for (i = 0; i < 2; i++) free(Tvv_inv_Tvr[i]);
free(Tvv_inv_sv);
for (i = 0; i < 2; i++) tracked_free(Tvv_inv_Tvr[i]);
tracked_free(Tvv_inv_sv);

}

Expand Down Expand Up @@ -1016,9 +1016,9 @@ double rec_HMLA_2photon_dxHIIdlna(HYREC_DATA *data, double xe, double xHII, doub
/* Average radiation field in each bin */
for (b = 0; b < NVIRT; b++) Dfnu_hist[b][iz] = xv[b]/x1s;

for (i = 0; i < 2; i++) free(Trv[i]);
for (i = 0; i < 2; i++) free(Tvr[i]);
for (i = 0; i < 3; i++) free(Tvv[i]);
for (i = 0; i < 2; i++) tracked_free(Trv[i]);
for (i = 0; i < 2; i++) tracked_free(Tvr[i]);
for (i = 0; i < 3; i++) tracked_free(Tvv[i]);

return dxedlna;

Expand Down
13 changes: 7 additions & 6 deletions external/HyRec2020/hyrectools.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ January 2015 - added cubic interpolation for non-evenly spaced table)
#include <string.h>

#include "hyrectools.h"
#include "alloc_track.h"


/******************************************************************************************************
Expand All @@ -30,7 +31,7 @@ Creates a [n1] array.

double *create_1D_array(unsigned n1, int *error, char error_message[SIZE_ErrorM]){

double *matrix = (double *) calloc(n1, sizeof(double));
double *matrix = (double *) tracked_calloc(n1, sizeof(double));
char sub_message[128];
if (*error == 1) return matrix;

Expand All @@ -49,7 +50,7 @@ Creates a [n1][n2] array.
double **create_2D_array(unsigned n1, unsigned n2, int *error, char error_message[SIZE_ErrorM]){

unsigned i;
double **matrix = (double **) calloc(n1, sizeof(double *));
double **matrix = (double **) tracked_calloc(n1, sizeof(double *));
char sub_message[128];
if (*error == 1) return matrix;

Expand All @@ -70,9 +71,9 @@ Frees the memory of a [n1][] array.
void free_2D_array(double **matrix, unsigned n1){

unsigned i;
for (i = 0; i < n1; i++) free(matrix[i]);
for (i = 0; i < n1; i++) tracked_free(matrix[i]);

free(matrix);
tracked_free(matrix);
}

/*********************************************************************************
Expand All @@ -82,7 +83,7 @@ Creates a [n1][n2][n3] matrix.
double ***create_3D_array(unsigned n1, unsigned n2, unsigned n3, int *error, char error_message[SIZE_ErrorM]){

unsigned i;
double ***matrix = (double ***) calloc(n1, sizeof(double **));
double ***matrix = (double ***) tracked_calloc(n1, sizeof(double **));
char sub_message[128];
if (*error == 1) return matrix;

Expand All @@ -104,7 +105,7 @@ void free_3D_array(double ***matrix, unsigned n1, unsigned n2) {
unsigned i;
for (i = 0; i < n1; i++) free_2D_array(matrix[i], n2);

free(matrix);
tracked_free(matrix);
}

/********************************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion external/HyRec2020/wrap_hyrec.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int thermodynamics_hyrec_free(struct thermohyrec* phy){

/* We just need to free hyrec (without error management) */
hyrec_free(phy->data);
free(phy->data);
class_free(phy->data);

return _SUCCESS_;
}
Expand Down
32 changes: 16 additions & 16 deletions external/heating/injection.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,42 +212,42 @@ int injection_free(struct thermodynamics* pth){
int index_inj, index_dep;

/* Redshift */
free(pin->z_table);
class_free(pin->z_table);

/* Energy injection */
for(index_inj=0;index_inj<pin->inj_size;++index_inj){
free(pin->injection_table[index_inj]);
class_free(pin->injection_table[index_inj]);
}
free(pin->injection_table);
class_free(pin->injection_table);

if(pin->has_PBH_eva==_TRUE_){
free(pin->PBH_table_z);
free(pin->PBH_table_mass);
free(pin->PBH_table_mass_dd);
free(pin->PBH_table_F);
free(pin->PBH_table_F_dd);
class_free(pin->PBH_table_z);
class_free(pin->PBH_table_mass);
class_free(pin->PBH_table_mass_dd);
class_free(pin->PBH_table_F);
class_free(pin->PBH_table_F_dd);
}

/* Injection efficiency */
if(pin->f_eff_type == f_eff_from_file){
free(pin->feff_table);
class_free(pin->feff_table);
}
if(pin->chi_type == chi_from_z_file){
free(pin->chiz_table);
class_free(pin->chiz_table);
}
if(pin->chi_type == chi_from_x_file || pin->chi_type == chi_Galli_file){
free(pin->chix_table);
class_free(pin->chix_table);
}

/* Deposition function */
free(pin->chi);
class_free(pin->chi);

/* Energy deposition */
for(index_dep=0;index_dep<pin->dep_size;++index_dep){
free(pin->deposition_table[index_dep]);
class_free(pin->deposition_table[index_dep]);
}
free(pin->deposition_table);
free(pin->pvecdeposition);
class_free(pin->deposition_table);
class_free(pin->pvecdeposition);

return _SUCCESS_;
}
Expand Down Expand Up @@ -874,7 +874,7 @@ int injection_rate_PBH_evaporation_mass_evolution(struct background * pba,
}

/** - Free local variables */
free(pvecback_loop);
class_free(pvecback_loop);

/** - Spline mass and F(M) evolution in z */
class_call(array_spline_table_lines(pin->PBH_table_z,
Expand Down
22 changes: 11 additions & 11 deletions external/heating/noninjection.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ int noninjection_init(struct precision* ppr,
}

/** - Free temporary variables */
free(pvecback);
free(pvecthermo);
class_free(pvecback);
class_free(pvecthermo);

return _SUCCESS_;
}
Expand All @@ -255,18 +255,18 @@ int noninjection_init(struct precision* ppr,
*/
int noninjection_free(struct noninjection* pni){

free(pni->z_table);
free(pni->photon_dep_table);
class_free(pni->z_table);
class_free(pni->photon_dep_table);

free(pni->k);
free(pni->k_weights);
free(pni->pk_primordial_k);
class_free(pni->k);
class_free(pni->k_weights);
class_free(pni->pk_primordial_k);

free(pni->z_table_coarse);
free(pni->noninjection_table);
free(pni->ddnoninjection_table);
class_free(pni->z_table_coarse);
class_free(pni->noninjection_table);
class_free(pni->ddnoninjection_table);

free(pni->integrand_approx);
class_free(pni->integrand_approx);

return _SUCCESS_;
}
Expand Down
Loading