Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::function for Comm callbacks.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 9 Mar 2022 21:48:21 +0000 (22:48 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 10 Mar 2022 10:19:27 +0000 (11:19 +0100)
12 files changed:
docs/source/app_s4u.rst
include/simgrid/s4u/Comm.hpp
include/simgrid/s4u/Engine.hpp
include/simgrid/s4u/Mailbox.hpp
src/kernel/activity/CommImpl.cpp
src/kernel/activity/CommImpl.hpp
src/kernel/activity/MailboxImpl.cpp
src/kernel/activity/MailboxImpl.hpp
src/kernel/actor/CommObserver.hpp
src/s4u/s4u_Comm.cpp
src/s4u/s4u_Engine.cpp
src/s4u/s4u_Mailbox.cpp

index 082c49b..5dd5358 100644 (file)
@@ -1069,7 +1069,7 @@ Receiving data
       .. doxygenfunction:: simgrid::s4u::Mailbox::get(double timeout)
       .. doxygenfunction:: simgrid::s4u::Mailbox::get_async(T **data)
       .. doxygenfunction:: simgrid::s4u::Mailbox::get_init()
-      .. doxygenfunction:: simgrid::s4u::Mailbox::iprobe(int type, bool(*match_fun)(void *, void *, kernel::activity::CommImpl *), void *data)
+      .. doxygenfunction:: simgrid::s4u::Mailbox::iprobe(int type, const std::function<bool(void *, void *, kernel::activity::CommImpl *)>& match_fun, void *data)
       .. doxygenfunction:: simgrid::s4u::Mailbox::listen
       .. doxygenfunction:: simgrid::s4u::Mailbox::ready
 
@@ -2176,7 +2176,7 @@ Querying info
       .. doxygenfunction:: simgrid::s4u::Comm::set_dst_data(void **buff)
       .. doxygenfunction:: simgrid::s4u::Comm::set_dst_data(void **buff, size_t size)
       .. doxygenfunction:: simgrid::s4u::Comm::detach()
-      .. doxygenfunction:: simgrid::s4u::Comm::detach(void(*clean_function)(void *))
+      .. doxygenfunction:: simgrid::s4u::Comm::detach(const std::function<void(void*)>& clean_function)
       .. doxygenfunction:: simgrid::s4u::Comm::set_payload_size(uint64_t bytes)
       .. doxygenfunction:: simgrid::s4u::Comm::set_rate(double rate)
       .. doxygenfunction:: simgrid::s4u::Comm::set_src_data(void *buff)
index d4ea842..6070543 100644 (file)
@@ -32,9 +32,9 @@ class XBT_PUBLIC Comm : public Activity_T<Comm> {
 
   /* FIXME: expose these elements in the API */
   bool detached_                                                          = false;
-  bool (*match_fun_)(void*, void*, kernel::activity::CommImpl*)           = nullptr;
-  void (*clean_fun_)(void*)                                               = nullptr;
-  void (*copy_data_function_)(kernel::activity::CommImpl*, void*, size_t) = nullptr;
+  std::function<bool(void*, void*, kernel::activity::CommImpl*)> match_fun_;
+  std::function<void(void*)> clean_fun_;
+  std::function<void(kernel::activity::CommImpl*, void*, size_t)> copy_data_function_;
 
   Comm() = default;
 
@@ -53,20 +53,21 @@ public:
   static void on_start_cb(const std::function<void(Comm const&)>& cb) { on_start.connect(cb); }
   static void on_completion_cb(const std::function<void(Activity const&)>& cb) { on_completion.connect(cb); }
   /* More callbacks */
-  CommPtr set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t));
+  CommPtr set_copy_data_callback(const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback);
   static void copy_buffer_callback(kernel::activity::CommImpl*, void*, size_t);
   static void copy_pointer_callback(kernel::activity::CommImpl*, void*, size_t);
 
   ~Comm() override;
 
   static void send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double task_size, double rate, void* src_buff,
-                   size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
-                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
-                   double timeout);
+                   size_t src_buff_size,
+                   const std::function<bool(void*, void*, simgrid::kernel::activity::CommImpl*)>& match_fun,
+                   const std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)>& copy_data_fun,
+                   void* data, double timeout);
   static void recv(kernel::actor::ActorImpl* receiver, const Mailbox* mbox, void* dst_buff, size_t* dst_buff_size,
-                   bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
-                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
-                   double timeout, double rate);
+                   const std::function<bool(void*, void*, simgrid::kernel::activity::CommImpl*)>& match_fun,
+                   const std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)>& copy_data_fun,
+                   void* data, double timeout, double rate);
 
   /* "One-sided" communications. This way of communicating bypasses the mailbox and actors mechanism. It creates a
    * communication (vetoabled, asynchronous, or synchronous) directly between two hosts. There is really no limit on
@@ -153,7 +154,7 @@ public:
   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
   Comm* detach();
   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
-  Comm* detach(void (*clean_function)(void*))
+  Comm* detach(const std::function<void(void*)>& clean_function)
   {
     clean_fun_ = clean_function;
     return detach();
index 7716e47..87e1815 100644 (file)
@@ -200,7 +200,8 @@ public:
   static void set_config(const std::string& name, double value);
   static void set_config(const std::string& name, const std::string& value);
 
-  Engine* set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t));
+  Engine*
+  set_default_comm_data_copy_callback(const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback);
 
   /** Add a callback fired when the platform is created (ie, the xml file parsed),
    * right before the actual simulation starts. */
index 3070b0a..cda0328 100644 (file)
@@ -99,8 +99,8 @@ public:
    */
   CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
 
-  kernel::activity::ActivityImplPtr iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*),
-                                           void* data);
+  kernel::activity::ActivityImplPtr
+  iprobe(int type, const std::function<bool(void*, void*, kernel::activity::CommImpl*)>& match_fun, void* data);
   /** Blocking data transmission.
    *
    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
index 08f4c31..a383818 100644 (file)
@@ -49,9 +49,9 @@ namespace activity {
 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
 
-void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback;
+std::function<void(CommImpl*, void*, size_t)> CommImpl::copy_data_callback_ = &s4u::Comm::copy_pointer_callback;
 
-void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
+void CommImpl::set_copy_data_callback(const std::function<void(CommImpl*, void*, size_t)>& callback)
 {
   copy_data_callback_ = callback;
 }
index 71f44a7..3912d23 100644 (file)
@@ -20,7 +20,7 @@ class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
   ~CommImpl() override;
   void cleanup_surf();
 
-  static void (*copy_data_callback_)(CommImpl*, void*, size_t);
+  static std::function<void(CommImpl*, void*, size_t)> copy_data_callback_;
 
   double rate_       = -1.0;
   double size_       = 0.0;
@@ -36,7 +36,7 @@ class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
 public:
   CommImpl() = default;
 
-  static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t));
+  static void set_copy_data_callback(const std::function<void(CommImpl*, void*, size_t)>& callback);
 
   CommImpl& set_type(CommImplType type);
   CommImplType get_type() const { return type_; }
@@ -75,11 +75,11 @@ public:
   void set_exception(actor::ActorImpl* issuer) override;
   void finish() override;
 
-  void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */
-  bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when
+  std::function<void(void*)> clean_fun; /* Function to clean the detached src_buf if something goes wrong */
+  std::function<bool(void*, void*, CommImpl*)> match_fun; /* Filter function used by the other side. It is used when
 looking if a given communication matches my needs. For that, myself must match the
 expectations of the other side, too. See  */
-  void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr;
+  std::function<void(CommImpl*, void*, size_t)> copy_data_fun;
 
   /* Surf action data */
   resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
index 7af7743..cafb036 100644 (file)
@@ -88,7 +88,7 @@ void MailboxImpl::clear()
   }
 }
 
-CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data)
+CommImplPtr MailboxImpl::iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data)
 {
   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
 
@@ -123,7 +123,8 @@ CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommIm
  *  @param remove_matching whether or not to clean the found object from the queue
  *  @return The communication activity if found, nullptr otherwise
  */
-CommImplPtr MailboxImpl::find_matching_comm(CommImplType type, bool (*match_fun)(void*, void*, CommImpl*),
+CommImplPtr MailboxImpl::find_matching_comm(CommImplType type,
+                                            const std::function<bool(void*, void*, CommImpl*)>& match_fun,
                                             void* this_user_data, const CommImplPtr& my_synchro, bool done,
                                             bool remove_matching)
 {
index ce73552..6421973 100644 (file)
@@ -57,9 +57,9 @@ public:
   void push_done(CommImplPtr done_comm) { done_comm_queue_.push_back(done_comm); }
   void remove(const CommImplPtr& comm);
   void clear();
-  CommImplPtr iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data);
-  CommImplPtr find_matching_comm(CommImplType type, bool (*match_fun)(void*, void*, CommImpl*), void* this_user_data,
-                                 const CommImplPtr& my_synchro, bool done, bool remove_matching);
+  CommImplPtr iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data);
+  CommImplPtr find_matching_comm(CommImplType type, const std::function<bool(void*, void*, CommImpl*)>& match_fun,
+                                 void* this_user_data, const CommImplPtr& my_synchro, bool done, bool remove_matching);
   bool is_permanent() const { return permanent_receiver_ != nullptr; }
   actor::ActorImplPtr get_permanent_receiver() const { return permanent_receiver_; }
   bool empty() const { return comm_queue_.empty(); }
index c64f2b0..dff6de5 100644 (file)
@@ -92,16 +92,18 @@ class CommIsendSimcall : public SimcallObserver {
   activity::CommImpl* comm_;
   int tag_;
 
-  bool (*match_fun_)(void*, void*, activity::CommImpl*);
-  void (*clean_fun_)(void*); // used to free the synchro in case of problem after a detached send
-  void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one
+  std::function<bool(void*, void*, activity::CommImpl*)> match_fun_;
+  std::function<void(void*)> clean_fun_; // used to free the synchro in case of problem after a detached send
+  std::function<void(activity::CommImpl*, void*, size_t)> copy_data_fun_; // used to copy data if not default one
 
 public:
-  CommIsendSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate,
-                   unsigned char* src_buff, size_t src_buff_size, bool (*match_fun)(void*, void*, activity::CommImpl*),
-                   void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
-                   void (*copy_data_fun)(activity::CommImpl*, void*, size_t), // used to copy data if not default one
-                   void* payload, bool detached)
+  CommIsendSimcall(
+      ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate, unsigned char* src_buff,
+      size_t src_buff_size, const std::function<bool(void*, void*, activity::CommImpl*)>& match_fun,
+      const std::function<void(void*)>& clean_fun, // used to free the synchro in case of problem after a detached send
+      const std::function<void(activity::CommImpl*, void*, size_t)>&
+          copy_data_fun, // used to copy data if not default one
+      void* payload, bool detached)
       : SimcallObserver(actor)
       , mbox_(mbox)
       , payload_size_(payload_size)
@@ -127,9 +129,9 @@ public:
   void set_comm(activity::CommImpl* comm) { comm_ = comm; }
   void set_tag(int tag) { tag_ = tag; }
 
-  auto get_match_fun() const { return match_fun_; }
-  auto get_clean_fun() const { return clean_fun_; }
-  auto get_copy_data_fun() const { return copy_data_fun_; }
+  auto const& get_match_fun() const { return match_fun_; }
+  auto const& get_clean_fun() const { return clean_fun_; }
+  auto const& get_copy_data_fun() const { return copy_data_fun_; }
 };
 
 class CommIrecvSimcall : public SimcallObserver {
@@ -141,13 +143,14 @@ class CommIrecvSimcall : public SimcallObserver {
   int tag_;
   activity::CommImpl* comm_;
 
-  bool (*match_fun_)(void*, void*, activity::CommImpl*);
-  void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one
+  std::function<bool(void*, void*, activity::CommImpl*)> match_fun_;
+  std::function<void(activity::CommImpl*, void*, size_t)> copy_data_fun_; // used to copy data if not default one
 
 public:
   CommIrecvSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, unsigned char* dst_buff, size_t* dst_buff_size,
-                   bool (*match_fun)(void*, void*, activity::CommImpl*),
-                   void (*copy_data_fun)(activity::CommImpl*, void*, size_t), void* payload, double rate)
+                   const std::function<bool(void*, void*, activity::CommImpl*)>& match_fun,
+                   const std::function<void(activity::CommImpl*, void*, size_t)>& copy_data_fun, void* payload,
+                   double rate)
       : SimcallObserver(actor)
       , mbox_(mbox)
       , dst_buff_(dst_buff)
@@ -168,8 +171,8 @@ public:
   void set_comm(activity::CommImpl* comm) { comm_ = comm; }
   void set_tag(int tag) { tag_ = tag; }
 
-  auto get_match_fun() const { return match_fun_; };
-  auto get_copy_data_fun() const { return copy_data_fun_; }
+  auto const& get_match_fun() const { return match_fun_; };
+  auto const& get_copy_data_fun() const { return copy_data_fun_; }
 };
 
 } // namespace actor
index 2443dd1..eec97a7 100644 (file)
@@ -24,7 +24,7 @@ xbt::signal<void(Comm const&)> Comm::on_send;
 xbt::signal<void(Comm const&)> Comm::on_recv;
 xbt::signal<void(Comm const&)> Comm::on_completion;
 
-CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
+CommPtr Comm::set_copy_data_callback(const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
 {
   copy_data_function_ = callback;
   return this;
@@ -61,8 +61,10 @@ Comm::~Comm()
 }
 
 void Comm::send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double task_size, double rate, void* src_buff,
-                size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
-                void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double timeout)
+                size_t src_buff_size,
+                const std::function<bool(void*, void*, simgrid::kernel::activity::CommImpl*)>& match_fun,
+                const std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)>& copy_data_fun,
+                void* data, double timeout)
 {
   /* checking for infinite values */
   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
@@ -102,9 +104,9 @@ void Comm::send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double ta
 }
 
 void Comm::recv(kernel::actor::ActorImpl* receiver, const Mailbox* mbox, void* dst_buff, size_t* dst_buff_size,
-                bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
-                void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double timeout,
-                double rate)
+                const std::function<bool(void*, void*, simgrid::kernel::activity::CommImpl*)>& match_fun,
+                const std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)>& copy_data_fun,
+                void* data, double timeout, double rate)
 {
   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
   xbt_assert(mbox, "No rendez-vous point defined for recv");
index b7152fa..ea62433 100644 (file)
@@ -452,7 +452,8 @@ void Engine::set_config(const std::string& name, const std::string& value)
   config::set_value(name.c_str(), value);
 }
 
-Engine* Engine::set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
+Engine* Engine::set_default_comm_data_copy_callback(
+    const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
 {
   kernel::activity::CommImpl::set_copy_data_callback(callback);
   return this;
index fd5a442..a0c99e5 100644 (file)
@@ -129,10 +129,10 @@ CommPtr Mailbox::get_init()
 }
 
 kernel::activity::ActivityImplPtr
-Mailbox::iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data)
+Mailbox::iprobe(int type, const std::function<bool(void*, void*, kernel::activity::CommImpl*)>& match_fun, void* data)
 {
   return kernel::actor::simcall_answered(
-      [this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
+      [this, type, &match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
 }
 } // namespace s4u
 } // namespace simgrid