Debian Codegraph @

kernels/bgq/3/bli_gemm_bgq_int_8x8.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
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(s) of the copyright holder(s) nor the names of its
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
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 UNIVERSITY
25 OF TEXAS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29 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.
32
33 */
34
35 #include "blis.h"
36 #undef restrict
37 #include <complex.h>
38 #include <assert.h>
39
40
41 /*
42 * Here is dgemm kernel for QPX.
43 * Instruction mix was divined by a statement in an email from John Gunnels when asked about the peak performance with a single thread:
44 * "Achievable peak can either be:
45 * 1) 12.8 GF 8 FMAs cycle * 1.6 GHz
46 * 2) 8.53 GF Takes intoo account the instruction mix in DGEMM and the fact that you can only do an FMA or a load/store in a single cycle with just one thread
47 * 3) 7.58 GF (2) + the fact that we can only issue 8 instructions in 9 cycles with one thread"
48 *
49 * Which I have taken to mean: 8.53 GFLOPS implies on average 5.33 flops/cycle.
50 * I know the kernel John uses is 8x8, so 16 flops per loop iteration.
51 * Thus there must be 24 total instructions per iteration because 16/24 = 5.33.
52 *
53 * Here, we have 6 loads per iteration. These are executed on a different pipeline from FMAs so
54 * we could (maybe) theoretically hit 100% of peak with this instruction mix
55 */
56
57 void bli_dgemm_bgq_int_8x8
58 (
59 dim_t m,
60 dim_t n,
61 dim_t k,
62 const void* alpha0,
63 const void* a0,
64 const void* b0,
65 const void* beta0,
66 void* c0, inc_t rs_c, inc_t cs_c,
67 const auxinfo_t* data,
68 const cntx_t* cntx
69 )
70 {
71 const double* alpha = alpha0;
72 const double* a = a0;
73 const double* b = b0;
74 const double* beta = beta0;
75 double* c = c0;
76
77 GEMM_UKR_SETUP_CT_ANY( d, 8, 8, false );
78
79 //Registers for storing C.
80 //4 4x4 subblocks of C, c00, c01, c10, c11
81 //4 registers per subblock: a, b, c, d
82 //There is an excel file that details which register ends up storing what
83 vector4double c00a = vec_splats( 0.0 );
84 vector4double c00b = vec_splats( 0.0 );
85 vector4double c00c = vec_splats( 0.0 );
86 vector4double c00d = vec_splats( 0.0 );
87
88 vector4double c01a = vec_splats( 0.0 );
89 vector4double c01b = vec_splats( 0.0 );
90 vector4double c01c = vec_splats( 0.0 );
91 vector4double c01d = vec_splats( 0.0 );
92
93 vector4double c10a = vec_splats( 0.0 );
94 vector4double c10b = vec_splats( 0.0 );
95 vector4double c10c = vec_splats( 0.0 );
96 vector4double c10d = vec_splats( 0.0 );
97
98 vector4double c11a = vec_splats( 0.0 );
99 vector4double c11b = vec_splats( 0.0 );
100 vector4double c11c = vec_splats( 0.0 );
101 vector4double c11d = vec_splats( 0.0 );
102
103 vector4double b0a, b1a;
104 vector4double b0b, b1b;
105 vector4double a0, a1;
106
107 for( dim_t i = 0; i < k; i++ )
108 {
109 b0a = vec_ld2a( 0 * sizeof(double), &b[8*i] );
110 b0b = vec_ld2a( 2 * sizeof(double), &b[8*i] );
111 b1a = vec_ld2a( 4 * sizeof(double), &b[8*i] );
112 b1b = vec_ld2a( 6 * sizeof(double), &b[8*i] );
113
114 a0 = vec_lda ( 0 * sizeof(double), &a[8*i] );
115 a1 = vec_lda ( 4 * sizeof(double), &a[8*i] );
116
117 c00a = vec_xmadd ( b0a, a0, c00a );
118 c00b = vec_xxmadd( a0, b0a, c00b );
119 c00c = vec_xmadd ( b0b, a0, c00c );
120 c00d = vec_xxmadd( a0, b0b, c00d );
121
122 c01a = vec_xmadd ( b1a, a0, c01a );
123 c01b = vec_xxmadd( a0, b1a, c01b );
124 c01c = vec_xmadd ( b1b, a0, c01c );
125 c01d = vec_xxmadd( a0, b1b, c01d );
126
127 c10a = vec_xmadd ( b0a, a1, c10a );
128 c10b = vec_xxmadd( a1, b0a, c10b );
129 c10c = vec_xmadd ( b0b, a1, c10c );
130 c10d = vec_xxmadd( a1, b0b, c10d );
131
132 c11a = vec_xmadd ( b1a, a1, c11a );
133 c11b = vec_xxmadd( a1, b1a, c11b );
134 c11c = vec_xmadd ( b1b, a1, c11c );
135 c11d = vec_xxmadd( a1, b1b, c11d );
136 }
137
138 // Create patterns for permuting Cb and Cd
139 vector4double pattern = vec_gpci( 01032 );
140
141 vector4double AB;
142 vector4double C = vec_splats( 0.0 );
143 vector4double betav = vec_lds( 0, ( double* )beta );
144 vector4double alphav = vec_lds( 0, ( double* )alpha );
145 double ct;
146
147 //Macro to update 4 elements of C in a column.
148 //REG is the register holding those 4 elements
149 //ADDR is the address to write them to
150 //OFFSET is the number of rows from ADDR to write to
151 #define UPDATE( REG, ADDR, OFFSET ) \
152 { \
153 ct = *(ADDR + (OFFSET + 0) * rs_c); \
154 C = vec_insert( ct, C, 0 ); \
155 ct = *(ADDR + (OFFSET + 1) * rs_c); \
156 C = vec_insert( ct, C, 1 ); \
157 ct = *(ADDR + (OFFSET + 2) * rs_c); \
158 C = vec_insert( ct, C, 2 ); \
159 ct = *(ADDR + (OFFSET + 3) * rs_c); \
160 C = vec_insert( ct, C, 3 ); \
161 \
162 AB = vec_mul( REG, alphav ); \
163 AB = vec_madd( C, betav, AB); \
164 \
165 ct = vec_extract( AB, 0 ); \
166 *(ADDR + (OFFSET + 0) * rs_c) = ct; \
167 ct = vec_extract( AB, 1 ); \
168 *(ADDR + (OFFSET + 1) * rs_c) = ct; \
169 ct = vec_extract( AB, 2 ); \
170 *(ADDR + (OFFSET + 2) * rs_c) = ct; \
171 ct = vec_extract( AB, 3 ); \
172 *(ADDR + (OFFSET + 3) * rs_c) = ct; \
173 }
174 //Update c00 and c10 sub-blocks
175 UPDATE( c00a, c, 0 );
176 UPDATE( c10a, c, 4 );
177
178 c = c + cs_c;
179 AB = vec_perm( c00b, c00b, pattern );
180 UPDATE( AB, c, 0 );
181 AB = vec_perm( c10b, c10b, pattern );
182 UPDATE( AB, c, 4 );
183
184 c = c + cs_c;
185 UPDATE( c00c, c, 0 );
186 UPDATE( c10c, c, 4 );
187
188 c = c + cs_c;
189 AB = vec_perm( c00d, c00d, pattern );
190 UPDATE( AB, c, 0 );
191 AB = vec_perm( c10d, c10d, pattern );
192 UPDATE( AB, c, 4 );
193
194 //Update c01 and c11 sub-blocks
195 c = c + cs_c;
196 UPDATE( c01a, c, 0 );
197 UPDATE( c11a, c, 4 );
198
199 c = c + cs_c;
200 AB = vec_perm( c01b, c01b, pattern );
201 UPDATE( AB, c, 0 );
202 AB = vec_perm( c11b, c11b, pattern );
203 UPDATE( AB, c, 4 );
204
205 c = c + cs_c;
206 UPDATE( c01c, c, 0 );
207 UPDATE( c11c, c, 4 );
208
209 c = c + cs_c;
210 AB = vec_perm( c01d, c01d, pattern );
211 UPDATE( AB, c, 0 );
212 AB = vec_perm( c11d, c11d, pattern );
213 UPDATE( AB, c, 4 );
214
215 GEMM_UKR_FLUSH_CT( d );
216 }
217
218 void printvec(vector4double v)
219 {
220 double a = vec_extract(v, 0);
221 double b = vec_extract(v, 1);
222 double c = vec_extract(v, 2);
223 double d = vec_extract(v, 3);
224 printf("%4.3f\t%4.3f\t%4.3f\t%4.3f\n", a, b, c, d);
225 }
226
227 void bli_zgemm_bgq_int_4x4
228 (
229 dim_t m,
230 dim_t n,
231 dim_t k,
232 const void* alpha0,
233 const void* a0,
234 const void* b0,
235 const void* beta0,
236 void* c0, inc_t rs_c, inc_t cs_c,
237 const auxinfo_t* data,
238 const cntx_t* cntx
239 )
240 {
241 const dcomplex* alpha = alpha0;
242 const dcomplex* a = a0;
243 const dcomplex* b = b0;
244 const dcomplex* beta = beta0;
245 dcomplex* c = c0;
246
247 GEMM_UKR_SETUP_CT_ANY( z, 4, 4, false );
248
249 const double* a_d = ( double* )a;
250 const double* b_d = ( double* )b;
251 double* c_d = ( double* )c;
252
253 //Registers for storing C.
254 //2 2x4 subblocks of C, c0, and c1
255 //Each sub-block has 4 columns, 0, 1, 2, 3
256 //Each column has 2 partial sum, a and b, and contains 2 complex numbers.
257 vector4double c00a = vec_splats( 0.0 );
258 vector4double c00b = vec_splats( 0.0 );
259 vector4double c01a = vec_splats( 0.0 );
260 vector4double c01b = vec_splats( 0.0 );
261 vector4double c02a = vec_splats( 0.0 );
262 vector4double c02b = vec_splats( 0.0 );
263 vector4double c03a = vec_splats( 0.0 );
264 vector4double c03b = vec_splats( 0.0 );
265
266 vector4double c10a = vec_splats( 0.0 );
267 vector4double c10b = vec_splats( 0.0 );
268 vector4double c11a = vec_splats( 0.0 );
269 vector4double c11b = vec_splats( 0.0 );
270 vector4double c12a = vec_splats( 0.0 );
271 vector4double c12b = vec_splats( 0.0 );
272 vector4double c13a = vec_splats( 0.0 );
273 vector4double c13b = vec_splats( 0.0 );
274
275
276 vector4double b0, b1, b2, b3;
277 vector4double a0, a1;
278
279 for( dim_t i = 0; i < k; i++ )
280 {
281
282 b0 = vec_ld2a( 0 * sizeof(double), &b_d[8*i] );
283 b1 = vec_ld2a( 2 * sizeof(double), &b_d[8*i] );
284 b2 = vec_ld2a( 4 * sizeof(double), &b_d[8*i] );
285 b3 = vec_ld2a( 6 * sizeof(double), &b_d[8*i] );
286
287 a0 = vec_lda ( 0 * sizeof(double), &a_d[8*i] );
288 a1 = vec_lda ( 4 * sizeof(double), &a_d[8*i] );
289
290 c00a = vec_xmadd ( b0, a0, c00a );
291 c00b = vec_xxcpnmadd( a0, b0, c00b );
292 c01a = vec_xmadd ( b1, a0, c01a );
293 c01b = vec_xxcpnmadd( a0, b1, c01b );
294
295 c02a = vec_xmadd ( b2, a0, c02a );
296 c02b = vec_xxcpnmadd( a0, b2, c02b );
297 c03a = vec_xmadd ( b3, a0, c03a );
298 c03b = vec_xxcpnmadd( a0, b3, c03b );
299
300
301 c10a = vec_xmadd ( b0, a1, c10a );
302 c10b = vec_xxcpnmadd( a1, b0, c10b );
303 c11a = vec_xmadd ( b1, a1, c11a );
304 c11b = vec_xxcpnmadd( a1, b1, c11b );
305
306 c12a = vec_xmadd ( b2, a1, c12a );
307 c12b = vec_xxcpnmadd( a1, b2, c12b );
308 c13a = vec_xmadd ( b3, a1, c13a );
309 c13b = vec_xxcpnmadd( a1, b3, c13b );
310
311 }
312
313 // Create patterns for permuting the "b" parts of each vector
314 vector4double pattern = vec_gpci( 01032 );
315 vector4double zed = vec_splats( 0.0 );
316
317 vector4double AB;
318 vector4double C = vec_splats( 0.0 );
319 vector4double C1 = vec_splats( 0.0 );
320 vector4double C2 = vec_splats( 0.0 );
321
322 double alphar = bli_zreal( *alpha );
323 double alphai = bli_zimag( *alpha );
324 double betar = bli_zreal( *beta );
325 double betai = bli_zimag( *beta );
326 vector4double alphav = vec_splats( 0.0 );
327 vector4double betav = vec_splats( 0.0 );
328 alphav = vec_insert( alphar, alphav, 0);
329 alphav = vec_insert( alphai, alphav, 1);
330 alphav = vec_insert( alphar, alphav, 2);
331 alphav = vec_insert( alphai, alphav, 3);
332 betav = vec_insert( betar, betav, 0);
333 betav = vec_insert( betai, betav, 1);
334 betav = vec_insert( betar, betav, 2);
335 betav = vec_insert( betai, betav, 3);
336 double ct;
337
338
339 //Macro to update 2 elements of C in a column.
340 //REG1 is the register holding the first partial sum of those 2 elements
341 //REG2 is the register holding the second partial sum of those 2 elements
342 //ADDR is the address to write them to
343 //OFFSET is the number of rows from ADDR to write to
344 #define ZUPDATE( REG1, REG2, ADDR, OFFSET ) \
345 { \
346 ct = *(ADDR + (OFFSET + 0) * rs_c); \
347 C = vec_insert( ct, C, 0 ); \
348 ct = *(ADDR + (OFFSET + 0) * rs_c + 1); \
349 C = vec_insert( ct, C, 1 ); \
350 ct = *(ADDR + (OFFSET + 2) * rs_c); \
351 C = vec_insert( ct, C, 2 ); \
352 ct = *(ADDR + (OFFSET + 2) * rs_c + 1); \
353 C = vec_insert( ct, C, 3 ); \
354 \
355 AB = vec_sub(REG1, REG2 ); \
356 \
357 /* Scale by alpha */ \
358 REG1 = vec_xmadd( alphav, AB, zed ); \
359 REG2 = vec_xxcpnmadd( AB, alphav, zed ); \
360 AB = vec_sub(REG1, REG2 ); \
361 \
362 \
363 /* Scale by beta */ \
364 REG1 = vec_xmadd( betav, C, zed ); \
365 REG2 = vec_xxcpnmadd( C, betav, zed ); \
366 C = vec_sub(REG1, REG2 ); \
367 \
368 /* Add AB to C */ \
369 C = vec_add( AB, C ); \
370 \
371 ct = vec_extract( C, 0 ); \
372 *(ADDR + (OFFSET + 0) * rs_c) = ct; \
373 ct = vec_extract( C, 1 ); \
374 *(ADDR + (OFFSET + 0) * rs_c + 1) = ct; \
375 ct = vec_extract( C, 2 ); \
376 *(ADDR + (OFFSET + 2) * rs_c) = ct; \
377 ct = vec_extract( C, 3 ); \
378 *(ADDR + (OFFSET + 2) * rs_c + 1) = ct; \
379 }
380
381
382 ZUPDATE( c00a, c00b, c_d, 0 );
383 ZUPDATE( c10a, c10b, c_d, 4 );
384 c_d += 2*cs_c;
385 ZUPDATE( c01a, c01b, c_d, 0 );
386 ZUPDATE( c11a, c11b, c_d, 4 );
387 c_d += 2*cs_c;
388 ZUPDATE( c02a, c02b, c_d, 0 );
389 ZUPDATE( c12a, c12b, c_d, 4 );
390 c_d += 2*cs_c;
391 ZUPDATE( c03a, c03b, c_d, 0 );
392 ZUPDATE( c13a, c13b, c_d, 4 );
393
394 GEMM_UKR_FLUSH_CT( z );
395 }