| 1 |
`/* Implementation of the MATMUL intrinsic |
| 2 |
Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Paul Brook <[email protected]> |
| 4 |
|
| 5 |
This file is part of the GNU Fortran runtime library (libgfortran). |
| 6 |
|
| 7 |
Libgfortran is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 3 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
Libgfortran is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
Under Section 7 of GPL version 3, you are granted additional |
| 18 |
permissions described in the GCC Runtime Library Exception, version |
| 19 |
3.1, as published by the Free Software Foundation. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License and |
| 22 |
a copy of the GCC Runtime Library Exception along with this program; |
| 23 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 24 |
<http://www.gnu.org/licenses/>. */ |
| 25 |
|
| 26 |
#include "libgfortran.h" |
| 27 |
#include <string.h> |
| 28 |
#include <assert.h>' |
| 29 |
|
| 30 |
include(iparm.m4)dnl |
| 31 |
ifelse(index(rtype_name,`GFC_INTEGER'),`0',dnl |
| 32 |
define(`rtype_name',patsubst(rtype_name,`GFC_INTEGER',`GFC_UINTEGER'))dnl |
| 33 |
define(`rtype',patsubst(rtype,`gfc_array_i',`gfc_array_m')))dnl |
| 34 |
|
| 35 |
`#if defined (HAVE_'rtype_name`) |
| 36 |
|
| 37 |
/* Prototype for the BLAS ?gemm subroutine, a pointer to which can be |
| 38 |
passed to us by the front-end, in which case we call it for large |
| 39 |
matrices. */ |
| 40 |
|
| 41 |
typedef void (*blas_call)(const char *, const char *, const int *, const int *, |
| 42 |
const int *, const 'rtype_name` *, const 'rtype_name` *, |
| 43 |
const int *, const 'rtype_name` *, const int *, |
| 44 |
const 'rtype_name` *, 'rtype_name` *, const int *, |
| 45 |
int, int); |
| 46 |
|
| 47 |
/* The order of loops is different in the case of plain matrix |
| 48 |
multiplication C=MATMUL(A,B), and in the frequent special case where |
| 49 |
the argument A is the temporary result of a TRANSPOSE intrinsic: |
| 50 |
C=MATMUL(TRANSPOSE(A),B). Transposed temporaries are detected by |
| 51 |
looking at their strides. |
| 52 |
|
| 53 |
The equivalent Fortran pseudo-code is: |
| 54 |
|
| 55 |
DIMENSION A(M,COUNT), B(COUNT,N), C(M,N) |
| 56 |
IF (.NOT.IS_TRANSPOSED(A)) THEN |
| 57 |
C = 0 |
| 58 |
DO J=1,N |
| 59 |
DO K=1,COUNT |
| 60 |
DO I=1,M |
| 61 |
C(I,J) = C(I,J)+A(I,K)*B(K,J) |
| 62 |
ELSE |
| 63 |
DO J=1,N |
| 64 |
DO I=1,M |
| 65 |
S = 0 |
| 66 |
DO K=1,COUNT |
| 67 |
S = S+A(I,K)*B(K,J) |
| 68 |
C(I,J) = S |
| 69 |
ENDIF |
| 70 |
*/ |
| 71 |
|
| 72 |
/* If try_blas is set to a nonzero value, then the matmul function will |
| 73 |
see if there is a way to perform the matrix multiplication by a call |
| 74 |
to the BLAS gemm function. */ |
| 75 |
|
| 76 |
extern void matmul_'rtype_code` ('rtype` * const restrict retarray, |
| 77 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 78 |
int blas_limit, blas_call gemm); |
| 79 |
export_proto(matmul_'rtype_code`); |
| 80 |
|
| 81 |
/* Put exhaustive list of possible architectures here here, ORed together. */ |
| 82 |
|
| 83 |
#if defined(HAVE_AVX) || defined(HAVE_AVX2) || defined(HAVE_AVX512F) |
| 84 |
|
| 85 |
#ifdef HAVE_AVX |
| 86 |
'define(`matmul_name',`matmul_'rtype_code`_avx')dnl |
| 87 |
`static void |
| 88 |
'matmul_name` ('rtype` * const restrict retarray, |
| 89 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 90 |
int blas_limit, blas_call gemm) __attribute__((__target__("avx"))); |
| 91 |
static' include(matmul_internal.m4)dnl |
| 92 |
`#endif /* HAVE_AVX */ |
| 93 |
|
| 94 |
#ifdef HAVE_AVX2 |
| 95 |
'define(`matmul_name',`matmul_'rtype_code`_avx2')dnl |
| 96 |
`static void |
| 97 |
'matmul_name` ('rtype` * const restrict retarray, |
| 98 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 99 |
int blas_limit, blas_call gemm) __attribute__((__target__("avx2,fma"))); |
| 100 |
static' include(matmul_internal.m4)dnl |
| 101 |
`#endif /* HAVE_AVX2 */ |
| 102 |
|
| 103 |
#ifdef HAVE_AVX512F |
| 104 |
'define(`matmul_name',`matmul_'rtype_code`_avx512f')dnl |
| 105 |
`static void |
| 106 |
'matmul_name` ('rtype` * const restrict retarray, |
| 107 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 108 |
int blas_limit, blas_call gemm) __attribute__((__target__("avx512f"))); |
| 109 |
static' include(matmul_internal.m4)dnl |
| 110 |
`#endif /* HAVE_AVX512F */ |
| 111 |
|
| 112 |
/* AMD-specifix funtions with AVX128 and FMA3/FMA4. */ |
| 113 |
|
| 114 |
#if defined(HAVE_AVX) && defined(HAVE_FMA3) && defined(HAVE_AVX128) |
| 115 |
'define(`matmul_name',`matmul_'rtype_code`_avx128_fma3')dnl |
| 116 |
`void |
| 117 |
'matmul_name` ('rtype` * const restrict retarray, |
| 118 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 119 |
int blas_limit, blas_call gemm) __attribute__((__target__("avx,fma"))); |
| 120 |
internal_proto('matmul_name`); |
| 121 |
#endif |
| 122 |
|
| 123 |
#if defined(HAVE_AVX) && defined(HAVE_FMA4) && defined(HAVE_AVX128) |
| 124 |
'define(`matmul_name',`matmul_'rtype_code`_avx128_fma4')dnl |
| 125 |
`void |
| 126 |
'matmul_name` ('rtype` * const restrict retarray, |
| 127 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 128 |
int blas_limit, blas_call gemm) __attribute__((__target__("avx,fma4"))); |
| 129 |
internal_proto('matmul_name`); |
| 130 |
#endif |
| 131 |
|
| 132 |
/* Function to fall back to if there is no special processor-specific version. */ |
| 133 |
'define(`matmul_name',`matmul_'rtype_code`_vanilla')dnl |
| 134 |
`static' include(matmul_internal.m4)dnl |
| 135 |
|
| 136 |
`/* Compiling main function, with selection code for the processor. */ |
| 137 |
|
| 138 |
/* Currently, this is i386 only. Adjust for other architectures. */ |
| 139 |
|
| 140 |
void matmul_'rtype_code` ('rtype` * const restrict retarray, |
| 141 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 142 |
int blas_limit, blas_call gemm) |
| 143 |
{ |
| 144 |
static void (*matmul_p) ('rtype` * const restrict retarray, |
| 145 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 146 |
int blas_limit, blas_call gemm); |
| 147 |
|
| 148 |
void (*matmul_fn) ('rtype` * const restrict retarray, |
| 149 |
'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas, |
| 150 |
int blas_limit, blas_call gemm); |
| 151 |
|
| 152 |
matmul_fn = __atomic_load_n (&matmul_p, __ATOMIC_RELAXED); |
| 153 |
if (matmul_fn == NULL) |
| 154 |
{ |
| 155 |
matmul_fn = matmul_'rtype_code`_vanilla; |
| 156 |
if (__builtin_cpu_is ("intel")) |
| 157 |
{ |
| 158 |
/* Run down the available processors in order of preference. */ |
| 159 |
#ifdef HAVE_AVX512F |
| 160 |
if (__builtin_cpu_supports ("avx512f")) |
| 161 |
{ |
| 162 |
matmul_fn = matmul_'rtype_code`_avx512f; |
| 163 |
goto store; |
| 164 |
} |
| 165 |
|
| 166 |
#endif /* HAVE_AVX512F */ |
| 167 |
|
| 168 |
#ifdef HAVE_AVX2 |
| 169 |
if (__builtin_cpu_supports ("avx2") |
| 170 |
&& __builtin_cpu_supports ("fma")) |
| 171 |
{ |
| 172 |
matmul_fn = matmul_'rtype_code`_avx2; |
| 173 |
goto store; |
| 174 |
} |
| 175 |
|
| 176 |
#endif |
| 177 |
|
| 178 |
#ifdef HAVE_AVX |
| 179 |
if (__builtin_cpu_supports ("avx")) |
| 180 |
{ |
| 181 |
matmul_fn = matmul_'rtype_code`_avx; |
| 182 |
goto store; |
| 183 |
} |
| 184 |
#endif /* HAVE_AVX */ |
| 185 |
} |
| 186 |
else if (__builtin_cpu_is ("amd")) |
| 187 |
{ |
| 188 |
#if defined(HAVE_AVX) && defined(HAVE_FMA3) && defined(HAVE_AVX128) |
| 189 |
if (__builtin_cpu_supports ("avx") |
| 190 |
&& __builtin_cpu_supports ("fma")) |
| 191 |
{ |
| 192 |
matmul_fn = matmul_'rtype_code`_avx128_fma3; |
| 193 |
goto store; |
| 194 |
} |
| 195 |
#endif |
| 196 |
#if defined(HAVE_AVX) && defined(HAVE_FMA4) && defined(HAVE_AVX128) |
| 197 |
if (__builtin_cpu_supports ("avx") |
| 198 |
&& __builtin_cpu_supports ("fma4")) |
| 199 |
{ |
| 200 |
matmul_fn = matmul_'rtype_code`_avx128_fma4; |
| 201 |
goto store; |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
} |
| 206 |
store: |
| 207 |
__atomic_store_n (&matmul_p, matmul_fn, __ATOMIC_RELAXED); |
| 208 |
} |
| 209 |
|
| 210 |
(*matmul_fn) (retarray, a, b, try_blas, blas_limit, gemm); |
| 211 |
} |
| 212 |
|
| 213 |
#else /* Just the vanilla function. */ |
| 214 |
|
| 215 |
'define(`matmul_name',`matmul_'rtype_code)dnl |
| 216 |
define(`target_attribute',`')dnl |
| 217 |
include(matmul_internal.m4)dnl |
| 218 |
`#endif |
| 219 |
#endif |
| 220 |
' |