File: exec-async.c

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (78 lines) | stat: -rw-r--r-- 2,513 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2007-2025. 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. */

#include "simgrid/actor.h"
#include "simgrid/engine.h"
#include "simgrid/exec.h"
#include "simgrid/host.h"

#include "xbt/asserts.h"
#include "xbt/log.h"

XBT_LOG_NEW_DEFAULT_CATEGORY(exec_async, "Messages specific for this example");

/* This actor simply waits for its task completion after starting it.
 * That's exactly equivalent to synchronous execution. */
static void waiter(int argc, char* argv[])
{
  double computation_amount = sg_host_get_speed(sg_host_self());
  XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
  sg_exec_t activity = sg_actor_exec_init(computation_amount);
  sg_exec_start(activity);
  sg_exec_wait(activity);

  XBT_INFO("Goodbye now!");
}

/* This actor tests the ongoing execution until its completion, and don't wait before it's terminated. */
static void monitor(int argc, char* argv[])
{
  double computation_amount = sg_host_get_speed(sg_host_self());
  XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
  sg_exec_t activity = sg_actor_exec_init(computation_amount);
  sg_exec_start(activity);

  while (!sg_exec_test(activity)) {
    XBT_INFO("Remaining amount of flops: %g (%.0f%%)", sg_exec_get_remaining(activity),
             100 * sg_exec_get_remaining_ratio(activity));
    sg_actor_sleep_for(0.3);
  }

  XBT_INFO("Goodbye now!");
}

/* This actor cancels the ongoing execution after a while. */
static void canceller(int argc, char* argv[])
{
  double computation_amount = sg_host_get_speed(sg_host_self());

  XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
  sg_exec_t activity = sg_actor_exec_async(computation_amount);
  sg_actor_sleep_for(0.5);
  XBT_INFO("I changed my mind, cancel!");
  sg_exec_cancel(activity);

  XBT_INFO("Goodbye now!");
}

int main(int argc, char* argv[])
{
  simgrid_init(&argc, argv);
  simgrid_load_platform(argv[1]);

  sg_host_t fafard  = sg_host_by_name("Fafard");
  sg_host_t ginette = sg_host_by_name("Ginette");
  sg_host_t boivin  = sg_host_by_name("Boivin");

  sg_actor_create("wait", fafard, &waiter, 0, NULL);
  sg_actor_create("monitor", ginette, &monitor, 0, NULL);
  sg_actor_create("cancel", boivin, &canceller, 0, NULL);

  simgrid_run();

  XBT_INFO("Simulation time %g", simgrid_get_clock());

  return 0;
}