1 /*
3 BLIS
4 An object-based framework for developing high-performance BLAS-like
5 libraries.
7 Copyright (C) 2014, The University of Texas at Austin
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 - Neither the name of The University of Texas at Austin nor the names
18 of its contributors may be used to endorse or promote products
19 derived from this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
35 #include "blis.h"
36 #if defined(BLIS_ENABLE_TI_ARM_OPENCL) //|| defined (BLIS_ENABLE_C66X_BUILD)
37 #include <CL/cl_ext.h>
38 #endif
40 /* This function is used to allocate memory during BLIS initialization.
41 Allocated memory will be freed when bli_finalize() is called */
42 void* bli_malloc( siz_t size )
43 {
44 void* p = NULL;
45 #if !defined(BLIS_ENABLE_TI_ARM_OPENCL) && !defined(_WIN32) && !defined(BLIS_ENABLE_C66X_BUILD) && (BLIS_HEAP_ADDR_ALIGN_SIZE != 1)
46 int r_val;
47 #endif
49 if ( size == 0 ) return NULL;
51 #if defined(BLIS_ENABLE_TI_ARM_OPENCL)
52 _Pragma( "omp critical (bli_malloc_critical)" )
53 {
54 p = __malloc_ddr( ( size_t )size );
55 }
56 #elif BLIS_HEAP_ADDR_ALIGN_SIZE == 1
57 p = malloc( ( size_t )size );
58 #elif defined(_WIN32)
59 p = _aligned_malloc( ( size_t )size,
60 ( size_t )BLIS_HEAP_ADDR_ALIGN_SIZE );
61 #elif defined (BLIS_ENABLE_C66X_BUILD)
62 p = malloc( ( size_t )size );
63 #else
64 r_val = posix_memalign( &p,
65 ( size_t )BLIS_HEAP_ADDR_ALIGN_SIZE,
66 ( size_t )size );
68 if ( r_val != 0 ) bli_abort();
69 #endif
71 if ( p == NULL ) bli_abort();
73 return p;
74 }
76 /* This function is used to allocate memory for kernel computation.
77 The allocated memory will be freed at the end of the computation.
78 For TI DSP implementation, LibArch scratch heap allocator will be
79 used to obtain a memory block from a scratch heap that is
80 initialized during BLIS initialization. */
81 void* bli_malloc_scratch( siz_t size )
82 {
83 void* p = NULL;
84 #if !defined(BLIS_ENABLE_TI_ARM_OPENCL) && !defined(_WIN32) && !defined(BLIS_ENABLE_C66X_BUILD) && (BLIS_HEAP_ADDR_ALIGN_SIZE != 1)
85 int r_val;
86 #endif
88 if ( size == 0 ) return NULL;
90 #if defined(BLIS_ENABLE_TI_ARM_OPENCL)
91 _Pragma( "omp critical (bli_malloc_critical)" )
92 {
93 p = __malloc_ddr( ( size_t )size );
94 }
95 #elif BLIS_HEAP_ADDR_ALIGN_SIZE == 1
96 p = malloc( ( size_t )size );
97 #elif defined(_WIN32)
98 p = _aligned_malloc( ( size_t )size,
99 ( size_t )BLIS_HEAP_ADDR_ALIGN_SIZE );
100 #elif defined (BLIS_ENABLE_C66X_BUILD)
101 /* Use LibArch slow scratch memory allocator */
102 p = lib_smem_salloc(blasGetMemHandle(), size, 1);
103 #else
104 r_val = posix_memalign( &p,
105 ( size_t )BLIS_HEAP_ADDR_ALIGN_SIZE,
106 ( size_t )size );
108 if ( r_val != 0 ) bli_abort();
109 #endif
111 if ( p == NULL ) bli_abort();
113 return p;
114 }
116 #ifdef BLIS_ENABLE_C66X_BUILD
117 /* This function is used to allocate memory for kernel computation
118 with required alignment. */
119 void* bli_memalign(siz_t alignment, siz_t size )
120 {
121 void* p = NULL;
123 p = memalign(BLIS_CACHE_LINE_SIZE, ( size_t )size);
125 if ( p == NULL ) bli_abort();
127 return p;
128 }
130 /* This function is used to allocate memory for kernel computation
131 with required alignment using LibArch scratch heap allocator.
132 A memory block will be obtained from a scratch heap that is
133 initialized during BLIS initialization. */
134 void* bli_malloc_scratch_align(siz_t alignment, siz_t size )
135 {
136 void* p = NULL;
138 p = lib_smem_salloc(blasGetMemHandle(), size, BLIS_CACHE_LINE_SIZE);
140 if ( p == NULL ) bli_abort();
142 return p;
143 }
145 #endif
147 /* This function is used to free the memory allocated by bli_malloc. */
148 void bli_free( void* p )
149 {
150 #if defined(BLIS_ENABLE_TI_ARM_OPENCL)
151 _Pragma( "omp critical (bli_malloc_critical)" )
152 {
153 __free_ddr( p );
154 }
155 #elif defined (BLIS_ENABLE_C66X_BUILD)
156 free(p);
157 #elif BLIS_HEAP_ADDR_ALIGN_SIZE == 1 || !defined(_WIN32)
158 free( p );
159 #else
160 _aligned_free( p );
161 #endif
162 }
164 /* This function is used to free the memory allocated by bli_malloc_scratch. */
165 void bli_free_scratch( void* p )
166 {
167 #if defined(BLIS_ENABLE_TI_ARM_OPENCL)
168 _Pragma( "omp critical (bli_malloc_critical)" )
169 {
170 __free_ddr( p );
171 }
172 #elif defined (BLIS_ENABLE_C66X_BUILD)
173 /* for DSP implementation, freeing scratch heap is not needed. */
175 #elif BLIS_HEAP_ADDR_ALIGN_SIZE == 1 || !defined(_WIN32)
176 free( p );
177 #else
178 _aligned_free( p );
179 #endif
180 }