Debian Codegraph @

frame/3/trmm/bli_trmm_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_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_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.
140 if ( diagoffb < 0 )
141 {
142 k += diagoffb;
143 a_cast -= diagoffb * PACKMR * dt_a_size;
144 diagoffb = 0;
145 }
146
147 // If there is a zero region to the right of where the diagonal
148 // of B intersects the bottom of the panel, shrink it to prevent
149 // "no-op" iterations from executing.
150 if ( diagoffb + k < n )
151 {
152 n = diagoffb + k;
153 }
154
155 // Compute number of primary and leftover components of the m and n
156 // dimensions.
157 const dim_t n_iter = n / NR + ( n % NR ? 1 : 0 );
158 const dim_t n_left = n % NR;
159
160 const dim_t m_iter = m / MR + ( m % MR ? 1 : 0 );
161 const dim_t m_left = m % MR;
162
163 // Determine some increments used to step through A, B, and C.
164 const inc_t rstep_a = ps_a * dt_a_size;
165
166 const inc_t cstep_b = ps_b * dt_b_size;
167
168 const inc_t rstep_c = rs_c * MR * dt_c_size;
169 const inc_t cstep_c = cs_c * NR * dt_c_size;
170
171 auxinfo_t aux;
172
173 // Save the pack schemas of A and B to the auxinfo_t object.
174 bli_auxinfo_set_schema_a( schema_a, &aux );
175 bli_auxinfo_set_schema_b( schema_b, &aux );
176
177 // Save the virtual microkernel address and the params.
178 bli_auxinfo_set_ukr( gemm_ukr, &aux );
179 bli_auxinfo_set_params( params, &aux );
180
181 // The 'thread' argument points to the thrinfo_t node for the 2nd (jr)
182 // loop around the microkernel. Here we query the thrinfo_t node for the
183 // 1st (ir) loop around the microkernel.
184 thrinfo_t* thread = bli_thrinfo_sub_node( 0, thread_par );
185 thrinfo_t* caucus = bli_thrinfo_sub_node( 0, thread );
186
187 // Query the number of threads and thread ids for each loop.
188 const dim_t jr_nt = bli_thrinfo_n_way( thread );
189 const dim_t jr_tid = bli_thrinfo_work_id( thread );
190 const dim_t ir_nt = bli_thrinfo_n_way( caucus );
191 const dim_t ir_tid = bli_thrinfo_work_id( caucus );
192
193 dim_t jr_start, jr_end, jr_inc;
194 dim_t ir_start, ir_end, ir_inc;
195
196 // Note that we partition the 2nd loop into two regions: the rectangular
197 // part of B, and the triangular portion.
198 dim_t n_iter_rct;
199 dim_t n_iter_tri;
200
201 if ( bli_is_strictly_below_diag_n( diagoffb, m, n ) )
202 {
203 // If the entire panel of B does not intersect the diagonal, there is
204 // no triangular region, and therefore we can skip the second set of
205 // loops.
206 n_iter_rct = n_iter;
207 n_iter_tri = 0;
208 }
209 else
210 {
211 // If the panel of B does intersect the diagonal, compute the number of
212 // iterations in the rectangular region by dividing NR into the diagonal
213 // offset. (There should never be any remainder in this division.) The
214 // number of iterations in the triangular (or trapezoidal) region is
215 // computed as the remaining number of iterations in the n dimension.
216 n_iter_rct = diagoffb / NR;
217 n_iter_tri = n_iter - n_iter_rct;
218 }
219
220 // Determine the thread range and increment for the 2nd and 1st loops for
221 // the initial rectangular region of B (if it exists).
222 // NOTE: The definition of bli_thread_range_slrr() will depend on whether
223 // slab or round-robin partitioning was requested at configure-time.
224 // NOTE: Parallelism in the 1st loop is disabled for now.
225 bli_thread_range_slrr( jr_tid, jr_nt, n_iter_rct, 1, FALSE, &jr_start, &jr_end, &jr_inc );
226 bli_thread_range_slrr( ir_tid, ir_nt, m_iter, 1, FALSE, &ir_start, &ir_end, &ir_inc );
227
228 // Loop over the n dimension (NR columns at a time).
229 for ( dim_t j = jr_start; j < jr_end; j += jr_inc )
230 {
231 const char* b1 = b_cast + j * cstep_b;
232 char* c1 = c_cast + j * cstep_c;
233
234 const dim_t n_cur = ( bli_is_not_edge_f( j, n_iter, n_left ) ? NR : n_left );
235
236 // Initialize our next panel of B to be the current panel of B.
237 const char* b2 = b1;
238
239 {
240 // Loop over the m dimension (MR rows at a time).
241 for ( dim_t i = ir_start; i < ir_end; i += ir_inc )
242 {
243 const char* a1 = a_cast + i * rstep_a;
244 char* c11 = c1 + i * rstep_c;
245
246 const dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left ) ? MR : m_left );
247
248 // Compute the addresses of the next panels of A and B.
249 const char* a2 = bli_trmm_get_next_a_upanel( a1, rstep_a, ir_inc );
250 if ( bli_is_last_iter_slrr( i, m_iter, ir_tid, ir_nt ) )
251 {
252 a2 = a_cast;
253 b2 = bli_trmm_get_next_b_upanel( b1, cstep_b, jr_inc );
254 if ( bli_is_last_iter_slrr( j, n_iter, jr_tid, jr_nt ) )
255 b2 = b_cast;
256 }
257
258 // Save addresses of next panels of A and B to the auxinfo_t
259 // object.
260 bli_auxinfo_set_next_a( a2, &aux );
261 bli_auxinfo_set_next_b( b2, &aux );
262
263 // Invoke the gemm micro-kernel.
264 gemm_ukr
265 (
266 m_cur,
267 n_cur,
268 k,
269 ( void* )alpha_cast,
270 ( void* )a1,
271 ( void* )b1,
272 ( void* )one,
273 c11, rs_c, cs_c,
274 &aux,
275 ( cntx_t* )cntx
276 );
277 }
278 }
279 }
280
281 // If there is no triangular region, then we're done.
282 if ( n_iter_tri == 0 ) return;
283
284 // Use round-robin assignment of micropanels to threads in the 2nd and
285 // 1st loops for the remaining triangular region of B (if it exists).
286 // NOTE: We don't need to call bli_thread_range_rr() here since we
287 // employ a hack that calls for each thread to execute every iteration
288 // of the jr and ir loops but skip all but the pointer increment for
289 // iterations that are not assigned to it.
290
291 // Advance the starting b1 and c1 pointers to the positions corresponding
292 // to the start of the triangular region of B.
293 jr_start = n_iter_rct;
294 const char* b1 = b_cast + jr_start * cstep_b;
295 char* c1 = c_cast + jr_start * cstep_c;
296
297 // Loop over the n dimension (NR columns at a time).
298 for ( dim_t j = jr_start; j < n_iter; ++j )
299 {
300 const doff_t diagoffb_j = diagoffb - ( doff_t )j*NR;
301
302 // Determine the offset to the beginning of the panel that
303 // was packed so we can index into the corresponding location
304 // in A. Then compute the length of that panel.
305 const dim_t off_b1121 = bli_max( -diagoffb_j, 0 );
306 const dim_t k_b1121 = k - off_b1121;
307
308 const char* a1 = a_cast;
309 char* c11 = c1;
310
311 const dim_t n_cur = ( bli_is_not_edge_f( j, n_iter, n_left ) ? NR : n_left );
312
313 // Initialize our next panel of B to be the current panel of B.
314 const char* b2 = b1;
315
316 // If the current panel of B intersects the diagonal, scale C
317 // by beta. If it is strictly below the diagonal, scale by one.
318 // This allows the current macro-kernel to work for both trmm
319 // and trmm3.
320 {
321 // Compute the panel stride for the current diagonal-
322 // intersecting micro-panel.
323 inc_t ps_b_cur = k_b1121 * PACKNR;
324 ps_b_cur += ( bli_is_odd( ps_b_cur ) ? 1 : 0 );
325 ps_b_cur *= dt_b_size;
326
327 if ( bli_trmm_my_iter_rr( j, thread ) ) {
328
329 // Loop over the m dimension (MR rows at a time).
330 for ( dim_t i = 0; i < m_iter; ++i )
331 {
332 if ( bli_trmm_my_iter_rr( i, caucus ) ) {
333
334 const dim_t m_cur = ( bli_is_not_edge_f( i, m_iter, m_left ) ? MR : m_left );
335
336 const char* a1_i = a1 + off_b1121 * PACKMR * dt_a_size;
337
338 // Compute the addresses of the next panels of A and B.
339 const char* a2 = a1;
340 if ( bli_is_last_iter_rr( i, m_iter, 0, 1 ) )
341 {
342 a2 = a_cast;
343 b2 = b1;
344 if ( bli_is_last_iter_rr( j, n_iter, jr_tid, jr_nt ) )
345 b2 = b_cast;
346 }
347
348 // Save addresses of next panels of A and B to the auxinfo_t
349 // object.
350 bli_auxinfo_set_next_a( a2, &aux );
351 bli_auxinfo_set_next_b( b2, &aux );
352
353 // Invoke the gemm micro-kernel.
354 gemm_ukr
355 (
356 m_cur,
357 n_cur,
358 k_b1121,
359 ( void* )alpha_cast,
360 ( void* )a1_i,
361 ( void* )b1,
362 ( void* )beta_cast,
363 c11, rs_c, cs_c,
364 &aux,
365 ( cntx_t* )cntx
366 );
367 }
368
369 a1 += rstep_a;
370 c11 += rstep_c;
371 }
372 }
373
374 b1 += ps_b_cur;
375 }
376
377 c1 += cstep_c;
378 }
379 }
380
381 //PASTEMAC(ch,fprintm)( stdout, "trmm_rl_ker_var2: a1", MR, k_b1121, a1, 1, MR, "%4.1f", "" );
382 //PASTEMAC(ch,fprintm)( stdout, "trmm_rl_ker_var2: b1", k_b1121, NR, b1_i, NR, 1, "%4.1f", "" );
383