forked from edf-hpc/verrou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateBackendInterOperator.py
executable file
·124 lines (90 loc) · 4.34 KB
/
generateBackendInterOperator.py
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
#!/usr/bin/env python3
# This file is part of Verrou, a FPU instrumentation tool.
# Copyright (C) 2014-2021 EDF
# F. Févotte <[email protected]>
# B. Lathuilière <[email protected]>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307, USA.
# The GNU Lesser General Public License is contained in the file COPYING.
import sys
import re
def generateNargs(fileOut, fileNameTemplate, listOfBackend, listOfOp, nargs, post=""):
templateStr=open(fileNameTemplate, "r").readlines()
FctNameRegExp=re.compile("(.*)FCTNAME\(([^,]*),([^)]*)\)(.*)")
BckNameRegExp=re.compile("(.*)BACKENDFUNC\(([^)]*)\)(.*)")
for backend in listOfBackend:
for op in listOfOp:
if nargs in [1,2]:
applyTemplate(fileOut, templateStr, FctNameRegExp, BckNameRegExp, backend,op, post)
if nargs==3:
sign=""
if "msub" in op:
sign="-"
applyTemplate(fileOut, templateStr,FctNameRegExp,BckNameRegExp, backend, op, post, sign)
def applyTemplate(fileOut, templateStr, FctRegExp, BckRegExp, backend, op, post, sign=None):
fileOut.write("// generation of operation %s backend %s\n"%(op,backend))
def fctName(typeVal,opt):
return "vr_"+backend+post+op+typeVal+opt
def bckName(typeVal):
if sign!="-":
return "interflop_"+backend+"_"+op+"_"+typeVal
else:
return "interflop_"+backend+"_"+op.replace("sub","add")+"_"+typeVal
def bckNamePost(typeVal):
if sign!="-":
return "interflop_"+post+"_"+op+"_"+typeVal
else:
return "interflop_"+post+"_"+op.replace("sub","add")+"_"+typeVal
contextName="backend_"+backend+"_context"
contextNamePost="backend_"+post+"_context"
for line in templateStr:
if "CONTEXT" in line:
line=line.replace("CONTEXT", contextName)
if "SIGN" in line:
if sign!=None:
line=line.replace("SIGN", sign)
else:
print("Generation failed")
sys.exit()
result=FctRegExp.match(line)
if result!=None:
res=result.group(1) + fctName(result.group(2), result.group(3)) + result.group(4)
fileOut.write(res+"\n")
continue
result=BckRegExp.match(line)
if result!=None:
res=result.group(1) + bckName(result.group(2)) + result.group(3)
fileOut.write(res+"\n")
if post!="":
res=result.group(1) + bckNamePost(result.group(2)) + result.group(3)
res=res.replace(contextName, contextNamePost)
fileOut.write(res+"\n")
continue
fileOut.write(line)
if __name__=="__main__":
fileNameOutput="vr_generated_from_templates.h"
fileOut=open(fileNameOutput,"w")
fileOut.write("//Generated by %s\n"%(str(sys.argv)[1:-1]))
template1Args="vr_interp_operator_template_cast.h"
listOfOp1Args=["cast"]
generateNargs(fileOut,template1Args, ["verrou","mcaquad","checkdenorm"], listOfOp1Args, 1)
template2Args="vr_interp_operator_template_2args.h"
listOfOp2Args=["add","sub","mul","div"]
generateNargs(fileOut,template2Args, ["verrou","mcaquad","checkdenorm"], listOfOp2Args, 2)
listOfOp2Args=["add","sub"]
generateNargs(fileOut,template2Args, ["verrou","mcaquad","checkdenorm"], listOfOp2Args, 2, post="checkcancellation")
template3Args="vr_interp_operator_template_3args.h"
listOfOp3Args=["madd","msub"]
generateNargs(fileOut,template3Args, ["verrou","mcaquad","checkdenorm"], listOfOp3Args, 3)
generateNargs(fileOut,template3Args, ["verrou","mcaquad","checkdenorm"], listOfOp3Args, 3, post="checkcancellation")
fileOut.close()