File: exec-async.py

package info (click to toggle)
simgrid 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 38,980 kB
  • sloc: cpp: 123,583; ansic: 66,779; python: 8,358; java: 6,406; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,337; perl: 1,436; makefile: 105; lisp: 49; javascript: 7; sed: 6
file content (73 lines) | stat: -rw-r--r-- 2,433 bytes parent folder | download
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
# Copyright (c) 2018-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.

"""
Usage: exec-async.py platform_file [other parameters]
"""

import sys
from simgrid import Actor, Engine, Host, this_actor


class Waiter:
    """
    This actor simply waits for its task completion after starting it.
    That's exactly equivalent to synchronous execution.
    """

    def __call__(self):
        computation_amount = this_actor.get_host().speed
        this_actor.info("Waiter executes {:.0f} flops, should take 1 second.".format(computation_amount))
        activity = this_actor.exec_init(computation_amount)
        activity.start()
        activity.wait()

        this_actor.info("Goodbye from waiter!")


class Monitor:
    """This actor tests the ongoing execution until its completion, and don't wait before it's terminated."""

    def __call__(self):
        computation_amount = this_actor.get_host().speed
        this_actor.info("Monitor executes {:.0f} flops, should take 1 second.".format(computation_amount))
        activity = this_actor.exec_init(computation_amount).start()

        while not activity.test():
            this_actor.info("Remaining amount of flops: {:.0f} ({:.0f}%)".format(
                activity.remaining, 100 * activity.remaining_ratio))
            this_actor.sleep_for(0.3)
        activity.wait()

        this_actor.info("Goodbye from monitor!")


class Canceller:
    """This actor cancels the ongoing execution after a while."""

    def __call__(self):
        computation_amount = this_actor.get_host().speed
        this_actor.info("Canceller executes {:.0f} flops, should take 1 second.".format(computation_amount))
        activity = this_actor.exec_async(computation_amount)

        this_actor.sleep_for(0.5)
        this_actor.info("I changed my mind, cancel!")
        activity.cancel()

        this_actor.info("Goodbye from canceller!")


if __name__ == '__main__':
    e = Engine(sys.argv)
    if len(sys.argv) < 2:
        raise AssertionError("Usage: exec-async.py platform_file [other parameters]")

    e.load_platform(sys.argv[1])

    e.add_actor("wait", Host.by_name("Fafard"), Waiter())
    e.add_actor("monitor", Host.by_name("Ginette"), Monitor())
    e.add_actor("cancel", Host.by_name("Boivin"), Canceller())

    e.run()