Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of ctrl_bps. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22"""Driver functions for each subcommand. 

23 

24Driver functions ensure that ensure all setup work is done before running 

25the subcommand method. 

26""" 

27 

28 

29__all__ = [ 

30 "acquire_qgraph_driver", 

31 "cluster_qgraph_driver", 

32 "transform_driver", 

33 "prepare_driver", 

34 "submit_driver", 

35 "report_driver", 

36 "cancel_driver", 

37] 

38 

39 

40import getpass 

41import logging 

42import os 

43import re 

44import time 

45import shutil 

46 

47 

48from lsst.obs.base import Instrument 

49 

50from . import BPS_SEARCH_ORDER, BpsConfig 

51from .pre_transform import acquire_quantum_graph, cluster_quanta 

52from .transform import transform 

53from .prepare import prepare 

54from .submit import submit 

55from .cancel import cancel 

56from .report import report 

57 

58 

59_LOG = logging.getLogger(__name__) 

60 

61 

62def _init_submission_driver(config_file, **kwargs): 

63 """Initialize runtime environment. 

64 

65 Parameters 

66 ---------- 

67 config_file : `str` 

68 Name of the configuration file. 

69 

70 Returns 

71 ------- 

72 config : `lsst.ctrl.bps.BpsConfig` 

73 Batch Processing Service configuration. 

74 """ 

75 config = BpsConfig(config_file, BPS_SEARCH_ORDER) 

76 

77 # Override config with command-line values 

78 # Handle diffs between pipetask argument names vs bps yaml 

79 translation = {"input": "inCollection", 

80 "output_run": "outputRun", 

81 "qgraph": "qgraphFile", 

82 "pipeline": "pipelineYaml"} 

83 for key, value in kwargs.items(): 

84 # Don't want to override config with None or empty string values. 

85 if value: 

86 # pipetask argument parser converts some values to list, 

87 # but bps will want string. 

88 if not isinstance(value, str): 

89 value = ",".join(value) 

90 new_key = translation.get(key, re.sub(r"_(\S)", lambda match: match.group(1).upper(), key)) 

91 config[f".bps_cmdline.{new_key}"] = value 

92 

93 # Set some initial values 

94 config[".bps_defined.timestamp"] = Instrument.makeCollectionTimestamp() 

95 if "operator" not in config: 

96 config[".bps_defined.operator"] = getpass.getuser() 

97 

98 if "outCollection" in config: 

99 raise KeyError("outCollection is deprecated. Replace all outCollection references with outputRun.") 

100 

101 if "outputRun" not in config: 

102 raise KeyError("Must specify the output run collection using outputRun") 

103 

104 if "uniqProcName" not in config: 

105 config[".bps_defined.uniqProcName"] = config["outputRun"].replace("/", "_") 

106 

107 if "submitPath" not in config: 

108 raise KeyError("Must specify the submit-side run directory using submitPath") 

109 

110 # make submit directory to contain all outputs 

111 submit_path = config["submitPath"] 

112 os.makedirs(submit_path, exist_ok=True) 

113 config[".bps_defined.submitPath"] = submit_path 

114 

115 # save copy of configs (orig and expanded config) 

116 shutil.copy2(config_file, submit_path) 

117 with open(f"{submit_path}/{config['uniqProcName']}_config.yaml", "w") as fh: 

118 config.dump(fh) 

119 

120 return config 

121 

122 

123def acquire_qgraph_driver(config_file, **kwargs): 

124 """Read a quantum graph from a file or create one from pipeline definition. 

125 

126 Parameters 

127 ---------- 

128 config_file : `str` 

129 Name of the configuration file. 

130 

131 Returns 

132 ------- 

133 config : `lsst.ctrl.bps.BpsConfig` 

134 Updated configuration. 

135 qgraph : `lsst.pipe.base.graph.QuantumGraph` 

136 A graph representing quanta. 

137 """ 

138 stime = time.time() 

139 config = _init_submission_driver(config_file, **kwargs) 

140 submit_path = config[".bps_defined.submitPath"] 

141 _LOG.info("Acquiring QuantumGraph (it will be created from pipeline definition if needed)") 

142 qgraph_file, qgraph, execution_butler_dir = acquire_quantum_graph(config, out_prefix=submit_path) 

143 config[".bps_defined.executionButlerDir"] = execution_butler_dir 

144 _LOG.info("Run QuantumGraph file %s", qgraph_file) 

145 config[".bps_defined.runQgraphFile"] = qgraph_file 

146 _LOG.info("Acquiring QuantumGraph took %.2f seconds", time.time() - stime) 

147 

148 return config, qgraph 

149 

150 

151def cluster_qgraph_driver(config_file, **kwargs): 

152 """Group quanta into clusters. 

153 

154 Parameters 

155 ---------- 

156 config_file : `str` 

157 Name of the configuration file. 

158 

159 Returns 

160 ------- 

161 config : `lsst.ctrl.bps.BpsConfig` 

162 Updated configuration. 

163 clustered_qgraph : `lsst.ctrl.bps.ClusteredQuantumGraph` 

164 A graph representing clustered quanta. 

165 """ 

166 stime = time.time() 

167 config, qgraph = acquire_qgraph_driver(config_file, **kwargs) 

168 _LOG.info("Clustering quanta") 

169 clustered_qgraph = cluster_quanta(config, qgraph, config["uniqProcName"]) 

170 _LOG.info("Clustering quanta took %.2f seconds", time.time() - stime) 

171 

172 submit_path = config[".bps_defined.submitPath"] 

173 _, save_clustered_qgraph = config.search("saveClusteredQgraph", opt={"default": False}) 

174 if save_clustered_qgraph: 

175 clustered_qgraph.save(os.path.join(submit_path, "bps_clustered_qgraph.pickle")) 

176 _, save_dot = config.search("saveDot", opt={"default": False}) 

177 if save_dot: 

178 clustered_qgraph.draw(os.path.join(submit_path, "bps_clustered_qgraph.dot")) 

179 return config, clustered_qgraph 

180 

181 

182def transform_driver(config_file, **kwargs): 

183 """Create a workflow for a specific workflow management system. 

184 

185 Parameters 

186 ---------- 

187 config_file : `str` 

188 Name of the configuration file. 

189 

190 Returns 

191 ------- 

192 generic_workflow_config : `lsst.ctrl.bps.BpsConfig` 

193 Configuration to use when creating the workflow. 

194 generic_workflow : `lsst.ctrl.bps.BaseWmsWorkflow` 

195 Representation of the abstract/scientific workflow specific to a given 

196 workflow management system. 

197 """ 

198 stime = time.time() 

199 config, clustered_qgraph = cluster_qgraph_driver(config_file, **kwargs) 

200 submit_path = config[".bps_defined.submitPath"] 

201 _LOG.info("Creating Generic Workflow") 

202 generic_workflow, generic_workflow_config = transform(config, clustered_qgraph, submit_path) 

203 _LOG.info("Creating Generic Workflow took %.2f seconds", time.time() - stime) 

204 _LOG.info("Generic Workflow name %s", generic_workflow.name) 

205 

206 _, save_workflow = config.search("saveGenericWorkflow", opt={"default": False}) 

207 if save_workflow: 

208 with open(os.path.join(submit_path, "bps_generic_workflow.pickle"), "wb") as outfh: 

209 generic_workflow.save(outfh, "pickle") 

210 _, save_dot = config.search("saveDot", opt={"default": False}) 

211 if save_dot: 

212 with open(os.path.join(submit_path, "bps_generic_workflow.dot"), "w") as outfh: 

213 generic_workflow.draw(outfh, "dot") 

214 return generic_workflow_config, generic_workflow 

215 

216 

217def prepare_driver(config_file, **kwargs): 

218 """Create a representation of the generic workflow. 

219 

220 Parameters 

221 ---------- 

222 config_file : `str` 

223 Name of the configuration file. 

224 

225 Returns 

226 ------- 

227 wms_config : `lsst.ctrl.bps.BpsConfig` 

228 Configuration to use when creating the workflow. 

229 workflow : `lsst.ctrl.bps.BaseWmsWorkflow` 

230 Representation of the abstract/scientific workflow specific to a given 

231 workflow management system. 

232 """ 

233 stime = time.time() 

234 generic_workflow_config, generic_workflow = transform_driver(config_file, **kwargs) 

235 submit_path = generic_workflow_config[".bps_defined.submitPath"] 

236 _LOG.info("Creating specific implementation of workflow") 

237 wms_workflow = prepare(generic_workflow_config, generic_workflow, submit_path) 

238 wms_workflow_config = generic_workflow_config 

239 _LOG.info("Creating specific implementation of workflow took %.2f seconds", time.time() - stime) 

240 print(f"Submit dir: {wms_workflow.submit_path}") 

241 return wms_workflow_config, wms_workflow 

242 

243 

244def submit_driver(config_file, **kwargs): 

245 """Submit workflow for execution. 

246 

247 Parameters 

248 ---------- 

249 config_file : `str` 

250 Name of the configuration file. 

251 """ 

252 wms_workflow_config, wms_workflow = prepare_driver(config_file, **kwargs) 

253 submit(wms_workflow_config, wms_workflow) 

254 print(f"Run Id: {wms_workflow.run_id}") 

255 

256 

257def report_driver(wms_service, run_id, user, hist_days, pass_thru): 

258 """Print out summary of jobs submitted for execution. 

259 

260 Parameters 

261 ---------- 

262 wms_service : `str` 

263 Name of the class. 

264 run_id : `str` 

265 A run id the report will be restricted to. 

266 user : `str` 

267 A user name the report will be restricted to. 

268 hist_days : int 

269 Number of days 

270 pass_thru : `str` 

271 A string to pass directly to the WMS service class. 

272 """ 

273 report(wms_service, run_id, user, hist_days, pass_thru) 

274 

275 

276def cancel_driver(wms_service, run_id, user, require_bps, pass_thru): 

277 """Cancel submitted workflows. 

278 

279 Parameters 

280 ---------- 

281 wms_service : `str` 

282 Name of the Workload Management System service class. 

283 run_id : `str` 

284 ID or path of job that should be canceled. 

285 user : `str` 

286 User whose submitted jobs should be canceled. 

287 require_bps : `bool` 

288 Whether to require given run_id/user to be a bps submitted job. 

289 pass_thru : `str` 

290 Information to pass through to WMS. 

291 """ 

292 cancel(wms_service, run_id, user, require_bps, pass_thru)