Peano
Loading...
Searching...
No Matches
Gemms.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import os
4import subprocess
5
6class Gemms:
7 """Specification of a dense matrix-matrix multiplication
8
9 C = alpha * A * B + beta * C
10 (M x N) (M x K) (K x N)
11
12 for (int it_1 = 0; it_1 < K; it_1++) {
13 for (int it_2 = 0; it_2 < N; it_2++) {
14 for (int it_3 = 0; it_3 < M; it_3++) {
15 C[it_1*LDC+it_3] = alpha * A[it_2*LDA+it_3] * B[it_1*LDB+it_2] + beta * C[it_1*LDC+it_3];
16 }
17 }
18 }
19 """
20
21 PrecisionTranslate = {
22 "DP": "DP",
23 "double": "DP",
24 "SP": "SP",
25 "float": "SP",
26 "single": "SP",
27 "_Float16": "HP",
28 "std::float16_t": "HP",
29 "__bf16": "HP",
30 "std::bfloat16_t": "HP",
31 "half_float::half": "HP",
32 }
33
34 # dgemm, dgemv, ....
35 operationType = ""
36
37 baseroutinename = ""
38
39 name = ""
40
41 # dimension of the matrices
42 M = -1
43 N = -1
44 K = -1
45
46 # leading dimension of A, B, and C
47 LDA = -1
48 LDB = -1
49 LDC = -1
50
51 # scalars
52 alpha = 1
53 # -1, 1
54 beta = 1
55 # 0, 1
56
57 # alignment flags
58 alignment_A = 0 # 1 aligned, 0 unaligned
59 alignment_C = 0 # 1 aligned, 0 unaligned
60
61 # prefetching
62 prefetchStrategy = ""
63
64 # precision
65 precision = "DP" # "DP" = double, "SP" = float
66
67 # Constructor
69 self,
70 M,
71 N,
72 K,
73 LDA,
74 LDB,
75 LDC,
76 alpha,
77 beta,
78 alignment_A,
79 alignment_C,
80 name,
81 prefetchStrategy,
82 operationType="gemm",
83 precision=["DP"],
84 ):
85 if type(precision) is not list:
86 precision = [precision]
87 for key in precision:
88 if key not in self.PrecisionTranslate.keys():
89 raise ("Unknown precision")
90 if (M > LDC) or (K > LDB) or (M > LDA):
91 raise ("Incompatible matrix sizes and leading dimensions")
92 if alignment_A not in [0, 1]:
93 raise ("Something is wrong with the alignment choice of matrix A")
94 if alignment_C not in [0, 1]:
95 raise ("Something is wrong with the alignment choice of matrix C")
96
97 self.MM = M
98 self.NN = N
99 self.KK = K
100 self.LDALDA = LDA
101 self.LDBLDB = LDB
102 self.LDCLDC = LDC
103 self.alphaalpha = alpha
104 self.betabeta = beta
105 self.alignment_Aalignment_A = alignment_A
106 self.alignment_Calignment_C = alignment_C
107 self.namename = name
108 self.prefetchStrategyprefetchStrategy = prefetchStrategy
110 operationType + "_" + str(M) + "_" + str(N) + "_" + str(K) + "_" + name
111 )
112 self.precisionprecision = [self.PrecisionTranslate[key] for key in precision]
113
114 def __repr__(self):
115 return (
116 "<%s: %s LDA=%s, LDB=%s, LDC=%s, alpha=%s, beta=%s, alignment_A=%s, alignment_C=%s>"
117 % (
118 self.namename,
120 self.LDALDA,
121 self.LDBLDB,
122 self.LDCLDC,
123 self.alphaalpha,
124 self.betabeta,
127 )
128 )
129
130def generate_gemms(output_path, output_file_name, namespace, dict, gemm_config_list):
131 for gemm in gemm_config_list:
132 for current_precision in gemm.precision:
133 command_line_arguments = (
134 " "
135 + "dense"
136 + " "
137 + os.path.join(output_path, output_file_name)
138 + " "
139 + "::".join(namespace)
140 + "::"
141 + gemm.baseroutinename
142 + " "
143 + str(gemm.M)
144 + " "
145 + str(gemm.N)
146 + " "
147 + str(gemm.K)
148 + " "
149 + str(gemm.LDA)
150 + " "
151 + str(gemm.LDB)
152 + " "
153 + str(gemm.LDC)
154 + " "
155 + str(gemm.alpha)
156 + " "
157 + str(gemm.beta)
158 + " "
159 + str(gemm.alignment_A)
160 + " "
161 + str(gemm.alignment_C)
162 + " "
163 + dict["ARCHITECTURE"]
164 + " "
165 + gemm.prefetchStrategy
166 + " "
167 + current_precision
168 ) # SP (single-precision), DP (double-precision) or l16 (only dense)
169 bash_command = (
170 "/opt/views/view/bin/libxsmm_gemm_generator" + command_line_arguments
171 )
172 subprocess.call(bash_command.split())
Specification of a dense matrix-matrix multiplication.
Definition Gemms.py:6
__init__(self, M, N, K, LDA, LDB, LDC, alpha, beta, alignment_A, alignment_C, name, prefetchStrategy, operationType="gemm", precision=["DP"])
Definition Gemms.py:84
generate_gemms(output_path, output_file_name, namespace, dict, gemm_config_list)
Definition Gemms.py:130