Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Channel: the S4U name of msg_mailbox_t
authorMartin Quinson <martin.quinson@loria.fr>
Sun, 26 Jul 2015 13:15:52 +0000 (15:15 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Sun, 26 Jul 2015 13:15:56 +0000 (15:15 +0200)
I hope that the name Channel will be easier to understand than mailbox
(users were often thinking that mailboxes must be attached to a given
host).

The communication primitives do not take a name to a mailbox, but a
mailbox directly. I hate python for raising no warning when I do a
typo on a variable name, and addressing mailboxes by name leads to the
exact same situation.

I would like Channel::byName to return a reference (since they are
created -- in kernel mode -- on need), but I fail to get the syntax
right.

At some point, a C API should be exported, the content of smx_rdv_t
should be integrated into that class, and smx_rdv_t should be killed.
But I want to get a working API to play with first.

buildtools/Cmake/DefinePackages.cmake
examples/s4u/dumb/s4u_test.cpp
include/simgrid/s4u.h
include/simgrid/s4u/channel.hpp [new file with mode: 0644]
include/simgrid/s4u/process.hpp
src/s4u/s4u_channel.cpp [new file with mode: 0644]
src/s4u/s4u_process.cpp

index 48e5ec5..5eee142 100644 (file)
@@ -390,6 +390,7 @@ else()
 endif()
 
 set(S4U_SRC
 endif()
 
 set(S4U_SRC
+  src/s4u/s4u_channel.cpp
   src/s4u/s4u_engine.cpp  
   src/s4u/s4u_host.cpp  
   src/s4u/s4u_process.cpp
   src/s4u/s4u_engine.cpp  
   src/s4u/s4u_host.cpp  
   src/s4u/s4u_process.cpp
@@ -694,6 +695,7 @@ set(headers_to_install
   include/simgrid/simix.h
   include/simgrid/host.h
   include/simgrid/link.h
   include/simgrid/simix.h
   include/simgrid/host.h
   include/simgrid/link.h
+  include/simgrid/s4u/channel.hpp  
   include/simgrid/s4u/engine.hpp  
   include/simgrid/s4u/host.hpp  
   include/simgrid/s4u/process.hpp
   include/simgrid/s4u/engine.hpp  
   include/simgrid/s4u/host.hpp  
   include/simgrid/s4u/process.hpp
index a69c7e6..f85ac31 100644 (file)
@@ -6,32 +6,33 @@
 #include "simgrid/s4u.h"
 
 using namespace simgrid;
 #include "simgrid/s4u.h"
 
 using namespace simgrid;
+using namespace s4u;
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
 
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
 
-class Worker : s4u::Process {
+class Worker : Process {
 public:
 public:
-       Worker(const char*procname, s4u::Host *host,int argc, char **argv)
+       Worker(const char*procname, Host *host,int argc, char **argv)
                        : s4u::Process(procname,host,argc,argv){}
 
        int main(int argc, char **argv) {
                XBT_INFO("Hello s4u, I'm ready to serve");
 
                        : s4u::Process(procname,host,argc,argv){}
 
        int main(int argc, char **argv) {
                XBT_INFO("Hello s4u, I'm ready to serve");
 
-               char *msg = recvstr("worker");
+               char *msg = recvstr(*Channel::byName("worker"));
                XBT_INFO("I received '%s'",msg);
                XBT_INFO("I'm done. See you.");
                return 1;
        }
 };
 
                XBT_INFO("I received '%s'",msg);
                XBT_INFO("I'm done. See you.");
                return 1;
        }
 };
 
-class Master : s4u::Process {
+class Master : Process {
 public:
 public:
-       Master(const char*procname, s4u::Host *host,int argc, char **argv)
-                       : s4u::Process(procname,host,argc,argv){}
+       Master(const char*procname, Host *host,int argc, char **argv)
+                       : Process(procname,host,argc,argv){}
 
        int main(int argc, char **argv) {
                XBT_INFO("Hello s4u, I have something to send");
 
        int main(int argc, char **argv) {
                XBT_INFO("Hello s4u, I have something to send");
-               sendstr("worker","GaBuZoMeu");
+               sendstr(*Channel::byName("worker"),"GaBuZoMeu");
 
                XBT_INFO("I'm done. See you.");
                return 1;
 
                XBT_INFO("I'm done. See you.");
                return 1;
@@ -40,11 +41,11 @@ public:
 
 
 int main(int argc, char **argv) {
 
 
 int main(int argc, char **argv) {
-       s4u::Engine *e = new s4u::Engine(&argc,argv);
+       Engine *e = new Engine(&argc,argv);
        e->loadPlatform("../../platforms/two_hosts_platform.xml");
 
        e->loadPlatform("../../platforms/two_hosts_platform.xml");
 
-       new Worker("worker", s4u::Host::byName("host0"), 0, NULL);
-       new Master("master", s4u::Host::byName("host1"), 0, NULL);
+       new Worker("worker", Host::byName("host0"), 0, NULL);
+       new Master("master", Host::byName("host1"), 0, NULL);
        e->run();
        return 0;
 }
        e->run();
        return 0;
 }
index d360a3c..1814e33 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef SIMGRID_S4U_S4U_H
 #define SIMGRID_S4U_S4U_H
 
 #ifndef SIMGRID_S4U_S4U_H
 #define SIMGRID_S4U_S4U_H
 
+#include "simgrid/s4u/channel.hpp"
 #include "simgrid/s4u/engine.hpp"
 #include "simgrid/s4u/host.hpp"
 #include "simgrid/s4u/process.hpp"
 #include "simgrid/s4u/engine.hpp"
 #include "simgrid/s4u/host.hpp"
 #include "simgrid/s4u/process.hpp"
diff --git a/include/simgrid/s4u/channel.hpp b/include/simgrid/s4u/channel.hpp
new file mode 100644 (file)
index 0000000..1c3abd2
--- /dev/null
@@ -0,0 +1,43 @@
+/* Copyright (c) 2006-2015. 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_CHANNEL_HPP
+#define SIMGRID_S4U_CHANNEL_HPP
+
+#include <boost/unordered_map.hpp>
+
+#include "simgrid/s4u/process.hpp"
+
+namespace simgrid {
+namespace s4u {
+
+/** @brief Channel
+ *
+ * Rendez-vous point for network communications, similar to URLs on which you could post and retrieve data.
+ * They are not network locations (you can post and retrieve on a given mailbox from anywhere on the network).
+ */
+class Channel {
+       friend Process;
+
+private:
+       Channel(const char*name, smx_rdv_t inferior);
+public:
+       ~Channel();
+       
+protected:
+       smx_rdv_t getInferior() { return p_inferior; }
+
+public:
+       /** Retrieve the channel associated to the given string */
+       static Channel *byName(const char *name);
+
+private:
+       std::string p_name;
+       smx_rdv_t p_inferior;
+       static boost::unordered_map<std::string, Channel *> *channels;
+};
+}} // namespace simgrid::s4u
+
+#endif /* SIMGRID_S4U_CHANNEL_HPP */
index bfe5c49..db9f76c 100644 (file)
@@ -12,6 +12,7 @@ namespace simgrid {
 namespace s4u {
 
 class Host;
 namespace s4u {
 
 class Host;
+class Channel;
 
 /** @brief Simulation Agent
  *
 
 /** @brief Simulation Agent
  *
@@ -84,10 +85,10 @@ public:
        //void* recv(const char *mailbox);
 
        /** Block the process until it gets a string message (to be freed after use) from the given mailbox */
        //void* recv(const char *mailbox);
 
        /** Block the process until it gets a string message (to be freed after use) from the given mailbox */
-       char *recvstr(const char* mailbox);
+       char *recvstr(Channel &chan);
 
        /** Block the process until it delivers a string message (that will be copied) to the given mailbox */
 
        /** Block the process until it delivers a string message (that will be copied) to the given mailbox */
-       void sendstr(const char*mailbox, const char*msg);
+       void sendstr(Channel &chan, const char*msg);
 
 protected:
        smx_process_t getInferior() {return p_smx_process;}
 
 protected:
        smx_process_t getInferior() {return p_smx_process;}
diff --git a/src/s4u/s4u_channel.cpp b/src/s4u/s4u_channel.cpp
new file mode 100644 (file)
index 0000000..396b887
--- /dev/null
@@ -0,0 +1,33 @@
+/* Copyright (c) 2006-2015. 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/log.h"
+#include "msg/msg_private.h"
+#include "msg/msg_mailbox.h"
+
+#include "simgrid/s4u/channel.hpp"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_channel,"S4U Communication Channels");
+
+
+using namespace simgrid;
+
+boost::unordered_map <std::string, s4u::Channel *> *s4u::Channel::channels = new boost::unordered_map<std::string, s4u::Channel*> ();
+
+
+s4u::Channel::Channel(const char*name, smx_rdv_t inferior) {
+       p_inferior = inferior;
+       channels->insert({name, this});
+}
+s4u::Channel *s4u::Channel::byName(const char*name) {
+       s4u::Channel * res;
+       try {
+               res = channels->at(name);
+       } catch (std::out_of_range& e) {
+               res = new Channel(name,simcall_rdv_create(name));
+       }
+       return res;
+}
index 608c748..bb86178 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "simgrid/s4u/host.hpp"
 #include "simgrid/s4u/process.hpp"
 
 #include "simgrid/s4u/host.hpp"
 #include "simgrid/s4u/process.hpp"
+#include "simgrid/s4u/channel.hpp"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_process,"S4U processes");
 
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_process,"S4U processes");
 
@@ -80,19 +81,17 @@ void s4u::Process::execute(double flops) {
        simcall_process_execute(NULL,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
 }
 
        simcall_process_execute(NULL,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
 }
 
-char *s4u::Process::recvstr(const char* mailbox) {
+char *s4u::Process::recvstr(Channel &chan) {
        char *res=NULL;
        size_t res_size=sizeof(res);
        char *res=NULL;
        size_t res_size=sizeof(res);
-       smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox);
 
 
-       simcall_comm_recv(rdv,&res,&res_size,NULL,NULL,NULL,-1 /* timeout */,-1 /*rate*/);
+       simcall_comm_recv(chan.getInferior(),&res,&res_size,NULL,NULL,NULL,-1 /* timeout */,-1 /*rate*/);
 
     return res;
 }
 
     return res;
 }
-void s4u::Process::sendstr(const char* mailbox, const char*msg) {
+void s4u::Process::sendstr(Channel &chan, const char*msg) {
        char *msg_cpy=xbt_strdup(msg);
        char *msg_cpy=xbt_strdup(msg);
-       smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox);
-       smx_synchro_t comm = simcall_comm_isend(p_smx_process, rdv, strlen(msg),
+       smx_synchro_t comm = simcall_comm_isend(p_smx_process, chan.getInferior(), strlen(msg),
                        -1/*rate*/, msg_cpy, sizeof(void *),
                        NULL, NULL, NULL,NULL/*data*/, 0);
        simcall_comm_wait(comm, -1/*timeout*/);
                        -1/*rate*/, msg_cpy, sizeof(void *),
                        NULL, NULL, NULL,NULL/*data*/, 0);
        simcall_comm_wait(comm, -1/*timeout*/);