Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Delete the CPU (fix a mem leak)
[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 #include "src/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::Actor *receiver, s4u::Mailbox &chan) {
28         s4u::Comm *res = new s4u::Comm();
29         res->p_receiver = receiver;
30         res->p_mailbox = &chan;
31
32         return *res;
33 }
34
35 void s4u::Comm::setRate(double rate) {
36         xbt_assert(p_state==inited);
37         p_rate = rate;
38 }
39
40 void s4u::Comm::setSrcData(void * buff) {
41         xbt_assert(p_state==inited);
42         xbt_assert(p_dstBuff == NULL, "Cannot set the src and dst buffers at the same time");
43         p_srcBuff = buff;
44 }
45 void s4u::Comm::setSrcDataSize(size_t size){
46         xbt_assert(p_state==inited);
47         p_srcBuffSize = size;
48 }
49 void s4u::Comm::setSrcData(void * buff, size_t size) {
50         xbt_assert(p_state==inited);
51
52         xbt_assert(p_dstBuff == NULL, "Cannot set the src and dst buffers at the same time");
53         p_srcBuff = buff;
54         p_srcBuffSize = size;
55 }
56 void s4u::Comm::setDstData(void ** buff) {
57         xbt_assert(p_state==inited);
58         xbt_assert(p_srcBuff == NULL, "Cannot set the src and dst buffers at the same time");
59         p_dstBuff = buff;
60 }
61 size_t s4u::Comm::getDstDataSize(){
62         xbt_assert(p_state==finished);
63         return p_dstBuffSize;
64 }
65 void s4u::Comm::setDstData(void ** buff, size_t size) {
66         xbt_assert(p_state==inited);
67
68         xbt_assert(p_srcBuff == NULL, "Cannot set the src and dst buffers at the same time");
69         p_dstBuff = buff;
70         p_dstBuffSize = size;
71 }
72
73 void s4u::Comm::start() {
74         xbt_assert(p_state == inited);
75
76         if (p_srcBuff != NULL) { // Sender side
77                 p_inferior = simcall_comm_isend(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
78                                 p_srcBuff, p_srcBuffSize,
79                                 p_matchFunction, p_cleanFunction, p_copyDataFunction,
80                                 p_userData, p_detached);
81         } else if (p_dstBuff != NULL) { // Receiver side
82                 p_inferior = simcall_comm_irecv(p_receiver->getInferior(), p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
83                                 p_matchFunction, p_copyDataFunction,
84                                 p_userData, p_rate);
85
86         } else {
87                 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
88         }
89         p_state = started;
90 }
91 void s4u::Comm::wait() {
92         xbt_assert(p_state == started || p_state == inited);
93
94         if (p_state == started)
95                 simcall_comm_wait(p_inferior, -1/*timeout*/);
96         else {// p_state == inited. Save a simcall and do directly a blocking send/recv
97                 if (p_srcBuff != NULL) {
98                         simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
99                                         p_srcBuff, p_srcBuffSize,
100                                         p_matchFunction, p_copyDataFunction,
101                                         p_userData, -1 /*timeout*/);
102                 } else {
103                         simcall_comm_recv(p_receiver->getInferior(), p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
104                                         p_matchFunction, p_copyDataFunction,
105                                         p_userData, -1/*timeout*/, p_rate);
106                 }
107         }
108         p_state = finished;
109 }
110 void s4u::Comm::wait(double timeout) {
111         xbt_assert(p_state == started || p_state == inited);
112
113         if (p_state == started) {
114                 simcall_comm_wait(p_inferior, timeout);
115                 p_state = finished;
116                 return;
117         }
118
119         // It's not started yet. Do it in one simcall
120         if (p_srcBuff != NULL) {
121                 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
122                                 p_srcBuff, p_srcBuffSize,
123                                 p_matchFunction, p_copyDataFunction,
124                                 p_userData, timeout);
125         } else { // Receiver
126                 simcall_comm_recv(p_receiver->getInferior(), p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
127                                 p_matchFunction, p_copyDataFunction,
128                                 p_userData, timeout, p_rate);
129         }
130         p_state = finished;
131 }
132
133 s4u::Comm &s4u::Comm::send_async(s4u::Actor *sender, Mailbox &dest, void *data, int simulatedSize) {
134         s4u::Comm &res = s4u::Comm::send_init(sender, dest);
135
136         res.setRemains(simulatedSize);
137         res.p_srcBuff = data;
138         res.p_srcBuffSize = sizeof(void*);
139
140         res.start();
141         return res;
142 }
143
144 s4u::Comm &s4u::Comm::recv_async(s4u::Actor *receiver, Mailbox &dest, void **data) {
145         s4u::Comm &res = s4u::Comm::recv_init(receiver, dest);
146
147         res.setDstData(data);
148
149         res.start();
150         return res;
151 }
152