From 7fb3555cef5c79aeff0c1520492909cc7a36de52 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Sat, 7 Mar 2020 14:32:52 +0100 Subject: [PATCH] C version of exec-waitany --- .gitignore | 4 ++ MANIFEST.in | 2 + examples/c/CMakeLists.txt | 4 +- examples/c/exec-waitany/exec-waitany.c | 76 +++++++++++++++++++++++ examples/c/exec-waitany/exec-waitany.tesh | 22 +++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 examples/c/exec-waitany/exec-waitany.c create mode 100644 examples/c/exec-waitany/exec-waitany.tesh diff --git a/.gitignore b/.gitignore index eba7785c68..75dc3ae533 100644 --- a/.gitignore +++ b/.gitignore @@ -124,6 +124,7 @@ examples/c/actor-join/actor-join-c examples/c/actor-kill/actor-kill-c examples/c/actor-lifetime/actor-lifetime-c examples/c/actor-migrate/actor-migrate-c +examples/c/actor-stacksize/actor-stacksize-c examples/c/actor-suspend/actor-suspend-c examples/c/actor-yield/actor-yield-c examples/c/app-chainsend/app-chainsend-c @@ -141,6 +142,8 @@ examples/c/energy-vm/energy-vm-c examples/c/exec-basic/exec-basic-c examples/c/exec-dvfs/exec-dvfs-c examples/c/exec-remote/exec-remote-c +examples/c/exec-async/exec-async-c +examples/c/exec-waitany/exec-waitany-c examples/c/io-disk-raw/io-disk-raw-c examples/c/io-file-remote/io-file-remote-c examples/c/plugin-hostload/plugin-hostload-c @@ -177,6 +180,7 @@ examples/s4u/actor-join/s4u-actor-join examples/s4u/actor-kill/s4u-actor-kill examples/s4u/actor-lifetime/s4u-actor-lifetime examples/s4u/actor-migrate/s4u-actor-migrate +examples/s4u/actor-stacksize/s4u-actor-stacksize examples/s4u/actor-suspend/s4u-actor-suspend examples/s4u/actor-yield/s4u-actor-yield examples/s4u/app-bittorrent/s4u-bittorrent diff --git a/MANIFEST.in b/MANIFEST.in index 51f834010f..36e80cca32 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -82,6 +82,8 @@ include examples/c/exec-dvfs/exec-dvfs.c include examples/c/exec-dvfs/exec-dvfs.tesh include examples/c/exec-remote/exec-remote.c include examples/c/exec-remote/exec-remote.tesh +include examples/c/exec-waitany/exec-waitany.c +include examples/c/exec-waitany/exec-waitany.tesh include examples/c/io-disk-raw/io-disk-raw.c include examples/c/io-disk-raw/io-disk-raw.tesh include examples/c/io-file-remote/io-file-remote.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index dff3d894c4..11d445be30 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -7,7 +7,7 @@ foreach(x app-pingpong app-token-ring async-wait async-waitall async-waitany cloud-capping cloud-migration cloud-simple - exec-async exec-basic exec-dvfs exec-remote + exec-async exec-basic exec-dvfs exec-remote exec-waitany energy-exec energy-exec-ptask energy-vm io-disk-raw io-file-remote plugin-hostload) @@ -62,7 +62,7 @@ foreach(x app-chainsend app-pingpong app-token-ring async-wait async-waitall async-waitany cloud-capping cloud-migration cloud-simple - exec-async exec-basic exec-dvfs exec-remote + exec-async exec-basic exec-dvfs exec-remote exec-waitany energy-exec energy-exec-ptask energy-vm io-disk-raw io-file-remote plugin-hostload) diff --git a/examples/c/exec-waitany/exec-waitany.c b/examples/c/exec-waitany/exec-waitany.c new file mode 100644 index 0000000000..880e1b39b3 --- /dev/null +++ b/examples/c/exec-waitany/exec-waitany.c @@ -0,0 +1,76 @@ +/* Copyright (c) 2019-2020. 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_waitany, "Messages specific for this example"); + +static void worker(int argc, char* argv[]) +{ + int with_timeout = !strcmp(argv[1], "true"); + + /* Vector in which we store all pending executions*/ + sg_exec_t* pending_execs = malloc(sizeof(sg_exec_t) * 3); + int pending_execs_count = 0; + + for (int i = 0; i < 3; i++) { + char* name = bprintf("Exec-%d", i); + double amount = (6 * (i % 2) + i + 1) * sg_host_speed(sg_host_self()); + + sg_exec_t exec = sg_actor_exec_init(amount); + sg_exec_set_name(exec, name); + pending_execs[pending_execs_count++] = exec; + sg_exec_start(exec); + + XBT_INFO("Activity %s has started for %.0f seconds", name, amount / sg_host_speed(sg_host_self())); + free(name); + } + + /* Now that executions were initiated, wait for their completion, in order of termination. + * + * This loop waits for first terminating execution with wait_any() and remove it with erase(), until all execs are + * terminated. + */ + while (pending_execs_count > 0) { + int pos; + if (with_timeout) + pos = sg_exec_wait_any_for(pending_execs, pending_execs_count, 4); + else + pos = sg_exec_wait_any(pending_execs, pending_execs_count); + + if (pos < 0) { + XBT_INFO("Do not wait any longer for an activity"); + pending_execs_count = 0; + } else { + XBT_INFO("Activity at position %d is complete", pos); + memmove(pending_execs + pos, pending_execs + pos + 1, sizeof(sg_exec_t) * (pending_execs_count - pos - 1)); + pending_execs_count--; + } + XBT_INFO("%d activities remain pending", pending_execs_count); + } + + free(pending_execs); +} + +int main(int argc, char* argv[]) +{ + simgrid_init(&argc, argv); + simgrid_load_platform(argv[1]); + + const char* worker_argv[] = {"worker", "false"}; + sg_actor_create("worker", sg_host_by_name("Tremblay"), worker, 2, worker_argv); + + worker_argv[1] = "true"; + sg_actor_create("worker_timeout", sg_host_by_name("Tremblay"), worker, 2, worker_argv); + + simgrid_run(); + return 0; +} diff --git a/examples/c/exec-waitany/exec-waitany.tesh b/examples/c/exec-waitany/exec-waitany.tesh new file mode 100644 index 0000000000..716c0ba046 --- /dev/null +++ b/examples/c/exec-waitany/exec-waitany.tesh @@ -0,0 +1,22 @@ +#!/usr/bin/env tesh + +! output sort 19 +$ ${bindir:=.}/exec-waitany-c ${platfdir}/multicore_machine.xml "--log=root.fmt:[%10.6r]%e[%14P]%e%m%n" +> [ 0.000000] [ worker] Activity Exec-0 has started for 1 seconds +> [ 0.000000] [worker_timeout] Activity Exec-0 has started for 1 seconds +> [ 0.000000] [ worker] Activity Exec-1 has started for 8 seconds +> [ 0.000000] [worker_timeout] Activity Exec-1 has started for 8 seconds +> [ 0.000000] [ worker] Activity Exec-2 has started for 3 seconds +> [ 0.000000] [worker_timeout] Activity Exec-2 has started for 3 seconds +> [ 1.000000] [worker_timeout] Activity at position 0 is complete +> [ 1.000000] [worker_timeout] 2 activities remain pending +> [ 1.000000] [ worker] Activity at position 0 is complete +> [ 1.000000] [ worker] 2 activities remain pending +> [ 3.000000] [worker_timeout] Activity at position 1 is complete +> [ 3.000000] [worker_timeout] 1 activities remain pending +> [ 3.000000] [ worker] Activity at position 1 is complete +> [ 3.000000] [ worker] 1 activities remain pending +> [ 7.000000] [worker_timeout] Do not wait any longer for an activity +> [ 7.000000] [worker_timeout] 0 activities remain pending +> [ 8.000000] [ worker] Activity at position 0 is complete +> [ 8.000000] [ worker] 0 activities remain pending -- 2.20.1