Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mess up with operations
authorFred Suter <suterf@ornl.gov>
Fri, 5 May 2023 01:20:01 +0000 (21:20 -0400)
committerFred Suter <suterf@ornl.gov>
Fri, 5 May 2023 01:20:01 +0000 (21:20 -0400)
+ chainable setters
+ getters
+ boost instrusive pointers

include/simgrid/plugins/operation.hpp
src/plugins/operation.cpp

index 0f2f0e6..4661613 100644 (file)
@@ -4,6 +4,7 @@
 #include <simgrid/s4u/Activity.hpp>
 #include <xbt/Extendable.hpp>
 
+#include <atomic>
 #include <map>
 #include <memory>
 #include <set>
 namespace simgrid::plugins {
 
 class Operation;
-using OperationPtr = std::shared_ptr<Operation>;
+using OperationPtr = boost::intrusive_ptr<Operation>;
+XBT_PUBLIC void intrusive_ptr_release(Operation* o);
+XBT_PUBLIC void intrusive_ptr_add_ref(Operation* o);
 class ExecOp;
-using ExecOpPtr = std::shared_ptr<ExecOp>;
+using ExecOpPtr = boost::intrusive_ptr<ExecOp>;
+XBT_PUBLIC void intrusive_ptr_release(ExecOp* e);
+XBT_PUBLIC void intrusive_ptr_add_ref(ExecOp* e);
 class CommOp;
-using CommOpPtr = std::shared_ptr<CommOp>;
+using CommOpPtr =  boost::intrusive_ptr<CommOp>;
+XBT_PUBLIC void intrusive_ptr_release(CommOp* c);
+XBT_PUBLIC void intrusive_ptr_add_ref(CommOp* c);
 
 class ExtendedAttributeActivity {
 public:
@@ -43,7 +50,7 @@ protected:
   int queued_execs_ = 0;
   int count_        = 0;
   bool working_     = false;
-  simgrid::s4u::ActivityPtr current_activity_;
+  s4u::ActivityPtr current_activity_;
   std::function<void(Operation*)> end_func_;
   std::function<void(Operation*)> start_func_;
   Operation(const std::string& name);
@@ -52,12 +59,15 @@ protected:
 
   static xbt::signal<void(Operation*)> on_start;
   static xbt::signal<void(Operation*)> on_end;
+  std::atomic_int_fast32_t refcount_{0};
 
 public:
   static void init();
-  std::string get_name();
+  const std::string& get_name() { return name_; }
+  const char* get_cname() { return name_.c_str(); }
   void enqueue_execs(int n);
   void set_amount(double amount);
+  double get_amount() const { return amount_; }
   void add_successor(OperationPtr op);
   void remove_successor(OperationPtr op);
   void on_this_start(std::function<void(Operation*)> func);
@@ -71,37 +81,58 @@ public:
    * Triggered after the on_this_end function, but before
    * sending tokens to successors.**/
   static void on_end_cb(const std::function<void(Operation*)>& cb) { on_end.connect(cb); }
+
+#ifndef DOXYGEN
+  friend void intrusive_ptr_release(Operation* o)
+  {
+    if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
+      std::atomic_thread_fence(std::memory_order_acquire);
+      delete o;
+    }
+  }
+  friend void intrusive_ptr_add_ref(Operation* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
+#endif
 };
 
 class ExecOp : public Operation {
 private:
-  simgrid::s4u::Host* host_;
+  s4u::Host* host_;
 
   ExecOp(const std::string& name);
   void execute() override;
 
 public:
   static ExecOpPtr init(const std::string& name);
-  static ExecOpPtr init(const std::string& name, double flops, simgrid::s4u::Host* host);
-  void set_host(simgrid::s4u::Host* host);
-  void set_flops(double flops);
+  static ExecOpPtr init(const std::string& name, double flops, s4u::Host* host);
+  ExecOpPtr set_host(s4u::Host* host);
+  s4u::Host* get_host() const { return host_; }
+  ExecOpPtr set_flops(double flops);
+  double get_flops() const { return get_amount(); }
+  friend void inline intrusive_ptr_release(ExecOp* e) { intrusive_ptr_release(static_cast<Operation*>(e)); }
+  friend void inline intrusive_ptr_add_ref(ExecOp* e) { intrusive_ptr_add_ref(static_cast<Operation*>(e)); }
 };
 
 class CommOp : public Operation {
 private:
-  simgrid::s4u::Host* source_;
-  simgrid::s4u::Host* destination_;
+  s4u::Host* source_;
+  s4u::Host* destination_;
 
   CommOp(const std::string& name);
   void execute() override;
 
 public:
   static CommOpPtr init(const std::string& name);
-  static CommOpPtr init(const std::string& name, double bytes, simgrid::s4u::Host* source,
-                        simgrid::s4u::Host* destination);
-  void set_source(simgrid::s4u::Host* source);
-  void set_destination(simgrid::s4u::Host* destination);
-  void set_bytes(double bytes);
+  static CommOpPtr init(const std::string& name, double bytes, s4u::Host* source,
+                        s4u::Host* destination);
+  CommOpPtr set_source(s4u::Host* source);
+  s4u::Host* get_source() const { return source_; }
+  CommOpPtr set_destination(s4u::Host* destination);
+  s4u::Host* get_destination() const { return destination_; }
+  CommOpPtr set_bytes(double bytes);
+  double get_bytes() const { return get_amount(); }
+   friend void inline intrusive_ptr_release(CommOp* c) { intrusive_ptr_release(static_cast<Operation*>(c)); }
+  friend void inline intrusive_ptr_add_ref(CommOp* c) { intrusive_ptr_add_ref(static_cast<Operation*>(c)); }
+
 };
 } // namespace simgrid::plugins
 #endif
index 0629065..6021786 100644 (file)
@@ -31,11 +31,6 @@ xbt::signal<void(Operation*)> Operation::on_end;
 
 Operation::Operation(const std::string& name) : name_(name) {}
 
-std::string Operation::get_name()
-{
-  return name_;
-}
-
 /**
  *  @param predecessor The Operation to add.
  *  @brief Add a predecessor to this Operation.
@@ -220,12 +215,9 @@ ExecOpPtr ExecOp::init(const std::string& name)
 /** @ingroup plugin_operation
  *  @brief Smart Constructor.
  */
-ExecOpPtr ExecOp::init(const std::string& name, double flops, simgrid::s4u::Host* host)
+ExecOpPtr ExecOp::init(const std::string& name, double flops, s4u::Host* host)
 {
-  auto op = ExecOpPtr(new ExecOp(name));
-  op->set_flops(flops);
-  op->set_host(host);
-  return op;
+  return ExecOpPtr(new ExecOp(name))->set_flops(flops)->set_host(host);
 }
 
 /**
@@ -238,35 +230,37 @@ void ExecOp::execute()
   if (start_func_)
     start_func_(this);
   Operation::on_start(this);
-  simgrid::kernel::actor::simcall_answered([this] {
+  kernel::actor::simcall_answered([this] {
     working_      = true;
     queued_execs_ = std::max(queued_execs_ - 1, 0);
   });
-  simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init();
+  s4u::ExecPtr exec = s4u::Exec::init();
   exec->set_name(name_);
   exec->set_flops_amount(amount_);
   exec->set_host(host_);
   exec->start();
   exec->extension_set(new ExtendedAttributeActivity());
   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
-  simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
+  kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
 }
 
 /** @ingroup plugin_operation
  *  @param host The host to set.
  *  @brief Set a new host.
  */
-void ExecOp::set_host(simgrid::s4u::Host* host)
+ExecOpPtr ExecOp::set_host(s4u::Host* host)
 {
-  simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; });
+  kernel::actor::simcall_answered([this, host] { host_ = host; });
+  return this;
 }
 
 /** @ingroup plugin_operation
  *  @param flops The amount of flops to set.
  */
-void ExecOp::set_flops(double flops)
+ExecOpPtr ExecOp::set_flops(double flops)
 {
-  simgrid::kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
+  kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
+  return this;
 }
 
 /**
@@ -286,8 +280,8 @@ CommOpPtr CommOp::init(const std::string& name)
 /** @ingroup plugin_operation
  *  @brief Smart constructor.
  */
-CommOpPtr CommOp::init(const std::string& name, double bytes, simgrid::s4u::Host* source,
-                       simgrid::s4u::Host* destination)
+CommOpPtr CommOp::init(const std::string& name, double bytes, s4u::Host* source,
+                       s4u::Host* destination)
 {
   auto op = CommOpPtr(new CommOp(name));
   op->set_bytes(bytes);
@@ -306,43 +300,46 @@ void CommOp::execute()
   if (start_func_)
     start_func_(this);
   Operation::on_start(this);
-  simgrid::kernel::actor::simcall_answered([this] {
+  kernel::actor::simcall_answered([this] {
     working_      = true;
     queued_execs_ = std::max(queued_execs_ - 1, 0);
   });
-  simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
+  s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
   comm->set_name(name_);
   comm->set_payload_size(amount_);
   comm->start();
   comm->extension_set(new ExtendedAttributeActivity());
   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
-  simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
+  kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
 }
 
 /** @ingroup plugin_operation
  *  @param source The host to set.
  *  @brief Set a new source host.
  */
-void CommOp::set_source(simgrid::s4u::Host* source)
+CommOpPtr CommOp::set_source(s4u::Host* source)
 {
-  simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; });
+  kernel::actor::simcall_answered([this, source] { source_ = source; });
+  return this;
 }
 
 /** @ingroup plugin_operation
  *  @param destination The host to set.
  *  @brief Set a new destination host.
  */
-void CommOp::set_destination(simgrid::s4u::Host* destination)
+CommOpPtr CommOp::set_destination(s4u::Host* destination)
 {
-  simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
+  kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
+  return this;
 }
 
 /** @ingroup plugin_operation
  *  @param bytes The amount of bytes to set.
  */
-void CommOp::set_bytes(double bytes)
+CommOpPtr CommOp::set_bytes(double bytes)
 {
-  simgrid::kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
+  kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
+  return this;
 }
 
 } // namespace simgrid::plugins