Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / s4u / s4u_comm.cpp
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/log.h"
8 #include "src/msg/msg_private.h"
9
10 #include "simgrid/s4u/comm.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_activity,"S4U asynchronous communications");
13
14 namespace simgrid {
15 namespace s4u {
16
17 Comm::~Comm() {
18
19 }
20
21
22
23 s4u::Comm &Comm::send_init(s4u::Mailbox &chan) {
24   s4u::Comm *res = new s4u::Comm();
25   res->sender_ = SIMIX_process_self();
26   res->mailbox_ = &chan;
27   return *res;
28 }
29
30 s4u::Comm &Comm::recv_init(s4u::Mailbox &chan) {
31   s4u::Comm *res = new s4u::Comm();
32   res->receiver_ = SIMIX_process_self();
33   res->mailbox_ = &chan;
34   return *res;
35 }
36
37 void Comm::setRate(double rate) {
38   xbt_assert(state_==inited);
39   rate_ = rate;
40 }
41
42 void Comm::setSrcData(void * buff) {
43   xbt_assert(state_==inited);
44   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
45   srcBuff_ = buff;
46 }
47 void Comm::setSrcDataSize(size_t size){
48   xbt_assert(state_==inited);
49   srcBuffSize_ = size;
50 }
51 void Comm::setSrcData(void * buff, size_t size) {
52   xbt_assert(state_==inited);
53
54   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
55   srcBuff_ = buff;
56   srcBuffSize_ = size;
57 }
58 void Comm::setDstData(void ** buff) {
59   xbt_assert(state_==inited);
60   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
61   dstBuff_ = buff;
62 }
63 size_t Comm::getDstDataSize(){
64   xbt_assert(state_==finished);
65   return dstBuffSize_;
66 }
67 void Comm::setDstData(void ** buff, size_t size) {
68   xbt_assert(state_==inited);
69
70   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
71   dstBuff_ = buff;
72   dstBuffSize_ = size;
73 }
74
75 void Comm::start() {
76   xbt_assert(state_ == inited);
77
78   if (srcBuff_ != nullptr) { // Sender side
79     pimpl_ = simcall_comm_isend(sender_, mailbox_->getInferior(), remains_, rate_,
80         srcBuff_, srcBuffSize_,
81         matchFunction_, cleanFunction_, copyDataFunction_,
82         userData_, detached_);
83   } else if (dstBuff_ != nullptr) { // Receiver side
84     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
85         matchFunction_, copyDataFunction_,
86         userData_, rate_);
87
88   } else {
89     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
90   }
91   state_ = started;
92 }
93 void Comm::wait() {
94   xbt_assert(state_ == started || state_ == inited);
95
96   if (state_ == started)
97     simcall_comm_wait(pimpl_, -1/*timeout*/);
98   else {// p_state == inited. Save a simcall and do directly a blocking send/recv
99     if (srcBuff_ != nullptr) {
100       simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
101           srcBuff_, srcBuffSize_,
102           matchFunction_, copyDataFunction_,
103           userData_, -1 /*timeout*/);
104     } else {
105       simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
106           matchFunction_, copyDataFunction_,
107           userData_, -1/*timeout*/, rate_);
108     }
109   }
110   state_ = finished;
111 }
112 void Comm::wait(double timeout) {
113   xbt_assert(state_ == started || state_ == inited);
114
115   if (state_ == started) {
116     simcall_comm_wait(pimpl_, timeout);
117     state_ = finished;
118     return;
119   }
120
121   // It's not started yet. Do it in one simcall
122   if (srcBuff_ != nullptr) {
123     simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
124         srcBuff_, srcBuffSize_,
125         matchFunction_, copyDataFunction_,
126         userData_, timeout);
127   } else { // Receiver
128     simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
129         matchFunction_, copyDataFunction_,
130         userData_, timeout, rate_);
131   }
132   state_ = finished;
133 }
134
135 s4u::Comm &Comm::send_async(Mailbox &dest, void *data, int simulatedSize) {
136   s4u::Comm &res = s4u::Comm::send_init(dest);
137   res.setRemains(simulatedSize);
138   res.srcBuff_ = data;
139   res.srcBuffSize_ = sizeof(void*);
140   res.start();
141   return res;
142 }
143
144 s4u::Comm &Comm::recv_async(Mailbox &dest, void **data) {
145   s4u::Comm &res = s4u::Comm::recv_init(dest);
146   res.setDstData(data);
147   res.start();
148   return res;
149 }
150
151 bool Comm::test() {
152   xbt_assert(state_ == inited || state_ == started || state_ == finished);
153   
154   if (state_ == finished) 
155     xbt_die("Don't call test on a finished comm.");
156   
157   if (state_ == inited) {
158     this->start();
159   }
160   
161   if(simcall_comm_test(pimpl_)){
162     state_ = finished;
163     return true;
164   }
165   return false;
166 }
167
168 }
169 }