-
Notifications
You must be signed in to change notification settings - Fork 18
/
wrench-init
executable file
·390 lines (358 loc) · 14.6 KB
/
wrench-init
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python
#
# Copyright (c) 2018. The WRENCH Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
import argparse
import logging
import os
__author__ = "Rafael Ferreira da Silva"
logger = logging.getLogger(__name__)
def _configure_logging(debug):
"""
Configure the application's logging.
:param debug: whether debugging is enabled
"""
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
def _create_subdirectories(project_dir):
'''
Create the subdirectories for the project
:param project_dir: project directory
'''
logger.debug('Creating subdirectories structure')
sub_dirs = ['src', 'test', 'doc', 'build', 'data', 'data/platform-files', 'data/workflow-files']
for sub_dir in sub_dirs:
if not os.path.isdir(project_dir + '/' + sub_dir):
os.mkdir(project_dir + '/' + sub_dir)
logger.debug(' Created subdirectory: %s' % project_dir + '/' + sub_dir)
def _write_contents(filename, contents):
'''
Write a list of strings to a file
:param filename: name of the file
:param contents: list of strings
'''
with open(filename, 'w') as f:
for line in contents:
f.write(line + '\n')
def _write_cmakelists(project_dir):
'''
Write the CMakeLists.txt file
:param project_dir: project directory
'''
logger.debug('Writing CMakeLists.txt file')
_write_contents(project_dir + '/CMakeLists.txt', [
'cmake_minimum_required(VERSION 3.2)',
'message(STATUS "Cmake version ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")',
'',
'project(%s)',
'',
'add_definitions("-Wall -Wno-unused-variable -Wno-unused-private-field")',
'',
'set(CMAKE_CXX_STANDARD 11)',
'',
'# include directories for dependencies and WRENCH libraries',
'include_directories(src/ /usr/local/include /usr/local/include/wrench)',
'',
'# source files',
'set(SOURCE_FILES',
' src/SimpleWMS.cpp'
' src/main.cpp',
' )',
'',
'# test files',
'set(TEST_FILES',
' )',
'',
'# wrench library and dependencies',
'find_library(WRENCH_LIBRARY NAMES wrench)',
'find_library(SIMGRID_LIBRARY NAMES simgrid)',
'find_library(PUGIXML_LIBRARY NAMES pugixml)',
'find_library(LEMON_LIBRARY NAMES emon)',
'find_library(GTEST_LIBRARY NAMES gtest)',
'',
'# generating the executable',
'add_executable(my-executable ${SOURCE_FILES})',
'target_link_libraries(my-executable',
' ${WRENCH_LIBRARY}',
' ${SIMGRID_LIBRARY}',
' ${PUGIXML_LIBRARY}',
' ${LEMON_LIBRARY}',
' )',
'',
'install(TARGETS my-executable DESTINATION bin)',
'',
'# generating unit tests',
'add_executable(unit_tests EXCLUDE_FROM_ALL',
' ${SOURCE_FILES}',
' ${TEST_FILES}',
' )',
'target_link_libraries(unit_tests',
' ${GTEST_LIBRARY} wrench -lpthread -lm',
' )'
])
def _write_main(project_dir):
logger.debug('Writing src/main.cpp example file')
_write_contents(project_dir + '/src/main.cpp', [
'/**',
' * ADD A HEADER TO YOUR WRENCH SIMULATOR',
' */',
'',
'#include <wrench.h>',
'#include "SimpleWMS.h"',
'',
'int main(int argc, char **argv) {',
'',
' // Declaration of the top-level WRENCH simulation object',
' wrench::Simulation simulation;',
'',
' // Initialization of the simulation',
' simulation.init(&argc, argv);',
'',
' // Parsing of the command-line arguments for this WRENCH simulation',
' if (argc != 3) {',
' std::cerr << "Usage: " << argv[0] << " <xml platform file> <workflow file>" << std::endl;',
' exit(1);',
' }',
'',
' // The first argument is the platform description file, written in XML following the SimGrid-defined DTD',
' char *platform_file = argv[1];',
' // The second argument is the workflow description file, written in XML using the DAX DTD',
' char *workflow_file = argv[2];',
'',
' // Reading and parsing the workflow description file to create a wrench::Workflow object',
' wrench::Workflow workflow;',
' workflow.loadFromDAXorJSON(workflow_file, "1000Gf");',
'',
' // Reading and parsing the platform description file to instantiate a simulated platform',
' simulation.instantiatePlatform(platform_file);',
'',
' // Get a vector of all the hosts in the simulated platform',
' std::vector<std::string> hostname_list = simulation.getHostnameList();',
'',
' // Instantiate a storage service',
' std::string storage_host = hostname_list[(hostname_list.size() > 2) ? 2 : 1];',
' wrench::StorageService *storage_service = simulation.add(new wrench::SimpleStorageService(storage_host, 10000000000000.0));',
'',
' // Construct a list of hosts (in this example only one host)',
' std::string executor_host = hostname_list[(hostname_list.size() > 1) ? 1 : 0];',
' std::vector<std::string> execution_hosts = {executor_host};',
'',
' // Instantiate a cloud service',
' std::string wms_host = hostname_list[0];',
' wrench::CloudService *cloud_service = new wrench::CloudService(',
' wms_host, execution_hosts, 0, {},',
' {{wrench::CloudServiceMessagePayload::STOP_DAEMON_MESSAGE_PAYLOAD, "1024"}});',
'',
' // Add the cloud service to the simulation',
' try {',
' simulation.add(cloud_service);',
' } catch (std::invalid_argument &e) {',
' std::cerr << "Error: " << e.what() << std::endl;',
' std::exit(1);',
' }',
'',
' // Create a list of compute services that will be used by the WMS',
' std::set<wrench::ComputeService *> compute_services;',
' compute_services.insert(cloud_service);',
'',
' // Create a list of storage services that will be used by the WMS',
' std::set<wrench::StorageService *> storage_services;',
' storage_services.insert(storage_service);',
'',
' // Instantiate a WMS',
' wrench::WMS *wms = simulation.add(',
' new SimpleWMS(std::unique_ptr<wrench::CloudStandardJobScheduler>(',
' new wrench::CloudStandardJobScheduler(storage_service)),',
' nullptr, compute_services, storage_services, wms_host));',
' wms->addWorkflow(&workflow);',
'',
' // Instantiate a file registry service',
' std::string file_registry_service_host = hostname_list[(hostname_list.size() > 2) ? 1 : 0];',
' wrench::FileRegistryService * file_registry_service =',
' new wrench::FileRegistryService(file_registry_service_host);',
' simulation.add(file_registry_service);',
'',
' // It is necessary to store, or "stage", input files',
' std::map<std::string, wrench::WorkflowFile *> input_files = workflow.getInputFiles();',
' try {',
' simulation.stageFiles(input_files, storage_service);',
' } catch (std::runtime_error &e) {',
' std::cerr << "Exception: " << e.what() << std::endl;',
' return 0;',
' }',
'',
' // Launch the simulation',
' try {',
' simulation.launch();',
' } catch (std::runtime_error &e) {',
' std::cerr << "Exception: " << e.what() << std::endl;',
' return 0;',
' }',
'',
' return 0;',
'}',
''
])
def _write_simple_wms(project_dir):
logger.debug('Writing src/SimpleWMS.h example file')
_write_contents(project_dir + '/src/SimpleWMS.h', [
'/**',
' * ADD A HEADER TO YOUR WRENCH WMS',
' */',
'',
'#ifndef WRENCH_SIMPLEWMS_H',
'#define WRENCH_SIMPLEWMS_H',
'',
'#include <wrench-dev.h>',
'',
'class Simulation;',
'',
'/**',
' * @brief A simple WMS implementation',
' */',
'class SimpleWMS : public wrench::WMS {',
'public:',
' SimpleWMS(std::unique_ptr<wrench::StandardJobScheduler> standard_job_scheduler,',
' std::unique_ptr<wrench::PilotJobScheduler> pilot_job_scheduler,',
' const std::set<wrench::ComputeService *> &compute_services,',
' const std::set<wrench::StorageService *> &storage_services,',
' const std::string &hostname);',
'',
'private:',
' int main() override;',
'',
' /** @brief The job manager */',
' std::shared_ptr<wrench::JobManager> job_manager;',
'};',
'',
'#endif //WRENCH_SIMPLEWMS_H',
''
])
logger.debug('Writing src/SimpleWMS.cpp example file')
_write_contents(project_dir + '/src/SimpleWMS.cpp', [
'/**',
' * ADD A HEADER TO YOUR WRENCH WMS',
' */',
'',
'#include <iostream>',
'',
'#include "SimpleWMS.h"',
'',
'XBT_LOG_NEW_DEFAULT_CATEGORY(simple_wms, "Log category for Simple WMS");',
'',
'/**',
' * @brief Create a Simple WMS with a workflow instance, a scheduler implementation, and a list of compute services',
' */',
'SimpleWMS::SimpleWMS(std::unique_ptr<wrench::StandardJobScheduler> standard_job_scheduler,',
' std::unique_ptr<wrench::PilotJobScheduler> pilot_job_scheduler,',
' const std::set<wrench::ComputeService *> &compute_services,',
' const std::set<wrench::StorageService *> &storage_services,',
' const std::string &hostname) : wrench::WMS(',
' std::move(standard_job_scheduler),',
' std::move(pilot_job_scheduler),',
' compute_services,',
' storage_services,',
' {}, nullptr,',
' hostname,',
' "simple") {}',
'',
'/**',
' * @brief main method of the SimpleWMS daemon',
' */',
'int SimpleWMS::main() {',
'',
' wrench::TerminalOutput::setThisProcessLoggingColor(wrench::TerminalOutput::COLOR_GREEN);',
'',
' // Check whether the WMS has a deferred start time',
' checkDeferredStart();',
'',
' WRENCH_INFO("About to execute a workflow with %lu tasks", this->getWorkflow()->getNumberOfTasks());',
'',
' // Create a job manager',
' this->job_manager = this->createJobManager();',
'',
' // Create a data movement manager',
' std::shared_ptr<wrench::DataMovementManager> data_movement_manager = this->createDataMovementManager();',
'',
' while (true) {',
' // Get the ready tasks',
' std::vector<wrench::WorkflowTask *> ready_tasks = this->getWorkflow()->getReadyTasks();',
'',
' // Get the available compute services',
' std::set<wrench::ComputeService *> compute_services = this->getAvailableComputeServices();',
'',
' if (compute_services.empty()) {',
' WRENCH_INFO("Aborting - No compute services available!");',
' break;',
' }',
'',
' // Run ready tasks with defined scheduler implementation',
' this->getStandardJobScheduler()->scheduleTasks(this->getAvailableComputeServices(), ready_tasks);',
'',
' // Wait for a workflow execution event, and process it',
' try {',
' this->waitForAndProcessNextEvent();',
' } catch (wrench::WorkflowExecutionException &e) {',
' WRENCH_INFO("Error while getting next execution event (%s)... ignoring and trying again",',
' (e.getCause()->toString().c_str()));',
' continue;',
' }',
'',
' if (this->getWorkflow()->isDone()) {',
' break;',
' }',
' }',
'',
' wrench::S4U_Simulation::sleep(10);',
'',
' this->job_manager.reset();',
'',
' return 0;',
'}'
])
def _write_simple_scheduler(project_dir):
logger.debug('Writing src/SimpleScheduler.h example file')
_write_contents(project_dir + '/src/SimpleScheduler.h', [
])
def main():
# Application's arguments
parser = argparse.ArgumentParser(description='Create a skeleton for a WRENCH-based project.')
parser.add_argument('project_dir', metavar='PROJECT_DIR', help='Project directory name')
parser.add_argument('-d', '--debug', action='store_true', help='Print debug messages to stderr')
parser.add_argument('-f', '--force', action='store_true',
help='Overwrites existing project directory (use sparingly)')
args = parser.parse_args()
# Configure logging
_configure_logging(args.debug)
# Sanity check
if os.path.isdir(args.project_dir) and not args.force:
logger.error('The provided project directory already exists:\n\t%s\nUse --force to overwrite the directory' \
% args.project_dir)
exit(1)
logger.info('Creating WRENCH skeleton project at: %s' % args.project_dir)
if not os.path.isdir(args.project_dir):
os.mkdir(args.project_dir)
# subdirectories structure
_create_subdirectories(args.project_dir)
# write CMakeLists.txt
_write_cmakelists(args.project_dir)
# create SimpleWMS
_write_simple_wms(args.project_dir)
# create SimpleScheduler
_write_simple_scheduler(args.project_dir)
# create main.cpp
_write_main(args.project_dir)
if __name__ == '__main__':
main()