Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement receive async
[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 "msg/msg_private.h"
9 #include "msg/msg_mailbox.h"
10
11 #include "simgrid/s4u/comm.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_async,"S4U asynchronous communications");
14 using namespace simgrid;
15
16 s4u::Comm::~Comm() {
17
18 }
19
20 s4u::Comm &s4u::Comm::send_init(s4u::Actor *sender, s4u::Mailbox &chan) {
21         s4u::Comm *res = new s4u::Comm();
22         res->p_sender = sender;
23         res->p_mailbox = &chan;
24
25         return *res;
26 }
27 s4u::Comm &s4u::Comm::recv_init(s4u::Mailbox &chan) {
28         s4u::Comm *res = new s4u::Comm();
29         res->p_mailbox = &chan;
30
31         return *res;
32 }
33
34 void s4u::Comm::setRate(double rate) {
35         xbt_assert(p_state==inited);
36         p_rate = rate;
37 }
38
39 void s4u::Comm::setSrcData(void * buff) {
40         xbt_assert(p_state==inited);
41         xbt_assert(p_dstBuff == NULL, "Cannot set the src and dst buffers at the same time");
42         p_srcBuff = buff;
43 }
44 void s4u::Comm::setSrcDataSize(size_t size){
45         xbt_assert(p_state==inited);
46         p_srcBuffSize = size;
47 }
48 void s4u::Comm::setSrcData(void * buff, size_t size) {
49         xbt_assert(p_state==inited);
50
51         xbt_assert(p_dstBuff == NULL, "Cannot set the src and dst buffers at the same time");
52         p_srcBuff = buff;
53         p_srcBuffSize = size;
54 }
55 void s4u::Comm::setDstData(void ** buff) {
56         xbt_assert(p_state==inited);
57         xbt_assert(p_srcBuff == NULL, "Cannot set the src and dst buffers at the same time");
58         p_dstBuff = buff;
59 }
60 size_t s4u::Comm::getDstDataSize(){
61         xbt_assert(p_state==finished);
62         return p_dstBuffSize;
63 }
64 void s4u::Comm::setDstData(void ** buff, size_t size) {
65         xbt_assert(p_state==inited);
66
67         xbt_assert(p_srcBuff == NULL, "Cannot set the src and dst buffers at the same time");
68         p_dstBuff = buff;
69         p_dstBuffSize = size;
70 }
71
72 void s4u::Comm::start() {
73         xbt_assert(p_state == inited);
74
75         if (p_srcBuff != NULL) { // Sender side
76                 p_inferior = simcall_comm_isend(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
77                                 p_srcBuff, p_srcBuffSize,
78                                 p_matchFunction, p_cleanFunction, p_copyDataFunction,
79                                 p_userData, p_detached);
80         } else if (p_dstBuff != NULL) { // Receiver side
81                 p_inferior = simcall_comm_irecv(p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
82                                 p_matchFunction, p_copyDataFunction,
83                                 p_userData, p_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 (p_srcBuff != NULL) {
97                         simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
98                                         p_srcBuff, p_srcBuffSize,
99                                         p_matchFunction, p_copyDataFunction,
100                                         p_userData, -1 /*timeout*/);
101                 } else {
102                         simcall_comm_recv(p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
103                                         p_matchFunction, p_copyDataFunction,
104                                         p_userData, -1/*timeout*/, p_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 (p_srcBuff != NULL) {
120                 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
121                                 p_srcBuff, p_srcBuffSize,
122                                 p_matchFunction, p_copyDataFunction,
123                                 p_userData, timeout);
124         } else { // Receiver
125                 simcall_comm_recv(p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
126                                 p_matchFunction, p_copyDataFunction,
127                                 p_userData, timeout, p_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.p_srcBuff = data;
137         res.p_srcBuffSize = sizeof(void*);
138
139         res.start();
140         return res;
141 }
142