From: Frederic Suter Date: Wed, 4 Jul 2018 14:01:51 +0000 (+0200) Subject: Attempt to move msg_bar_t to S4U X-Git-Tag: v3_21~566 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b8253e68725ba0f4f15a07fd04bf2ed2b12236c8 Attempt to move msg_bar_t to S4U --- diff --git a/include/simgrid/s4u.hpp b/include/simgrid/s4u.hpp index a84b64e459..cd02cf184e 100644 --- a/include/simgrid/s4u.hpp +++ b/include/simgrid/s4u.hpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/include/simgrid/s4u/Barrier.hpp b/include/simgrid/s4u/Barrier.hpp new file mode 100644 index 0000000000..b3c6e65dc3 --- /dev/null +++ b/include/simgrid/s4u/Barrier.hpp @@ -0,0 +1,36 @@ +/* Copyright (c) 2018. 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. */ + +#ifndef SIMGRID_S4U_BARRIERHPP +#define SIMGRID_S4U_BARRIER_HPP + +#include "simgrid/s4u/ConditionVariable.hpp" +#include +#include + +#include + +namespace simgrid { +namespace s4u { + +class XBT_PUBLIC Barrier { +private: + MutexPtr mutex_; + ConditionVariablePtr cond_; + unsigned int expected_processes_; + unsigned int arrived_processes_ = 0; + +public: + explicit Barrier(unsigned int count); + ~Barrier() = default; + Barrier(Barrier const&) = delete; + Barrier& operator=(Barrier const&) = delete; + + int wait(); +}; +} +} // namespace simgrid::s4u + +#endif diff --git a/src/s4u/s4u_Barrier.cpp b/src/s4u/s4u_Barrier.cpp new file mode 100644 index 0000000000..482a16426d --- /dev/null +++ b/src/s4u/s4u_Barrier.cpp @@ -0,0 +1,46 @@ +/* Copyright (c) 2018. 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 +#include + +#include +#include + +#include "simgrid/s4u/Barrier.hpp" +#include "simgrid/simix.h" + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_barrier, "S4U barrier"); + +namespace simgrid { +namespace s4u { + +Barrier::Barrier(unsigned int count) : expected_processes_(count) +{ + mutex_ = Mutex::create(); + cond_ = ConditionVariable::create(); +} + +/** + * Wait functions + */ +int Barrier::wait() +{ + mutex_->lock(); + arrived_processes_++; + XBT_DEBUG("waiting %p %u/%u", this, arrived_processes_, expected_processes_); + if (arrived_processes_ == expected_processes_) { + cond_->notify_all(); + mutex_->unlock(); + arrived_processes_ = 0; + return -1; + } + + cond_->wait(mutex_); + mutex_->unlock(); + return 0; +} +} // namespace s4u +} // namespace simgrid diff --git a/teshsuite/s4u/CMakeLists.txt b/teshsuite/s4u/CMakeLists.txt index 3e6a5aa14d..fd2a102d0a 100644 --- a/teshsuite/s4u/CMakeLists.txt +++ b/teshsuite/s4u/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(x actor actor-autorestart +foreach(x actor actor-autorestart actor-migration comm-pt2pt cloud-interrupt-migration concurrent_rw storage_client_server host_on_off_wait listen_async pid ) @@ -11,7 +11,7 @@ endforeach() ## Add the tests. ## Some need to be run with all factories, some need not tesh to run -foreach(x actor actor-autorestart cloud-interrupt-migration concurrent_rw) +foreach(x actor actor-autorestart actor-migration cloud-interrupt-migration concurrent_rw) set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh) ADD_TESH_FACTORIES(tesh-s4u-${x} "thread;ucontext;raw;boost" --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x} --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_BINARY_DIR}/teshsuite/s4u/${x} ${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x}/${x}.tesh) endforeach() diff --git a/teshsuite/s4u/actor-migration/actor-migration.cpp b/teshsuite/s4u/actor-migration/actor-migration.cpp new file mode 100644 index 0000000000..9b3f413fd2 --- /dev/null +++ b/teshsuite/s4u/actor-migration/actor-migration.cpp @@ -0,0 +1,59 @@ +/* Copyright (c) 2018. 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/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_migration, "Messages specific for this s4u example"); + +simgrid::s4u::Barrier* barrier; +static simgrid::s4u::ActorPtr controlled_process; + +/* The Emigrant process will be moved from host to host. */ +static void emigrant() +{ + XBT_INFO("I'll look for a new job on another machine ('Boivin') where the grass is greener."); + simgrid::s4u::this_actor::migrate( + simgrid::s4u::Host::by_name("Boivin")); /* - First, move to another host by myself */ + + XBT_INFO("Yeah, found something to do"); + simgrid::s4u::this_actor::execute(98095000); /* - Execute some work there */ + simgrid::s4u::this_actor::sleep_for(2); + XBT_INFO("Moving back home after work"); + simgrid::s4u::this_actor::migrate(simgrid::s4u::Host::by_name("Jacquelin")); /* - Move back to original location */ + simgrid::s4u::this_actor::migrate(simgrid::s4u::Host::by_name("Boivin")); /* - Go back to the other host to sleep*/ + simgrid::s4u::this_actor::sleep_for(4); + controlled_process = simgrid::s4u::Actor::self(); /* - Get controlled at checkpoint */ + barrier->wait(); + simgrid::s4u::this_actor::suspend(); + simgrid::s4u::Host* h = simgrid::s4u::this_actor::get_host(); + XBT_INFO("I've been moved on this new host: %s", h->get_cname()); + XBT_INFO("Uh, nothing to do here. Stopping now"); +} + +/* The policeman check for emigrants and move them back to 'Jacquelin' */ +static void policeman() +{ + XBT_INFO("Wait at the checkpoint."); /* - block on the mutex+condition */ + barrier->wait(); + controlled_process->migrate(simgrid::s4u::Host::by_name("Jacquelin")); /* - Move an emigrant to Jacquelin */ + XBT_INFO("I moved the emigrant"); + controlled_process->resume(); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + simgrid::s4u::Actor::create("emigrant", simgrid::s4u::Host::by_name("Jacquelin"), emigrant); + simgrid::s4u::Actor::create("policeman", simgrid::s4u::Host::by_name("Boivin"), policeman); + + barrier = new simgrid::s4u::Barrier(2); + e.run(); + XBT_INFO("Simulation time %g", e.get_clock()); + delete barrier; + + return 0; +} diff --git a/teshsuite/s4u/actor-migration/actor-migration.tesh b/teshsuite/s4u/actor-migration/actor-migration.tesh new file mode 100644 index 0000000000..c3c3790302 --- /dev/null +++ b/teshsuite/s4u/actor-migration/actor-migration.tesh @@ -0,0 +1,14 @@ +#!/usr/bin/env tesh + +p Testing the migration feature of S4U + +! output sort 19 +$ $SG_TEST_EXENV ${bindir:=.}/actor-migration ${platfdir:=.}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:emigrant@Jacquelin) I'll look for a new job on another machine ('Boivin') where the grass is greener. +> [ 0.000000] (1:emigrant@Boivin) Yeah, found something to do +> [ 0.000000] (2:policeman@Boivin) Wait at the checkpoint. +> [ 3.000000] (1:emigrant@Boivin) Moving back home after work +> [ 7.000000] (0:maestro@) Simulation time 7 +> [ 7.000000] (1:emigrant@Jacquelin) I've been moved on this new host: Jacquelin +> [ 7.000000] (1:emigrant@Jacquelin) Uh, nothing to do here. Stopping now +> [ 7.000000] (2:policeman@Boivin) I moved the emigrant diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 9b9281354c..588be4aaaa 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -423,6 +423,7 @@ endif() set(S4U_SRC src/s4u/s4u_Actor.cpp src/s4u/s4u_Activity.cpp + src/s4u/s4u_Barrier.cpp src/s4u/s4u_ConditionVariable.cpp src/s4u/s4u_Comm.cpp src/s4u/s4u_Engine.cpp @@ -687,6 +688,7 @@ set(headers_to_install include/simgrid/zone.h include/simgrid/s4u/Activity.hpp include/simgrid/s4u/Actor.hpp + include/simgrid/s4u/Barrier.hpp include/simgrid/s4u/Comm.hpp include/simgrid/s4u/ConditionVariable.hpp include/simgrid/s4u/Engine.hpp