#include <string>
class Master {
long number_of_tasks = 0;
double comp_size = 0;
double comm_size = 0;
long workers_count = 0;
public:
explicit Master(std::vector<std::string> args)
{
xbt_assert(args.size() == 5,
"The master function expects 4 arguments from the XML deployment file");
number_of_tasks = std::stol(args[1]);
comp_size = std::stod(args[2]);
comm_size = std::stod(args[3]);
workers_count = std::stol(args[4]);
XBT_INFO(
"Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
}
void operator()()
{
for (int i = 0; i < number_of_tasks; i++) {
if (number_of_tasks < 10000 || i % 10000 == 0)
number_of_tasks, mailbox->getCname());
mailbox->put(new double(comp_size), comm_size);
}
XBT_INFO(
"All tasks have been dispatched. Let's tell everybody the computation is over.");
for (int i = 0; i < workers_count; i++) {
mailbox->put(new double(-1.0), 0);
}
}
};
class Worker {
long id = -1;
public:
explicit Worker(std::vector<std::string> args)
{
xbt_assert(args.size() == 2,
"The worker expects a single argument from the XML deployment file: " "its worker ID (its numerical rank)");
id = std::stol(args[1]);
}
void operator()()
{
while (1) {
double* task = static_cast<double*>(mailbox->get());
xbt_assert(task !=
nullptr,
"mailbox->get() failed");
double comp_size = *task;
delete task;
if (comp_size < 0) {
break;
}
}
}
};
{
xbt_assert(argc > 2,
"Usage: %s platform_file deployment_file\n" "\tExample: %s msg_platform.xml msg_deployment.xml\n",
argv[0], argv[0]);
e.loadPlatform(argv[1]);
e.registerFunction<Master>("master");
e.registerFunction<Worker>("worker");
e.loadDeployment(argv[2]);
e.run();
XBT_INFO(
"Simulation time %g", e.getClock());
return 0;
}