Debian Codegraph @

frame/3/trmm/bli_trmm_rl_ker_var2b.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_trmm_rl_ker_var2b
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 // Detach and multiply the scalars attached to A and B.
81 obj_t scalar_a, scalar_b;
82 bli_obj_scalar_detach( a, &scalar_a );
83 bli_obj_scalar_detach( b, &scalar_b );
84 bli_mulsc( &scalar_a, &scalar_b );
85
86 // Grab the addresses of the internal scalar buffers for the scalar
87 // merged above and the scalar attached to C.
88 const void* buf_alpha = bli_obj_internal_scalar_buffer( &scalar_b );
89 const void* buf_beta = bli_obj_internal_scalar_buffer( c );
90
91 // Alias some constants to simpler names.
92 const dim_t MR = pd_a;
93 const dim_t NR = pd_b;
94 const dim_t PACKMR = cs_a;
95 const dim_t PACKNR = rs_b;
96
97 // Query the context for the micro-kernel address and cast it to its
98 // function pointer type.
99 gemm_ukr_ft gemm_ukr = bli_gemm_var_cntl_ukr( cntl );
100 const void* params = bli_gemm_var_cntl_params( cntl );
101
102 const void* one = bli_obj_buffer_for_const( dt_comp, &BLIS_ONE );
103 const char* a_cast = buf_a;
104 const char* b_cast = buf_b;
105 char* c_cast = buf_c;
106 const char* alpha_cast = buf_alpha;
107 const char* beta_cast = buf_beta;
108
109 /*
110 Assumptions/assertions:
111 rs_a == 1
112 cs_a == PACKMR
113 pd_a == MR
114 ps_a == stride to next micro-panel of A
115 rs_b == PACKNR
116 cs_b == 1
117 pd_b == NR
118 ps_b == stride to next micro-panel of B
119 rs_c == (no assumptions)
120 cs_c == (no assumptions)
121 */
122
123 // Safety trap: Certain indexing within this macro-kernel does not
124 // work as intended if both MR and NR are odd.
125 if ( ( bli_is_odd( PACKMR ) && bli_is_odd( NR ) ) ||
126 ( bli_is_odd( PACKNR ) && bli_is_odd( MR ) ) ) bli_abort();
127
128 // If any dimension is zero, return immediately.
129 if ( bli_zero_dim3( m, n, k ) ) return;
130
131 // Safeguard: If the current panel of B is entirely above the diagonal,
132 // it is implicitly zero. So we do nothing.
133 if ( bli_is_strictly_above_diag_n( diagoffb, k, n ) ) return;
134
135 // If there is a zero region above where the diagonal of B intersects
136 // the left edge of the panel, adjust the pointer to A and treat this
137 // case as if the diagonal offset were zero. Note that we don't need to
138 // adjust the pointer to B since packm would have simply skipped over
139 // the region that was not stored. (Note we assume the diagonal offset
140 // is a multiple of NR; this assumption will hold as long as the cache
141 // blocksizes KC and NC are each a multiple of NR.)
142 if ( diagoffb < 0 )
143 {
144 k += diagoffb;
145 a_cast -= diagoffb * PACKMR * dt_a_size;
146 diagoffb = 0;
147 }
148
149 // If there is a zero region to the right of where the diagonal
150 // of B intersects the bottom of the panel, shrink it to prevent
151 // "no-op" iterations from executing.
152 if ( diagoffb + k < n )
153 {
154 n = diagoffb + k;
155 }
156
157 // Compute number of primary and leftover components of the m and n
158 // dimensions.
159 const dim_t n_iter = n / NR + ( n % NR ? 1 : 0 );
160 const dim_t n_left = n % NR;
161
162 const dim_t m_iter = m / MR + ( m % MR ? 1 : 0 );
163 const dim_t m_left = m % MR;
164
165 // Computing the number of NR x NR tiles in the k dimension is needed
166 // when computing the thread ranges below.
167 const dim_t k_iter = k / NR + ( k % NR ? 1 : 0 );
168
169 // Determine some increments used to step through A, B, and C.
170 const inc_t rstep_a = ps_a * dt_a_size;
171
172 const inc_t cstep_b = ps_b * dt_b_size;
173
174 const inc_t rstep_c = rs_c * MR * dt_c_size;
175 const inc_t cstep_c = cs_c * NR * dt_c_size;
176
177 auxinfo_t aux;
178
179 // Save the pack schemas of A and B to the auxinfo_t object.
180 bli_auxinfo_set_schema_a( schema_a, &aux );
181 bli_auxinfo_set_schema_b( schema_b, &aux );
182
183 // Save the virtual microkernel address and the params.
184 bli_auxinfo_set_ukr( gemm_ukr, &aux );
185 bli_auxinfo_set_params( params, &aux );
186
187 // The 'thread' argument points to the thrinfo_t node for the 2nd (jr)
188 // loop around the microkernel while the 'caucus' points to the thrinfo_t
189 // node for the 1st loop (ir).
190 thrinfo_t* thread = bli_thrinfo_sub_node( 0, thread_par );
191 //thrinfo_t* caucus = bli_thrinfo_sub_node( 0, thread );
192
193 // Query the number of threads and thread ids for each loop.
194 #if 0
195 {
196 const dim_t jr_nt = 17;
197 const dim_t jr_tid = jr_nt - 1;
198
199 const doff_t m_iter = 10;
200 const doff_t k_iter = 10;
201 const doff_t n_iter = 20;
202
203 diagoffb = 30 * NR;
204 #else
205 const dim_t jr_nt = bli_thrinfo_n_way( thread );
206 const dim_t jr_tid = bli_thrinfo_work_id( thread );
207 //const dim_t ir_nt = bli_thrinfo_n_way( caucus );
208 //const dim_t ir_tid = bli_thrinfo_work_id( caucus );
209 #endif
210 dim_t jr_st, ir_st;
211 const dim_t n_ut_for_me
212 =
213 bli_thread_range_tlb_trmm_rl( jr_nt, jr_tid, diagoffb, m_iter, n_iter, k_iter,
214 MR, NR, &jr_st, &ir_st );
215
216 #if 0
217 printf( "tid %ld: final range: jr_st, ir_st: %ld %ld (n_ut_for_me: %ld)\n",
218 jr_tid, jr_st, ir_st, n_ut_for_me );
219 return;
220 }
221 const dim_t n_ut_for_me = -1; dim_t jr_st, ir_st;
222 #endif
223
224 // It's possible that there are so few microtiles relative to the number
225 // of threads that one or more threads gets no work. If that happens, those
226 // threads can return early.
227 if ( n_ut_for_me == 0 ) return;
228
229 // Start the jr/ir loops with the current thread's microtile offsets computed
230 // by bli_thread_range_tlb_trmm_r().
231 dim_t i = ir_st;
232 dim_t j = jr_st;
233
234 // Initialize a counter to track the number of microtiles computed by the
235 // current thread.
236 dim_t ut = 0;
237
238 const char* b1 = b_cast;
239
240 // Get pointers into position by stepping through to the jth micropanel of
241 // B and jth microtile of C (within the appropriate row of microtiles).
242 for ( dim_t jj = 0; jj < jr_st; ++jj )
243 {
244 const doff_t diagoffb_jj = diagoffb - ( doff_t )jj*NR;
245
246 if ( bli_intersects_diag_n( diagoffb_jj, k, NR ) )
247 {
248 // Determine the length of the panel that was packed.
249 const dim_t off_b1121 = bli_max( -diagoffb_jj, 0 );
250 const dim_t k_b1121 = k - off_b1121;
251
252 // Compute the panel stride for the current diagonal-
253 // intersecting micro-panel.
254 inc_t ps_b_cur = k_b1121 * PACKNR;
255 ps_b_cur += ( bli_is_odd( ps_b_cur ) ? 1 : 0 );
256 ps_b_cur *= dt_b_size;
257
258 b1 += ps_b_cur;
259 }
260 else if ( bli_is_strictly_below_diag_n( diagoffb_jj, k, NR ) )
261 {
262 b1 += cstep_b;
263 }
264 }
265
266 // Loop over the n dimension (NR columns at a time).
267 for ( ; true; ++j )
268 {
269 char* c1 = c_cast + j * cstep_c;
270
271 const doff_t diagoffb_j = diagoffb - ( doff_t )j*NR;
272
273 const dim_t n_cur = ( bli_is_not_edge_f( j, n_iter, n_left )
274 ? NR : n_left );
275
276 // Determine the offset to and length of the panel that was packed
277 // so we can index into the corresponding location in A.
278 const dim_t off_b1121 = bli_max( -diagoffb_j, 0 );
279 const dim_t k_b1121 = k - off_b1121;
280
281 // Initialize our next panel of B to be the current panel of B.
282 const char* b2 = b1;
283
284 bli_auxinfo_set_next_b( b2, &aux );
285
286 // If the current panel of B intersects the diagonal, scale C
287 // by beta. If it is strictly below the diagonal, scale by one.
288 // This allows the current macro-kernel to work for both trmm
289 // and trmm3.
290 if ( bli_intersects_diag_n( diagoffb_j, k, NR ) )
291 {
292 // Compute the panel stride for the current diagonal-
293 // intersecting micro-panel.
294 inc_t ps_b_cur = k_b1121 * PACKNR;
295 ps_b_cur += ( bli_is_odd( ps_b_cur ) ? 1 : 0 );
296 ps_b_cur *= dt_b_size;
297
298 // Loop over the m dimension (MR rows at a time).
299 for ( ; i < m_iter; ++i )
300 {
301 const char* a1 = a_cast + i * rstep_a;
302 char* c11 = c1 + i * rstep_c;
303
304 const dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left )
305 ? MR : m_left );
306
307 const char* a1_i = a1 + off_b1121 * PACKMR * dt_a_size;
308
309 // Compute the addresses of the next panels of A and B.
310 const char* a2 = bli_trmm_get_next_a_upanel( a1, rstep_a, 1 );
311 if ( bli_is_last_iter_sl( i, m_iter ) )
312 {
313 a2 = a_cast;
314 b2 = bli_trmm_get_next_b_upanel( b1, ps_b_cur, 1 );
315 bli_auxinfo_set_next_b( b2, &aux );
316 }
317
318 // Save addresses of next panels of A and B to the auxinfo_t
319 // object.
320 bli_auxinfo_set_next_a( a2, &aux );
321
322 // Invoke the gemm micro-kernel.
323 gemm_ukr
324 (
325 m_cur,
326 n_cur,
327 k_b1121,
328 ( void* )alpha_cast,
329 ( void* )a1_i,
330 ( void* )b1,
331 ( void* )beta_cast,
332 c11, rs_c, cs_c,
333 &aux,
334 ( cntx_t* )cntx
335 );
336
337 // Increment the microtile counter and check if the thread is done.
338 ut += 1; if ( ut == n_ut_for_me ) return;
339 }
340
341 // Upon reaching the end of the column of microtiles, reset the ir
342 // loop index so that we're ready to start the next pass through the
343 // m dimension (i.e., the next jr loop iteration).
344 i = 0;
345
346 b1 += ps_b_cur;
347 }
348 else if ( bli_is_strictly_below_diag_n( diagoffb_j, k, NR ) )
349 {
350 // Loop over the m dimension (MR rows at a time).
351 for ( ; i < m_iter; ++i )
352 {
353 const char* a1 = a_cast + i * rstep_a;
354 char* c11 = c1 + i * rstep_c;
355
356 const dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left )
357 ? MR : m_left );
358
359 // Compute the addresses of the next panels of A and B.
360 const char* a2 = bli_trmm_get_next_a_upanel( a1, rstep_a, 1 );
361 if ( bli_is_last_iter_sl( i, m_iter ) )
362 {
363 a2 = a_cast;
364 b2 = bli_trmm_get_next_b_upanel( b1, cstep_b, 1 );
365 bli_auxinfo_set_next_b( b2, &aux );
366 }
367
368 // Save addresses of next panels of A and B to the auxinfo_t
369 // object.
370 bli_auxinfo_set_next_a( a2, &aux );
371
372 // Invoke the gemm micro-kernel.
373 gemm_ukr
374 (
375 m_cur,
376 n_cur,
377 k,
378 ( void* )alpha_cast,
379 ( void* )a1,
380 ( void* )b1,
381 ( void* )one,
382 c11, rs_c, cs_c,
383 &aux,
384 ( cntx_t* )cntx
385 );
386
387 // Increment the microtile counter and check if the thread is done.
388 ut += 1; if ( ut == n_ut_for_me ) return;
389 }
390
391 // Upon reaching the end of the column of microtiles, reset the ir
392 // loop index so that we're ready to start the next pass through the
393 // m dimension (i.e., the next jr loop iteration).
394 i = 0;
395
396 b1 += cstep_b;
397 }
398 }
399 }
400
401 //PASTEMAC(ch,fprintm)( stdout, "trmm_rl_ker_var2: a1", MR, k_b1121, a1, 1, MR, "%4.1f", "" );
402 //PASTEMAC(ch,fprintm)( stdout, "trmm_rl_ker_var2: b1", k_b1121, NR, b1_i, NR, 1, "%4.1f", "" );
403