Debian Codegraph @

frame/3/trsm/bli_trsm_rl_ker_var2.c

raw symbols SCIP
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_trsm_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_comp = bli_gemm_var_cntl_comp_dt( cntl );
49 const num_t dt_a = bli_obj_dt( a );
50 const num_t dt_b = bli_obj_dt( b );
51 const num_t dt_c = bli_obj_dt( c );
52
53 const siz_t dt_a_size = bli_dt_size( dt_a );
54 const siz_t dt_b_size = bli_dt_size( dt_b );
55 const siz_t dt_c_size = bli_dt_size( dt_c );
56
57 doff_t diagoffb = bli_obj_diag_offset( b );
58
59 const pack_t schema_a = bli_obj_pack_schema( a );
60 const pack_t schema_b = bli_obj_pack_schema( b );
61
62 dim_t m = bli_obj_length( c );
63 dim_t n = bli_obj_width( c );
64 dim_t k = bli_obj_width( a );
65
66 const void* buf_a = bli_obj_buffer_at_off( a );
67 const inc_t cs_a = bli_obj_col_stride( a );
68 const dim_t pd_a = bli_obj_panel_dim( a );
69 const inc_t ps_a = bli_obj_panel_stride( a );
70
71 const void* buf_b = bli_obj_buffer_at_off( b );
72 const inc_t rs_b = bli_obj_row_stride( b );
73 const dim_t pd_b = bli_obj_panel_dim( b );
74 const inc_t ps_b = bli_obj_panel_stride( b );
75
76 void* buf_c = bli_obj_buffer_at_off( c );
77 const inc_t rs_c = bli_obj_row_stride( c );
78 const inc_t cs_c = bli_obj_col_stride( c );
79
80 // Grab the address of the internal scalar buffer for the scalar
81 // attached to A (the non-triangular matrix). This will be the alpha
82 // scalar used in the gemmtrsm subproblems (ie: the scalar that would
83 // be applied to the packed copy of A prior to it being updated by
84 // the trsm subproblem). This scalar may be unit, if for example it
85 // was applied during packing.
86 const void* buf_alpha1 = bli_obj_internal_scalar_buffer( a );
87
88 // Grab the address of the internal scalar buffer for the scalar
89 // attached to C. This will be the "beta" scalar used in the gemm-only
90 // subproblems that correspond to micro-panels that do not intersect
91 // the diagonal. We need this separate scalar because it's possible
92 // that the alpha attached to B was reset, if it was applied during
93 // packing.
94 const void* buf_alpha2 = bli_obj_internal_scalar_buffer( c );
95
96 // Alias some constants to simpler names.
97 const dim_t MR = pd_a;
98 const dim_t NR = pd_b;
99 const dim_t PACKMR = cs_a;
100 const dim_t PACKNR = rs_b;
101
102 // Cast the micro-kernel address to its function pointer type.
103 // NOTE: We use the upper-triangular gemmtrsm ukernel because, while
104 // the current macro-kernel targets the "rl" case (right-side/lower-
105 // triangular), it becomes upper-triangular after the kernel operation
106 // is transposed so that all kernel instances are of the "left"
107 // variety (since those are the only trsm ukernels that exist).
108 gemmtrsm_ukr_ft gemmtrsm_ukr = bli_trsm_var_cntl_gemmtrsm_ukr( cntl );
109 gemm_ukr_ft gemm_ukr = bli_trsm_var_cntl_gemm_ukr( cntl );
110 const void* params = bli_trsm_var_cntl_params( cntl );
111
112 const void* minus_one = bli_obj_buffer_for_const( dt_comp, &BLIS_MINUS_ONE );
113 const char* a_cast = buf_a;
114 const char* b_cast = buf_b;
115 char* c_cast = buf_c;
116 const char* alpha1_cast = buf_alpha1;
117 const char* alpha2_cast = buf_alpha2;
118
119 /*
120 Assumptions/assertions:
121 rs_a == 1
122 cs_a == PACKNR
123 pd_a == NR
124 ps_a == stride to next micro-panel of A
125 rs_b == PACKMR
126 cs_b == 1
127 pd_b == MR
128 ps_b == stride to next micro-panel of B
129 rs_c == (no assumptions)
130 cs_c == (no assumptions)
131
132 Note that MR/NR and PACKMR/PACKNR have been swapped to reflect the
133 swapping of values in the control tree (ie: those values used when
134 packing). This swapping is needed since we cast right-hand trsm in
135 terms of transposed left-hand trsm. So, if we're going to be
136 transposing the operation, then A needs to be packed with NR and B
137 needs to be packed with MR (remember: B is the triangular matrix in
138 the right-hand side parameter case).
139 */
140
141 // Safety trap: Certain indexing within this macro-kernel does not
142 // work as intended if both MR and NR are odd.
143 if ( ( bli_is_odd( PACKMR ) && bli_is_odd( NR ) ) ||
144 ( bli_is_odd( PACKNR ) && bli_is_odd( MR ) ) ) bli_abort();
145
146 // If any dimension is zero, return immediately.
147 if ( bli_zero_dim3( m, n, k ) ) return;
148
149 // Safeguard: If the current panel of B is entirely above its diagonal,
150 // it is implicitly zero. So we do nothing.
151 if ( bli_is_strictly_above_diag_n( diagoffb, k, n ) ) return;
152
153 // If there is a zero region above where the diagonal of B intersects
154 // the left edge of the panel, adjust the pointer to A and treat this
155 // case as if the diagonal offset were zero. Note that we don't need to
156 // adjust the pointer to B since packm would have simply skipped over
157 // the region that was not stored.
158 if ( diagoffb < 0 )
159 {
160 k += diagoffb;
161 a_cast -= diagoffb * PACKMR * dt_a_size;
162 diagoffb = 0;
163 }
164
165 // If there is a zero region to the right of where the diagonal
166 // of B intersects the bottom of the panel, shrink it so that
167 // we can index to the correct place in C (corresponding to the
168 // part of the panel of B that was packed).
169 // NOTE: This is NOT being done to skip over "no-op" iterations,
170 // as with the trsm_lu macro-kernel. This MUST be done for correct
171 // execution because we use n (via n_iter) to compute diagonal and
172 // index offsets for backwards movement through B.
173 if ( diagoffb + k < n )
174 {
175 n = diagoffb + k;
176 }
177
178 // Check the k dimension, which needs to be a multiple of NR. If k
179 // isn't a multiple of NR, we adjust it higher to satisfy the micro-
180 // kernel, which is expecting to perform an NR x NR triangular solve.
181 // This adjustment of k is consistent with what happened when B was
182 // packed: all of its bottom/right edges were zero-padded, and
183 // furthermore, the panel that stores the bottom-right corner of the
184 // matrix has its diagonal extended into the zero-padded region (as
185 // identity). This allows the trsm of that bottom-right panel to
186 // proceed without producing any infs or NaNs that would infect the
187 // "good" values of the corresponding block of A.
188 if ( k % NR != 0 ) k += NR - ( k % NR );
189
190 // NOTE: We don't need to check that n is a multiple of PACKNR since we
191 // know that the underlying buffer was already allocated to have an n
192 // dimension that is a multiple of PACKNR, with the region between the
193 // last column and the next multiple of NR zero-padded accordingly.
194
195 thrinfo_t* thread = bli_thrinfo_sub_node( 0, thread_par );
196
197 // Compute number of primary and leftover components of the m and n
198 // dimensions.
199 dim_t n_iter = n / NR;
200 dim_t n_left = n % NR;
201
202 dim_t m_iter = m / MR;
203 dim_t m_left = m % MR;
204
205 if ( n_left ) ++n_iter;
206 if ( m_left ) ++m_iter;
207
208 // Determine some increments used to step through A, B, and C.
209 inc_t rstep_a = ps_a * dt_a_size;
210
211 inc_t cstep_b = ps_b * dt_b_size;
212
213 inc_t rstep_c = rs_c * MR * dt_c_size;
214 inc_t cstep_c = cs_c * NR * dt_c_size;
215
216 auxinfo_t aux;
217
218 // Save the pack schemas of A and B to the auxinfo_t object.
219 // NOTE: We swap the values for A and B since the triangular
220 // "A" matrix is actually contained within B.
221 bli_auxinfo_set_schema_a( schema_b, &aux );
222 bli_auxinfo_set_schema_b( schema_a, &aux );
223
224 // Save the virtual microkernel address and the params.
225 bli_auxinfo_set_ukr( gemm_ukr, &aux );
226 bli_auxinfo_set_params( params, &aux );
227
228 const char* b1 = b_cast;
229 char* c1 = c_cast;
230
231 // Loop over the n dimension (NR columns at a time).
232 for ( dim_t jb = 0; jb < n_iter; ++jb )
233 {
234 dim_t j = n_iter - 1 - jb;
235 doff_t diagoffb_j = diagoffb - ( doff_t )j*NR;
236
237 dim_t n_cur = ( bli_is_not_edge_b( jb, n_iter, n_left ) ? NR : n_left );
238
239 const char* a1 = a_cast;
240 char* c11 = c1 + (n_iter-1)*cstep_c;
241
242 // Initialize our next panel of B to be the current panel of B.
243 const char* b2 = b1;
244
245 // If the current panel of B intersects the diagonal, use a
246 // special micro-kernel that performs a fused gemm and trsm.
247 // If the current panel of B resides below the diagonal, use a
248 // a regular gemm micro-kernel. Otherwise, if it is above the
249 // diagonal, it was not packed (because it is implicitly zero)
250 // and so we do nothing.
251 if ( bli_intersects_diag_n( diagoffb_j, k, NR ) )
252 {
253 // Determine the offset to and length of the panel that was packed
254 // so we can index into the corresponding location in A.
255 dim_t off_b11 = bli_max( -diagoffb_j, 0 );
256 dim_t k_b1121 = k - off_b11;
257 dim_t k_b11 = NR;
258 dim_t k_b21 = k_b1121 - NR;
259 dim_t off_b21 = off_b11 + k_b11;
260
261 // Compute the addresses of the triangular block B11 and the
262 // panel B21.
263 const char* b11 = b1;
264 const char* b21 = b1 + k_b11 * PACKNR * dt_b_size;
265 //b21 = bli_ptr_inc_by_frac( b1, sizeof( ctype ), k_b11 * PACKNR, 1 );
266
267 // Compute the panel stride for the current micro-panel.
268 inc_t ps_b_cur = k_b1121 * PACKNR;
269 ps_b_cur += ( bli_is_odd( ps_b_cur ) ? 1 : 0 );
270 ps_b_cur *= dt_b_size;
271
272 // Loop over the m dimension (MR rows at a time).
273 for ( dim_t i = 0; i < m_iter; ++i )
274 {
275 if ( bli_trsm_my_iter_rr( i, thread ) ){
276
277 dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left ) ? MR : m_left );
278
279 // Compute the addresses of the A11 block and A12 panel.
280 const char* a11 = a1 + off_b11 * PACKMR * dt_a_size;
281 const char* a12 = a1 + off_b21 * PACKMR * dt_a_size;
282
283 // Compute the addresses of the next panels of A and B.
284 const char* a2 = a1;
285 //if ( bli_is_last_iter_rr( i, m_iter, 0, 1 ) )
286 if ( i + bli_thrinfo_num_threads(thread) >= m_iter )
287 {
288 a2 = a_cast;
289 b2 = b1 + ps_b_cur;
290 if ( bli_is_last_iter_rr( jb, n_iter, 0, 1 ) )
291 b2 = b_cast;
292 }
293
294 // Save addresses of next panels of A and B to the auxinfo_t
295 // object. NOTE: We swap the values for A and B since the
296 // triangular "A" matrix is actually contained within B.
297 bli_auxinfo_set_next_a( b2, &aux );
298 bli_auxinfo_set_next_b( a2, &aux );
299
300 gemmtrsm_ukr
301 (
302 m_cur,
303 n_cur,
304 k_b21,
305 ( void* )alpha1_cast,
306 ( void* )b21,
307 ( void* )b11,
308 ( void* )a12,
309 ( void* )a11,
310 c11, cs_c, rs_c,
311 &aux,
312 ( cntx_t* )cntx
313 );
314
315 }
316
317 a1 += rstep_a;
318 c11 += rstep_c;
319 }
320
321 b1 += ps_b_cur;
322 }
323 else if ( bli_is_strictly_below_diag_n( diagoffb_j, k, NR ) )
324 {
325 // Loop over the m dimension (MR rows at a time).
326 for ( dim_t i = 0; i < m_iter; ++i )
327 {
328 if ( bli_trsm_my_iter_rr( i, thread ) ){
329
330 dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left ) ? MR : m_left );
331
332 // Compute the addresses of the next panels of A and B.
333 const char* a2 = a1;
334 //if ( bli_is_last_iter_rr( i, m_iter, 0, 1 ) )
335 if ( i + bli_thrinfo_num_threads(thread) >= m_iter )
336 {
337 a2 = a_cast;
338 b2 = b1 + cstep_b;
339 if ( bli_is_last_iter_rr( jb, n_iter, 0, 1 ) )
340 b2 = b_cast;
341 }
342
343 // Save addresses of next panels of A and B to the auxinfo_t
344 // object. NOTE: We swap the values for A and B since the
345 // triangular "A" matrix is actually contained within B.
346 bli_auxinfo_set_next_a( b2, &aux );
347 bli_auxinfo_set_next_b( a2, &aux );
348
349 // Invoke the gemm micro-kernel.
350 gemm_ukr
351 (
352 m_cur,
353 n_cur,
354 k,
355 ( void* )minus_one,
356 ( void* )b1,
357 ( void* )a1,
358 ( void* )alpha2_cast,
359 c11, cs_c, rs_c,
360 &aux,
361 ( cntx_t* )cntx
362 );
363
364 }
365
366 a1 += rstep_a;
367 c11 += rstep_c;
368 }
369
370 b1 += cstep_b;
371 }
372
373 c1 -= cstep_c;
374 }
375 }
376