1 # Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.
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.
7 This example shows how to simulate a non-linear resource sharing for CPUs.
12 from simgrid import Actor, Engine, NetZone, Host, this_actor
16 computation_amount = this_actor.get_host().speed
19 this_actor.info("Execute %d tasks of %g flops, should take %d second in a CPU without degradation. \
20 It will take the double here." % (n_task, computation_amount, n_task))
21 tasks = [this_actor.exec_init(computation_amount).start()
22 for _ in range(n_task)]
24 this_actor.info("Waiting for all tasks to be done!")
28 this_actor.info("Finished executing. Goodbye now!")
31 def cpu_nonlinear(host: Host, capacity: float, n: int) -> float:
32 """ Non-linear resource sharing for CPU """
33 # emulates a degradation in CPU according to the number of tasks
34 # totally unrealistic but for learning purposes
35 capacity = capacity / 2 if n > 1 else capacity
36 this_actor.info("Host %s, %d concurrent tasks, new capacity %f" %
37 (host.name, n, capacity))
42 """ Create a simple 1-host platform """
43 zone = NetZone.create_empty_zone("Zone1")
44 runner_host = zone.create_host("runner", 1e6)
45 runner_host.set_sharing_policy(
46 Host.SharingPolicy.NONLINEAR, functools.partial(cpu_nonlinear, runner_host))
51 Actor.create("runner", runner_host, runner)
54 if __name__ == '__main__':
63 # explicitly deleting Engine object to avoid segfault during cleanup phase.
64 # During Engine destruction, the cleanup of std::function linked to non_linear callback is called.
65 # If we let the cleanup by itself, it fails trying on its destruction because the python main program
66 # has already freed its variables