]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/m4/matmul_internal.m4
2018-04-06 Thomas Koenig <tkoenig@gcc.gnu.org>
[thirdparty/gcc.git] / libgfortran / m4 / matmul_internal.m4
1 `void
2 'matmul_name` ('rtype` * const restrict retarray,
3 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
4 int blas_limit, blas_call gemm)
5 {
6 const 'rtype_name` * restrict abase;
7 const 'rtype_name` * restrict bbase;
8 'rtype_name` * restrict dest;
9
10 index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
11 index_type x, y, n, count, xcount, ycount;
12
13 assert (GFC_DESCRIPTOR_RANK (a) == 2
14 || GFC_DESCRIPTOR_RANK (b) == 2);
15
16 /* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
17
18 Either A or B (but not both) can be rank 1:
19
20 o One-dimensional argument A is implicitly treated as a row matrix
21 dimensioned [1,count], so xcount=1.
22
23 o One-dimensional argument B is implicitly treated as a column matrix
24 dimensioned [count, 1], so ycount=1.
25 */
26
27 if (retarray->base_addr == NULL)
28 {
29 if (GFC_DESCRIPTOR_RANK (a) == 1)
30 {
31 GFC_DIMENSION_SET(retarray->dim[0], 0,
32 GFC_DESCRIPTOR_EXTENT(b,1) - 1, 1);
33 }
34 else if (GFC_DESCRIPTOR_RANK (b) == 1)
35 {
36 GFC_DIMENSION_SET(retarray->dim[0], 0,
37 GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
38 }
39 else
40 {
41 GFC_DIMENSION_SET(retarray->dim[0], 0,
42 GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
43
44 GFC_DIMENSION_SET(retarray->dim[1], 0,
45 GFC_DESCRIPTOR_EXTENT(b,1) - 1,
46 GFC_DESCRIPTOR_EXTENT(retarray,0));
47 }
48
49 retarray->base_addr
50 = xmallocarray (size0 ((array_t *) retarray), sizeof ('rtype_name`));
51 retarray->offset = 0;
52 }
53 else if (unlikely (compile_options.bounds_check))
54 {
55 index_type ret_extent, arg_extent;
56
57 if (GFC_DESCRIPTOR_RANK (a) == 1)
58 {
59 arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
60 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
61 if (arg_extent != ret_extent)
62 runtime_error ("Incorrect extent in return array in"
63 " MATMUL intrinsic: is %ld, should be %ld",
64 (long int) ret_extent, (long int) arg_extent);
65 }
66 else if (GFC_DESCRIPTOR_RANK (b) == 1)
67 {
68 arg_extent = GFC_DESCRIPTOR_EXTENT(a,0);
69 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
70 if (arg_extent != ret_extent)
71 runtime_error ("Incorrect extent in return array in"
72 " MATMUL intrinsic: is %ld, should be %ld",
73 (long int) ret_extent, (long int) arg_extent);
74 }
75 else
76 {
77 arg_extent = GFC_DESCRIPTOR_EXTENT(a,0);
78 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
79 if (arg_extent != ret_extent)
80 runtime_error ("Incorrect extent in return array in"
81 " MATMUL intrinsic for dimension 1:"
82 " is %ld, should be %ld",
83 (long int) ret_extent, (long int) arg_extent);
84
85 arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
86 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,1);
87 if (arg_extent != ret_extent)
88 runtime_error ("Incorrect extent in return array in"
89 " MATMUL intrinsic for dimension 2:"
90 " is %ld, should be %ld",
91 (long int) ret_extent, (long int) arg_extent);
92 }
93 }
94 '
95 sinclude(`matmul_asm_'rtype_code`.m4')dnl
96 `
97 if (GFC_DESCRIPTOR_RANK (retarray) == 1)
98 {
99 /* One-dimensional result may be addressed in the code below
100 either as a row or a column matrix. We want both cases to
101 work. */
102 rxstride = rystride = GFC_DESCRIPTOR_STRIDE(retarray,0);
103 }
104 else
105 {
106 rxstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
107 rystride = GFC_DESCRIPTOR_STRIDE(retarray,1);
108 }
109
110
111 if (GFC_DESCRIPTOR_RANK (a) == 1)
112 {
113 /* Treat it as a a row matrix A[1,count]. */
114 axstride = GFC_DESCRIPTOR_STRIDE(a,0);
115 aystride = 1;
116
117 xcount = 1;
118 count = GFC_DESCRIPTOR_EXTENT(a,0);
119 }
120 else
121 {
122 axstride = GFC_DESCRIPTOR_STRIDE(a,0);
123 aystride = GFC_DESCRIPTOR_STRIDE(a,1);
124
125 count = GFC_DESCRIPTOR_EXTENT(a,1);
126 xcount = GFC_DESCRIPTOR_EXTENT(a,0);
127 }
128
129 if (count != GFC_DESCRIPTOR_EXTENT(b,0))
130 {
131 if (count > 0 || GFC_DESCRIPTOR_EXTENT(b,0) > 0)
132 runtime_error ("dimension of array B incorrect in MATMUL intrinsic");
133 }
134
135 if (GFC_DESCRIPTOR_RANK (b) == 1)
136 {
137 /* Treat it as a column matrix B[count,1] */
138 bxstride = GFC_DESCRIPTOR_STRIDE(b,0);
139
140 /* bystride should never be used for 1-dimensional b.
141 The value is only used for calculation of the
142 memory by the buffer. */
143 bystride = 256;
144 ycount = 1;
145 }
146 else
147 {
148 bxstride = GFC_DESCRIPTOR_STRIDE(b,0);
149 bystride = GFC_DESCRIPTOR_STRIDE(b,1);
150 ycount = GFC_DESCRIPTOR_EXTENT(b,1);
151 }
152
153 abase = a->base_addr;
154 bbase = b->base_addr;
155 dest = retarray->base_addr;
156
157 /* Now that everything is set up, we perform the multiplication
158 itself. */
159
160 #define POW3(x) (((float) (x)) * ((float) (x)) * ((float) (x)))
161 #define min(a,b) ((a) <= (b) ? (a) : (b))
162 #define max(a,b) ((a) >= (b) ? (a) : (b))
163
164 if (try_blas && rxstride == 1 && (axstride == 1 || aystride == 1)
165 && (bxstride == 1 || bystride == 1)
166 && (((float) xcount) * ((float) ycount) * ((float) count)
167 > POW3(blas_limit)))
168 {
169 const int m = xcount, n = ycount, k = count, ldc = rystride;
170 const 'rtype_name` one = 1, zero = 0;
171 const int lda = (axstride == 1) ? aystride : axstride,
172 ldb = (bxstride == 1) ? bystride : bxstride;
173
174 if (lda > 0 && ldb > 0 && ldc > 0 && m > 1 && n > 1 && k > 1)
175 {
176 assert (gemm != NULL);
177 gemm (axstride == 1 ? "N" : "T", bxstride == 1 ? "N" : "T", &m,
178 &n, &k, &one, abase, &lda, bbase, &ldb, &zero, dest,
179 &ldc, 1, 1);
180 return;
181 }
182 }
183
184 if (rxstride == 1 && axstride == 1 && bxstride == 1)
185 {
186 /* This block of code implements a tuned matmul, derived from
187 Superscalar GEMM-based level 3 BLAS, Beta version 0.1
188
189 Bo Kagstrom and Per Ling
190 Department of Computing Science
191 Umea University
192 S-901 87 Umea, Sweden
193
194 from netlib.org, translated to C, and modified for matmul.m4. */
195
196 const 'rtype_name` *a, *b;
197 'rtype_name` *c;
198 const index_type m = xcount, n = ycount, k = count;
199
200 /* System generated locals */
201 index_type a_dim1, a_offset, b_dim1, b_offset, c_dim1, c_offset,
202 i1, i2, i3, i4, i5, i6;
203
204 /* Local variables */
205 'rtype_name` f11, f12, f21, f22, f31, f32, f41, f42,
206 f13, f14, f23, f24, f33, f34, f43, f44;
207 index_type i, j, l, ii, jj, ll;
208 index_type isec, jsec, lsec, uisec, ujsec, ulsec;
209 'rtype_name` *t1;
210
211 a = abase;
212 b = bbase;
213 c = retarray->base_addr;
214
215 /* Parameter adjustments */
216 c_dim1 = rystride;
217 c_offset = 1 + c_dim1;
218 c -= c_offset;
219 a_dim1 = aystride;
220 a_offset = 1 + a_dim1;
221 a -= a_offset;
222 b_dim1 = bystride;
223 b_offset = 1 + b_dim1;
224 b -= b_offset;
225
226 /* Empty c first. */
227 for (j=1; j<=n; j++)
228 for (i=1; i<=m; i++)
229 c[i + j * c_dim1] = ('rtype_name`)0;
230
231 /* Early exit if possible */
232 if (m == 0 || n == 0 || k == 0)
233 return;
234
235 /* Adjust size of t1 to what is needed. */
236 index_type t1_dim;
237 t1_dim = (a_dim1 - (ycount > 1)) * 256 + b_dim1;
238 if (t1_dim > 65536)
239 t1_dim = 65536;
240
241 t1 = malloc (t1_dim * sizeof('rtype_name`));
242
243 /* Start turning the crank. */
244 i1 = n;
245 for (jj = 1; jj <= i1; jj += 512)
246 {
247 /* Computing MIN */
248 i2 = 512;
249 i3 = n - jj + 1;
250 jsec = min(i2,i3);
251 ujsec = jsec - jsec % 4;
252 i2 = k;
253 for (ll = 1; ll <= i2; ll += 256)
254 {
255 /* Computing MIN */
256 i3 = 256;
257 i4 = k - ll + 1;
258 lsec = min(i3,i4);
259 ulsec = lsec - lsec % 2;
260
261 i3 = m;
262 for (ii = 1; ii <= i3; ii += 256)
263 {
264 /* Computing MIN */
265 i4 = 256;
266 i5 = m - ii + 1;
267 isec = min(i4,i5);
268 uisec = isec - isec % 2;
269 i4 = ll + ulsec - 1;
270 for (l = ll; l <= i4; l += 2)
271 {
272 i5 = ii + uisec - 1;
273 for (i = ii; i <= i5; i += 2)
274 {
275 t1[l - ll + 1 + ((i - ii + 1) << 8) - 257] =
276 a[i + l * a_dim1];
277 t1[l - ll + 2 + ((i - ii + 1) << 8) - 257] =
278 a[i + (l + 1) * a_dim1];
279 t1[l - ll + 1 + ((i - ii + 2) << 8) - 257] =
280 a[i + 1 + l * a_dim1];
281 t1[l - ll + 2 + ((i - ii + 2) << 8) - 257] =
282 a[i + 1 + (l + 1) * a_dim1];
283 }
284 if (uisec < isec)
285 {
286 t1[l - ll + 1 + (isec << 8) - 257] =
287 a[ii + isec - 1 + l * a_dim1];
288 t1[l - ll + 2 + (isec << 8) - 257] =
289 a[ii + isec - 1 + (l + 1) * a_dim1];
290 }
291 }
292 if (ulsec < lsec)
293 {
294 i4 = ii + isec - 1;
295 for (i = ii; i<= i4; ++i)
296 {
297 t1[lsec + ((i - ii + 1) << 8) - 257] =
298 a[i + (ll + lsec - 1) * a_dim1];
299 }
300 }
301
302 uisec = isec - isec % 4;
303 i4 = jj + ujsec - 1;
304 for (j = jj; j <= i4; j += 4)
305 {
306 i5 = ii + uisec - 1;
307 for (i = ii; i <= i5; i += 4)
308 {
309 f11 = c[i + j * c_dim1];
310 f21 = c[i + 1 + j * c_dim1];
311 f12 = c[i + (j + 1) * c_dim1];
312 f22 = c[i + 1 + (j + 1) * c_dim1];
313 f13 = c[i + (j + 2) * c_dim1];
314 f23 = c[i + 1 + (j + 2) * c_dim1];
315 f14 = c[i + (j + 3) * c_dim1];
316 f24 = c[i + 1 + (j + 3) * c_dim1];
317 f31 = c[i + 2 + j * c_dim1];
318 f41 = c[i + 3 + j * c_dim1];
319 f32 = c[i + 2 + (j + 1) * c_dim1];
320 f42 = c[i + 3 + (j + 1) * c_dim1];
321 f33 = c[i + 2 + (j + 2) * c_dim1];
322 f43 = c[i + 3 + (j + 2) * c_dim1];
323 f34 = c[i + 2 + (j + 3) * c_dim1];
324 f44 = c[i + 3 + (j + 3) * c_dim1];
325 i6 = ll + lsec - 1;
326 for (l = ll; l <= i6; ++l)
327 {
328 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
329 * b[l + j * b_dim1];
330 f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
331 * b[l + j * b_dim1];
332 f12 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
333 * b[l + (j + 1) * b_dim1];
334 f22 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
335 * b[l + (j + 1) * b_dim1];
336 f13 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
337 * b[l + (j + 2) * b_dim1];
338 f23 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
339 * b[l + (j + 2) * b_dim1];
340 f14 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
341 * b[l + (j + 3) * b_dim1];
342 f24 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
343 * b[l + (j + 3) * b_dim1];
344 f31 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
345 * b[l + j * b_dim1];
346 f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
347 * b[l + j * b_dim1];
348 f32 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
349 * b[l + (j + 1) * b_dim1];
350 f42 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
351 * b[l + (j + 1) * b_dim1];
352 f33 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
353 * b[l + (j + 2) * b_dim1];
354 f43 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
355 * b[l + (j + 2) * b_dim1];
356 f34 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
357 * b[l + (j + 3) * b_dim1];
358 f44 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
359 * b[l + (j + 3) * b_dim1];
360 }
361 c[i + j * c_dim1] = f11;
362 c[i + 1 + j * c_dim1] = f21;
363 c[i + (j + 1) * c_dim1] = f12;
364 c[i + 1 + (j + 1) * c_dim1] = f22;
365 c[i + (j + 2) * c_dim1] = f13;
366 c[i + 1 + (j + 2) * c_dim1] = f23;
367 c[i + (j + 3) * c_dim1] = f14;
368 c[i + 1 + (j + 3) * c_dim1] = f24;
369 c[i + 2 + j * c_dim1] = f31;
370 c[i + 3 + j * c_dim1] = f41;
371 c[i + 2 + (j + 1) * c_dim1] = f32;
372 c[i + 3 + (j + 1) * c_dim1] = f42;
373 c[i + 2 + (j + 2) * c_dim1] = f33;
374 c[i + 3 + (j + 2) * c_dim1] = f43;
375 c[i + 2 + (j + 3) * c_dim1] = f34;
376 c[i + 3 + (j + 3) * c_dim1] = f44;
377 }
378 if (uisec < isec)
379 {
380 i5 = ii + isec - 1;
381 for (i = ii + uisec; i <= i5; ++i)
382 {
383 f11 = c[i + j * c_dim1];
384 f12 = c[i + (j + 1) * c_dim1];
385 f13 = c[i + (j + 2) * c_dim1];
386 f14 = c[i + (j + 3) * c_dim1];
387 i6 = ll + lsec - 1;
388 for (l = ll; l <= i6; ++l)
389 {
390 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
391 257] * b[l + j * b_dim1];
392 f12 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
393 257] * b[l + (j + 1) * b_dim1];
394 f13 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
395 257] * b[l + (j + 2) * b_dim1];
396 f14 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
397 257] * b[l + (j + 3) * b_dim1];
398 }
399 c[i + j * c_dim1] = f11;
400 c[i + (j + 1) * c_dim1] = f12;
401 c[i + (j + 2) * c_dim1] = f13;
402 c[i + (j + 3) * c_dim1] = f14;
403 }
404 }
405 }
406 if (ujsec < jsec)
407 {
408 i4 = jj + jsec - 1;
409 for (j = jj + ujsec; j <= i4; ++j)
410 {
411 i5 = ii + uisec - 1;
412 for (i = ii; i <= i5; i += 4)
413 {
414 f11 = c[i + j * c_dim1];
415 f21 = c[i + 1 + j * c_dim1];
416 f31 = c[i + 2 + j * c_dim1];
417 f41 = c[i + 3 + j * c_dim1];
418 i6 = ll + lsec - 1;
419 for (l = ll; l <= i6; ++l)
420 {
421 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
422 257] * b[l + j * b_dim1];
423 f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) -
424 257] * b[l + j * b_dim1];
425 f31 += t1[l - ll + 1 + ((i - ii + 3) << 8) -
426 257] * b[l + j * b_dim1];
427 f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) -
428 257] * b[l + j * b_dim1];
429 }
430 c[i + j * c_dim1] = f11;
431 c[i + 1 + j * c_dim1] = f21;
432 c[i + 2 + j * c_dim1] = f31;
433 c[i + 3 + j * c_dim1] = f41;
434 }
435 i5 = ii + isec - 1;
436 for (i = ii + uisec; i <= i5; ++i)
437 {
438 f11 = c[i + j * c_dim1];
439 i6 = ll + lsec - 1;
440 for (l = ll; l <= i6; ++l)
441 {
442 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
443 257] * b[l + j * b_dim1];
444 }
445 c[i + j * c_dim1] = f11;
446 }
447 }
448 }
449 }
450 }
451 }
452 free(t1);
453 return;
454 }
455 else if (rxstride == 1 && aystride == 1 && bxstride == 1)
456 {
457 if (GFC_DESCRIPTOR_RANK (a) != 1)
458 {
459 const 'rtype_name` *restrict abase_x;
460 const 'rtype_name` *restrict bbase_y;
461 'rtype_name` *restrict dest_y;
462 'rtype_name` s;
463
464 for (y = 0; y < ycount; y++)
465 {
466 bbase_y = &bbase[y*bystride];
467 dest_y = &dest[y*rystride];
468 for (x = 0; x < xcount; x++)
469 {
470 abase_x = &abase[x*axstride];
471 s = ('rtype_name`) 0;
472 for (n = 0; n < count; n++)
473 s += abase_x[n] * bbase_y[n];
474 dest_y[x] = s;
475 }
476 }
477 }
478 else
479 {
480 const 'rtype_name` *restrict bbase_y;
481 'rtype_name` s;
482
483 for (y = 0; y < ycount; y++)
484 {
485 bbase_y = &bbase[y*bystride];
486 s = ('rtype_name`) 0;
487 for (n = 0; n < count; n++)
488 s += abase[n*axstride] * bbase_y[n];
489 dest[y*rystride] = s;
490 }
491 }
492 }
493 else if (axstride < aystride)
494 {
495 for (y = 0; y < ycount; y++)
496 for (x = 0; x < xcount; x++)
497 dest[x*rxstride + y*rystride] = ('rtype_name`)0;
498
499 for (y = 0; y < ycount; y++)
500 for (n = 0; n < count; n++)
501 for (x = 0; x < xcount; x++)
502 /* dest[x,y] += a[x,n] * b[n,y] */
503 dest[x*rxstride + y*rystride] +=
504 abase[x*axstride + n*aystride] *
505 bbase[n*bxstride + y*bystride];
506 }
507 else if (GFC_DESCRIPTOR_RANK (a) == 1)
508 {
509 const 'rtype_name` *restrict bbase_y;
510 'rtype_name` s;
511
512 for (y = 0; y < ycount; y++)
513 {
514 bbase_y = &bbase[y*bystride];
515 s = ('rtype_name`) 0;
516 for (n = 0; n < count; n++)
517 s += abase[n*axstride] * bbase_y[n*bxstride];
518 dest[y*rxstride] = s;
519 }
520 }
521 else
522 {
523 const 'rtype_name` *restrict abase_x;
524 const 'rtype_name` *restrict bbase_y;
525 'rtype_name` *restrict dest_y;
526 'rtype_name` s;
527
528 for (y = 0; y < ycount; y++)
529 {
530 bbase_y = &bbase[y*bystride];
531 dest_y = &dest[y*rystride];
532 for (x = 0; x < xcount; x++)
533 {
534 abase_x = &abase[x*axstride];
535 s = ('rtype_name`) 0;
536 for (n = 0; n < count; n++)
537 s += abase_x[n*aystride] * bbase_y[n*bxstride];
538 dest_y[x*rxstride] = s;
539 }
540 }
541 }
542 }
543 #undef POW3
544 #undef min
545 #undef max
546 '