Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:mquinson/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_async,"S4U asynchronous communications");
13 using namespace simgrid;
14
15 s4u::Comm::~Comm() {
16
17 }
18
19 s4u::Comm &s4u::Comm::send_init(s4u::Actor *sender, s4u::Mailbox &chan) {
20   s4u::Comm *res = new s4u::Comm();
21   res->sender_ = sender;
22   res->mailbox_ = &chan;
23
24   return *res;
25 }
26 s4u::Comm &s4u::Comm::recv_init(s4u::Actor *receiver, s4u::Mailbox &chan) {
27   s4u::Comm *res = new s4u::Comm();
28   res->receiver_ = receiver;
29   res->mailbox_ = &chan;
30
31   return *res;
32 }
33
34 void s4u::Comm::setRate(double rate) {
35   xbt_assert(p_state==inited);
36   rate_ = rate;
37 }
38
39 void s4u::Comm::setSrcData(void * buff) {
40   xbt_assert(p_state==inited);
41   xbt_assert(dstBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
42   srcBuff_ = buff;
43 }
44 void s4u::Comm::setSrcDataSize(size_t size){
45   xbt_assert(p_state==inited);
46   srcBuffSize_ = size;
47 }
48 void s4u::Comm::setSrcData(void * buff, size_t size) {
49   xbt_assert(p_state==inited);
50
51   xbt_assert(dstBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
52   srcBuff_ = buff;
53   srcBuffSize_ = size;
54 }
55 void s4u::Comm::setDstData(void ** buff) {
56   xbt_assert(p_state==inited);
57   xbt_assert(srcBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
58   dstBuff_ = buff;
59 }
60 size_t s4u::Comm::getDstDataSize(){
61   xbt_assert(p_state==finished);
62   return dstBuffSize_;
63 }
64 void s4u::Comm::setDstData(void ** buff, size_t size) {
65   xbt_assert(p_state==inited);
66
67   xbt_assert(srcBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
68   dstBuff_ = buff;
69   dstBuffSize_ = size;
70 }
71
72 void s4u::Comm::start() {
73   xbt_assert(p_state == inited);
74
75   if (srcBuff_ != NULL) { // Sender side
76     p_inferior = simcall_comm_isend(sender_->getInferior(), mailbox_->getInferior(), p_remains, rate_,
77         srcBuff_, srcBuffSize_,
78         matchFunction_, cleanFunction_, copyDataFunction_,
79         p_userData, detached_);
80   } else if (dstBuff_ != NULL) { // Receiver side
81     p_inferior = simcall_comm_irecv(receiver_->getInferior(), mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
82         matchFunction_, copyDataFunction_,
83         p_userData, rate_);
84
85   } else {
86     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
87   }
88   p_state = started;
89 }
90 void s4u::Comm::wait() {
91   xbt_assert(p_state == started || p_state == inited);
92
93   if (p_state == started)
94     simcall_comm_wait(p_inferior, -1/*timeout*/);
95   else {// p_state == inited. Save a simcall and do directly a blocking send/recv
96     if (srcBuff_ != NULL) {
97       simcall_comm_send(sender_->getInferior(), mailbox_->getInferior(), p_remains, rate_,
98           srcBuff_, srcBuffSize_,
99           matchFunction_, copyDataFunction_,
100           p_userData, -1 /*timeout*/);
101     } else {
102       simcall_comm_recv(receiver_->getInferior(), mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
103           matchFunction_, copyDataFunction_,
104           p_userData, -1/*timeout*/, rate_);
105     }
106   }
107   p_state = finished;
108 }
109 void s4u::Comm::wait(double timeout) {
110   xbt_assert(p_state == started || p_state == inited);
111
112   if (p_state == started) {
113     simcall_comm_wait(p_inferior, timeout);
114     p_state = finished;
115     return;
116   }
117
118   // It's not started yet. Do it in one simcall
119   if (srcBuff_ != NULL) {
120     simcall_comm_send(sender_->getInferior(), mailbox_->getInferior(), p_remains, rate_,
121         srcBuff_, srcBuffSize_,
122         matchFunction_, copyDataFunction_,
123         p_userData, timeout);
124   } else { // Receiver
125     simcall_comm_recv(receiver_->getInferior(), mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
126         matchFunction_, copyDataFunction_,
127         p_userData, timeout, rate_);
128   }
129   p_state = finished;
130 }
131
132 s4u::Comm &s4u::Comm::send_async(s4u::Actor *sender, Mailbox &dest, void *data, int simulatedSize) {
133   s4u::Comm &res = s4u::Comm::send_init(sender, dest);
134
135   res.setRemains(simulatedSize);
136   res.srcBuff_ = data;
137   res.srcBuffSize_ = sizeof(void*);
138
139   res.start();
140   return res;
141 }
142
143 s4u::Comm &s4u::Comm::recv_async(s4u::Actor *receiver, Mailbox &dest, void **data) {
144   s4u::Comm &res = s4u::Comm::recv_init(receiver, dest);
145
146   res.setDstData(data);
147
148   res.start();
149   return res;
150 }
151