lesgourg / class_public

Public repository of the Cosmic Linear Anisotropy Solving System (master for the most recent version of the standard code; GW_CLASS to include Cosmic Gravitational Wave Background anisotropies; classnet branch for acceleration with neutral networks; ExoCLASS branch for exotic energy injection; class_matter branch for FFTlog)
223 stars 292 forks source link

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

Open kcroker opened 1 month ago

kcroker commented 1 month ago

Addresses Open Issues: https://github.com/lesgourg/class_public/issues/176 and possibly https://github.com/lesgourg/class_public/issues/528 Impacts Users: @hdp1213 @carlosggarcia @danielsf @mcosta18

Replaces all malloc(), realloc(), calloc(), and free() calls with reference-tracked versions. POSIX semantics are honoured. This allows all allocated memory to be freed at any time, regardless of CLASS state. FAILURE conditions have been adjusted to release all allocated memory.

This repairs memory leaks that occur when CLASS is driven from the python wrapper, but fails to complete computations for various reasons, e.g. untabulated He fraction in the reionization code, choices of model parameters during MC that result in non-viable cosmologies/intergrator precision issues, etc.

Memory is tracked with a minimal doubly-linked list. Additions occur at the head for maximal performance on allocation and free. Cooperates with OpenMP as it is used within the v3.2.0 code.

(Note that this patch does not introduce tracked memory allocations within the python wrapper, and so will not fix leaks within the wrapper, if they exist.)