Debian Codegraph @

music-config/config.py

raw
1 #
2 # This file is part of MUSIC.
3 # Copyright (C) 2012, 2021 Mikael Djurfeldt
4 #
5 # MUSIC is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # MUSIC is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # This API allows for querying the MPI rank and specification of the
20 # information in MUSIC configuration files
21
22 import os
23
24 from music.config.predict_rank import predictRank
25
26 # This function now defined in predict_rank.py
27 #
28 #def predictRank ():
29 # """
30 # Returns the predicted MPI rank of this process
31 #
32 # :rtype: Integer
33 # """
34 # return 0
35
36 CONFIGVARNAME = '_MUSIC_CONFIG_'
37
38 OUTPUT = '0'
39 INPUT = '1'
40
41
42 def launchedByMusic ():
43 return CONFIGVARNAME in os.environ
44
45
46 def supersedeArgv (argv):
47 """
48 Replace argc and argv before MUSIC initialization
49 """
50 # Should insert escape characters
51 args = argv[0]
52 for arg in argv[1:]:
53 args += ' ' + arg
54 os.environ['MUSIC_ARGV'] = args
55
56
57 def postponeSetup ():
58 """
59 Postpones processing of configuration info until the creation of
60 the first port. Must be called before creation of MUSIC::Setup.
61 """
62 if not thisApp:
63 raise RuntimeError('must define Application before calling postponeSetup ()')
64 os.environ[CONFIGVARNAME] = 'POSTPONE:' + str (thisApp.number)
65
66
67 portCodes = {}
68 code = 0
69
70 def portCode (appName, portName):
71 global code
72 key = (appName, portName)
73 if key in portCodes:
74 return portCodes[key]
75 portCodes[key] = code
76 ans = code
77 code += 1
78 return ans
79
80
81 class ConnectivityMap (object):
82 def __init__ (self):
83 self.ports = {}
84
85 def register (self, portName, direction, width, *connection):
86 if portName in self.ports:
87 (direction_, width_, connections) = self.ports[portName]
88 #*fixme* error checks here
89 else:
90 connections = []
91 self.ports[portName] = (direction, width, connections)
92 connections.append (connection)
93
94 def conf (self):
95 conf = str (len (self.ports)) # size
96 for portName in self.ports:
97 (direction, width, connections) = self.ports[portName]
98 conf += ':' + portName + ':' + direction + ':' + width
99 conf += ':' + str (len (connections))
100 for connection in connections:
101 for item in connection:
102 conf += ':' + item
103 return conf
104
105
106 configDict = {}
107 appNumber = 0
108 rankSum = 0
109 thisRank = predictRank ()
110 thisApp = None
111
112 class Application (object):
113 def __init__ (self, np = None, binary = None, args = None, name = None):
114 global appNumber, rankSum, thisApp
115 self.name = name
116 self.number = appNumber
117 appNumber += 1
118 self.np = np
119 self.leader = rankSum
120 rankSum += np
121 self.this = False
122 if self.leader <= thisRank and thisRank < rankSum:
123 self.this = True
124 thisApp = self
125
126 self.configDict = {}
127 self.connectivityMap = ConnectivityMap ()
128
129 if binary:
130 self.define ('binary', binary)
131 if args:
132 self.define ('args', args)
133
134 applicationMap.register (self)
135
136 def __getitem__ (self, varName):
137 if varName in self.configDict:
138 return self.configDict[varName]
139 return configDict[varName]
140
141 def define (self, varName, value):
142 """
143 Define configuration variable varName to value value.
144 """
145 self.configDict[varName] = str (value)
146
147 def connect (self, fromPort, toApp, toPort, width):
148 """
149 Connect fromPort to toPort.
150 """
151 connections.append ((self, fromPort, toApp, toPort, str (width)))
152
153
154 class ApplicationMap (object):
155 def __init__ (self):
156 self.applications = []
157 self.nameMap = {}
158
159 def register (self, app):
160 if not app.name:
161 app.name = 'application'
162 if app.name in self.nameMap:
163 # Adjust the name to become unique
164 record = self.nameMap[app.name]
165 base, suffix = record
166 if base == app.name:
167 suffix += 1
168 record[1] = suffix
169 else:
170 base = app.name
171 suffix = 0
172 self.nameMap[base] = [base, suffix]
173 app.name += str (suffix)
174 else:
175 self.nameMap[app.name] = [app.name, 0]
176 self.applications.append (app)
177
178 def conf (self):
179 conf = str (len (self.applications))
180 for app in self.applications:
181 conf += ':' + app.name + ':' + str (app.np)
182 return conf
183
184 applicationMap = ApplicationMap ()
185
186
187 def define (varName, value):
188 """
189 Define configuration variable varName to value value.
190 """
191 configDict[varName] = str (value)
192
193
194 # Communication algorithms
195
196 commAlgorithms = { 'collective' : 0, 'point-to-point' : 1 }
197
198 # Processing methods
199
200 procMethods = { 'tree' : 0, 'table' : 1 }
201
202
203 def connect (fromApp, fromPort, toApp, toPort, width,
204 commAlgName = 'collective', procMethodName = 'table'):
205 """
206 Connect fromPort to toPort specifying port width width.
207 """
208 width = str (width)
209 commAlg = commAlgorithms[commAlgName]
210 procMethod = procMethods[procMethodName]
211 fromApp.connectivityMap.register (fromPort, OUTPUT, width,
212 toApp.name, toPort,
213 str (portCode (toApp.name, toPort)),
214 str (toApp.number), str (toApp.np),
215 str (commAlg), str (procMethod))
216 toApp.connectivityMap.register (toPort, INPUT, width,
217 toApp.name, toPort,
218 str (portCode (toApp.name, toPort)),
219 str (fromApp.number), str (fromApp.np),
220 str (commAlg), str (procMethod))
221
222
223 configured = False
224
225 def configure ():
226 """
227 Configure the MUSIC library using the information provided by
228 define and connect.
229 """
230 conf = thisApp.name \
231 + ':' + str (thisApp.number) \
232 + ':' + applicationMap.conf () \
233 + ':' + thisApp.connectivityMap.conf ()
234
235 configDict.update (thisApp.configDict)
236
237 for key in configDict:
238 conf += ':' + key + '=' + configDict[key]
239
240 os.environ[CONFIGVARNAME] = conf
241
242 configured = True
243
244
245 def launch ():
246 if not configured:
247 configure ()
248 binary = thisApp['binary']
249 os.execvp (binary, [binary] + thisApp['args'].split (' '))