-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmasterworker-v3_34.cpp
290 lines (236 loc) · 12 KB
/
masterworker-v3_34.cpp
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
/* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
/* ************************************************************************* */
/* Take this tutorial online: https://simgrid.org/doc/latest/Tutorial_Algorithms.html */
/* ************************************************************************* */
#include <simgrid/s4u.hpp>
namespace sg4 = simgrid::s4u;
#include <stdlib.h>
XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker_fun, "Messages specific for this example");
double double_randfrom(double min, double max)
{
double range = (max - min);
double div = RAND_MAX / range;
return min + (rand() / div);
}
// Inclusive
double int_randfrom(int min, int max)
{
return min + (rand() % (max - min + 1));
}
// master-begin
static void master(std::vector<std::string> args) {
long num_tasks;
double min_comp_size, max_comp_size;
double min_comm_size, max_comm_size;
long num_workers;
xbt_assert(args.size()==7, "The master function expects 7 arguments from the XML deployment file");
xbt_assert(sscanf(args.at(1).c_str(),"%ld", &num_tasks) == 1, "Invalid number of tasks"); /* - Number of tasks */
xbt_assert(sscanf(args.at(2).c_str(),"%lf", &min_comp_size) == 1, "Invalid minimum computation size"); /* - Number of tasks */
xbt_assert(sscanf(args.at(3).c_str(),"%lf", &max_comp_size) == 1, "Invalid maximum computation size"); /* - Number of tasks */
xbt_assert(sscanf(args.at(4).c_str(),"%lf", &min_comm_size) == 1, "Invalid minimum communication size"); /* - Number of tasks */
xbt_assert(sscanf(args.at(5).c_str(),"%lf", &max_comm_size) == 1, "Invalid maximum communication size"); /* - Number of tasks */
xbt_assert(sscanf(args.at(6).c_str(),"%ld", &num_workers) == 1, "Invalid number of workers"); /* - Number of tasks */
XBT_INFO("Got %ld workers and %ld tasks to process", num_workers, num_tasks);
sg4::Mailbox* master_mailbox = sg4::Mailbox::by_name("master_mailbox");
for (int i = 0; i < num_tasks; i++) { /* For each task to be executed: */
/* Wait for a worker request */
auto worker_id = master_mailbox->get_unique<long>();
/* - Send the computation cost to that worker */
char mailbox_name[80];
snprintf(mailbox_name, 79, "worker-%ld", *worker_id);
sg4::Mailbox* mailbox = sg4::Mailbox::by_name(mailbox_name);
XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, num_tasks, mailbox->get_cname());
double compute_cost = double_randfrom(min_comp_size, max_comp_size);
double communication_cost = double_randfrom(min_comm_size, max_comm_size);
mailbox->put(new double(compute_cost), communication_cost);
}
XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
for (unsigned int i = 0; i < num_workers; i++) {
/* Wait for a worker request */
auto worker_id = master_mailbox->get_unique<long>();
/* - Send the computation cost to that worker */
char mailbox_name[80];
snprintf(mailbox_name,79, "worker-%ld", *worker_id);
sg4::Mailbox* mailbox = sg4::Mailbox::by_name(mailbox_name);
mailbox->put(new double(-1.0), 0);
}
XBT_INFO("ALL DONE");
}
// master-end
// worker-begin
static void worker(std::vector<std::string> args)
{
long id;
xbt_assert(args.size() == 2, "The worker expects exactly one argument");
xbt_assert(sscanf(args.at(1).c_str(),"%lu", &id) == 1, "Invalid id");
// const sg4::Host* my_host = sg4::this_actor::get_host();
char mailbox_name[80];
snprintf(mailbox_name,79, "worker-%ld", id);
sg4::Mailbox* mailbox = sg4::Mailbox::by_name(mailbox_name);
sg4::Mailbox* master_mailbox = sg4::Mailbox::by_name("master_mailbox");
double compute_cost;
do {
master_mailbox->put(new long(id), 1024);
auto msg = mailbox->get_unique<double>();
compute_cost = *msg;
if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
sg4::this_actor::execute(compute_cost);
} while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */
XBT_INFO("Exiting now.");
}
// worker-end
void create_platform_file(std::string filepath,
int num_hosts,
int num_cores_per_host,
double min_core_speed,
double max_core_speed,
int num_links,
double min_link_bandwidth,
double max_link_bandwidth,
int route_length) {
FILE *pf = fopen(filepath.c_str(), "w");
// XML Header
fprintf(pf, "<?xml version='1.0'?>\n");
fprintf(pf, "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
fprintf(pf, "<platform version=\"4.1\">\n");
fprintf(pf, "<zone id=\"world\" routing=\"Full\">\n\n");
// Create master hosts
fprintf(pf, " <host id=\"MasterHost\" speed=\"1Mf,0.5Mf,0.2Mf\" core=\"1\">\n\n");
fprintf(pf, " <prop id=\"wattage_per_state\" value=\"100.0:120.0:200.0, 93.0:115.0:170.0, 90.0:110.0:150.0\" />\n");
fprintf(pf, " <prop id=\"wattage_off\" value=\"10\" />\n");
fprintf(pf, " </host>\n");
// Create worker hosts
for (int i=0; i < num_hosts; i++) {
double core_speed = double_randfrom(min_core_speed, max_core_speed);
fprintf(pf, " <host id=\"WorkerHost-%d\" speed=\"%.2lfMf,%.2lfMf,%.2lfMf\" core=\"%d\">\n", i, core_speed, core_speed * 0.5, core_speed * 0.2, num_cores_per_host, num_cores_per_host);
fprintf(pf, " <prop id=\"wattage_per_state\" value=\"100.0:93.33333333333333:200.0, 93.0:90.0:170.0, 90.0:90.0:150.0\" />\n");
fprintf(pf, " <prop id=\"wattage_off\" value=\"10\" />\n");
fprintf(pf, " </host>\n");
}
// Create links
fprintf(pf, "\n");
for (int i=0; i < num_links; i++) {
fprintf(pf, " <link id=\"Link-%d\" bandwidth=\"%.2lfMBps\" latency=\"100us\" />\n", i, double_randfrom(min_link_bandwidth, max_link_bandwidth));
}
// Create routes
fprintf(pf, "\n");
int link_ids[route_length];
for (int i=0; i < num_hosts; i++) {
fprintf(pf, " <route src=\"MasterHost\" dst=\"WorkerHost-%d\">\n", i);
for (int j=0; j < route_length; j++) {
int id = -1;
while(id == -1) {
id = int_randfrom(0, num_links-1);
for (int k=0; k < j; k++) {
if (link_ids[k] == id) {
id = -1;
break;
}
}
if (id != -1) {
link_ids[j] = id;
break;
}
}
}
for (int j=0; j < route_length; j++) {
fprintf(pf, " <link_ctn id=\"Link-%d\"/>\n", link_ids[j]);
}
fprintf(pf, " </route>\n");
}
// XML footer
fprintf(pf, "</zone>\n");
fprintf(pf, "</platform>\n");
fclose(pf);
}
void create_deployment_file(std::string filepath,
int num_hosts,
int num_cores_per_host,
int num_tasks,
double min_computation,
double max_computation,
double min_data_size,
double max_data_size) {
FILE *df = fopen(filepath.c_str(), "w");
// XML Header
fprintf(df, "<?xml version='1.0'?>\n");
fprintf(df, "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
fprintf(df, "<platform version=\"4.1\">\n");
// Create master process
fprintf(df, " <actor host=\"MasterHost\" function=\"master\">\n");
fprintf(df, " <argument value=\"%d\" />\n", num_tasks);
fprintf(df, " <argument value=\"%.2lf\" />\n", min_computation * 1000000.0);
fprintf(df, " <argument value=\"%.2lf\" />\n", max_computation * 1000000.0);
fprintf(df, " <argument value=\"%.2lf\" />\n", min_data_size * 1000000.0);
fprintf(df, " <argument value=\"%.2lf\" />\n", max_data_size * 1000000.0);
fprintf(df, " <argument value=\"%d\" />\n", num_hosts * num_cores_per_host);
fprintf(df, " </actor>\n");
// Create worker processes
int current_worker_host_index = 0;
for (int i=0; i < num_hosts * num_cores_per_host; i++) {
fprintf(df, " <actor host=\"WorkerHost-%d\" function=\"worker\">\n", current_worker_host_index);
fprintf(df, " <argument value=\"%d\" />\n", i);
fprintf(df, " </actor>\n");
current_worker_host_index = (current_worker_host_index + 1) % num_hosts;
}
// XML footer
fprintf(df, "</platform>\n");
fclose(df);
}
// main-begin
int main(int argc, char* argv[])
{
sg4::Engine e(&argc, argv);
xbt_assert(argc == 15, "Usage: %s <#hosts> <#cores per host> <min core speed (Mf)> <max core speed (Mf)> <#links> <bandwidth min (MBps)> <bandwidth max (MBps)> <route length> <#tasks> <min computation (Mf)> <max computation (Mf)> <min data size (MB)> <max data size (MB)> <seed>\n", argv[0]);
// Parse commandline arguments
int num_hosts;
int num_cores_per_host;
double min_core_speed;
double max_core_speed;
int num_links;
double min_link_bandwidth;
double max_link_bandwidth;
int route_length;
int num_workers;
int num_tasks;
double min_computation;
double max_computation;
double min_data_size;
double max_data_size;
int seed;
xbt_assert(sscanf(argv[1], "%d", &num_hosts) == 1, "Invalid <#hosts> argument");
xbt_assert(sscanf(argv[2], "%d", &num_cores_per_host) == 1, "Invalid <#cores per host> argument");
xbt_assert(sscanf(argv[3], "%lf", &min_core_speed) == 1, "Invalid <min core speed (Mf)> argument");
xbt_assert(sscanf(argv[4], "%lf", &max_core_speed) == 1, "Invalid <max core speed (Mf)> argument");
xbt_assert(sscanf(argv[5], "%d", &num_links) == 1, "Invalid <#links> argument");
xbt_assert(sscanf(argv[6], "%lf", &min_link_bandwidth) == 1, "Invalid <min link bandwidth (MB)> argument");
xbt_assert(sscanf(argv[7], "%lf", &max_link_bandwidth) == 1, "Invalid <max link bandwidth (MB)> argument");
xbt_assert(sscanf(argv[8], "%d", &route_length) == 1, "Invalid <route length> argument");
xbt_assert(sscanf(argv[9], "%d", &num_tasks) == 1, "Invalid <#tasks> argument");
xbt_assert(sscanf(argv[10], "%lf", &min_computation) == 1, "Invalid <min computation (Mf)> argument");
xbt_assert(sscanf(argv[11], "%lf", &max_computation) == 1, "Invalid <max computation (Mf)> argument");
xbt_assert(sscanf(argv[12], "%lf", &min_data_size) == 1, "Invalid <min data size (MB)> argument");
xbt_assert(sscanf(argv[13], "%lf", &max_data_size) == 1, "Invalid <max data size (MB)> argument");
xbt_assert(sscanf(argv[14], "%d", &seed) == 1, "Invalid <seed> argument");
xbt_assert(route_length <= num_links, "The <route length> argument cannot be larger than the <#links> argument");
// Seed the RNG
srand(seed);
// Create platform and deployment file
std::string platform_file="/tmp/platform_master_worker.xml";
std::string deployment_file="/tmp/platform_master_worker_d.xml";
create_platform_file(platform_file, num_hosts, num_cores_per_host, min_core_speed, max_core_speed, num_links, min_link_bandwidth, max_link_bandwidth, route_length);
create_deployment_file(deployment_file, num_hosts, num_cores_per_host, num_tasks, min_computation, max_computation, min_data_size, max_data_size);
/* Register the functions representing the actors */
e.register_function("master", &master);
e.register_function("worker", &worker);
/* Load the platform description and then deploy the application */
e.load_platform(platform_file);
e.load_deployment(deployment_file);
/* Run the simulation */
e.run();
XBT_INFO("Simulation is over");
return 0;
}
// main-end