Skip to content

Commit

Permalink
Revised initialization of malloc module; internalized several control…
Browse files Browse the repository at this point in the history
… variables. Reworked internals (malloc).
  • Loading branch information
hfp committed Sep 19, 2019
1 parent d98ba74 commit 947250e
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 166 deletions.
27 changes: 19 additions & 8 deletions include/libxsmm_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include "libxsmm_memory.h"

/** Can be used with libxsmm_[get|set]_scratch_limit. */
#define LIBXSMM_MALLOC_UNLIMITED ((size_t)LIBXSMM_UNLIMITED)
#define LIBXSMM_SCRATCH_UNLIMITED ((size_t)LIBXSMM_UNLIMITED)
#define LIBXSMM_SCRATCH_DEFAULT 0


/** Function types accepted for memory allocation (see libxsmm_*_allocator). */
Expand Down Expand Up @@ -157,18 +158,28 @@ LIBXSMM_API int libxsmm_get_scratch_info(libxsmm_scratch_info* info);

/**
* Limit the total size (Bytes) of the scratch memory.
* The environment variable LIBXSMM_SCRATCH_LIMIT takes
* the following units: none (Bytes), k/K, m/M, and g/G.
* LIBXSMM_MALLOC_UNLIMITED removes any limit.
* LIBXSMM_SCRATCH_UNLIMITED removes any limit, and
* LIBXSMM_SCRATCH_DEFAULT populates the default.
* The related environment variable LIBXSMM_SCRATCH_LIMIT
* allows units: <none>/b/B (Bytes), k/K, m/M, and g/G.
*/
LIBXSMM_API void libxsmm_set_scratch_limit(size_t nbytes);
/** Get the maximum size of the scratch memory domain. */
LIBXSMM_API size_t libxsmm_get_scratch_limit(void);

/** Intercepts malloc/free to use scratch memory allocator. */
LIBXSMM_API void libxsmm_set_scratch_malloc(int enabled);
/** Checks if malloc/free can be and are intercepted. */
LIBXSMM_API int libxsmm_get_scratch_malloc(void);
/**
* Intercepts malloc/free to use scratch memory allocator.
* (related environment variable LIBXSMM_MALLOC).
* Optionally set the range of malloc-sizes to be intercepted.
* The related environment variable LIBXSMM_MALLOC_LIMIT
* allows units: <none>/b/B (Bytes), k/K, m/M, and g/G.
*/
LIBXSMM_API void libxsmm_set_malloc(int enabled, const size_t* lo, const size_t* hi);
/**
* Determines if malloc/free are (and can be) intercepted.
* Optionally gets the range of enabled malloc-sizes.
*/
LIBXSMM_API int libxsmm_get_malloc(size_t* lo, size_t* hi);

/**
* Calculate the linear offset of the n-dimensional (ndims) offset (can be NULL),
Expand Down
24 changes: 13 additions & 11 deletions src/libxsmm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,13 @@ LIBXSMM_APIVAR(unsigned int internal_statistic_num_trmm);
LIBXSMM_APIVAR(int internal_gemm_auto_prefetch_locked);
LIBXSMM_APIVAR(const char* internal_build_state);

#if !defined(INTERNAL_DELIMS)
# define INTERNAL_DELIMS ";,:"
#endif

#if defined(_WIN32)
# define INTERNAL_DELIMS ";,"
LIBXSMM_APIVAR(HANDLE internal_singleton_handle);
#else
# define INTERNAL_DELIMS ";,:"
LIBXSMM_APIVAR_ARRAY(char internal_singleton_fname, 64);
LIBXSMM_APIVAR(int internal_singleton_handle);
#endif
Expand Down Expand Up @@ -630,7 +632,7 @@ LIBXSMM_API_INTERN size_t internal_parse_nbytes(const char* nbytes, size_t ndefa
size_t result = ndefault;
if (NULL != nbytes && 0 != *nbytes) {
size_t u = internal_strlen(nbytes, 32) - 1;
const char unit[] = "kmgKMG", * const hit = strchr(unit, nbytes[u]);
const char unit[] = "kmgKMG", *const hit = strchr(unit, nbytes[u]);
const long long int ibytes = atol(nbytes); /* take with increased type-width */
result = (size_t)ibytes;
if ((size_t)LIBXSMM_UNLIMITED != result) {
Expand Down Expand Up @@ -701,8 +703,7 @@ LIBXSMM_API_INTERN void internal_init(void)
}
LIBXSMM_ASSERT(1 <= libxsmm_scratch_scale);
}
libxsmm_scratch_limit = internal_parse_nbytes(
getenv("LIBXSMM_SCRATCH_LIMIT"), LIBXSMM_MALLOC_SCRATCH_LIMIT);
libxsmm_set_scratch_limit(internal_parse_nbytes(getenv("LIBXSMM_SCRATCH_LIMIT"), LIBXSMM_SCRATCH_DEFAULT));
#endif /*defined(LIBXSMM_MALLOC_SCRATCH_MAX_NPOOLS) && (0 < (LIBXSMM_MALLOC_SCRATCH_MAX_NPOOLS))*/
#if defined(LIBXSMM_MAXTARGET)
libxsmm_set_target_arch(LIBXSMM_STRINGIFY(LIBXSMM_MAXTARGET));
Expand Down Expand Up @@ -786,19 +787,20 @@ LIBXSMM_API_INTERN void internal_init(void)
}
}
#endif
{ /* setup libxsmm_malloc_kind after internal allocations */
{ /* setup malloc-interception after internal allocations */
const libxsmm_malloc_function null_malloc_fn = { 0 };
const libxsmm_free_function null_free_fn = { 0 };
const char *const env_k = getenv("LIBXSMM_MALLOC");
char *const env_t = getenv("LIBXSMM_MALLOC_LIMIT");
const char* env_i = (NULL != env_t ? strtok(env_t, INTERNAL_DELIMS) : NULL);
size_t limit = libxsmm_scratch_limit;
if (NULL != env_k && 0 != *env_k) libxsmm_malloc_kind = atoi(env_k);
libxsmm_malloc_limit[0] = internal_parse_nbytes(env_i, LIBXSMM_MALLOC_LIMIT);
if (NULL != env_i) limit = internal_parse_nbytes(strtok(NULL, INTERNAL_DELIMS), libxsmm_scratch_limit);
libxsmm_malloc_limit[1] = LIBXSMM_MAX(limit, libxsmm_malloc_limit[0]);
const size_t malloc_lo = internal_parse_nbytes(env_i, LIBXSMM_MALLOC_LIMIT);
const size_t malloc_hi = (NULL != env_i ? internal_parse_nbytes(
strtok(NULL, INTERNAL_DELIMS), LIBXSMM_SCRATCH_UNLIMITED) : LIBXSMM_SCRATCH_UNLIMITED);
const int malloc_kind = ((NULL == env_k || 0 == *env_k) ? 0/*disabled*/ : atoi(env_k));
libxsmm_set_malloc(malloc_kind, &malloc_lo, &malloc_hi);
libxsmm_xset_default_allocator(NULL/*lock*/, NULL/*context*/, null_malloc_fn, null_free_fn);
libxsmm_xset_scratch_allocator(NULL/*lock*/, NULL/*context*/, null_malloc_fn, null_free_fn);
libxsmm_malloc_init();
}
{ /* commit the registry buffer and enable global visibility */
void *const pv_registry = &internal_registry;
Expand Down
13 changes: 4 additions & 9 deletions src/libxsmm_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
#if !defined(LIBXSMM_MALLOC_SCRATCH_MAX_NPOOLS)
# define LIBXSMM_MALLOC_SCRATCH_MAX_NPOOLS LIBXSMM_MAX_NTHREADS
#endif
#if !defined(LIBXSMM_MALLOC_SCRATCH_LIMIT)
# define LIBXSMM_MALLOC_SCRATCH_LIMIT 0xFFFFFFFF /* ~4 GB */
#endif
#if !defined(LIBXSMM_MALLOC_SCRATCH_SCALE)
# define LIBXSMM_MALLOC_SCRATCH_SCALE 1.0
#endif
Expand Down Expand Up @@ -799,6 +796,10 @@ LIBXSMM_API int libxsmm_cast(libxsmm_datatype datatype, double dvalue, void* val
/** Retrieve internal information about a buffer (default memory domain). */
LIBXSMM_API int libxsmm_get_malloc_xinfo(const void* memory, size_t* size, int* flags, void** extra);

/** Initializes malloc hooks and other internals. */
LIBXSMM_API_INTERN void libxsmm_malloc_init(void);
LIBXSMM_API_INTERN void libxsmm_malloc_finalize(void);

/** Calculates an alignment depending on supposedly allocated size; alignment can be zero ("auto"). */
LIBXSMM_API_INTERN size_t libxsmm_alignment(size_t size, size_t alignment);

Expand Down Expand Up @@ -885,14 +886,8 @@ LIBXSMM_APIVAR(const void* libxsmm_default_allocator_context);
LIBXSMM_APIVAR(const void* libxsmm_scratch_allocator_context);
/** Number of scratch memory pools used; clamped against internal maximum. */
LIBXSMM_APIVAR(unsigned int libxsmm_scratch_pools);
/** Maximum total size of the scratch memory domain. */
LIBXSMM_APIVAR(size_t libxsmm_scratch_limit);
/** Growth factor used to scale the scratch memory in case of reallocation. */
LIBXSMM_APIVAR(double libxsmm_scratch_scale);
/** Minimum number of bytes needed for interception (libxsmm_malloc_kind) */
LIBXSMM_APIVAR_ARRAY(size_t libxsmm_malloc_limit, 2);
/** 0: regular, 1/odd: intercept/scratch, otherwise: all/scratch */
LIBXSMM_APIVAR(int libxsmm_malloc_kind);
/** Counts the number of attempts to create an SPMDM-handle */
LIBXSMM_APIVAR(unsigned int libxsmm_statistic_num_spmdm);
/** Number of seconds per RDTSC-cycle (zero if RDTSC is not used for wall-clock) */
Expand Down
Loading

0 comments on commit 947250e

Please sign in to comment.