Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid sending address of local variables.
[simgrid.git] / examples / s4u / app-masterworker / s4u_app-masterworker.cpp
index f9e0c1b..c0ce006 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2017. 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. */
@@ -22,10 +22,10 @@ public:
   {
     xbt_assert(args.size() == 5, "The master function expects 4 arguments from the XML deployment file");
 
-    number_of_tasks = xbt_str_parse_int(args[1].c_str(), "Invalid amount of tasks: %s"); /* - Number of tasks */
-    comp_size       = xbt_str_parse_double(args[2].c_str(), "Invalid computational size: %s"); /* - Task compute cost */
-    comm_size       = xbt_str_parse_double(args[3].c_str(), "Invalid communication size: %s"); /* - Communication size */
-    workers_count   = xbt_str_parse_int(args[4  ].c_str(), "Invalid amount of workers: %s"); /* - Number of workers */
+    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);
   }
@@ -38,18 +38,17 @@ public:
 
       if (number_of_tasks < 10000 || i % 10000 == 0)
         XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", (std::string("Task_") + std::to_string(i)).c_str(),
-                                                              number_of_tasks, mailbox->name());
+                 number_of_tasks, mailbox->getName());
 
-      /* - Send the task to the @ref worker */
-      char* payload = bprintf("%f", comp_size);
-      simgrid::s4u::this_actor::send(mailbox, payload, comm_size);
+      /* - Send the computation amount to the @ref worker */
+      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++) {
       /* - Eventually tell all the workers to stop by sending a "finalize" task */
       mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
-      simgrid::s4u::this_actor::send(mailbox, xbt_strdup("finalize"), 0);
+      mailbox->put(new double(-1.0), 0);
     }
   }
 };
@@ -63,26 +62,24 @@ public:
   {
     xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
                                  "its worker ID (its numerical rank)");
-    id      = xbt_str_parse_int(args[1].c_str(), "Invalid argument %s");
+    id      = std::stol(args[1]);
     mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(id));
   }
 
   void operator()()
   {
     while (1) { /* The worker waits in an infinite loop for tasks sent by the \ref master */
-      char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
-      xbt_assert(res != nullptr, "MSG_task_get failed");
-
-      if (strcmp(res, "finalize") == 0) { /* - Exit if 'finalize' is received */
-        xbt_free(res);
+      double* task = static_cast<double*>(mailbox->get());
+      xbt_assert(task != nullptr, "mailbox->get() failed");
+      double comp_size = *task;
+      delete task;
+      if (comp_size < 0) { /* - Exit when -1.0 is received */
+        XBT_INFO("I'm done. See you!");
         break;
       }
       /*  - Otherwise, process the task */
-      double comp_size = xbt_str_parse_double(res, nullptr);
-      xbt_free(res);
       simgrid::s4u::this_actor::execute(comp_size);
     }
-    XBT_INFO("I'm done. See you!");
   }
 };
 
@@ -102,5 +99,6 @@ int main(int argc, char* argv[])
 
   XBT_INFO("Simulation time %g", e->getClock());
 
+  delete e;
   return 0;
 }