diff --git a/CMakeLists.txt b/CMakeLists.txt index 52c5f4c..7f4f961 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,13 @@ file(GLOB_RECURSE SOURCES "src/*.c") # Create core library add_library(dnlp_diff ${SOURCES}) +# Optional peak-memory tracking via the sp_malloc/sp_free counters. OFF by default +# so the library carries no mutable global state. +option(SP_TRACK_MEMORY "Track peak memory usage via sp_malloc/sp_free counters" OFF) +if(SP_TRACK_MEMORY) + target_compile_definitions(dnlp_diff PUBLIC SP_TRACK_MEMORY) +endif() + # Link math library (Unix/Linux only) if(NOT MSVC) target_link_libraries(dnlp_diff m) diff --git a/include/utils/tracked_alloc.h b/include/utils/tracked_alloc.h index 50c3351..c5f7b38 100644 --- a/include/utils/tracked_alloc.h +++ b/include/utils/tracked_alloc.h @@ -21,6 +21,10 @@ #include #include +/* Peak-memory tracking is only compiled in with -DSP_TRACK_MEMORY=ON (CMake + option). Otherwise sp_* are plain malloc/calloc/realloc/free. */ +#ifdef SP_TRACK_MEMORY + /* Platform shim for "how many usable bytes are at this malloc'd pointer". Used to track total live bytes */ #if defined(__APPLE__) @@ -95,4 +99,30 @@ static inline void *sp_realloc(void *ptr, size_t size) return new_ptr; } +#else /* !SP_TRACK_MEMORY */ + +/* Tracking disabled (default): the wrappers forward straight to libc and the + library has no mutable global state. */ +static inline void *sp_malloc(size_t size) +{ + return malloc(size); +} + +static inline void *sp_calloc(size_t count, size_t size) +{ + return calloc(count, size); +} + +static inline void sp_free(void *ptr) +{ + free(ptr); +} + +static inline void *sp_realloc(void *ptr, size_t size) +{ + return realloc(ptr, size); +} + +#endif /* SP_TRACK_MEMORY */ + #endif /* TRACKED_ALLOC_H */ diff --git a/src/problem.c b/src/problem.c index 11b49f0..4b753b5 100644 --- a/src/problem.c +++ b/src/problem.c @@ -32,10 +32,12 @@ static void problem_lagrange_hess_fill_sparsity(problem *prob, int *iwork); problem *new_problem(expr *objective, expr **constraints, int n_constraints, bool verbose) { +#ifdef SP_TRACK_MEMORY /* we don't reset g_peak_bytes or g_allocated_bytes since allocations using sp_malloc/sp_calloc might have happened before new_problem in eg., left_matmul, and their frees will subtract from this counter. */ g_peak_bytes = g_allocated_bytes; +#endif problem *prob = (problem *) sp_calloc(1, sizeof(problem)); if (!prob) return NULL; @@ -305,6 +307,7 @@ void problem_init_derivatives(problem *prob) problem_init_hessian(prob); } +#ifdef SP_TRACK_MEMORY static inline void format_memory(size_t bytes, char *buf, size_t buf_size) { if (bytes < 1024) @@ -320,6 +323,7 @@ static inline void format_memory(size_t bytes, char *buf, size_t buf_size) snprintf(buf, buf_size, "%.2f MB", (double) bytes / (1024.0 * 1024.0)); } } +#endif static inline void print_end_message(const Diff_engine_stats *stats) { @@ -337,9 +341,11 @@ static inline void print_end_message(const Diff_engine_stats *stats) printf(" Affine constraints (nnz): %d\n", stats->nnz_affine); printf(" Jacobian nonlinear constraints (nnz): %d\n", stats->nnz_nonlinear); printf(" Lagrange Hessian (nnz): %d\n", stats->nnz_hessian); +#ifdef SP_TRACK_MEMORY char mem_buf[64]; format_memory(stats->memory_bytes, mem_buf, sizeof(mem_buf)); printf(" Peak memory: %s\n", mem_buf); +#endif printf("\nTiming (seconds):\n"); printf(" Derivative structure (sparsity): %8.3f\n", @@ -367,7 +373,9 @@ void free_problem(problem *prob) { if (prob == NULL) return; +#ifdef SP_TRACK_MEMORY prob->stats.memory_bytes = g_peak_bytes; +#endif if (prob->verbose) { print_end_message(&prob->stats); diff --git a/src/utils/permuted_dense_linalg.c b/src/utils/permuted_dense_linalg.c index 4fdf806..e1703cd 100644 --- a/src/utils/permuted_dense_linalg.c +++ b/src/utils/permuted_dense_linalg.c @@ -561,10 +561,10 @@ void BTDA_csc_pd_fill_values(const CSC_matrix *B, const double *d, #if defined(__GNUC__) || defined(__clang__) __attribute__((unused)) #endif -static void -BTDA_csc_pd_fill_values_via_transpose_dead(const CSC_matrix *B, const double *d, - const permuted_dense *A, - permuted_dense *C) +static void BTDA_csc_pd_fill_values_via_transpose_dead(const CSC_matrix *B, + const double *d, + const permuted_dense *A, + permuted_dense *C) { if (C->base.nnz == 0) { diff --git a/src/utils/tracked_alloc.c b/src/utils/tracked_alloc.c index 9b0f4d8..8caf72d 100644 --- a/src/utils/tracked_alloc.c +++ b/src/utils/tracked_alloc.c @@ -17,5 +17,7 @@ */ #include "utils/tracked_alloc.h" +#ifdef SP_TRACK_MEMORY size_t g_allocated_bytes = 0; size_t g_peak_bytes = 0; +#endif