Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Enough is enough. Add semaphores as SIMIX synchronization mechanism.
[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   comm->rdv = rdv;
58 }
59
60 /**
61  *  \brief Remove a communication request from a rendez-vous point
62  *  \param rdv The rendez-vous point
63  *  \param comm The communication request
64  */
65 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
66 {
67   xbt_fifo_remove(rdv->comm_fifo, comm);
68   comm->rdv = NULL;
69 }
70   
71 /**
72  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
73  *  \param type The type of communication we are looking for (comm_send, comm_recv)
74  *  \return The communication request if found, NULL otherwise.
75  */
76 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type)
77 {
78   smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
79                                   xbt_fifo_get_first_item(rdv->comm_fifo));
80
81   if(comm && comm->type == type){
82     DEBUG0("Communication request found!");
83     xbt_fifo_shift(rdv->comm_fifo);
84     SIMIX_communication_use(comm);
85     comm->rdv = NULL;    
86     return comm;
87   }
88
89   /* no relevant request found. Return NULL */
90   DEBUG0("Communication request not found");
91   return NULL;
92 }
93
94 /**
95  *  \brief counts the number of communication requests of a given host pending
96  *         on a rendez-vous point
97  *  \param rdv The rendez-vous point
98  *  \param host The host to be counted
99  *  \return The number of comm request pending in the rdv
100  */
101 int 
102 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
103 {
104   smx_comm_t comm = NULL;
105   xbt_fifo_item_t item = NULL;
106   int count = 0;
107
108   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
109     if (comm->src_proc->smx_host == host)
110       count++;
111   }
112
113   return count;
114 }
115
116 /**
117  *  \brief returns the communication at the head of the rendez-vous
118  *  \param rdv The rendez-vous point
119  *  \return The communication or NULL if empty
120  */
121 smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
122 {
123   return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
124 }
125
126 /** @brief adds some API-related data to the rendez-vous point */
127 void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
128   rdv->data=data;
129 }
130 /** @brief gets API-related data from the rendez-vous point */
131 void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
132   return rdv->data;
133 }
134
135 /******************************************************************************/
136 /*                           Communication Requests                           */
137 /******************************************************************************/ 
138
139 /**
140  *  \brief Creates a new communication request
141  *  \param type The type of communication (comm_send, comm_recv)
142  *  \return The new communication request
143  */  
144 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
145 {
146   /* alloc structures */
147   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
148   comm->type = type;
149   comm->cond = SIMIX_cond_init();
150   comm->refcount = 1;
151   
152   return comm;
153 }
154
155 /**
156  *  \brief Destroy a communication request
157  *  \param comm The request to be destroyed
158  */
159 void SIMIX_communication_destroy(smx_comm_t comm)
160 {
161   comm->refcount--;
162   if(comm->refcount > 0)
163     return;
164
165   if(comm->cond){
166     SIMIX_cond_destroy(comm->cond);
167     comm->cond = NULL;
168   }
169   
170   if(comm->act){
171     SIMIX_action_destroy(comm->act);
172     comm->act = NULL;
173   }
174   
175   xbt_free(comm);
176 }
177
178 /**
179  *  \brief Increase the number of users of the communication.
180  *  \param comm The communication request
181  *  Each communication request can be used by more than one process, so it is
182  *  necessary to know number of them at destroy time, to avoid freeing stuff that
183  *  maybe is in use by others.
184  *  \
185  */
186 static inline void SIMIX_communication_use(smx_comm_t comm)
187 {
188   comm->refcount++;
189 }
190
191 /**
192  *  \brief Start the simulation of a communication request
193  *  \param comm The   comm->rdv = NULL;communication request
194  */
195 static inline void SIMIX_communication_start(smx_comm_t comm)
196 {
197   /* If both the sender and the receiver are already there, start the communication */
198   if(comm->src_proc && comm->dst_proc){
199     DEBUG1("Starting communication %p", comm);
200     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host, 
201                                          comm->dst_proc->smx_host, NULL, 
202                                          comm->task_size, comm->rate);
203
204     /* If any of the process is suspend, create the action but stop its execution,
205        it will be restart when the sender process resume */
206     if(SIMIX_process_is_suspended(comm->src_proc) || 
207        SIMIX_process_is_suspended(comm->dst_proc)) {
208       SIMIX_action_set_priority(comm->act, 0);
209     }
210     
211     /* Add the communication as user data of the action */
212     comm->act->data = comm;
213     
214     SIMIX_register_action_to_condition(comm->act, comm->cond);
215   }
216 }
217
218 /**
219  *  \brief Waits for communication completion and performs error checking
220  *  \param comm The communication
221  *  \param timeout The max amount of time to wait for the communication to finish
222  *
223  *  Throws:
224  *   - host_error if peer failed
225  *   - timeout_error if communication reached the timeout specified
226  *   - network_error if network failed or peer issued a timeout
227  */
228 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
229 {
230   xbt_ex_t e;
231
232   DEBUG1("Waiting for the completion of communication %p", comm);
233   
234   if(timeout > 0){
235     TRY{
236       SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
237     }
238     CATCH(e){
239       /* If there is a timeout then cancel the communication if it is running or 
240          remove it from the rendez-vous otherwise. Then signal the other peer,
241          destroy the communication and retrow the exception. */
242       if(e.category == timeout_error){
243         DEBUG1("Communication timeout! %p", comm);
244         if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
245           SIMIX_communication_cancel(comm);
246         else
247           SIMIX_rdv_remove(comm->rdv, comm);
248
249         SIMIX_cond_signal(comm->cond);
250         SIMIX_communication_destroy(comm);
251       }
252       RETHROW;
253     }
254   }else{
255     SIMIX_cond_wait(comm->cond, NULL);
256   }
257
258   DEBUG1("Communication %p complete! Let's check for errors", comm);
259   
260   /* Check for errors other than timeouts (they are catched above) */
261   if(!SIMIX_host_get_state(SIMIX_host_self())){
262     if(comm->rdv)
263       SIMIX_rdv_remove(comm->rdv, comm);
264     SIMIX_communication_destroy(comm);
265     THROW0(host_error, 0, "Host failed");
266   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
267     SIMIX_communication_destroy(comm);
268     THROW0(network_error, 0, "Link failure");
269   }
270 }
271
272 /**
273  *  \brief Cancels a communication
274  *  \brief comm The communication to cancel
275  */
276 void SIMIX_communication_cancel(smx_comm_t comm)
277 {
278   SIMIX_action_cancel(comm->act);
279 }
280
281 /**
282  *  \brief get the amount remaining from the communication
283  *  \param comm The communication
284  */
285 double SIMIX_communication_get_remains(smx_comm_t comm)
286 {
287   return SIMIX_action_get_remains(comm->act);
288 }  
289
290 /**
291  *  \brief Copy the communication data from the sender's buffer to the receiver's one
292  *  \param comm The communication
293  */
294 void SIMIX_network_copy_data(smx_comm_t comm)
295 {
296   /* If there is no data to be copy then return */
297   if(!comm->src_buff || !comm->dst_buff)
298     return;
299   
300   size_t src_buff_size = comm->src_buff_size;
301   size_t dst_buff_size = *comm->dst_buff_size;
302   
303   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
304   dst_buff_size = MIN(dst_buff_size, src_buff_size);
305   
306   /* Update the receiver's buffer size to the copied amount */
307   if (comm->dst_buff_size)
308     *comm->dst_buff_size = dst_buff_size;
309
310   if(dst_buff_size == 0)
311     return;
312
313   memcpy(comm->dst_buff, comm->src_buff, dst_buff_size);
314
315   DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)", 
316          comm, comm->src_proc->smx_host->name, comm->dst_proc->smx_host->name,
317          dst_buff_size);
318 }
319
320 /**
321  *  \brief Return the user data associated to the communication
322  *  \param comm The communication
323  *  \return the user data
324  */
325 void *SIMIX_communication_get_data(smx_comm_t comm)
326 {
327   return comm->data;
328 }
329
330 /******************************************************************************/
331 /*                        Synchronous Communication                           */
332 /******************************************************************************/
333 /**
334  *  \brief Put a send communication request in a rendez-vous point and waits for
335  *         its completion (blocking)
336  *  \param rdv The rendez-vous point
337  *  \param task_size The size of the communication action (for surf simulation)
338  *  \param rate The rate of the communication action (for surf)
339  *  \param timeout The timeout used for the waiting the completion 
340  *  \param src_buff The source buffer containing the message to be sent
341  *  \param src_buff_size The size of the source buffer
342  *  \param comm_ref The communication object used for the send
343  *  \param data User data associated to the communication object
344  *  Throws:
345  *   - host_error if peer failed
346  *   - timeout_error if communication reached the timeout specified
347  *   - network_error if network failed or peer issued a timeout
348  */
349 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate, 
350                         double timeout, void *src_buff, size_t src_buff_size,
351                         smx_comm_t *comm_ref, void *data)
352 {
353   smx_comm_t comm;
354   
355   /* Look for communication request matching our needs. 
356      If it is not found then create it and push it into the rendez-vous point */
357   comm = SIMIX_rdv_get_request(rdv, comm_recv);
358
359   if(!comm){
360     comm = SIMIX_communication_new(comm_send);
361     SIMIX_rdv_push(rdv, comm);
362   }
363
364   /* Update the communication reference with the comm to be used */
365   *comm_ref = comm;
366   
367   /* Setup the communication request */
368   comm->src_proc = SIMIX_process_self();
369   comm->task_size = task_size;
370   comm->rate = rate;
371   comm->src_buff = src_buff;
372   comm->src_buff_size = src_buff_size;
373   comm->data = data;
374
375   SIMIX_communication_start(comm);
376
377   /* Wait for communication completion */
378   SIMIX_communication_wait_for_completion(comm, timeout);
379
380   SIMIX_communication_destroy(comm);
381 }
382
383 /**
384  *  \brief Put a receive communication request in a rendez-vous point and waits
385  *         for its completion (blocking)
386  *  \param rdv The rendez-vous point
387  *  \param timeout The timeout used for the waiting the completion 
388  *  \param dst_buff The destination buffer to copy the received message
389  *  \param src_buff_size The size of the destination buffer
390  *  \param comm_ref The communication object used for the send
391  *  Throws:
392  *   - host_error if peer failed
393  *   - timeout_error if communication reached the timeout specified
394  *   - network_error if network failed or peer issued a timeout
395  */
396 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff, 
397                         size_t *dst_buff_size, smx_comm_t *comm_ref)
398 {
399   smx_comm_t comm;
400
401   /* Look for communication request matching our needs. 
402      If it is not found then create it and push it into the rendez-vous point */
403   comm = SIMIX_rdv_get_request(rdv, comm_send);
404
405   if(!comm){
406     comm = SIMIX_communication_new(comm_recv);
407     SIMIX_rdv_push(rdv, comm);
408   }
409
410   /* Update the communication reference with the comm to be used */
411   *comm_ref = comm;
412  
413   /* Setup communication request */
414   comm->dst_proc = SIMIX_process_self();
415   comm->dst_buff = dst_buff;
416   comm->dst_buff_size = dst_buff_size;
417
418   SIMIX_communication_start(comm);
419
420   /* Wait for communication completion */
421   SIMIX_communication_wait_for_completion(comm, timeout);
422
423   SIMIX_communication_destroy(comm);
424 }
425
426 /******************************************************************************/
427 /*                        Asynchronous Communication                          */
428 /******************************************************************************/
429
430
431 void SIMIX_network_wait(smx_action_t comm, double timeout)
432 {
433   THROW_UNIMPLEMENTED;
434 }
435
436 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)
437 {
438   THROW_UNIMPLEMENTED;
439 }
440
441
442
443
444
445
446