Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Register function names
authorGabriel Corona <gabriel.corona@loria.fr>
Wed, 22 Jun 2016 10:03:54 +0000 (12:03 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Wed, 22 Jun 2016 13:03:11 +0000 (15:03 +0200)
examples/s4u/CMakeLists.txt
examples/s4u/basic/s4u_basic.cpp
examples/s4u/basic/s4u_basic.h [new file with mode: 0644]
examples/s4u/basic/s4u_basic.tesh
examples/s4u/basic/s4u_basic_function.cpp [new file with mode: 0644]
include/simgrid/s4u/actor.hpp
include/simgrid/s4u/engine.hpp
include/simgrid/simix.hpp
src/s4u/s4u_actor.cpp
src/simix/smx_deployment.cpp
src/simix/smx_private.h

index a3a2a04..f649ba1 100644 (file)
@@ -7,6 +7,10 @@ foreach (example basic io mutex actions-comm)
   set(examples_src  ${examples_src}  ${CMAKE_CURRENT_SOURCE_DIR}/${example}/s4u_${example}.cpp)
 endforeach()
 
+add_executable       (s4u_basic_function  basic/s4u_basic_function.cpp)
+target_link_libraries(s4u_basic_function  simgrid)
+set_target_properties(s4u_basic_function  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/basic)
+
 set(examples_src  ${examples_src}                                     PARENT_SCOPE)
 set(tesh_files    ${tesh_files}                                       PARENT_SCOPE)
 set(xml_files     ${xml_files}    ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_split_d.xml
index 12156f2..73cf613 100644 (file)
@@ -5,31 +5,9 @@
 
 #include <xbt/sysdep.h>
 
-#include "simgrid/s4u.h"
-
-XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
-
-class Worker {
-public:
-  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:
-  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.");
-  }
-};
+#include <simgrid/s4u.h>
 
+#include "s4u_basic.h"
 
 int main(int argc, char **argv) {
   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
diff --git a/examples/s4u/basic/s4u_basic.h b/examples/s4u/basic/s4u_basic.h
new file mode 100644 (file)
index 0000000..7a3f9c6
--- /dev/null
@@ -0,0 +1,35 @@
+/* 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.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
+
+class Worker {
+public:
+  Worker() {};
+  Worker(simgrid::xbt::args 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(simgrid::xbt::args 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.");
+  }
+};
index 111fea7..627fb9d 100644 (file)
@@ -6,3 +6,10 @@ $ $SG_TEST_EXENV ${bindir:=.}/s4u_basic
 > [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_basic_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.
diff --git a/examples/s4u/basic/s4u_basic_function.cpp b/examples/s4u/basic/s4u_basic_function.cpp
new file mode 100644 (file)
index 0000000..dd39a93
--- /dev/null
@@ -0,0 +1,22 @@
+/* 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.h>
+
+#include "s4u_basic.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");
+  simgrid::xbt::args args;
+  simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args);
+  simgrid::s4u::Actor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args);
+  e->run();
+  return 0;
+}
index 7f8016f..052ad86 100644 (file)
@@ -206,6 +206,13 @@ public:
     Actor(name, host, wrap_task(std::move(code), std::move(args)...))
   {}
 
+  // Create actor from function name:
+
+  Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args);
+
+  Actor(const char* name, s4u::Host *host, const char* function, simgrid::xbt::args args) :
+    Actor(name, host, -1.0, function, std::move(args)) {}
+
   /** Retrieves the actor that have the given PID (or NULL if not existing) */
   //static Actor *byPid(int pid); not implemented
 
index bee4afa..07863e1 100644 (file)
@@ -7,6 +7,9 @@
 #define SIMGRID_S4U_ENGINE_HPP
 
 #include <xbt/base.h>
+#include <xbt/functional.hpp>
+
+#include <simgrid/simix.hpp>
 
 #include <simgrid/s4u/forward.hpp>
 
@@ -58,6 +61,27 @@ public:
   /** @brief Retrieve the AS of the given name (or nullptr if not found) */
   simgrid::s4u::As *asByNameOrNull(const char *name);
 
+  template<class F>
+  void registerFunction(const char* name)
+  {
+    simgrid::simix::registerFunction(name, [](simgrid::xbt::args args){
+      return simgrid::simix::ActorCode([args] {
+        F code(std::move(args));
+        code();
+      });
+    });
+  }
+
+  template<class F>
+  void registerFunction(const char* name, F code)
+  {
+    simgrid::simix::registerFunction(name, [code](simgrid::xbt::args args){
+      return simgrid::simix::ActorCode([code,args] {
+        code(std::move(args));
+      });
+    });
+  }
+
 private:
   static s4u::Engine *instance_;
 };
index 99d2410..eb92f42 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <xbt/function_types.h>
 #include <xbt/future.hpp>
+#include <xbt/functional.hpp>
 
 #include <simgrid/simix.h>
 
@@ -171,6 +172,14 @@ public:
 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
 
+// What's executed as SIMIX actor code:
+typedef std::function<void()> ActorCode;
+
+// Create ActorCode based on argv:
+typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
+
+XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
+
 }
 }
 
index 5cec45a..ca6cfec 100644 (file)
@@ -12,6 +12,8 @@
 #include "simgrid/s4u/host.hpp"
 #include "simgrid/s4u/mailbox.hpp"
 
+#include "src/simix/smx_private.h"
+
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
 
 using namespace simgrid;
@@ -25,6 +27,15 @@ s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::funct
     killTime, nullptr, 0));
 }
 
+s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args)
+{
+  simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
+  simgrid::simix::ActorCode code = factory(std::move(args));
+  this->pimpl_ = SIMIX_process_ref(simcall_process_create(
+    name, std::move(code), nullptr, host->name().c_str(),
+    killTime, nullptr, 0));
+}
+
 void s4u::Actor::join() {
   simcall_process_join(pimpl_, -1);
 }
index b5e84b1..a740981 100644 (file)
@@ -151,3 +151,14 @@ void SIMIX_process_set_function(const char *process_host,
   process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
   sg_platf_new_process(&process);
 }
+
+namespace simgrid {
+namespace simix {
+
+void registerFunction(const char* name, ActorCodeFactory factory)
+{
+  simix_global->registered_functions[name] = std::move(factory);
+}
+
+}
+}
\ No newline at end of file
index 6906a08..bb735b8 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <xbt/functional.hpp>
 
+
 #include "src/internal_config.h"
 #include "simgrid/simix.h"
 #include "surf/surf.h"
@@ -67,12 +68,6 @@ typedef struct s_smx_context_factory *smx_context_factory_t;
 namespace simgrid {
 namespace simix {
 
-// What's executed as SIMIX actor code:
-typedef std::function<void()> ActorCode;
-
-// Create ActorCode based on argv:
-typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
-
 class Global {
 public:
   smx_context_factory_t context_factory = nullptr;