Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
400e8d0afb0bf4df6072c703e72ae0d7d5889d7b
[simgrid.git] / src / simix / smx_network.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009 Cristian Rosa.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "private.h"
10 #include "xbt/log.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13                                 "Logging specific to SIMIX (network)");
14
15 /******************************************************************************/
16 /*                           Rendez-Vous Points                               */
17 /******************************************************************************/ 
18
19 /**
20  *  \brief Creates a new rendez-vous point
21  *  \param name The name of the rendez-vous point
22  *  \return The created rendez-vous point
23  */
24 smx_rdv_t SIMIX_rdv_create(const char *name)
25 {
26   smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
27   rdv->name = name ? xbt_strdup(name) : NULL;
28   rdv->read = SIMIX_mutex_init();
29   rdv->write = SIMIX_mutex_init();
30   rdv->comm_fifo = xbt_fifo_new();
31
32   return rdv;
33 }
34
35 /**
36  *  \brief Destroy a rendez-vous point
37  *  \param name The rendez-vous point to destroy
38  */
39 void SIMIX_rdv_destroy(smx_rdv_t rdv)
40 {
41   if(rdv->name)
42     xbt_free(rdv->name);
43   SIMIX_mutex_destroy(rdv->read);
44   SIMIX_mutex_destroy(rdv->write);
45   xbt_fifo_free(rdv->comm_fifo);
46   xbt_free(rdv);
47 }
48
49 /**
50  *  \brief Push a communication request into a rendez-vous point
51  *  \param rdv The rendez-vous point
52  *  \param comm The communication request
53  */
54 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
55 {
56   xbt_fifo_push(rdv->comm_fifo, comm);
57 }
58
59 /**
60  *  \brief Remove a communication request from a rendez-vous point
61  *  \param rdv The rendez-vous point
62  *  \param comm The communication request
63  */
64 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
65 {
66   xbt_fifo_remove(rdv->comm_fifo, comm);
67 }
68   
69
70 /**
71  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
72  *  \param rdv The rendez-vous with the queue
73  *  \param look_for_src boolean. True: we are receiver looking for sender; False: other way round
74  *  \return The communication request if found, or a newly created one otherwise.
75  */
76 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, int (filter)(smx_comm_t, void*), void *arg) {
77   smx_comm_t comm;
78   xbt_fifo_item_t item;
79
80   /* Traverse the rendez-vous queue looking for a comm request matching the
81      filter conditions. If found return it and remove it from the list. */
82   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
83     if(filter(comm, arg)){
84       SIMIX_communication_use(comm);
85       xbt_fifo_remove_item(rdv->comm_fifo, item);
86       DEBUG1("Communication request found! %p", comm);
87       return comm;
88     }
89   }
90
91   /* no relevant request found. Return NULL */
92   DEBUG0("Communication request not found");
93   return NULL;
94 }
95
96 /******************************************************************************/
97 /*                           Communication Requests                           */
98 /******************************************************************************/ 
99
100 /**
101  *  \brief Creates a new communication request
102  *  \param sender The process starting the communication (by send)
103  *  \param receiver The process receiving the communication (by recv)
104  *  \return the communication request
105  */  
106 smx_comm_t SIMIX_communication_new(smx_comm_type_t type, smx_rdv_t rdv)
107 {
108   /* alloc structures */
109   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
110   comm->type = type;
111   comm->cond = SIMIX_cond_init();
112   comm->rdv = rdv;
113   comm->refcount = 1;
114   
115   return comm;
116 }
117
118 /**
119  *  \brief Destroy a communication request
120  *  \param comm The request to be destroyed
121  */
122 void SIMIX_communication_destroy(smx_comm_t comm)
123 {
124   comm->refcount--;
125   if(comm->refcount == 0){
126     if(comm->act != NULL)
127       SIMIX_action_destroy(comm->act);
128
129     xbt_free(comm->cond);
130     xbt_free(comm);
131   }
132 }
133
134 /**
135  *  \brief Increase the number of users of the communication.
136  *  \param comm The communication request
137  *  Each communication request can be used by more than one process, so it is
138  *  necessary to know number of them at destroy time, to avoid freeing stuff that
139  *  maybe is in use by others.
140  *  \
141  */
142 static inline void SIMIX_communication_use(smx_comm_t comm)
143 {
144   comm->refcount++;
145 }
146
147 /**
148  *  \brief Start the simulation of a communication request
149  *  \param comm The communication request
150  */
151 static inline void SIMIX_communication_start(smx_comm_t comm)
152 {
153   /* If both the sender and the receiver are already there, start the communication */
154   if(comm->src_host != NULL && comm->dst_host != NULL){
155     DEBUG1("Starting communication %p", comm);
156     comm->act = SIMIX_action_communicate(comm->src_host, comm->dst_host, NULL, 
157                                          comm->task_size, comm->rate);
158
159     /* Add the communication as user data of the action */
160     comm->act->data = comm;
161     
162     SIMIX_register_action_to_condition(comm->act, comm->cond);
163   }else{
164     DEBUG1("Communication %p cannot be started, peer missing", comm);
165   }
166 }
167
168 /**
169  *  \brief Waits for communication completion and performs error checking
170  *  \param comm The communication
171  *  \param timeout The max amount of time to wait for the communication to finish
172  *
173  *  Throws:
174  *   - host_error if peer failed
175  *   - timeout_error if communication reached the timeout specified
176  *   - network_error if network failed or peer issued a timeout
177  */
178 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
179 {
180   xbt_ex_t e;
181
182   DEBUG1("Waiting for the completion of communication %p", comm);
183   
184   if(timeout > 0){
185     TRY{
186       SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
187     }
188     CATCH(e){
189       /* If it's a timeout then cancel the communication and signal the other peer */
190       if(e.category == timeout_error){
191         DEBUG1("Communication timeout! %p", comm);
192         if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
193           SIMIX_action_cancel(comm->act);
194         else
195           SIMIX_rdv_remove(comm->rdv, comm);
196           
197         SIMIX_cond_signal(comm->cond);
198         SIMIX_communication_destroy(comm);
199       }
200       RETHROW;
201     }
202   }else{
203     SIMIX_cond_wait(comm->cond, NULL);
204   }
205
206   DEBUG1("Communication %p complete! Let's check for errors", comm);
207   
208   /* Check for errors */
209   if(!SIMIX_host_get_state(SIMIX_host_self())){
210     SIMIX_communication_destroy(comm);
211     THROW0(host_error, 0, "Host failed");
212   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
213     SIMIX_communication_destroy(comm);
214     THROW0(network_error, 0, "Link failure");
215   }
216
217   SIMIX_unregister_action_to_condition(comm->act, comm->cond);
218 }
219
220 /**
221  *  \brief Copy the communication data from the sender's buffer to the receiver's one
222  *  \param comm The communication
223  */
224 void SIMIX_network_copy_data(smx_comm_t comm)
225 {
226   size_t src_buff_size = comm->src_buff_size;
227   size_t dst_buff_size = *comm->dst_buff_size;
228
229   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
230   dst_buff_size = MIN(dst_buff_size, src_buff_size);
231
232   /* Update the receiver's buffer size to the copied amount */
233   *comm->dst_buff_size = dst_buff_size;
234
235   memcpy(comm->dst_buff, comm->src_buff, dst_buff_size);
236
237   DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)", 
238          comm, comm->src_host->name, comm->dst_host->name, dst_buff_size);
239 }
240
241 /* FIXME: move to some other place */
242 int comm_filter_get(smx_comm_t comm, void *arg)
243 {
244   if(comm->type == comm_send){
245     if(arg && comm->src_host != (smx_host_t)arg)
246      return FALSE;
247     else
248      return TRUE;
249   }else{
250     return FALSE;
251   }
252 }
253
254 int comm_filter_put(smx_comm_t comm, void *arg)
255 {
256   return comm->type == comm_recv ? TRUE : FALSE;
257 }
258 /******************************************************************************/
259 /*                        Synchronous Communication                           */
260 /******************************************************************************/
261 /*  Throws:
262  *   - host_error if peer failed
263  *   - timeout_error if communication reached the timeout specified
264  *   - network_error if network failed or peer issued a timeout
265  */
266 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate, 
267                         double timeout, void *data, size_t data_size,
268                         int (filter)(smx_comm_t, void *), void *arg)
269 {
270   smx_comm_t comm;
271   
272   /* Look for communication request matching our needs. 
273      If it is not found then create it and push it into the rendez-vous point */
274   comm = SIMIX_rdv_get_request(rdv, filter, arg);
275
276   if(comm == NULL){
277     comm = SIMIX_communication_new(comm_send, rdv);
278     SIMIX_rdv_push(rdv, comm);
279   }
280
281   /* Setup the communication request */
282   comm->src_host = SIMIX_host_self();
283   comm->task_size = task_size;
284   comm->rate = rate;
285   comm->src_buff = data;
286   comm->src_buff_size = data_size;
287
288   SIMIX_communication_start(comm);
289
290   /* Wait for communication completion */
291   /* FIXME: if the semantic is non blocking, it shouldn't wait on the condition here */
292   SIMIX_communication_wait_for_completion(comm, timeout);
293
294   SIMIX_communication_destroy(comm);
295 }
296
297 /*  Throws:
298  *   - host_error if peer failed
299  *   - timeout_error if communication reached the timeout specified
300  *   - network_error if network failed or peer issued a timeout
301  */
302 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *data, 
303                         size_t *data_size, int (filter)(smx_comm_t, void *), void *arg)
304 {
305   smx_comm_t comm;
306
307   /* Look for communication request matching our needs. 
308      If it is not found then create it and push it into the rendez-vous point */
309   comm = SIMIX_rdv_get_request(rdv, filter, arg);
310
311   if(comm == NULL){
312     comm = SIMIX_communication_new(comm_recv, rdv);
313     SIMIX_rdv_push(rdv, comm);
314   }
315
316   /* Setup communication request */
317   comm->dst_host = SIMIX_host_self();
318   comm->dst_buff = data;
319   comm->dst_buff_size = data_size;
320
321   SIMIX_communication_start(comm);
322
323   /* Wait for communication completion */
324   /* FIXME: if the semantic is non blocking, it shouldn't wait on the condition here */
325   SIMIX_communication_wait_for_completion(comm, timeout);
326
327   SIMIX_communication_destroy(comm);
328 }
329
330 /******************************************************************************/
331 /*                        Asynchronous Communication                          */
332 /******************************************************************************/
333
334 /*
335 void SIMIX_network_wait(smx_action_t comm, double timeout)
336 {
337     if (timeout > 0)
338       SIMIX_cond_wait_timeout(rdv_cond, rdv_comm_mutex, timeout - start_time);
339     else
340       SIMIX_cond_wait(rdv_cond, rdv_comm_mutex);    
341
342 }
343
344
345 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)
346 {
347   if(SIMIX_action_get_state (comm) == SURF_ACTION_DONE){
348     memcpy(comm->data
349
350   return SIMIX_action_get_state (comm) == SURF_ACTION_DONE ? TRUE : FALSE;
351 }*/
352
353
354
355
356
357
358