Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions include/utils/tracked_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include <stddef.h>
#include <stdlib.h>

/* 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__)
Expand Down Expand Up @@ -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 */
8 changes: 8 additions & 0 deletions src/problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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",
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/utils/permuted_dense_linalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/utils/tracked_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading