Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert actor-yield to s4u API
[simgrid.git] / examples / s4u / actor-yield / s4u-actor-yield.cpp
1 /* Copyright (c) 2017. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u.hpp"
7 #include "src/simix/ActorImpl.hpp"
8 #include "src/simix/smx_private.h"
9 #include <string>
10
11 /* This example does not much: It just spans over-polite processes that yield a large amount
12 * of time before ending.
13 *
14 * This serves as an example for the s4u-actor-yield() function, with which a process can request
15 * to be rescheduled after the other processes that are ready at the current timestamp.
16 *
17 * It can also be used to benchmark our context-switching mechanism.
18 */
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_yield, "Messages specific for this s4u example");
20 /* Main function of the Yielder process */
21 class yielder {
22  long number_of_yields;
23 public: 
24  explicit yielder() = default;
25  explicit yielder(std::vector<std::string> args)
26
27  number_of_yields = std::stod(args[1]);
28 }
29 void operator()()
30 {
31  for (int i = 0; i < number_of_yields; i++)
32  simgrid::simix::kernelImmediate([] { /* do nothing*/ });
33  XBT_INFO("I yielded %ld times. Goodbye now!", number_of_yields);
34 }
35 };
36
37 int main(int argc, char* argv[])
38 {
39  simgrid::s4u::Engine e(&argc, argv);
40  
41  xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
42  "\tExample: %s msg_platform.xml msg_deployment.xml\n",
43  argv[0], argv[0]);
44  
45  e.loadPlatform(argv[1]);  /* - Load the platform description */
46  e.registerFunction<yielder>("yielder");
47  std::vector<std::string> args; 
48
49  e.loadDeployment(argv[2]);
50
51  e.run();  /* - Run the simulation */
52
53  return 0;
54 }