set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/s4u_${example}.cpp)
endforeach()
-add_executable (s4u_launching_function launching/s4u_launching_function.cpp)
-target_link_libraries(s4u_launching_function simgrid)
-set_target_properties(s4u_launching_function PROPERTIES RUNTIME_OUTPUT_DIRECTORY
- ${CMAKE_CURRENT_BINARY_DIR}/launching)
-
-add_executable (s4u_launching_deployment launching/s4u_launching_deployment.cpp)
-target_link_libraries(s4u_launching_deployment simgrid)
-set_target_properties(s4u_launching_deployment PROPERTIES RUNTIME_OUTPUT_DIRECTORY
- ${CMAKE_CURRENT_BINARY_DIR}/launching)
-
-set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/launching/s4u_launching_function.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/launching/s4u_launching_deployment.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/launching/s4u_launching.h)
-
set(examples_src ${examples_src} PARENT_SCOPE)
set(tesh_files ${tesh_files} PARENT_SCOPE)
set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/launching/deployment.xml
@ingroup s4u_api
@brief Find the S4U example fitting your needs in the archive.
- - @ref s4u_ex_launching
+ - @ref s4u_ex_basics
- @ref s4u_ex_synchro
-@section s4u_ex_launching Launching actors to populate your simulation
+@section s4u_ex_basics Basics of SimGrid simulation
+
+ - <b>Launching actors</b>
+ Shows how to start your actors to populate your simulation\n
+ @ref examples/s4u/launching/s4u_launching.cpp and @ref examples/s4u/launching/deployment.xml
@section s4u_ex_synchro Inter-Actor Synchronization
- - <b>Mutex</b>.
- @ref examples/s4u/mutex/s4u_mutex.cpp\n
- Shows how to use simgrid::s4u::Mutex synchronization objects
+ - <b>Mutex.</b>
+ Shows how to use simgrid::s4u::Mutex synchronization objects\n
+ @ref examples/s4u/mutex/s4u_mutex.cpp
*/
/**
+@example examples/s4u/launching/s4u_launching.cpp
+@example examples/s4u/launching/deployment.xml
+
@example examples/s4u/mutex/s4u_mutex.cpp
*/
\ No newline at end of file
s4u_launching
-s4u_launching_deployment
-s4u_launching_function
<?xml version='1.0'?>
<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
<platform version="4">
- <process host="Tremblay" function="worker">
- </process>
- <process host="Jupiter" function="master">
+
+ <!-- This a weird deployment file: we only start one actor from here and the others from the main().
+ --
+ -- This is only for the example, but don't do that at home.
+ -- Instead, you want to start all your actors from the deployment file.
+ -->
+
+ <process host="Fafard" function="receiver">
+ <!-- Pass the right mailbox name to the actor -->
+ <argument value="mb42" />
</process>
</platform>
/* 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 <xbt/sysdep.h>
+
+/* This example shows how to declare and start your actors.
+ *
+ * The first step is to declare the code of your actors (what they do exactly
+ * does not matter to this example) and then you ask SimGrid to start your
+ * actors. There is three ways of doing so:
+ * - Directly, by instantiating your actor as paramter to Actor::create();
+ * - By first registering your actors before instantiating it;
+ * - Through the deployment file.
+ *
+ * This example shows all these solutions, even if you obviously should use
+ * only one of these solutions to start your actors. The most advised solution
+ * is to use a deployment file, as it creates a clear separation between your
+ * application and the settings to test it. This is a better scientific
+ * methodology. Actually, starting an actor with Actor::create() is mostly
+ * useful to start an actor from another actor.
+ *
+ */
+
#include <simgrid/s4u.hpp>
-#include "s4u_launching.h"
+// This declares a logging channel so that XBT_INFO can be used later
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_launching_test, "The logging channel used in this example");
+
+
+/* Declares a first class of actors which sends a message to the mailbox 'mb42'.
+ * The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default)
+ *
+ * Later, this actor class is instantiated twice in the simulation.
+ */
+class Sender {
+public:
+ const char *msg = "GaBuZoMeu";
+ Sender() {
+ /* Constructor used when no parameter is passed to the actor */
+ };
+ Sender(std::vector<std::string> args) {
+ /* This constructor is used when we pass parameters to the actor */
+ if (args.size() > 0)
+ msg = args[0].c_str();
+ }
+ void operator()() {
+ XBT_INFO("Hello s4u, I have something to send");
+ simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mb42");
+
+ simgrid::s4u::this_actor::send(mailbox, xbt_strdup(msg), strlen(msg));
+ XBT_INFO("I'm done. See you.");
+ }
+};
+
+
+/* Declares a second class of actor which receive two messages on the mailbox which
+ * name is passed as parameter ('thingy' by default, ie the wrong one).
+ *
+ * Later, this actor class is instantiated once in the simulation.
+ */
+class Receiver {
+public:
+ simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("thingy");
+
+ Receiver() {};
+ Receiver(std::vector<std::string> args) {
+ /* This constructor is used when we pass parameters to the actor */
+ /* as with argc/argv, args[0] is the actor's name, so the first parameter is args[1] */
+ if (args.size() > 1)
+ mailbox = simgrid::s4u::Mailbox::byName(args[1].c_str());
+ }
+ void operator()() {
+ XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->getName());
+
+ char *msg1 = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
+ char *msg2 = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
+ XBT_INFO("I received '%s' and '%s'",msg1,msg2);
+ XBT_INFO("I'm done. See you.");
+ }
+};
+
+/* Here comes the main function of your program */
int main(int argc, char **argv) {
+ /* When your program starts, you have to first start a new simulation engine, as follows */
simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
- e->loadPlatform("../../platforms/two_hosts.xml");
- simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), Worker());
- simgrid::s4u::Actor::createActor("master", simgrid::s4u::Host::by_name("Jupiter"), Master());
+
+ /* Then you should load a platform file, describing your simulated platform */
+ e->loadPlatform("../../platforms/small_platform.xml");
+
+ /* And now you have to ask SimGrid to actually start your actors.
+ *
+ * You can first directly start your actor, as follows. Note the last parameter: 'Sender()',
+ * as if you would call the Sender function.
+ */
+ simgrid::s4u::Actor::createActor("sender1", simgrid::s4u::Host::by_name("Tremblay"), Sender());
+
+ /* The second way is to first register your function, and then retrieve it */
+ e->registerFunction<Sender>("sender"); // The sender is passed as a template parameter here
+ std::vector<std::string> args; // Here we declare the parameter that the actor will get
+ args.push_back("GloubiBoulga"); // Add a parameter to the set (we could have done it in the first approach too)
+
+ simgrid::s4u::Actor::createActor("sender2", simgrid::s4u::Host::by_name("Jupiter"), "sender", args);
+
+ /* The third way to start your actors is to use a deployment file. */
+ e->registerFunction<Receiver>("receiver"); // You first have to register the actor as with the second approach
+ e->loadDeployment("deployment.xml"); // And then, you load the deployment file
+
+ /* Once every actors are started in the engine, the simulation can start */
e->run();
+
+ /* Once the simulation is done, the program is ended */
return 0;
}
+++ /dev/null
-/* Copyright (c) 2006-2016. 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 <xbt/sysdep.h>
-
-#include <simgrid/s4u.hpp>
-
-XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
-
-class Worker {
-public:
- Worker() {};
- Worker(std::vector<std::string> args) {}
- void operator()() {
- XBT_INFO("Hello s4u, I'm ready to serve");
- char *msg = static_cast<char*>(simgrid::s4u::this_actor::recv(
- *simgrid::s4u::Mailbox::byName("worker")));
- XBT_INFO("I received '%s'",msg);
- XBT_INFO("I'm done. See you.");
- }
-};
-
-class Master {
-public:
- Master() {};
- Master(std::vector<std::string> args) {}
- void operator()() {
- const char *msg = "GaBuZoMeu";
- XBT_INFO("Hello s4u, I have something to send");
- simgrid::s4u::this_actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg));
- XBT_INFO("I'm done. See you.");
- }
-};
#! ./tesh
$ $SG_TEST_EXENV ${bindir:=.}/s4u_launching
-> [Tremblay:worker:(0) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to serve
-> [Jupiter:master:(0) 0.000000] [s4u_test/INFO] Hello s4u, I have something to send
-> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu'
-> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
-> [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
-
-$ $SG_TEST_EXENV ${bindir:=.}/s4u_launching_function
-> [Tremblay:worker:(0) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to serve
-> [Jupiter:master:(0) 0.000000] [s4u_test/INFO] Hello s4u, I have something to send
-> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu'
-> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
-> [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
-
-$ $SG_TEST_EXENV ${bindir:=.}/s4u_launching_deployment deployment.xml
-> [Tremblay:worker:(0) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to serve
-> [Jupiter:master:(0) 0.000000] [s4u_test/INFO] Hello s4u, I have something to send
-> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu'
-> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
-> [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
+> [Tremblay:sender1:(0) 0.000000] [s4u_launching_test/INFO] Hello s4u, I have something to send
+> [Jupiter:sender2:(0) 0.000000] [s4u_launching_test/INFO] Hello s4u, I have something to send
+> [Fafard:receiver:(0) 0.000000] [s4u_launching_test/INFO] Hello s4u, I'm ready to get any message you'd want on mb42
+> [Tremblay:sender1:(0) 0.025709] [s4u_launching_test/INFO] I'm done. See you.
+> [Jupiter:sender2:(0) 0.070434] [s4u_launching_test/INFO] I'm done. See you.
+> [Fafard:receiver:(0) 0.070434] [s4u_launching_test/INFO] I received 'GaBuZoMeu' and 'GloubiBoulga'
+> [Fafard:receiver:(0) 0.070434] [s4u_launching_test/INFO] I'm done. See you.
+++ /dev/null
-/* Copyright (c) 2006-2016. 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 <string>
-#include <vector>
-
-#include <xbt/sysdep.h>
-#include <simgrid/s4u.hpp>
-
-#include "s4u_launching.h"
-
-int main(int argc, char **argv) {
- simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
- e->loadPlatform("../../platforms/two_hosts.xml");
- e->registerFunction<Worker>("worker");
- e->registerFunction<Master>("master");
- e->loadDeployment(argv[1]);
- e->run();
- return 0;
-}
+++ /dev/null
-/* Copyright (c) 2006-2016. 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 <string>
-#include <vector>
-
-#include <xbt/sysdep.h>
-
-#include <simgrid/s4u.hpp>
-
-#include "s4u_launching.h"
-
-int main(int argc, char **argv) {
- simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
- e->loadPlatform("../../platforms/two_hosts.xml");
- e->registerFunction<Worker>("worker");
- e->registerFunction<Master>("master");
- std::vector<std::string> args;
- simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args);
- simgrid::s4u::Actor::createActor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args);
- e->run();
- return 0;
-}
#include <simgrid/chrono.hpp>
#include <simgrid/simix.h>
#include <simgrid/s4u/forward.hpp>
+#include <simgrid/s4u/mailbox.hpp>
namespace simgrid {
namespace s4u {
xbt_assert(actor != nullptr);
SIMIX_process_unref(actor->pimpl_);
}
- using Ptr = boost::intrusive_ptr<Actor>;
// ***** Actor creation *****
/** Retrieve a reference to myself */
- static Ptr self();
+ static ActorPtr self();
/** Create an actor using a function
*
* If the actor is restarted, the actor has a fresh copy of the function.
*/
- static Ptr createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code);
+ static ActorPtr createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code);
- static Ptr createActor(const char* name, s4u::Host *host, std::function<void()> code)
+ static ActorPtr createActor(const char* name, s4u::Host *host, std::function<void()> code)
{
return createActor(name, host, -1.0, std::move(code));
}
// This constructor is enabled only if the call code(args...) is valid:
typename = typename std::result_of<F(Args...)>::type
>
- static Ptr createActor(const char* name, s4u::Host *host, F code, Args... args)
+ static ActorPtr createActor(const char* name, s4u::Host *host, F code, Args... args)
{
return createActor(name, host, wrap_task(std::move(code), std::move(args)...));
}
// Create actor from function name:
- static Ptr createActor(const char* name, s4u::Host *host, double killTime,
+ static ActorPtr createActor(const char* name, s4u::Host *host, double killTime,
const char* function, std::vector<std::string> args);
- static Ptr createActor(const char* name, s4u::Host *host, const char* function,
+ static ActorPtr createActor(const char* name, s4u::Host *host, const char* function,
std::vector<std::string> args)
{
return createActor(name, host, -1.0, function, std::move(args));
void kill();
static void kill(int pid);
- static Ptr forPid(int pid);
+ static ActorPtr forPid(int pid);
/**
* Wait for the actor to finish.
smx_process_t getImpl();
};
-using ActorPtr = Actor::Ptr;
-
/** @ingroup s4u_api
* @brief Static methods working on the current actor (see @ref s4u::Actor) */
namespace this_actor {
*
* See \ref Comm for the full communication API (including non blocking communications).
*/
- XBT_PUBLIC(void*) recv(Mailbox &chan);
+ XBT_PUBLIC(void*) recv(MailboxPtr chan);
/** Block the actor until it delivers a message of the given simulated size to the given mailbox
*
* See \ref Comm for the full communication API (including non blocking communications).
*/
- XBT_PUBLIC(void) send(Mailbox &chan, void*payload, size_t simulatedSize);
+ XBT_PUBLIC(void) send(MailboxPtr chan, void*payload, size_t simulatedSize);
/**
* Return the PID of the current actor.
return res;
}
/** Creates (but don't start) an async send to the mailbox @p dest */
- static Comm &send_init(Mailbox &dest);
+ static Comm &send_init(MailboxPtr dest);
/** Creates and start an async send to the mailbox @p dest */
- static Comm &send_async(Mailbox &dest, void *data, int simulatedByteAmount);
+ static Comm &send_async(MailboxPtr dest, void *data, int simulatedByteAmount);
/** Creates (but don't start) an async recv onto the mailbox @p from */
- static Comm &recv_init(Mailbox &from);
+ static Comm &recv_init(MailboxPtr from);
/** Creates and start an async recv to the mailbox @p from */
- static Comm &recv_async(Mailbox &from, void **data);
+ static Comm &recv_async(MailboxPtr from, void **data);
void start() override;
void wait() override;
smx_process_t sender_ = nullptr;
smx_process_t receiver_ = nullptr;
- Mailbox *mailbox_ = nullptr;
+ MailboxPtr mailbox_ = nullptr;
};
}} // namespace simgrid::s4u
#ifndef SIMGRID_S4U_FORWARD_HPP
#define SIMGRID_S4U_FORWARD_HPP
+#include <boost/intrusive_ptr.hpp>
+
namespace simgrid {
namespace s4u {
+class Actor;
+using ActorPtr = boost::intrusive_ptr<Actor>;
+
class Activity;
class Comm;
class Engine;
class Host;
class Mailbox;
+using MailboxPtr = boost::intrusive_ptr<Mailbox>;
+
class Storage;
}
#include <string>
-#include <boost/intrusive_ptr.hpp>
-
#include <xbt/base.h>
#include <simgrid/s4u/forward.hpp>
// We don't have to manage the lifetime of mailboxes:
friend void intrusive_ptr_add_ref(Mailbox*) {}
friend void intrusive_ptr_release(Mailbox*) {}
- using Ptr = boost::intrusive_ptr<Mailbox>;
/** Get the name of that mailbox */
const char *getName();
/** Retrieve the mailbox associated to the given string */
- static Ptr byName(const char *name);
+ static MailboxPtr byName(const char *name);
/** Returns whether the mailbox contains queued communications */
bool empty();
ActorPtr receiver();
};
-using MailboxPtr = Mailbox::Ptr;
-
}} // namespace simgrid::s4u
XBT_PUBLIC(sg_mbox_t) sg_mbox_by_name(const char*name);
smx_process_t process = simcall_process_create(
name, std::move(code), nullptr, host->name().c_str(),
killTime, nullptr, 0);
- return Ptr(&process->getIface());
+ return ActorPtr(&process->getIface());
}
ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
return simcall_execution_wait(s);
}
-void* recv(Mailbox &chan) {
+void* recv(MailboxPtr chan) {
void *res = nullptr;
Comm& c = Comm::recv_init(chan);
c.setDstData(&res,sizeof(res));
return res;
}
-void send(Mailbox &chan, void *payload, size_t simulatedSize) {
+void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
Comm& c = Comm::send_init(chan);
c.setRemains(simulatedSize);
c.setSrcData(payload);
-s4u::Comm &Comm::send_init(s4u::Mailbox &chan) {
+s4u::Comm &Comm::send_init(s4u::MailboxPtr chan) {
s4u::Comm *res = new s4u::Comm();
res->sender_ = SIMIX_process_self();
- res->mailbox_ = &chan;
+ res->mailbox_ = chan;
return *res;
}
-s4u::Comm &Comm::recv_init(s4u::Mailbox &chan) {
+s4u::Comm &Comm::recv_init(s4u::MailboxPtr chan) {
s4u::Comm *res = new s4u::Comm();
res->receiver_ = SIMIX_process_self();
- res->mailbox_ = &chan;
+ res->mailbox_ = chan;
return *res;
}
state_ = finished;
}
-s4u::Comm &Comm::send_async(Mailbox &dest, void *data, int simulatedSize) {
+s4u::Comm &Comm::send_async(MailboxPtr dest, void *data, int simulatedSize) {
s4u::Comm &res = s4u::Comm::send_init(dest);
res.setRemains(simulatedSize);
res.srcBuff_ = data;
return res;
}
-s4u::Comm &Comm::recv_async(Mailbox &dest, void **data) {
+s4u::Comm &Comm::recv_async(MailboxPtr dest, void **data) {
s4u::Comm &res = s4u::Comm::recv_init(dest);
res.setDstData(data);
res.start();