Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9f4ad0b438c1971bbc6b73e29406a97153701480
[simgrid.git] / src / s4u / s4u_comm.cpp
1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "xbt/log.h"
7 #include "src/msg/msg_private.h"
8
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Mailbox.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_activity,"S4U asynchronous communications");
13
14 namespace simgrid {
15 namespace s4u {
16 Comm::~Comm()
17 {
18   if (state_ == started && not detached_ && (pimpl_ == nullptr || pimpl_->state == SIMIX_RUNNING)) {
19     XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, state_);
20     if (pimpl_ != nullptr)
21       XBT_INFO("pimpl_->state: %d", pimpl_->state);
22     else
23       XBT_INFO("pimpl_ is null");
24     xbt_backtrace_display_current();
25   }
26 }
27
28 void Comm::setRate(double rate) {
29   xbt_assert(state_==inited);
30   rate_ = rate;
31 }
32
33 void Comm::setSrcData(void * buff) {
34   xbt_assert(state_==inited);
35   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
36   srcBuff_ = buff;
37 }
38 void Comm::setSrcDataSize(size_t size){
39   xbt_assert(state_==inited);
40   srcBuffSize_ = size;
41 }
42 void Comm::setSrcData(void * buff, size_t size) {
43   xbt_assert(state_==inited);
44
45   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
46   srcBuff_ = buff;
47   srcBuffSize_ = size;
48 }
49 void Comm::setDstData(void ** buff) {
50   xbt_assert(state_==inited);
51   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
52   dstBuff_ = buff;
53 }
54 size_t Comm::getDstDataSize(){
55   xbt_assert(state_==finished);
56   return dstBuffSize_;
57 }
58 void Comm::setDstData(void ** buff, size_t size) {
59   xbt_assert(state_==inited);
60
61   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
62   dstBuff_ = buff;
63   dstBuffSize_ = size;
64 }
65
66 void Comm::start() {
67   xbt_assert(state_ == inited);
68
69   if (srcBuff_ != nullptr) { // Sender side
70     pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
71         srcBuff_, srcBuffSize_,
72         matchFunction_, cleanFunction_, copyDataFunction_,
73         userData_, detached_);
74   } else if (dstBuff_ != nullptr) { // Receiver side
75     xbt_assert(not detached_, "Receive cannot be detached");
76     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
77         matchFunction_, copyDataFunction_,
78         userData_, rate_);
79
80   } else {
81     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
82   }
83   state_ = started;
84 }
85 void Comm::wait() {
86   xbt_assert(state_ == started || state_ == inited || state_ == finished);
87
88   if (state_ == finished)
89     return;
90
91   if (state_ == started)
92     simcall_comm_wait(pimpl_, -1/*timeout*/);
93   else { // state_ == inited. Save a simcall and do directly a blocking send/recv
94     if (srcBuff_ != nullptr) {
95       simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
96           srcBuff_, srcBuffSize_,
97           matchFunction_, copyDataFunction_,
98           userData_, -1 /*timeout*/);
99     } else {
100       simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
101           matchFunction_, copyDataFunction_,
102           userData_, -1/*timeout*/, rate_);
103     }
104   }
105   state_ = finished;
106 }
107
108 void Comm::wait(double timeout) {
109   xbt_assert(state_ == started || state_ == inited || state_ == finished);
110
111   if (state_ == finished)
112     return;
113
114   if (state_ == started) {
115     simcall_comm_wait(pimpl_, timeout);
116     state_ = finished;
117     return;
118   }
119
120   // It's not started yet. Do it in one simcall
121   if (srcBuff_ != nullptr) {
122     simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
123         srcBuff_, srcBuffSize_,
124         matchFunction_, copyDataFunction_,
125         userData_, timeout);
126   } else { // Receiver
127     simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
128         matchFunction_, copyDataFunction_,
129         userData_, timeout, rate_);
130   }
131   state_ = finished;
132 }
133
134 void Comm::detach()
135 {
136   xbt_assert(state_ == inited, "You cannot detach communications once they are started.");
137   xbt_assert(srcBuff_ != nullptr && srcBuffSize_ != 0, "You can only detach sends, not recvs");
138   detached_ = true;
139   start();
140 }
141
142 void Comm::cancel()
143 {
144   simgrid::kernel::activity::CommImplPtr commPimpl =
145       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
146   commPimpl->cancel();
147 }
148
149 bool Comm::test() {
150   xbt_assert(state_ == inited || state_ == started || state_ == finished);
151
152   if (state_ == finished) {
153     return true;
154   }
155
156   if (state_ == inited) {
157     this->start();
158   }
159
160   if(simcall_comm_test(pimpl_)){
161     state_ = finished;
162     return true;
163   }
164   return false;
165 }
166
167 void intrusive_ptr_release(simgrid::s4u::Comm* c)
168 {
169   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
170     std::atomic_thread_fence(std::memory_order_acquire);
171     delete c;
172   }
173 }
174 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
175 {
176   c->refcount_.fetch_add(1, std::memory_order_relaxed);
177 }
178 }
179 } // namespaces