| 1 |
/* |
| 2 |
|
| 3 |
BLIS |
| 4 |
An object-based framework for developing high-performance BLAS-like |
| 5 |
libraries. |
| 6 |
|
| 7 |
Copyright (C) 2014, The University of Texas at Austin |
| 8 |
Copyright (C) 2018 - 2019, Advanced Micro Devices, Inc. |
| 9 |
|
| 10 |
Redistribution and use in source and binary forms, with or without |
| 11 |
modification, are permitted provided that the following conditions are |
| 12 |
met: |
| 13 |
- Redistributions of source code must retain the above copyright |
| 14 |
notice, this list of conditions and the following disclaimer. |
| 15 |
- Redistributions in binary form must reproduce the above copyright |
| 16 |
notice, this list of conditions and the following disclaimer in the |
| 17 |
documentation and/or other materials provided with the distribution. |
| 18 |
- Neither the name(s) of the copyright holder(s) nor the names of its |
| 19 |
contributors may be used to endorse or promote products derived |
| 20 |
from this software without specific prior written permission. |
| 21 |
|
| 22 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
|
| 34 |
*/ |
| 35 |
|
| 36 |
#include "blis.h" |
| 37 |
|
| 38 |
void bli_trmm_rl_ker_var2 |
| 39 |
( |
| 40 |
const obj_t* a, |
| 41 |
const obj_t* b, |
| 42 |
const obj_t* c, |
| 43 |
const cntx_t* cntx, |
| 44 |
const cntl_t* cntl, |
| 45 |
thrinfo_t* thread_par |
| 46 |
) |
| 47 |
{ |
| 48 |
const num_t dt = bli_obj_exec_dt( c ); |
| 49 |
const dim_t dt_size = bli_dt_size( dt ); |
| 50 |
|
| 51 |
doff_t diagoffb = bli_obj_diag_offset( b ); |
| 52 |
|
| 53 |
const pack_t schema_a = bli_obj_pack_schema( a ); |
| 54 |
const pack_t schema_b = bli_obj_pack_schema( b ); |
| 55 |
|
| 56 |
dim_t m = bli_obj_length( c ); |
| 57 |
dim_t n = bli_obj_width( c ); |
| 58 |
dim_t k = bli_obj_width( a ); |
| 59 |
|
| 60 |
const void* buf_a = bli_obj_buffer_at_off( a ); |
| 61 |
const inc_t cs_a = bli_obj_col_stride( a ); |
| 62 |
const dim_t pd_a = bli_obj_panel_dim( a ); |
| 63 |
const inc_t ps_a = bli_obj_panel_stride( a ); |
| 64 |
|
| 65 |
const void* buf_b = bli_obj_buffer_at_off( b ); |
| 66 |
const inc_t rs_b = bli_obj_row_stride( b ); |
| 67 |
const dim_t pd_b = bli_obj_panel_dim( b ); |
| 68 |
const inc_t ps_b = bli_obj_panel_stride( b ); |
| 69 |
|
| 70 |
void* buf_c = bli_obj_buffer_at_off( c ); |
| 71 |
const inc_t rs_c = bli_obj_row_stride( c ); |
| 72 |
const inc_t cs_c = bli_obj_col_stride( c ); |
| 73 |
|
| 74 |
// Detach and multiply the scalars attached to A and B. |
| 75 |
obj_t scalar_a, scalar_b; |
| 76 |
bli_obj_scalar_detach( a, &scalar_a ); |
| 77 |
bli_obj_scalar_detach( b, &scalar_b ); |
| 78 |
bli_mulsc( &scalar_a, &scalar_b ); |
| 79 |
|
| 80 |
// Grab the addresses of the internal scalar buffers for the scalar |
| 81 |
// merged above and the scalar attached to C. |
| 82 |
const void* buf_alpha = bli_obj_internal_scalar_buffer( &scalar_b ); |
| 83 |
const void* buf_beta = bli_obj_internal_scalar_buffer( c ); |
| 84 |
|
| 85 |
// Alias some constants to simpler names. |
| 86 |
const dim_t MR = pd_a; |
| 87 |
const dim_t NR = pd_b; |
| 88 |
const dim_t PACKMR = cs_a; |
| 89 |
const dim_t PACKNR = rs_b; |
| 90 |
|
| 91 |
// Query the context for the micro-kernel address and cast it to its |
| 92 |
// function pointer type. |
| 93 |
gemm_ukr_vft gemm_ukr = bli_cntx_get_l3_vir_ukr_dt( dt, BLIS_GEMM_UKR, cntx ); |
| 94 |
|
| 95 |
const void* one = bli_obj_buffer_for_const( dt, &BLIS_ONE ); |
| 96 |
const char* a_cast = buf_a; |
| 97 |
const char* b_cast = buf_b; |
| 98 |
char* c_cast = buf_c; |
| 99 |
const char* alpha_cast = buf_alpha; |
| 100 |
const char* beta_cast = buf_beta; |
| 101 |
|
| 102 |
/* |
| 103 |
Assumptions/assertions: |
| 104 |
rs_a == 1 |
| 105 |
cs_a == PACKMR |
| 106 |
pd_a == MR |
| 107 |
ps_a == stride to next micro-panel of A |
| 108 |
rs_b == PACKNR |
| 109 |
cs_b == 1 |
| 110 |
pd_b == NR |
| 111 |
ps_b == stride to next micro-panel of B |
| 112 |
rs_c == (no assumptions) |
| 113 |
cs_c == (no assumptions) |
| 114 |
*/ |
| 115 |
|
| 116 |
// Safety trap: Certain indexing within this macro-kernel does not |
| 117 |
// work as intended if both MR and NR are odd. |
| 118 |
if ( ( bli_is_odd( PACKMR ) && bli_is_odd( NR ) ) || |
| 119 |
( bli_is_odd( PACKNR ) && bli_is_odd( MR ) ) ) bli_abort(); |
| 120 |
|
| 121 |
// If any dimension is zero, return immediately. |
| 122 |
if ( bli_zero_dim3( m, n, k ) ) return; |
| 123 |
|
| 124 |
// Safeguard: If the current panel of B is entirely above the diagonal, |
| 125 |
// it is implicitly zero. So we do nothing. |
| 126 |
if ( bli_is_strictly_above_diag_n( diagoffb, k, n ) ) return; |
| 127 |
|
| 128 |
// If there is a zero region above where the diagonal of B intersects |
| 129 |
// the left edge of the panel, adjust the pointer to A and treat this |
| 130 |
// case as if the diagonal offset were zero. Note that we don't need to |
| 131 |
// adjust the pointer to B since packm would have simply skipped over |
| 132 |
// the region that was not stored. |
| 133 |
if ( diagoffb < 0 ) |
| 134 |
{ |
| 135 |
k += diagoffb; |
| 136 |
a_cast -= diagoffb * PACKMR * dt_size; |
| 137 |
diagoffb = 0; |
| 138 |
} |
| 139 |
|
| 140 |
// If there is a zero region to the right of where the diagonal |
| 141 |
// of B intersects the bottom of the panel, shrink it to prevent |
| 142 |
// "no-op" iterations from executing. |
| 143 |
if ( diagoffb + k < n ) |
| 144 |
{ |
| 145 |
n = diagoffb + k; |
| 146 |
} |
| 147 |
|
| 148 |
// Compute number of primary and leftover components of the m and n |
| 149 |
// dimensions. |
| 150 |
const dim_t n_iter = n / NR + ( n % NR ? 1 : 0 ); |
| 151 |
const dim_t n_left = n % NR; |
| 152 |
|
| 153 |
const dim_t m_iter = m / MR + ( m % MR ? 1 : 0 ); |
| 154 |
const dim_t m_left = m % MR; |
| 155 |
|
| 156 |
// Determine some increments used to step through A, B, and C. |
| 157 |
const inc_t rstep_a = ps_a * dt_size; |
| 158 |
|
| 159 |
const inc_t cstep_b = ps_b * dt_size; |
| 160 |
|
| 161 |
const inc_t rstep_c = rs_c * MR * dt_size; |
| 162 |
const inc_t cstep_c = cs_c * NR * dt_size; |
| 163 |
|
| 164 |
auxinfo_t aux; |
| 165 |
|
| 166 |
// Save the pack schemas of A and B to the auxinfo_t object. |
| 167 |
bli_auxinfo_set_schema_a( schema_a, &aux ); |
| 168 |
bli_auxinfo_set_schema_b( schema_b, &aux ); |
| 169 |
|
| 170 |
// The 'thread' argument points to the thrinfo_t node for the 2nd (jr) |
| 171 |
// loop around the microkernel while the 'caucus' points to the thrinfo_t |
| 172 |
// node for the 1st loop (ir). |
| 173 |
thrinfo_t* thread = bli_thrinfo_sub_node( thread_par ); |
| 174 |
thrinfo_t* caucus = bli_thrinfo_sub_node( thread ); |
| 175 |
|
| 176 |
// Query the number of threads and thread ids for each loop. |
| 177 |
//const dim_t jr_nt = bli_thrinfo_n_way( thread ); |
| 178 |
//const dim_t jr_tid = bli_thrinfo_work_id( thread ); |
| 179 |
//const dim_t ir_nt = bli_thrinfo_n_way( caucus ); |
| 180 |
//const dim_t ir_tid = bli_thrinfo_work_id( caucus ); |
| 181 |
|
| 182 |
dim_t jr_start, jr_end, jr_inc; |
| 183 |
dim_t ir_start, ir_end, ir_inc; |
| 184 |
|
| 185 |
// Determine the thread range and increment for the 2nd and 1st loops. |
| 186 |
// NOTE: The definition of bli_thread_range_jrir() will depend on whether |
| 187 |
// slab or round-robin partitioning was requested at configure-time. |
| 188 |
// NOTE: Parallelism in the 1st loop is disabled for now. |
| 189 |
bli_thread_range_jrir( thread, n_iter, 1, FALSE, &jr_start, &jr_end, &jr_inc ); |
| 190 |
bli_thread_range_jrir( caucus, m_iter, 1, FALSE, &ir_start, &ir_end, &ir_inc ); |
| 191 |
|
| 192 |
const char* b1 = b_cast; |
| 193 |
// char* c1 = c_cast; |
| 194 |
|
| 195 |
// Loop over the n dimension (NR columns at a time). |
| 196 |
for ( dim_t j = jr_start; j < jr_end; j += jr_inc ) |
| 197 |
{ |
| 198 |
const char* a1 = a_cast; |
| 199 |
char* c1 = c_cast + j * cstep_c; |
| 200 |
char* c11 = c1; |
| 201 |
|
| 202 |
const doff_t diagoffb_j = diagoffb - ( doff_t )j*NR; |
| 203 |
|
| 204 |
const dim_t n_cur = ( bli_is_not_edge_f( j, n_iter, n_left ) |
| 205 |
? NR : n_left ); |
| 206 |
|
| 207 |
// Determine the offset to the beginning of the panel that |
| 208 |
// was packed so we can index into the corresponding location |
| 209 |
// in A. Then compute the length of that panel. |
| 210 |
const dim_t off_b1121 = bli_max( -diagoffb_j, 0 ); |
| 211 |
const dim_t k_b1121 = k - off_b1121; |
| 212 |
|
| 213 |
// Initialize our next panel of B to be the current panel of B. |
| 214 |
const char* b2 = b1; |
| 215 |
|
| 216 |
// If the current panel of B intersects the diagonal, scale C |
| 217 |
// by beta. If it is strictly below the diagonal, scale by one. |
| 218 |
// This allows the current macro-kernel to work for both trmm |
| 219 |
// and trmm3. |
| 220 |
if ( bli_intersects_diag_n( diagoffb_j, k, NR ) ) |
| 221 |
{ |
| 222 |
// Compute the panel stride for the current diagonal- |
| 223 |
// intersecting micro-panel. |
| 224 |
inc_t ps_b_cur = k_b1121 * PACKNR; |
| 225 |
ps_b_cur += ( bli_is_odd( ps_b_cur ) ? 1 : 0 ); |
| 226 |
ps_b_cur *= dt_size; |
| 227 |
|
| 228 |
// Loop over the m dimension (MR rows at a time). |
| 229 |
for ( dim_t i = 0; i < m_iter; ++i ) |
| 230 |
//for ( dim_t i = ir_start; i < ir_end; i += ir_inc ) |
| 231 |
{ |
| 232 |
const dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left ) |
| 233 |
? MR : m_left ); |
| 234 |
|
| 235 |
const char* a1_i = a1 + off_b1121 * PACKMR * dt_size; |
| 236 |
|
| 237 |
// Compute the addresses of the next panels of A and B. |
| 238 |
const char* a2 = bli_trmm_get_next_a_upanel( a1, rstep_a, 1 ); |
| 239 |
if ( bli_is_last_iter( i, m_iter, 0, 1 ) ) |
| 240 |
{ |
| 241 |
a2 = a_cast; |
| 242 |
b2 = bli_trmm_get_next_b_upanel( b1, cstep_b, jr_inc ); |
| 243 |
//if ( bli_is_last_iter( j, n_iter, jr_tid, jr_nt ) ) |
| 244 |
// b2 = b_cast; |
| 245 |
} |
| 246 |
|
| 247 |
// Save addresses of next panels of A and B to the auxinfo_t |
| 248 |
// object. |
| 249 |
bli_auxinfo_set_next_a( a2, &aux ); |
| 250 |
bli_auxinfo_set_next_b( b2, &aux ); |
| 251 |
|
| 252 |
// Invoke the gemm micro-kernel. |
| 253 |
gemm_ukr |
| 254 |
( |
| 255 |
m_cur, |
| 256 |
n_cur, |
| 257 |
k_b1121, |
| 258 |
( void* )alpha_cast, |
| 259 |
( void* )a1_i, |
| 260 |
( void* )b1, |
| 261 |
( void* )beta_cast, |
| 262 |
c11, rs_c, cs_c, |
| 263 |
&aux, |
| 264 |
( cntx_t* )cntx |
| 265 |
); |
| 266 |
|
| 267 |
a1 += rstep_a; |
| 268 |
c11 += rstep_c; |
| 269 |
} |
| 270 |
|
| 271 |
b1 += ps_b_cur; |
| 272 |
} |
| 273 |
else if ( bli_is_strictly_below_diag_n( diagoffb_j, k, NR ) ) |
| 274 |
{ |
| 275 |
// Loop over the m dimension (MR rows at a time). |
| 276 |
for ( dim_t i = 0; i < m_iter; ++i ) |
| 277 |
{ |
| 278 |
const dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left ) |
| 279 |
? MR : m_left ); |
| 280 |
|
| 281 |
// Compute the addresses of the next panels of A and B. |
| 282 |
const char* a2 = bli_trmm_get_next_a_upanel( a1, rstep_a, 1 ); |
| 283 |
if ( bli_is_last_iter( i, m_iter, 0, 1 ) ) |
| 284 |
{ |
| 285 |
a2 = a_cast; |
| 286 |
b2 = bli_trmm_get_next_b_upanel( b1, cstep_b, jr_inc ); |
| 287 |
//if ( bli_is_last_iter( j, n_iter, jr_tid, jr_nt ) ) |
| 288 |
// b2 = b_cast; |
| 289 |
} |
| 290 |
|
| 291 |
// Save addresses of next panels of A and B to the auxinfo_t |
| 292 |
// object. |
| 293 |
bli_auxinfo_set_next_a( a2, &aux ); |
| 294 |
bli_auxinfo_set_next_b( b2, &aux ); |
| 295 |
|
| 296 |
// Invoke the gemm micro-kernel. |
| 297 |
gemm_ukr |
| 298 |
( |
| 299 |
m_cur, |
| 300 |
n_cur, |
| 301 |
k, |
| 302 |
( void* )alpha_cast, |
| 303 |
( void* )a1, |
| 304 |
( void* )b1, |
| 305 |
( void* )one, |
| 306 |
c11, rs_c, cs_c, |
| 307 |
&aux, |
| 308 |
( cntx_t* )cntx |
| 309 |
); |
| 310 |
|
| 311 |
a1 += rstep_a; |
| 312 |
c11 += rstep_c; |
| 313 |
} |
| 314 |
|
| 315 |
b1 += cstep_b; |
| 316 |
} |
| 317 |
|
| 318 |
//c1 += cstep_c; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
//PASTEMAC(ch,fprintm)( stdout, "trmm_rl_ker_var2: a1", MR, k_b1121, a1, 1, MR, "%4.1f", "" ); |
| 323 |
//PASTEMAC(ch,fprintm)( stdout, "trmm_rl_ker_var2: b1", k_b1121, NR, b1_i, NR, 1, "%4.1f", "" ); |
| 324 |
|