Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add an example for Engine::run_until()
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 14 Dec 2021 16:18:01 +0000 (17:18 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 14 Dec 2021 16:18:51 +0000 (17:18 +0100)
examples/cpp/CMakeLists.txt
examples/cpp/engine-run-partial/s4u-engine-run-partial.cpp [new file with mode: 0644]
examples/cpp/engine-run-partial/s4u-engine-run-partial.tesh [new file with mode: 0644]

index a09d9cf..6c372ef 100644 (file)
@@ -68,12 +68,12 @@ foreach (example actor-create actor-daemon actor-exiting actor-join actor-kill
                  cloud-capping cloud-migration cloud-simple
                  dht-chord dht-kademlia
                  energy-exec energy-boot energy-link energy-vm energy-exec-ptask energy-wifi
-                 engine-filtering
+                 engine-filtering engine-run-partial
                  exec-async exec-basic exec-dvfs exec-remote exec-waitany exec-waitfor exec-dependent exec-unassigned
                  exec-ptask-multicore exec-cpu-nonlinear exec-cpu-factors exec-failure
                  maestro-set
                  mc-bugged1 mc-bugged2 mc-electric-fence mc-failing-assert
-                                network-wifi
+                 network-wifi
                  io-async io-priority io-degradation io-file-system io-file-remote io-disk-raw io-dependent
                  platform-failures platform-profile platform-properties
                  plugin-host-load plugin-link-load plugin-prodcons
diff --git a/examples/cpp/engine-run-partial/s4u-engine-run-partial.cpp b/examples/cpp/engine-run-partial/s4u-engine-run-partial.cpp
new file mode 100644 (file)
index 0000000..ab7df14
--- /dev/null
@@ -0,0 +1,71 @@
+/* Copyright (c) 2007-2021. 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. */
+
+/* This example is to show how to use Engine::run(date) to simulate only up to that point in time */
+
+#include "simgrid/s4u.hpp"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
+
+/* This actor simply executes and waits the activity it got as a parameter. */
+static void runner(simgrid::s4u::ExecPtr activity)
+{
+  activity->start();
+  activity->wait();
+
+  XBT_INFO("Goodbye now!");
+}
+
+/* This actor tests the ongoing execution until its completion, and don't wait before it's terminated. */
+static void monitor()
+{
+  double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
+  XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
+  simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_init(computation_amount);
+  activity->start();
+
+  while (not activity->test()) {
+    XBT_INFO("Remaining amount of flops: %g (%.0f%%)", activity->get_remaining(),
+             100 * activity->get_remaining_ratio());
+    simgrid::s4u::this_actor::sleep_for(0.3);
+  }
+
+  XBT_INFO("Goodbye now!");
+}
+
+/* This actor cancels the ongoing execution after a while. */
+static void canceller()
+{
+  double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
+
+  XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
+  simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_async(computation_amount);
+  simgrid::s4u::this_actor::sleep_for(0.5);
+  XBT_INFO("I changed my mind, cancel!");
+  activity->cancel();
+
+  XBT_INFO("Goodbye now!");
+}
+
+int main(int argc, char* argv[])
+{
+  simgrid::s4u::Engine e(&argc, argv);
+  e.load_platform(argv[1]);
+
+  simgrid::s4u::Host* fafard = e.host_by_name("Fafard");
+
+  simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_init(fafard->get_speed() * 10.)->set_host(fafard);
+  simgrid::s4u::Actor::create("runner", fafard, runner, activity);
+
+  while (activity->get_remaining() > 0) {
+    XBT_INFO("Remaining amount of flops: %g (%.0f%%)", activity->get_remaining(),
+             100 * activity->get_remaining_ratio());
+    e.run_until(e.get_clock() + 1);
+  }
+
+  XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
+
+  return 0;
+}
diff --git a/examples/cpp/engine-run-partial/s4u-engine-run-partial.tesh b/examples/cpp/engine-run-partial/s4u-engine-run-partial.tesh
new file mode 100644 (file)
index 0000000..cc87d7d
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env tesh
+
+$ ${bindir:=.}/s4u-engine-run-partial ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
+> [  0.000000] (0:maestro@) Remaining amount of flops: 7.6296e+08 (100%)
+> [  1.000000] (0:maestro@) Remaining amount of flops: 6.86664e+08 (90%)
+> [  2.000000] (0:maestro@) Remaining amount of flops: 6.10368e+08 (80%)
+> [  3.000000] (0:maestro@) Remaining amount of flops: 5.34072e+08 (70%)
+> [  4.000000] (0:maestro@) Remaining amount of flops: 4.57776e+08 (60%)
+> [  5.000000] (0:maestro@) Remaining amount of flops: 3.8148e+08 (50%)
+> [  6.000000] (0:maestro@) Remaining amount of flops: 3.05184e+08 (40%)
+> [  7.000000] (0:maestro@) Remaining amount of flops: 2.28888e+08 (30%)
+> [  8.000000] (0:maestro@) Remaining amount of flops: 1.52592e+08 (20%)
+> [  9.000000] (0:maestro@) Remaining amount of flops: 7.6296e+07 (10%)
+> [ 10.000000] (1:runner@Fafard) Goodbye now!
+> [ 10.000000] (0:maestro@) Simulation time 10