File: libxsmm_interface.py

package info (click to toggle)
libxsmm 1.17-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 14,976 kB
  • sloc: ansic: 119,587; cpp: 27,680; fortran: 9,179; sh: 5,765; makefile: 5,040; pascal: 2,312; python: 1,812; f90: 1,773
file content (195 lines) | stat: -rwxr-xr-x 8,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
###############################################################################
# Copyright (c) Intel Corporation - All rights reserved.                      #
# This file is part of the LIBXSMM library.                                   #
#                                                                             #
# For information on the license, see the LICENSE file.                       #
# Further information: https://github.com/hfp/libxsmm/                        #
# SPDX-License-Identifier: BSD-3-Clause                                       #
###############################################################################
# Hans Pabst (Intel Corp.)
###############################################################################
from string import Template
import libxsmm_utilities
import fnmatch
import sys


if __name__ == "__main__":
    argc = len(sys.argv)
    if 1 < argc:
        # required argument(s)
        filename = sys.argv[1]

        # default configuration if no arguments are given
        precision = 0  # all
        ifversion = 1  # interface
        prefetch = -1  # auto
        mnklist = list()

        # optional argument(s)
        if 2 < argc:
            ivalue = int(sys.argv[2])
            ifversion = (ivalue >> 2)
            precision = (ivalue & 3)
        if 3 < argc:
            prefetch = int(sys.argv[3])
        if 4 < argc:
            mnklist = sorted(libxsmm_utilities.load_mnklist(sys.argv[4:], 0))

        template = Template(open(filename, "r").read())
        if fnmatch.fnmatch(filename, "*.h*"):
            optional = [", ...", ""][0 <= prefetch]
            substitute = {"MNK_INTERFACE_LIST": ""}
            for mnk in mnklist:
                mnkstr = "_".join(map(str, mnk))
                if 2 != precision:
                    pfsig = [
                        optional + ");",
                        ",\n  "
                        "const float* pa, "
                        "const float* pb, "
                        "const float* pc);"
                    ][0 < prefetch]
                    substitute["MNK_INTERFACE_LIST"] += (
                        "\nLIBXSMM_API void libxsmm_smm_"
                        + mnkstr
                        + "(const float* a, const float* b, float* c"
                        + pfsig
                    )
                if 1 != precision:
                    pfsig = [
                        optional + ");",
                        ",\n  "
                        "const double* pa, "
                        "const double* pb, "
                        "const double* pc);"
                    ][0 < prefetch]
                    substitute["MNK_INTERFACE_LIST"] += (
                        "\nLIBXSMM_API void libxsmm_dmm_"
                        + mnkstr
                        + "(const double* a, const double* b, double* c"
                        + pfsig
                    )
                if 0 == precision:
                    substitute["MNK_INTERFACE_LIST"] += "\n"
            if mnklist and 0 != precision:
                substitute["MNK_INTERFACE_LIST"] += "\n"
            print(template.substitute(substitute))
        else:  # Fortran interface
            if 1 > ifversion and 0 != ifversion:
                raise ValueError("Fortran interface level is inconsistent!")
            # Fortran's OPTIONAL allows to always generate an interface
            # with prefetch signature (more flexible usage)
            if 0 == prefetch:
                prefetch = -1
            version, branch, realversion = libxsmm_utilities.version_branch(16)
            major, minor, update, patch = libxsmm_utilities.version_numbers(
                version
            )
            substitute = {
                "VERSION": realversion,
                "BRANCH": branch,
                "MAJOR": major,
                "MINOR": minor,
                "UPDATE": update,
                "PATCH": patch,
                "MNK_INTERFACE_LIST": "",
                "CONTIGUOUS": ["", ", CONTIGUOUS"][1 < ifversion]
            }
            if mnklist:
                substitute["MNK_INTERFACE_LIST"] += "\n"
                for mnk in mnklist:
                    mnkstr = "_".join(map(str, mnk))
                    if 0 == precision:
                        substitute["MNK_INTERFACE_LIST"] += (
                            "\n        "
                            "!DIR$ ATTRIBUTES OFFLOAD:MIC :: libxsmm_smm_"
                            + mnkstr
                            + ", libxsmm_dmm_"
                            + mnkstr
                        )
                    elif 2 != precision:
                        substitute["MNK_INTERFACE_LIST"] += (
                            "\n        "
                            "!DIR$ ATTRIBUTES OFFLOAD:MIC :: libxsmm_smm_"
                            + mnkstr
                        )
                    elif 1 != precision:
                        substitute["MNK_INTERFACE_LIST"] += (
                            "\n        "
                            "!DIR$ ATTRIBUTES OFFLOAD:MIC :: libxsmm_dmm_"
                            + mnkstr
                        )
                substitute["MNK_INTERFACE_LIST"] += "\n        INTERFACE"
                optional = [", OPTIONAL", ""][0 < prefetch]
                bindc = ["", "BIND(C)"][0 < prefetch]
                for mnk in mnklist:
                    mnkstr = "_".join(map(str, mnk))
                    if 2 != precision:
                        pfsiga = [
                            ") BIND(C)\n",
                            ","
                            + "&".rjust(26 - len(mnkstr))
                            + "\n     &    pa, pb, pc) "
                            + bindc
                            + "\n"
                        ][0 != prefetch]
                        pfsigb = [
                            "",
                            "            REAL(C_FLOAT), "
                            "INTENT(IN)" + optional + " :: "
                            "pa(*), "
                            "pb(*), "
                            "pc(*)\n"
                        ][0 != prefetch]
                        substitute["MNK_INTERFACE_LIST"] += (
                            "\n          "
                            "PURE SUBROUTINE libxsmm_smm_"
                            + mnkstr
                            + "(a, b, c"
                            + pfsiga
                            + "            IMPORT :: C_FLOAT\n"
                            "            REAL(C_FLOAT), "
                            "INTENT(IN) :: a(*), b(*)\n"
                            "            REAL(C_FLOAT), "
                            "INTENT(INOUT) :: c(*)\n"
                            + pfsigb
                            + "          END SUBROUTINE"
                        )
                    if 1 != precision:
                        pfsiga = [
                            ") BIND(C)\n",
                            ","
                            + "&".rjust(26 - len(mnkstr))
                            + "\n     &    pa, pb, pc) "
                            + bindc
                            + "\n"
                        ][0 != prefetch]
                        pfsigb = [
                            "",
                            "            REAL(C_DOUBLE), "
                            "INTENT(IN)" + optional + " :: "
                            "pa(*), "
                            "pb(*), "
                            "pc(*)\n"
                        ][0 != prefetch]
                        substitute["MNK_INTERFACE_LIST"] += (
                            "\n          "
                            "PURE SUBROUTINE libxsmm_dmm_"
                            + mnkstr
                            + "(a, b, c"
                            + pfsiga
                            + "            IMPORT :: C_DOUBLE\n"
                            "            REAL(C_DOUBLE), "
                            "INTENT(IN) :: a(*), b(*)\n"
                            "            REAL(C_DOUBLE), "
                            "INTENT(INOUT) :: c(*)\n"
                            + pfsigb
                            + "          END SUBROUTINE"
                        )
                substitute["MNK_INTERFACE_LIST"] += "\n        END INTERFACE"
            print(template.safe_substitute(substitute))
    else:
        sys.tracebacklimit = 0
        raise ValueError(sys.argv[0] + ": wrong number of arguments!")