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)
6 const 'rtype_name` * restrict abase;
7 const 'rtype_name` * restrict bbase;
8 'rtype_name` * restrict dest;
10 index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
11 index_type x, y, n, count, xcount, ycount;
13 assert (GFC_DESCRIPTOR_RANK (a) == 2
14 || GFC_DESCRIPTOR_RANK (b) == 2);
16 /* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
18 Either A or B (but not both) can be rank 1:
20 o One-dimensional argument A is implicitly treated as a row matrix
21 dimensioned [1,count], so xcount=1.
23 o One-dimensional argument B is implicitly treated as a column matrix
24 dimensioned [count, 1], so ycount=1.
27 if (retarray->base_addr == NULL)
29 if (GFC_DESCRIPTOR_RANK (a) == 1)
31 GFC_DIMENSION_SET(retarray->dim[0], 0,
32 GFC_DESCRIPTOR_EXTENT(b,1) - 1, 1);
34 else if (GFC_DESCRIPTOR_RANK (b) == 1)
36 GFC_DIMENSION_SET(retarray->dim[0], 0,
37 GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
41 GFC_DIMENSION_SET(retarray->dim[0], 0,
42 GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
44 GFC_DIMENSION_SET(retarray->dim[1], 0,
45 GFC_DESCRIPTOR_EXTENT(b,1) - 1,
46 GFC_DESCRIPTOR_EXTENT(retarray,0));
50 = xmallocarray (size0 ((array_t *) retarray), sizeof ('rtype_name`));
53 else if (unlikely (compile_options.bounds_check))
55 index_type ret_extent, arg_extent;
57 if (GFC_DESCRIPTOR_RANK (a) == 1)
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);
66 else if (GFC_DESCRIPTOR_RANK (b) == 1)
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);
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);
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);
95 sinclude(`matmul_asm_'rtype_code`.m4')dnl
97 if (GFC_DESCRIPTOR_RANK (retarray) == 1)
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
102 rxstride = rystride = GFC_DESCRIPTOR_STRIDE(retarray,0);
106 rxstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
107 rystride = GFC_DESCRIPTOR_STRIDE(retarray,1);
111 if (GFC_DESCRIPTOR_RANK (a) == 1)
113 /* Treat it as a a row matrix A[1,count]. */
114 axstride = GFC_DESCRIPTOR_STRIDE(a,0);
118 count = GFC_DESCRIPTOR_EXTENT(a,0);
122 axstride = GFC_DESCRIPTOR_STRIDE(a,0);
123 aystride = GFC_DESCRIPTOR_STRIDE(a,1);
125 count = GFC_DESCRIPTOR_EXTENT(a,1);
126 xcount = GFC_DESCRIPTOR_EXTENT(a,0);
129 if (count != GFC_DESCRIPTOR_EXTENT(b,0))
131 if (count > 0 || GFC_DESCRIPTOR_EXTENT(b,0) > 0)
132 runtime_error ("dimension of array B incorrect in MATMUL intrinsic");
135 if (GFC_DESCRIPTOR_RANK (b) == 1)
137 /* Treat it as a column matrix B[count,1] */
138 bxstride = GFC_DESCRIPTOR_STRIDE(b,0);
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. */
148 bxstride = GFC_DESCRIPTOR_STRIDE(b,0);
149 bystride = GFC_DESCRIPTOR_STRIDE(b,1);
150 ycount = GFC_DESCRIPTOR_EXTENT(b,1);
153 abase = a->base_addr;
154 bbase = b->base_addr;
155 dest = retarray->base_addr;
157 /* Now that everything is set up, we perform the multiplication
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))
164 if (try_blas && rxstride == 1 && (axstride == 1 || aystride == 1)
165 && (bxstride == 1 || bystride == 1)
166 && (((float) xcount) * ((float) ycount) * ((float) count)
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;
174 if (lda > 0 && ldb > 0 && ldc > 0 && m > 1 && n > 1 && k > 1)
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,
184 if (rxstride == 1 && axstride == 1 && bxstride == 1)
186 /* This block of code implements a tuned matmul, derived from
187 Superscalar GEMM-based level 3 BLAS, Beta version 0.1
189 Bo Kagstrom and Per Ling
190 Department of Computing Science
192 S-901 87 Umea, Sweden
194 from netlib.org, translated to C, and modified for matmul.m4. */
196 const 'rtype_name` *a, *b;
198 const index_type m = xcount, n = ycount, k = count;
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;
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;
213 c = retarray->base_addr;
215 /* Parameter adjustments */
217 c_offset = 1 + c_dim1;
220 a_offset = 1 + a_dim1;
223 b_offset = 1 + b_dim1;
229 c[i + j * c_dim1] = ('rtype_name`)0;
231 /* Early exit if possible */
232 if (m == 0 || n == 0 || k == 0)
235 /* Adjust size of t1 to what is needed. */
237 t1_dim = (a_dim1-1) * 256 + b_dim1;
241 t1 = malloc (t1_dim * sizeof('rtype_name`));
243 /* Start turning the crank. */
245 for (jj = 1; jj <= i1; jj += 512)
251 ujsec = jsec - jsec % 4;
253 for (ll = 1; ll <= i2; ll += 256)
259 ulsec = lsec - lsec % 2;
262 for (ii = 1; ii <= i3; ii += 256)
268 uisec = isec - isec % 2;
270 for (l = ll; l <= i4; l += 2)
273 for (i = ii; i <= i5; i += 2)
275 t1[l - ll + 1 + ((i - ii + 1) << 8) - 257] =
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];
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];
295 for (i = ii; i<= i4; ++i)
297 t1[lsec + ((i - ii + 1) << 8) - 257] =
298 a[i + (ll + lsec - 1) * a_dim1];
302 uisec = isec - isec % 4;
304 for (j = jj; j <= i4; j += 4)
307 for (i = ii; i <= i5; i += 4)
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];
326 for (l = ll; l <= i6; ++l)
328 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
330 f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
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]
346 f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
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];
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;
381 for (i = ii + uisec; i <= i5; ++i)
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];
388 for (l = ll; l <= i6; ++l)
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];
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;
409 for (j = jj + ujsec; j <= i4; ++j)
412 for (i = ii; i <= i5; i += 4)
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];
419 for (l = ll; l <= i6; ++l)
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];
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;
436 for (i = ii + uisec; i <= i5; ++i)
438 f11 = c[i + j * c_dim1];
440 for (l = ll; l <= i6; ++l)
442 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
443 257] * b[l + j * b_dim1];
445 c[i + j * c_dim1] = f11;
455 else if (rxstride == 1 && aystride == 1 && bxstride == 1)
457 if (GFC_DESCRIPTOR_RANK (a) != 1)
459 const 'rtype_name` *restrict abase_x;
460 const 'rtype_name` *restrict bbase_y;
461 'rtype_name` *restrict dest_y;
464 for (y = 0; y < ycount; y++)
466 bbase_y = &bbase[y*bystride];
467 dest_y = &dest[y*rystride];
468 for (x = 0; x < xcount; x++)
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];
480 const 'rtype_name` *restrict bbase_y;
483 for (y = 0; y < ycount; y++)
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;
493 else if (axstride < aystride)
495 for (y = 0; y < ycount; y++)
496 for (x = 0; x < xcount; x++)
497 dest[x*rxstride + y*rystride] = ('rtype_name`)0;
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];
507 else if (GFC_DESCRIPTOR_RANK (a) == 1)
509 const 'rtype_name` *restrict bbase_y;
512 for (y = 0; y < ycount; y++)
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;
523 const 'rtype_name` *restrict abase_x;
524 const 'rtype_name` *restrict bbase_y;
525 'rtype_name` *restrict dest_y;
528 for (y = 0; y < ycount; y++)
530 bbase_y = &bbase[y*bystride];
531 dest_y = &dest[y*rystride];
532 for (x = 0; x < xcount; x++)
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;