mirror of
https://github.com/amd/blis.git
synced 2026-05-11 09:39:59 +00:00
Implemented developer-configurable malloc()/free().
Details: - Replaced all instances of bli_malloc() and bli_free() with one of: - bli_malloc_pool()/bli_free_pool() - bli_malloc_user()/bli_free_user() - bli_malloc_intl()/bli_free_intl() each of which can be configured to call malloc()/free() substitutes, so long as the substitute functions have the same function type signatures as malloc() and free() defined by C's stdlib.h. The _pool() function is called when allocating blocks for the memory pools (used for packing buffers, primarily), the _user() function is called when obj_t's are created (via bli_obj_create() and friends), and the _intl() function is called for internal use by BLIS, such as when creating control tree nodes or temporary buffers for manipulating internal data structures. Substitutes for any of the three types of bli_malloc() may be specified by #defining the following pairs of cpp macros in bli_kernel.h: - BLIS_MALLOC_POOL/BLIS_FREE_POOL - BLIS_MALLOC_USER/BLIS_FREE_USER - BLIS_MALLOC_INTL/BLIS_FREE_INTL to be the name of the substitute functions. (Obviously, the object code that contains these functions must be provided at link-time.) These macros default to malloc() and free(). Subsitute functions are also automatically prototyped by BLIS (in bli_malloc_prototypes.h). - Removed definitions for bli_malloc() and bli_free(). - Note that bli_malloc_pool() and bli_malloc_user() are now defined in terms of a new function, bli_malloc_align(), which aligns memory to an arbitrary (power of two) alignment boundary, but does so manually, whereas before alignment was performed behind the scenes by posix_memalign(). Currently, bli_malloc_intl() is defined in terms of bli_malloc_noalign(), which serves as a simple wrapper to the designated function that is passed in (e.g. BLIS_MALLOC_INTL). Similarly, there are bli_free_align() and bli_free_noalign(), which are used in concert with their bli_malloc_*() counterparts.
This commit is contained in:
@@ -38,8 +38,49 @@
|
||||
|
||||
// -- MEMORY ALLOCATION --------------------------------------------------------
|
||||
|
||||
// Size of a virtual memory page. This is used to align certain memory
|
||||
// buffers which are allocated and used internally.
|
||||
// Memory allocation functions. These macros define the three types of
|
||||
// malloc()-style functions, and their free() counterparts: one for each
|
||||
// type of memory to be allocated.
|
||||
// NOTE: ANY ALTERNATIVE TO malloc()/free() USED FOR ANY OF THE FOLLOWING
|
||||
// THREE PAIRS OF MACROS MUST USE THE SAME FUNCTION PROTOTYPE AS malloc()
|
||||
// and free():
|
||||
//
|
||||
// void* malloc( size_t size );
|
||||
// void free( void* p );
|
||||
//
|
||||
|
||||
// This allocation function is called to allocate memory for blocks within
|
||||
// BLIS's internal memory pools.
|
||||
#ifndef BLIS_MALLOC_POOL
|
||||
#define BLIS_MALLOC_POOL malloc
|
||||
#endif
|
||||
|
||||
#ifndef BLIS_FREE_POOL
|
||||
#define BLIS_FREE_POOL free
|
||||
#endif
|
||||
|
||||
// This allocation function is called to allocate memory for internally-
|
||||
// used objects and structures, such as control tree nodes.
|
||||
#ifndef BLIS_MALLOC_INTL
|
||||
#define BLIS_MALLOC_INTL malloc
|
||||
#endif
|
||||
|
||||
#ifndef BLIS_FREE_INTL
|
||||
#define BLIS_FREE_INTL free
|
||||
#endif
|
||||
|
||||
// This allocation function is called to allocate memory for objects
|
||||
// created by user-level API functions, such as bli_obj_create().
|
||||
#ifndef BLIS_MALLOC_USER
|
||||
#define BLIS_MALLOC_USER malloc
|
||||
#endif
|
||||
|
||||
#ifndef BLIS_FREE_USER
|
||||
#define BLIS_FREE_USER free
|
||||
#endif
|
||||
|
||||
// Size of a virtual memory page. This is used to align blocks within the
|
||||
// memory pools.
|
||||
#ifndef BLIS_PAGE_SIZE
|
||||
#define BLIS_PAGE_SIZE 4096
|
||||
#endif
|
||||
@@ -76,17 +117,16 @@
|
||||
// functions.
|
||||
#define BLIS_STACK_BUF_ALIGN_SIZE BLIS_SIMD_ALIGN_SIZE
|
||||
|
||||
// Alignment size used when allocating memory dynamically from the operating
|
||||
// system (eg: posix_memalign()). To disable heap alignment and just use
|
||||
// malloc() instead, set this to 1.
|
||||
// Alignment size used when allocating memory via BLIS_MALLOC_USER.
|
||||
// To disable heap alignment, set this to 1.
|
||||
#define BLIS_HEAP_ADDR_ALIGN_SIZE BLIS_SIMD_ALIGN_SIZE
|
||||
|
||||
// Alignment size used when sizing leading dimensions of dynamically
|
||||
// allocated memory.
|
||||
// Alignment size used when sizing leading dimensions of memory allocated
|
||||
// via BLIS_MALLOC_USER.
|
||||
#define BLIS_HEAP_STRIDE_ALIGN_SIZE BLIS_SIMD_ALIGN_SIZE
|
||||
|
||||
// Alignment size used when allocating blocks to the internal memory
|
||||
// pool (for packing buffers).
|
||||
// pool, via BLIS_MALLOC_POOL.
|
||||
#define BLIS_POOL_ADDR_ALIGN_SIZE BLIS_PAGE_SIZE
|
||||
|
||||
|
||||
|
||||
50
frame/include/bli_malloc_prototypes.h
Normal file
50
frame/include/bli_malloc_prototypes.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
|
||||
BLIS
|
||||
An object-based framework for developing high-performance BLAS-like
|
||||
libraries.
|
||||
|
||||
Copyright (C) 2014, The University of Texas at Austin
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
- Neither the name of The University of Texas at Austin nor the names
|
||||
of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef BLIS_MALLOC_PROTOTYPES_H
|
||||
#define BLIS_MALLOC_PROTOTYPES_H
|
||||
|
||||
// Generate prototypes for each of the malloc() and free() functions
|
||||
// defined in BLIS
|
||||
|
||||
void* BLIS_MALLOC_POOL( size_t size );
|
||||
void BLIS_FREE_POOL( void* p );
|
||||
|
||||
void* BLIS_MALLOC_INTL( size_t size );
|
||||
void BLIS_FREE_INTL( void* p );
|
||||
|
||||
void* BLIS_MALLOC_USER( size_t size );
|
||||
void BLIS_FREE_USER( void* p );
|
||||
|
||||
#endif
|
||||
@@ -48,7 +48,7 @@
|
||||
\
|
||||
( ( uintptr_t )(p) % ( uintptr_t )(size) != 0 )
|
||||
|
||||
#define bli_offset_from_alignment( p, size ) \
|
||||
#define bli_offset_past_alignment( p, size ) \
|
||||
\
|
||||
( ( uintptr_t )(p) % ( uintptr_t )(size) )
|
||||
|
||||
|
||||
@@ -1000,6 +1000,8 @@ typedef enum
|
||||
BLIS_REQUESTED_CONTIG_BLOCK_TOO_BIG = (-121),
|
||||
BLIS_EXHAUSTED_CONTIG_MEMORY_POOL = (-122),
|
||||
BLIS_INSUFFICIENT_STACK_BUF_SIZE = (-123),
|
||||
BLIS_ALIGNMENT_NOT_POWER_OF_TWO = (-124),
|
||||
BLIS_ALIGNMENT_NOT_MULT_OF_PTR_SIZE = (-125),
|
||||
|
||||
// Object-related errors
|
||||
BLIS_EXPECTED_OBJECT_ALIAS = (-130),
|
||||
|
||||
@@ -90,6 +90,8 @@ extern "C" {
|
||||
|
||||
#include "bli_kernel_prototypes.h"
|
||||
|
||||
#include "bli_malloc_prototypes.h"
|
||||
|
||||
|
||||
// -- Base operation prototypes --
|
||||
|
||||
|
||||
Reference in New Issue
Block a user