Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9c17f8b87c27b189d031634e6c88477944a672c3
[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   smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
78                                   xbt_fifo_get_first_item(rdv->comm_fifo));
79
80   if(comm && comm->type == type){
81     DEBUG0("Communication request found!");
82     xbt_fifo_shift(rdv->comm_fifo);
83     SIMIX_communication_use(comm);
84     comm->rdv = NULL;    
85     return comm;
86   }
87
88   DEBUG0("Communication request not found");
89   return NULL;
90 }
91
92 /**
93  *  \brief counts the number of communication requests of a given host pending
94  *         on a rendez-vous point
95  *  \param rdv The rendez-vous point
96  *  \param host The host to be counted
97  *  \return The number of comm request pending in the rdv
98  */
99 int 
100 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
101 {
102   smx_comm_t comm = NULL;
103   xbt_fifo_item_t item = NULL;
104   int count = 0;
105
106   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
107     if (comm->src_proc->smx_host == host)
108       count++;
109   }
110
111   return count;
112 }
113
114 /**
115  *  \brief returns the communication at the head of the rendez-vous
116  *  \param rdv The rendez-vous point
117  *  \return The communication or NULL if empty
118  */
119 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
120 {
121   return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
122 }
123
124 /** @brief adds some API-related data to the rendez-vous point */
125 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
126   rdv->data=data;
127 }
128 /** @brief gets API-related data from the rendez-vous point */
129 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
130   return rdv->data;
131 }
132
133 /******************************************************************************/
134 /*                           Communication Requests                           */
135 /******************************************************************************/ 
136
137 /**
138  *  \brief Creates a new communication request
139  *  \param type The type of communication (comm_send, comm_recv)
140  *  \return The new communication request
141  */  
142 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
143 {
144   /* alloc structures */
145   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
146   comm->type = type;
147   comm->sem = SIMIX_sem_init(0);
148   comm->refcount = 1;
149   
150   return comm;
151 }
152
153 /**
154  *  \brief Destroy a communication request
155  *  \param comm The request to be destroyed
156  */
157 void SIMIX_communication_destroy(smx_comm_t comm)
158 {
159   comm->refcount--;
160   if(comm->refcount > 0)
161     return;
162
163   if(comm->sem){
164     SIMIX_sem_destroy(comm->sem);
165     comm->sem = NULL;
166   }
167   
168   if(comm->act){
169     SIMIX_action_destroy(comm->act);
170     comm->act = NULL;
171   }
172
173   if(comm->src_timeout){
174     SIMIX_action_destroy(comm->src_timeout);
175     comm->src_timeout = NULL;
176   }
177
178   if(comm->dst_timeout){
179       SIMIX_action_destroy(comm->dst_timeout);
180       comm->dst_timeout = NULL;
181     }
182
183   xbt_free(comm);
184 }
185
186 /**
187  *  \brief Increase the number of users of the communication.
188  *  \param comm The communication request
189  *  Each communication request can be used by more than one process, so it is
190  *  necessary to know number of them at destroy time, to avoid freeing stuff that
191  *  maybe is in use by others.
192  *  \
193  */
194 static inline void SIMIX_communication_use(smx_comm_t comm)
195 {
196   comm->refcount++;
197 }
198
199 /**
200  *  \brief Start the simulation of a communication request
201  *  \param comm The   comm->rdv = NULL;communication request
202  */
203 static inline void SIMIX_communication_start(smx_comm_t comm)
204 {
205   /* If both the sender and the receiver are already there, start the communication */
206   if(comm->src_proc && comm->dst_proc){
207     DEBUG1("Starting communication %p", comm);
208     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host, 
209                                          comm->dst_proc->smx_host, NULL, 
210                                          comm->task_size, comm->rate);
211 #ifdef HAVE_TRACING
212     TRACE_smx_action_communicate (comm->act, comm->src_proc);
213 #endif
214
215     /* If any of the process is suspend, create the action but stop its execution,
216        it will be restarted when the sender process resume */
217     if(SIMIX_process_is_suspended(comm->src_proc) || 
218        SIMIX_process_is_suspended(comm->dst_proc)) {
219       SIMIX_action_set_priority(comm->act, 0);
220     }
221     
222     /* Add the communication as user data of the action */
223     comm->act->data = comm;
224
225     /* The semaphore will only get signaled once, but since the first unlocked guy will
226      * release_forever() the semaphore, that will unlock the second (and any other)
227      * communication partner */
228     SIMIX_register_action_to_semaphore(comm->act, comm->sem);
229   }
230 }
231
232 /**
233  *  \brief Waits for communication completion and performs error checking
234  *  \param comm The communication
235  *  \param timeout The max amount of time to wait for the communication to finish
236  *
237  *  Throws:
238  *   - host_error if peer failed
239  *   - timeout_error if communication reached the timeout specified
240  *   - network_error if network failed or peer issued a timeout
241  */
242 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
243 {
244   smx_action_t act_sleep = NULL;
245   int src_timeout = 0;
246   int dst_timeout = 0;
247
248   DEBUG1("Waiting for the completion of communication %p", comm);
249   
250   if (timeout >= 0) {
251     act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
252                 if(SIMIX_process_self()==comm->src_proc)
253                         comm->src_timeout = act_sleep;
254                 else
255                         comm->dst_timeout = act_sleep;
256     SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
257     SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
258     SIMIX_process_self()->waiting_action = act_sleep;
259     SIMIX_sem_block_onto(comm->sem);
260     SIMIX_process_self()->waiting_action = NULL;
261     SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
262   } else {
263     SIMIX_sem_acquire(comm->sem);
264   }
265
266   /* Check for timeouts */
267   if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
268       (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
269                         /* Somebody did a timeout! */
270     if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
271     if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
272
273     if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
274       SIMIX_communication_cancel(comm);
275     else if (comm->rdv)
276       SIMIX_rdv_remove(comm->rdv, comm);
277
278     /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
279     SIMIX_sem_release_forever(comm->sem);
280     SIMIX_communication_destroy(comm);
281
282     THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
283   }
284
285   DEBUG1("Communication %p complete! Let's check for errors", comm);
286
287   /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
288   SIMIX_sem_release_forever(comm->sem);
289   
290   /* Check for errors other than timeouts (they are catched above) */
291   if(!SIMIX_host_get_state(SIMIX_host_self())){
292     if(comm->rdv)
293       SIMIX_rdv_remove(comm->rdv, comm);
294     SIMIX_communication_destroy(comm);
295     THROW0(host_error, 0, "Host failed");
296   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
297     SIMIX_communication_destroy(comm);
298     THROW0(network_error, 0, "Link failure");
299   }
300   SIMIX_communication_destroy(comm);
301 }
302
303 /**
304  *  \brief Cancels a communication
305  *  \brief comm The communication to cancel
306  */
307 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
308 {
309   if (comm->act)
310     SIMIX_action_cancel(comm->act);
311 }
312
313 /**
314  *  \brief get the amount remaining from the communication
315  *  \param comm The communication
316  */
317 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
318 {
319   return SIMIX_action_get_remains(comm->act);
320 }  
321
322 /******************************************************************************/
323 /*                    SIMIX_network_copy_data callbacks                       */
324 /******************************************************************************/
325 static void (*SIMIX_network_copy_data_callback)(smx_comm_t, size_t) = &SIMIX_network_copy_pointer_callback;
326
327 void SIMIX_network_set_copy_data_callback(void (*callback)(smx_comm_t, size_t)) {
328   SIMIX_network_copy_data_callback = callback;
329 }
330
331 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size) {
332   xbt_assert1((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)",buff_size);
333   *(void**)(comm->dst_buff) = comm->src_buff;
334 }
335
336 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size) {
337   memcpy(comm->dst_buff, comm->src_buff, buff_size);
338 }
339
340 /**
341  *  \brief Copy the communication data from the sender's buffer to the receiver's one
342  *  \param comm The communication
343  */
344 void SIMIX_network_copy_data(smx_comm_t comm)
345 {
346   /* If there is no data to be copy then return */
347   if(!comm->src_buff || !comm->dst_buff)
348     return;
349   
350   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
351   size_t buff_size = comm->src_buff_size;
352   if (comm->dst_buff_size)
353     buff_size = MIN(buff_size,*(comm->dst_buff_size));
354   
355   /* Update the receiver's buffer size to the copied amount */
356   if (comm->dst_buff_size)
357     *comm->dst_buff_size = buff_size;
358
359   if(buff_size == 0)
360     return;
361   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
362       comm,
363       comm->src_proc->smx_host->name, comm->src_buff,
364       comm->dst_proc->smx_host->name, comm->dst_buff,
365       buff_size);
366   (*SIMIX_network_copy_data_callback)(comm, buff_size);
367 }
368
369 /**
370  *  \brief Return the user data associated to the communication
371  *  \param comm The communication
372  *  \return the user data
373  */
374 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
375 {
376   return comm->data;
377 }
378
379 /******************************************************************************/
380 /*                        Synchronous Communication                           */
381 /******************************************************************************/
382 /**
383  *  \brief Put a send communication request in a rendez-vous point and waits for
384  *         its completion (blocking)
385  *  \param rdv The rendez-vous point
386  *  \param task_size The size of the communication action (for surf simulation)
387  *  \param rate The rate of the communication action (for surf)
388  *  \param timeout The timeout used for the waiting the completion 
389  *  \param src_buff The source buffer containing the message to be sent
390  *  \param src_buff_size The size of the source buffer
391  *  \param comm_ref The communication object used for the send  (useful if someone else wants to cancel this communication afterward)
392  *  \param data User data associated to the communication object
393  *  Throws:
394  *   - host_error if peer failed
395  *   - timeout_error if communication reached the timeout specified
396  *   - network_error if network failed or peer issued a timeout
397  */
398 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
399                         double timeout, void *src_buff, size_t src_buff_size,
400                         smx_comm_t *comm_ref, void *data)
401 {
402   *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
403   SIMIX_network_wait(*comm_ref,timeout);
404 }
405
406 /**
407  *  \brief Put a receive communication request in a rendez-vous point and waits
408  *         for its completion (blocking)
409  *  \param rdv The rendez-vous point
410  *  \param timeout The timeout used for the waiting the completion 
411  *  \param dst_buff The destination buffer to copy the received message
412  *  \param src_buff_size The size of the destination buffer
413  *  \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
414  *  Throws:
415  *   - host_error if peer failed
416  *   - timeout_error if communication reached the timeout specified
417  *   - network_error if network failed or peer issued a timeout
418  */
419 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
420                         size_t *dst_buff_size, smx_comm_t *comm_ref)
421 {
422   *comm_ref = SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
423   SIMIX_network_wait(*comm_ref,timeout);
424 }
425
426 /******************************************************************************/
427 /*                        Asynchronous Communication                          */
428 /******************************************************************************/
429 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
430     void *src_buff, size_t src_buff_size, void *data)
431 {
432   smx_comm_t comm;
433
434   /* Look for communication request matching our needs.
435      If it is not found then create it and push it into the rendez-vous point */
436   comm = SIMIX_rdv_get_request(rdv, comm_recv);
437
438   if(!comm){
439     comm = SIMIX_communication_new(comm_send);
440     SIMIX_rdv_push(rdv, comm);
441   }
442
443   /* Setup the communication request */
444   comm->src_proc = SIMIX_process_self();
445   comm->task_size = task_size;
446   comm->rate = rate;
447   comm->src_buff = src_buff;
448   comm->src_buff_size = src_buff_size;
449   comm->data = data;
450
451   SIMIX_communication_start(comm);
452   return comm;
453 }
454
455 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size) {
456   smx_comm_t comm;
457
458   /* Look for communication request matching our needs.
459      If it is not found then create it and push it into the rendez-vous point */
460   comm = SIMIX_rdv_get_request(rdv, comm_send);
461
462   if(!comm){
463     comm = SIMIX_communication_new(comm_recv);
464     SIMIX_rdv_push(rdv, comm);
465   }
466
467   /* Setup communication request */
468   comm->dst_proc = SIMIX_process_self();
469   comm->dst_buff = dst_buff;
470   comm->dst_buff_size = dst_buff_size;
471
472   SIMIX_communication_start(comm);
473   return comm;
474 }
475
476 /** @brief blocks until the communication terminates or the timeout occurs */
477 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout) {
478   /* Wait for communication completion */
479   SIMIX_communication_wait_for_completion(comm, timeout);
480 }
481
482 /** @Returns whether the (asynchronous) communication is done yet or not */
483 XBT_INLINE int SIMIX_network_test(smx_comm_t comm) {
484   return comm->sem?SIMIX_sem_would_block(comm->sem):0;
485 }
486
487 /** @brief wait for the completion of any communication of a set
488  *
489  *  @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
490  */
491 unsigned int SIMIX_network_waitany(xbt_dynar_t comms) {
492   xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
493   unsigned int cursor, found_comm=-1;
494   smx_comm_t comm,comm_finished=NULL;
495
496   xbt_dynar_foreach(comms,cursor,comm){
497     xbt_dynar_push(sems,&(comm->sem));
498   }
499
500   DEBUG1("Waiting for the completion of communication set %p", comms);
501
502   found_comm = SIMIX_sem_acquire_any(sems);
503   xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
504   xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
505
506   DEBUG1("Communication %p complete! Let's check for errors", comm_finished);
507
508   /* Make sure that everyone sleeping on that semaphore is awake,
509    * and that nobody will ever block on it */
510   SIMIX_sem_release_forever(comm_finished->sem);
511
512   /* Check for errors */
513   if(!SIMIX_host_get_state(SIMIX_host_self())){
514     if(comm_finished->rdv)
515       SIMIX_rdv_remove(comm_finished->rdv, comm_finished);
516     SIMIX_communication_destroy(comm_finished);
517     THROW0(host_error, 0, "Host failed");
518   } else if (SIMIX_action_get_state(comm_finished->act) == SURF_ACTION_FAILED){
519     SIMIX_communication_destroy(comm_finished);
520     THROW0(network_error, 0, "Link failure");
521   }
522   SIMIX_communication_destroy(comm_finished);
523
524   return found_comm;
525 }