1 /* Copyright (c) 2009-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.h"
10 #include "mc/mc_replay.h"
12 #include "smpi/private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
15 "SIMIX network-related synchronization");
17 static xbt_dict_t rdv_points = NULL;
18 XBT_EXPORT_NO_IMPORT(unsigned long int) smx_total_comms = 0;
20 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
21 static void SIMIX_comm_copy_data(smx_synchro_t comm);
22 static smx_synchro_t SIMIX_comm_new(e_smx_comm_type_t type);
23 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_synchro_t comm);
24 static smx_synchro_t SIMIX_fifo_probe_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
25 int (*match_fun)(void *, void *,smx_synchro_t),
26 void *user_data, smx_synchro_t my_synchro);
27 static smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
28 int (*match_fun)(void *, void *,smx_synchro_t),
29 void *user_data, smx_synchro_t my_synchro);
30 static void SIMIX_rdv_free(void *data);
31 static void SIMIX_comm_start(smx_synchro_t synchro);
33 void SIMIX_network_init(void)
35 rdv_points = xbt_dict_new_homogeneous(SIMIX_rdv_free);
38 void SIMIX_network_exit(void)
40 xbt_dict_free(&rdv_points);
43 /******************************************************************************/
44 /* Rendez-Vous Points */
45 /******************************************************************************/
47 smx_rdv_t SIMIX_rdv_create(const char *name)
49 /* two processes may have pushed the same rdv_create simcall at the same time */
50 smx_rdv_t rdv = name ? xbt_dict_get_or_null(rdv_points, name) : NULL;
53 rdv = xbt_new0(s_smx_rvpoint_t, 1);
54 rdv->name = name ? xbt_strdup(name) : NULL;
55 rdv->comm_fifo = xbt_fifo_new();
56 rdv->done_comm_fifo = xbt_fifo_new();
57 rdv->permanent_receiver=NULL;
59 XBT_DEBUG("Creating a mailbox at %p with name %s", rdv, name);
62 xbt_dict_set(rdv_points, rdv->name, rdv, NULL);
67 void SIMIX_rdv_destroy(smx_rdv_t rdv)
70 xbt_dict_remove(rdv_points, rdv->name);
73 void SIMIX_rdv_free(void *data)
75 XBT_DEBUG("rdv free %p", data);
76 smx_rdv_t rdv = (smx_rdv_t) data;
78 xbt_fifo_free(rdv->comm_fifo);
79 xbt_fifo_free(rdv->done_comm_fifo);
84 xbt_dict_t SIMIX_get_rdv_points()
89 smx_rdv_t SIMIX_rdv_get_by_name(const char *name)
91 return xbt_dict_get_or_null(rdv_points, name);
94 int SIMIX_rdv_comm_count_by_host(smx_rdv_t rdv, sg_host_t host)
96 smx_synchro_t comm = NULL;
97 xbt_fifo_item_t item = NULL;
100 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_synchro_t) {
101 if (comm->comm.src_proc->host == host)
108 smx_synchro_t SIMIX_rdv_get_head(smx_rdv_t rdv)
110 return xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
114 * \brief get the receiver (process associated to the mailbox)
115 * \param rdv The rendez-vous point
116 * \return process The receiving process (NULL if not set)
118 smx_process_t SIMIX_rdv_get_receiver(smx_rdv_t rdv)
120 return rdv->permanent_receiver;
124 * \brief set the receiver of the rendez vous point to allow eager sends
125 * \param rdv The rendez-vous point
126 * \param process The receiving process
128 void SIMIX_rdv_set_receiver(smx_rdv_t rdv, smx_process_t process)
130 rdv->permanent_receiver=process;
134 * \brief Pushes a communication synchro into a rendez-vous point
135 * \param rdv The rendez-vous point
136 * \param comm The communication synchro
138 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_synchro_t comm)
140 xbt_fifo_push(rdv->comm_fifo, comm);
141 comm->comm.rdv = rdv;
145 * \brief Removes a communication synchro from a rendez-vous point
146 * \param rdv The rendez-vous point
147 * \param comm The communication synchro
149 XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_synchro_t comm)
151 xbt_fifo_remove(rdv->comm_fifo, comm);
152 comm->comm.rdv = NULL;
156 * \brief Checks if there is a communication synchro queued in a fifo matching our needs
157 * \param type The type of communication we are looking for (comm_send, comm_recv)
158 * \return The communication synchro if found, NULL otherwise
160 smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
161 int (*match_fun)(void *, void *,smx_synchro_t),
162 void *this_user_data, smx_synchro_t my_synchro)
164 smx_synchro_t synchro;
165 xbt_fifo_item_t item;
166 void* other_user_data = NULL;
168 xbt_fifo_foreach(fifo, item, synchro, smx_synchro_t) {
169 if (synchro->comm.type == SIMIX_COMM_SEND) {
170 other_user_data = synchro->comm.src_data;
171 } else if (synchro->comm.type == SIMIX_COMM_RECEIVE) {
172 other_user_data = synchro->comm.dst_data;
174 if (synchro->comm.type == type &&
175 (!match_fun || match_fun(this_user_data, other_user_data, synchro)) &&
176 (!synchro->comm.match_fun || synchro->comm.match_fun(other_user_data, this_user_data, my_synchro))) {
177 XBT_DEBUG("Found a matching communication synchro %p", synchro);
178 xbt_fifo_remove_item(fifo, item);
179 xbt_fifo_free_item(item);
180 synchro->comm.refcount++;
182 synchro->comm.rdv_cpy = synchro->comm.rdv;
184 synchro->comm.rdv = NULL;
187 XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
188 " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
189 synchro, (int)synchro->comm.type, (int)type);
191 XBT_DEBUG("No matching communication synchro found");
197 * \brief Checks if there is a communication synchro queued in a fifo matching our needs, but leave it there
198 * \param type The type of communication we are looking for (comm_send, comm_recv)
199 * \return The communication synchro if found, NULL otherwise
201 smx_synchro_t SIMIX_fifo_probe_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
202 int (*match_fun)(void *, void *,smx_synchro_t),
203 void *this_user_data, smx_synchro_t my_synchro)
205 smx_synchro_t synchro;
206 xbt_fifo_item_t item;
207 void* other_user_data = NULL;
209 xbt_fifo_foreach(fifo, item, synchro, smx_synchro_t) {
210 if (synchro->comm.type == SIMIX_COMM_SEND) {
211 other_user_data = synchro->comm.src_data;
212 } else if (synchro->comm.type == SIMIX_COMM_RECEIVE) {
213 other_user_data = synchro->comm.dst_data;
215 if (synchro->comm.type == type &&
216 (!match_fun || match_fun(this_user_data, other_user_data, synchro)) &&
217 (!synchro->comm.match_fun || synchro->comm.match_fun(other_user_data, this_user_data, my_synchro))) {
218 XBT_DEBUG("Found a matching communication synchro %p", synchro);
219 synchro->comm.refcount++;
223 XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
224 " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
225 synchro, (int)synchro->comm.type, (int)type);
227 XBT_DEBUG("No matching communication synchro found");
230 /******************************************************************************/
231 /* Communication synchros */
232 /******************************************************************************/
235 * \brief Creates a new communicate synchro
236 * \param type The direction of communication (comm_send, comm_recv)
237 * \return The new communicate synchro
239 smx_synchro_t SIMIX_comm_new(e_smx_comm_type_t type)
241 smx_synchro_t synchro;
243 /* alloc structures */
244 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
246 synchro->type = SIMIX_SYNC_COMMUNICATE;
247 synchro->state = SIMIX_WAITING;
249 /* set communication */
250 synchro->comm.type = type;
251 synchro->comm.refcount = 1;
252 synchro->comm.src_data=NULL;
253 synchro->comm.dst_data=NULL;
256 #ifdef HAVE_LATENCY_BOUND_TRACKING
257 //initialize with unknown value
258 synchro->latency_limited = -1;
261 synchro->category = NULL;
263 XBT_DEBUG("Create communicate synchro %p", synchro);
270 * \brief Destroy a communicate synchro
271 * \param synchro The communicate synchro to be destroyed
273 void SIMIX_comm_destroy(smx_synchro_t synchro)
275 XBT_DEBUG("Destroy synchro %p (refcount: %d), state: %d",
276 synchro, synchro->comm.refcount, (int)synchro->state);
278 if (synchro->comm.refcount <= 0) {
279 xbt_backtrace_display_current();
280 xbt_die("The refcount of comm %p is already 0 before decreasing it. "
281 "That's a bug! If you didn't test and/or wait the same communication twice in your code, then the bug is SimGrid's...", synchro);
283 synchro->comm.refcount--;
284 if (synchro->comm.refcount > 0)
286 XBT_DEBUG("Really free communication %p; refcount is now %d", synchro,
287 synchro->comm.refcount);
289 #ifdef HAVE_LATENCY_BOUND_TRACKING
290 synchro->latency_limited = SIMIX_comm_is_latency_bounded( synchro ) ;
293 xbt_free(synchro->name);
294 SIMIX_comm_destroy_internal_actions(synchro);
296 if (synchro->comm.detached && synchro->state != SIMIX_DONE) {
297 /* the communication has failed and was detached:
298 * we have to free the buffer */
299 if (synchro->comm.clean_fun) {
300 synchro->comm.clean_fun(synchro->comm.src_buff);
302 synchro->comm.src_buff = NULL;
305 if(synchro->comm.rdv)
306 SIMIX_rdv_remove(synchro->comm.rdv, synchro);
308 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
311 void SIMIX_comm_destroy_internal_actions(smx_synchro_t synchro)
313 if (synchro->comm.surf_comm){
314 #ifdef HAVE_LATENCY_BOUND_TRACKING
315 synchro->latency_limited = SIMIX_comm_is_latency_bounded(synchro);
317 surf_action_unref(synchro->comm.surf_comm);
318 synchro->comm.surf_comm = NULL;
321 if (synchro->comm.src_timeout){
322 surf_action_unref(synchro->comm.src_timeout);
323 synchro->comm.src_timeout = NULL;
326 if (synchro->comm.dst_timeout){
327 surf_action_unref(synchro->comm.dst_timeout);
328 synchro->comm.dst_timeout = NULL;
332 void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_process_t src, smx_rdv_t rdv,
333 double task_size, double rate,
334 void *src_buff, size_t src_buff_size,
335 int (*match_fun)(void *, void *,smx_synchro_t),
336 void (*copy_data_fun)(smx_synchro_t, void*, size_t),
337 void *data, double timeout){
338 smx_synchro_t comm = simcall_HANDLER_comm_isend(simcall, src, rdv, task_size, rate,
339 src_buff, src_buff_size, match_fun, NULL, copy_data_fun,
341 SIMCALL_SET_MC_VALUE(simcall, 0);
342 simcall_HANDLER_comm_wait(simcall, comm, timeout);
344 smx_synchro_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_process_t src_proc, smx_rdv_t rdv,
345 double task_size, double rate,
346 void *src_buff, size_t src_buff_size,
347 int (*match_fun)(void *, void *,smx_synchro_t),
348 void (*clean_fun)(void *), // used to free the synchro in case of problem after a detached send
349 void (*copy_data_fun)(smx_synchro_t, void*, size_t),// used to copy data if not default one
350 void *data, int detached)
352 XBT_DEBUG("send from %p", rdv);
354 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
355 smx_synchro_t this_synchro = SIMIX_comm_new(SIMIX_COMM_SEND);
357 /* Look for communication synchro matching our needs. We also provide a description of
358 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
360 * If it is not found then push our communication into the rendez-vous point */
361 smx_synchro_t other_synchro = SIMIX_fifo_get_comm(rdv->comm_fifo, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro);
363 if (!other_synchro) {
364 other_synchro = this_synchro;
366 if (rdv->permanent_receiver!=NULL){
367 //this mailbox is for small messages, which have to be sent right now
368 other_synchro->state = SIMIX_READY;
369 other_synchro->comm.dst_proc=rdv->permanent_receiver;
370 other_synchro->comm.refcount++;
371 xbt_fifo_push(rdv->done_comm_fifo,other_synchro);
372 other_synchro->comm.rdv=rdv;
373 XBT_DEBUG("pushing a message into the permanent receive fifo %p, comm %p", rdv, &(other_synchro->comm));
376 SIMIX_rdv_push(rdv, this_synchro);
379 XBT_DEBUG("Receive already pushed");
381 SIMIX_comm_destroy(this_synchro);
382 --smx_total_comms; // this creation was a pure waste
384 other_synchro->state = SIMIX_READY;
385 other_synchro->comm.type = SIMIX_COMM_READY;
388 xbt_fifo_push(src_proc->comms, other_synchro);
390 /* if the communication synchro is detached then decrease the refcount
391 * by one, so it will be eliminated by the receiver's destroy call */
393 other_synchro->comm.detached = 1;
394 other_synchro->comm.refcount--;
395 other_synchro->comm.clean_fun = clean_fun;
397 other_synchro->comm.clean_fun = NULL;
400 /* Setup the communication synchro */
401 other_synchro->comm.src_proc = src_proc;
402 other_synchro->comm.task_size = task_size;
403 other_synchro->comm.rate = rate;
404 other_synchro->comm.src_buff = src_buff;
405 other_synchro->comm.src_buff_size = src_buff_size;
406 other_synchro->comm.src_data = data;
408 other_synchro->comm.match_fun = match_fun;
409 other_synchro->comm.copy_data_fun = copy_data_fun;
412 if (MC_is_active() || MC_record_replay_is_active()) {
413 other_synchro->state = SIMIX_RUNNING;
414 return (detached ? NULL : other_synchro);
417 SIMIX_comm_start(other_synchro);
418 return (detached ? NULL : other_synchro);
421 void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_process_t receiver, smx_rdv_t rdv,
422 void *dst_buff, size_t *dst_buff_size,
423 int (*match_fun)(void *, void *, smx_synchro_t),
424 void (*copy_data_fun)(smx_synchro_t, void*, size_t),
425 void *data, double timeout, double rate)
427 smx_synchro_t comm = SIMIX_comm_irecv(receiver, rdv, dst_buff,
428 dst_buff_size, match_fun, copy_data_fun, data, rate);
429 SIMCALL_SET_MC_VALUE(simcall, 0);
430 simcall_HANDLER_comm_wait(simcall, comm, timeout);
433 smx_synchro_t simcall_HANDLER_comm_irecv(smx_simcall_t simcall, smx_process_t receiver, smx_rdv_t rdv,
434 void *dst_buff, size_t *dst_buff_size,
435 int (*match_fun)(void *, void *, smx_synchro_t),
436 void (*copy_data_fun)(smx_synchro_t, void*, size_t),
437 void *data, double rate)
439 return SIMIX_comm_irecv(receiver, rdv, dst_buff, dst_buff_size,
440 match_fun, copy_data_fun, data, rate);
443 smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_rdv_t rdv,
444 void *dst_buff, size_t *dst_buff_size,
445 int (*match_fun)(void *, void *, smx_synchro_t),
446 void (*copy_data_fun)(smx_synchro_t, void*, size_t), // used to copy data if not default one
447 void *data, double rate)
449 XBT_DEBUG("recv from %p %p", rdv, rdv->comm_fifo);
450 smx_synchro_t this_synchro = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
452 smx_synchro_t other_synchro;
453 //communication already done, get it inside the fifo of completed comms
454 //permanent receive v1
455 //int already_received=0;
456 if(rdv->permanent_receiver && xbt_fifo_size(rdv->done_comm_fifo)!=0){
458 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
459 //find a match in the already received fifo
460 other_synchro = SIMIX_fifo_get_comm(rdv->done_comm_fifo, SIMIX_COMM_SEND, match_fun, data, this_synchro);
461 //if not found, assume the receiver came first, register it to the mailbox in the classical way
462 if (!other_synchro) {
463 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into fifo");
464 other_synchro = this_synchro;
465 SIMIX_rdv_push(rdv, this_synchro);
467 if(other_synchro->comm.surf_comm && SIMIX_comm_get_remains(other_synchro)==0.0)
469 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it",&(other_synchro->comm));
470 other_synchro->state = SIMIX_DONE;
471 other_synchro->comm.type = SIMIX_COMM_DONE;
472 other_synchro->comm.rdv = NULL;
474 XBT_DEBUG("Not yet finished, we have to wait %d", xbt_fifo_size(rdv->comm_fifo));
476 other_synchro->comm.refcount--;
477 SIMIX_comm_destroy(this_synchro);
478 --smx_total_comms; // this creation was a pure waste
481 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
483 /* Look for communication synchro matching our needs. We also provide a description of
484 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
486 * If it is not found then push our communication into the rendez-vous point */
487 other_synchro = SIMIX_fifo_get_comm(rdv->comm_fifo, SIMIX_COMM_SEND, match_fun, data, this_synchro);
489 if (!other_synchro) {
490 XBT_DEBUG("Receive pushed first %d", xbt_fifo_size(rdv->comm_fifo));
491 other_synchro = this_synchro;
492 SIMIX_rdv_push(rdv, this_synchro);
494 SIMIX_comm_destroy(this_synchro);
495 --smx_total_comms; // this creation was a pure waste
496 other_synchro->state = SIMIX_READY;
497 other_synchro->comm.type = SIMIX_COMM_READY;
498 //other_synchro->comm.refcount--;
500 xbt_fifo_push(dst_proc->comms, other_synchro);
503 /* Setup communication synchro */
504 other_synchro->comm.dst_proc = dst_proc;
505 other_synchro->comm.dst_buff = dst_buff;
506 other_synchro->comm.dst_buff_size = dst_buff_size;
507 other_synchro->comm.dst_data = data;
510 (other_synchro->comm.rate == -1.0 || rate < other_synchro->comm.rate))
511 other_synchro->comm.rate = rate;
513 other_synchro->comm.match_fun = match_fun;
514 other_synchro->comm.copy_data_fun = copy_data_fun;
517 /*if(already_received)//do the actual copy, because the first one after the comm didn't have all the info
518 SIMIX_comm_copy_data(other_synchro);*/
521 if (MC_is_active() || MC_record_replay_is_active()) {
522 other_synchro->state = SIMIX_RUNNING;
523 return other_synchro;
526 SIMIX_comm_start(other_synchro);
528 return other_synchro;
531 smx_synchro_t simcall_HANDLER_comm_iprobe(smx_simcall_t simcall, smx_rdv_t rdv,
532 int type, int src, int tag,
533 int (*match_fun)(void *, void *, smx_synchro_t),
535 return SIMIX_comm_iprobe(simcall->issuer, rdv, type, src, tag, match_fun, data);
538 smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_rdv_t rdv, int type, int src,
539 int tag, int (*match_fun)(void *, void *, smx_synchro_t), void *data)
541 XBT_DEBUG("iprobe from %p %p", rdv, rdv->comm_fifo);
542 smx_synchro_t this_synchro;
545 this_synchro=SIMIX_comm_new(SIMIX_COMM_SEND);
546 smx_type = SIMIX_COMM_RECEIVE;
548 this_synchro=SIMIX_comm_new(SIMIX_COMM_RECEIVE);
549 smx_type = SIMIX_COMM_SEND;
551 smx_synchro_t other_synchro=NULL;
552 if(rdv->permanent_receiver && xbt_fifo_size(rdv->done_comm_fifo)!=0){
553 //find a match in the already received fifo
554 XBT_DEBUG("first try in the perm recv mailbox");
556 other_synchro = SIMIX_fifo_probe_comm(rdv->done_comm_fifo, smx_type, match_fun, data, this_synchro);
560 XBT_DEBUG("try in the normal mailbox");
561 other_synchro = SIMIX_fifo_probe_comm(rdv->comm_fifo, smx_type, match_fun, data, this_synchro);
564 if(other_synchro)other_synchro->comm.refcount--;
566 SIMIX_comm_destroy(this_synchro);
568 return other_synchro;
571 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_synchro_t synchro, double timeout)
573 /* the simcall may be a wait, a send or a recv */
576 /* Associate this simcall to the wait synchro */
577 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro);
579 xbt_fifo_push(synchro->simcalls, simcall);
580 simcall->issuer->waiting_synchro = synchro;
582 if (MC_is_active() || MC_record_replay_is_active()) {
583 int idx = SIMCALL_GET_MC_VALUE(simcall);
585 synchro->state = SIMIX_DONE;
587 /* If we reached this point, the wait simcall must have a timeout */
588 /* Otherwise it shouldn't be enabled and executed by the MC */
592 if (synchro->comm.src_proc == simcall->issuer)
593 synchro->state = SIMIX_SRC_TIMEOUT;
595 synchro->state = SIMIX_DST_TIMEOUT;
598 SIMIX_comm_finish(synchro);
602 /* If the synchro has already finish perform the error handling, */
603 /* otherwise set up a waiting timeout on the right side */
604 if (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING) {
605 SIMIX_comm_finish(synchro);
606 } else { /* if (timeout >= 0) { we need a surf sleep action even when there is no timeout, otherwise surf won't tell us when the host fails */
607 sleep = surf_host_sleep(simcall->issuer->host, timeout);
608 surf_action_set_data(sleep, synchro);
610 if (simcall->issuer == synchro->comm.src_proc)
611 synchro->comm.src_timeout = sleep;
613 synchro->comm.dst_timeout = sleep;
617 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_synchro_t synchro)
619 if(MC_is_active() || MC_record_replay_is_active()){
620 simcall_comm_test__set__result(simcall, synchro->comm.src_proc && synchro->comm.dst_proc);
621 if(simcall_comm_test__get__result(simcall)){
622 synchro->state = SIMIX_DONE;
623 xbt_fifo_push(synchro->simcalls, simcall);
624 SIMIX_comm_finish(synchro);
626 SIMIX_simcall_answer(simcall);
631 simcall_comm_test__set__result(simcall, (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING));
632 if (simcall_comm_test__get__result(simcall)) {
633 xbt_fifo_push(synchro->simcalls, simcall);
634 SIMIX_comm_finish(synchro);
636 SIMIX_simcall_answer(simcall);
640 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, xbt_dynar_t synchros)
643 smx_synchro_t synchro;
644 simcall_comm_testany__set__result(simcall, -1);
646 if (MC_is_active() || MC_record_replay_is_active()){
647 int idx = SIMCALL_GET_MC_VALUE(simcall);
649 SIMIX_simcall_answer(simcall);
651 synchro = xbt_dynar_get_as(synchros, idx, smx_synchro_t);
652 simcall_comm_testany__set__result(simcall, idx);
653 xbt_fifo_push(synchro->simcalls, simcall);
654 synchro->state = SIMIX_DONE;
655 SIMIX_comm_finish(synchro);
660 xbt_dynar_foreach(simcall_comm_testany__get__comms(simcall), cursor,synchro) {
661 if (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING) {
662 simcall_comm_testany__set__result(simcall, cursor);
663 xbt_fifo_push(synchro->simcalls, simcall);
664 SIMIX_comm_finish(synchro);
668 SIMIX_simcall_answer(simcall);
671 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros)
673 smx_synchro_t synchro;
674 unsigned int cursor = 0;
676 if (MC_is_active() || MC_record_replay_is_active()){
677 int idx = SIMCALL_GET_MC_VALUE(simcall);
678 synchro = xbt_dynar_get_as(synchros, idx, smx_synchro_t);
679 xbt_fifo_push(synchro->simcalls, simcall);
680 simcall_comm_waitany__set__result(simcall, idx);
681 synchro->state = SIMIX_DONE;
682 SIMIX_comm_finish(synchro);
686 xbt_dynar_foreach(synchros, cursor, synchro){
687 /* associate this simcall to the the synchro */
688 xbt_fifo_push(synchro->simcalls, simcall);
690 /* see if the synchro is already finished */
691 if (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING){
692 SIMIX_comm_finish(synchro);
698 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
700 smx_synchro_t synchro;
701 unsigned int cursor = 0;
702 xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
704 xbt_dynar_foreach(synchros, cursor, synchro) {
705 xbt_fifo_remove(synchro->simcalls, simcall);
710 * \brief Starts the simulation of a communication synchro.
711 * \param synchro the communication synchro
713 static XBT_INLINE void SIMIX_comm_start(smx_synchro_t synchro)
715 /* If both the sender and the receiver are already there, start the communication */
716 if (synchro->state == SIMIX_READY) {
718 sg_host_t sender = synchro->comm.src_proc->host;
719 sg_host_t receiver = synchro->comm.dst_proc->host;
721 XBT_DEBUG("Starting communication %p from '%s' to '%s'", synchro,
722 SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
724 synchro->comm.surf_comm = surf_network_model_communicate(surf_network_model,
726 synchro->comm.task_size, synchro->comm.rate);
728 surf_action_set_data(synchro->comm.surf_comm, synchro);
730 synchro->state = SIMIX_RUNNING;
732 /* If a link is failed, detect it immediately */
733 if (surf_action_get_state(synchro->comm.surf_comm) == SURF_ACTION_FAILED) {
734 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure",
735 SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
736 synchro->state = SIMIX_LINK_FAILURE;
737 SIMIX_comm_destroy_internal_actions(synchro);
740 /* If any of the process is suspend, create the synchro but stop its execution,
741 it will be restarted when the sender process resume */
742 if (SIMIX_process_is_suspended(synchro->comm.src_proc) ||
743 SIMIX_process_is_suspended(synchro->comm.dst_proc)) {
744 /* FIXME: check what should happen with the synchro state */
746 if (SIMIX_process_is_suspended(synchro->comm.src_proc))
747 XBT_DEBUG("The communication is suspended on startup because src (%s:%s) were suspended since it initiated the communication",
748 SIMIX_host_get_name(synchro->comm.src_proc->host), synchro->comm.src_proc->name);
750 XBT_DEBUG("The communication is suspended on startup because dst (%s:%s) were suspended since it initiated the communication",
751 SIMIX_host_get_name(synchro->comm.dst_proc->host), synchro->comm.dst_proc->name);
753 surf_action_suspend(synchro->comm.surf_comm);
760 * \brief Answers the SIMIX simcalls associated to a communication synchro.
761 * \param synchro a finished communication synchro
763 void SIMIX_comm_finish(smx_synchro_t synchro)
765 unsigned int destroy_count = 0;
766 smx_simcall_t simcall;
768 while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
770 /* If a waitany simcall is waiting for this synchro to finish, then remove
771 it from the other synchros in the waitany list. Afterwards, get the
772 position of the actual synchro in the waitany dynar and
773 return it as the result of the simcall */
775 if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
776 continue; // if process handling comm is killed
777 if (simcall->call == SIMCALL_COMM_WAITANY) {
778 SIMIX_waitany_remove_simcall_from_actions(simcall);
779 if (!MC_is_active() && !MC_record_replay_is_active())
780 simcall_comm_waitany__set__result(simcall, xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
783 /* If the synchro is still in a rendez-vous point then remove from it */
784 if (synchro->comm.rdv)
785 SIMIX_rdv_remove(synchro->comm.rdv, synchro);
787 XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state);
789 /* Check out for errors */
791 if (surf_host_get_state(surf_host_resource_priv(
792 simcall->issuer->host)) != SURF_RESOURCE_ON) {
793 simcall->issuer->context->iwannadie = 1;
794 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
797 switch (synchro->state) {
800 XBT_DEBUG("Communication %p complete!", synchro);
801 SIMIX_comm_copy_data(synchro);
804 case SIMIX_SRC_TIMEOUT:
805 SMX_EXCEPTION(simcall->issuer, timeout_error, 0,
806 "Communication timeouted because of sender");
809 case SIMIX_DST_TIMEOUT:
810 SMX_EXCEPTION(simcall->issuer, timeout_error, 0,
811 "Communication timeouted because of receiver");
814 case SIMIX_SRC_HOST_FAILURE:
815 if (simcall->issuer == synchro->comm.src_proc)
816 simcall->issuer->context->iwannadie = 1;
817 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
819 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Remote peer failed");
822 case SIMIX_DST_HOST_FAILURE:
823 if (simcall->issuer == synchro->comm.dst_proc)
824 simcall->issuer->context->iwannadie = 1;
825 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
827 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Remote peer failed");
830 case SIMIX_LINK_FAILURE:
832 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) detached:%d",
834 synchro->comm.src_proc ? sg_host_name(synchro->comm.src_proc->host) : NULL,
835 synchro->comm.dst_proc ? sg_host_name(synchro->comm.dst_proc->host) : NULL,
836 simcall->issuer->name, simcall->issuer, synchro->comm.detached);
837 if (synchro->comm.src_proc == simcall->issuer) {
838 XBT_DEBUG("I'm source");
839 } else if (synchro->comm.dst_proc == simcall->issuer) {
840 XBT_DEBUG("I'm dest");
842 XBT_DEBUG("I'm neither source nor dest");
844 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Link failure");
848 if (simcall->issuer == synchro->comm.dst_proc)
849 SMX_EXCEPTION(simcall->issuer, cancel_error, 0,
850 "Communication canceled by the sender");
852 SMX_EXCEPTION(simcall->issuer, cancel_error, 0,
853 "Communication canceled by the receiver");
857 xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state);
860 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
861 if (simcall->issuer->doexception) {
862 if (simcall->call == SIMCALL_COMM_WAITANY) {
863 simcall->issuer->running_ctx->exception.value = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
865 else if (simcall->call == SIMCALL_COMM_TESTANY) {
866 simcall->issuer->running_ctx->exception.value = xbt_dynar_search(simcall_comm_testany__get__comms(simcall), &synchro);
870 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
871 simcall->issuer->context->iwannadie = 1;
874 simcall->issuer->waiting_synchro = NULL;
875 xbt_fifo_remove(simcall->issuer->comms, synchro);
876 if(synchro->comm.detached){
877 if(simcall->issuer == synchro->comm.src_proc){
878 if(synchro->comm.dst_proc)
879 xbt_fifo_remove(synchro->comm.dst_proc->comms, synchro);
881 if(simcall->issuer == synchro->comm.dst_proc){
882 if(synchro->comm.src_proc)
883 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
886 SIMIX_simcall_answer(simcall);
890 while (destroy_count-- > 0)
891 SIMIX_comm_destroy(synchro);
895 * \brief This function is called when a Surf communication synchro is finished.
896 * \param synchro the corresponding Simix communication
898 void SIMIX_post_comm(smx_synchro_t synchro)
900 /* Update synchro state */
901 if (synchro->comm.src_timeout &&
902 surf_action_get_state(synchro->comm.src_timeout) == SURF_ACTION_DONE)
903 synchro->state = SIMIX_SRC_TIMEOUT;
904 else if (synchro->comm.dst_timeout &&
905 surf_action_get_state(synchro->comm.dst_timeout) == SURF_ACTION_DONE)
906 synchro->state = SIMIX_DST_TIMEOUT;
907 else if (synchro->comm.src_timeout &&
908 surf_action_get_state(synchro->comm.src_timeout) == SURF_ACTION_FAILED)
909 synchro->state = SIMIX_SRC_HOST_FAILURE;
910 else if (synchro->comm.dst_timeout &&
911 surf_action_get_state(synchro->comm.dst_timeout) == SURF_ACTION_FAILED)
912 synchro->state = SIMIX_DST_HOST_FAILURE;
913 else if (synchro->comm.surf_comm &&
914 surf_action_get_state(synchro->comm.surf_comm) == SURF_ACTION_FAILED) {
915 XBT_DEBUG("Puta madre. Surf says that the link broke");
916 synchro->state = SIMIX_LINK_FAILURE;
918 synchro->state = SIMIX_DONE;
920 XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
921 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc, synchro->comm.detached);
923 /* destroy the surf actions associated with the Simix communication */
924 SIMIX_comm_destroy_internal_actions(synchro);
926 /* if there are simcalls associated with the synchro, then answer them */
927 if (xbt_fifo_size(synchro->simcalls)) {
928 SIMIX_comm_finish(synchro);
932 void SIMIX_comm_cancel(smx_synchro_t synchro)
934 /* if the synchro is a waiting state means that it is still in a rdv */
935 /* so remove from it and delete it */
936 if (synchro->state == SIMIX_WAITING) {
937 SIMIX_rdv_remove(synchro->comm.rdv, synchro);
938 synchro->state = SIMIX_CANCELED;
940 else if (!MC_is_active() /* when running the MC there are no surf actions */
941 && !MC_record_replay_is_active()
942 && (synchro->state == SIMIX_READY || synchro->state == SIMIX_RUNNING)) {
944 surf_action_cancel(synchro->comm.surf_comm);
948 void SIMIX_comm_suspend(smx_synchro_t synchro)
950 /*FIXME: shall we suspend also the timeout synchro? */
951 if (synchro->comm.surf_comm)
952 surf_action_suspend(synchro->comm.surf_comm);
953 /* in the other case, the action will be suspended on creation, in SIMIX_comm_start() */
956 void SIMIX_comm_resume(smx_synchro_t synchro)
958 /*FIXME: check what happen with the timeouts */
959 if (synchro->comm.surf_comm)
960 surf_action_resume(synchro->comm.surf_comm);
961 /* in the other case, the synchro were not really suspended yet, see SIMIX_comm_suspend() and SIMIX_comm_start() */
965 /************* synchro Getters **************/
968 * \brief get the amount remaining from the communication
969 * \param synchro The communication
971 double SIMIX_comm_get_remains(smx_synchro_t synchro)
979 switch (synchro->state) {
982 remains = surf_action_get_remains(synchro->comm.surf_comm);
987 remains = 0; /*FIXME: check what should be returned */
991 remains = 0; /*FIXME: is this correct? */
997 e_smx_state_t SIMIX_comm_get_state(smx_synchro_t synchro)
999 return synchro->state;
1003 * \brief Return the user data associated to the sender of the communication
1004 * \param synchro The communication
1005 * \return the user data
1007 void* SIMIX_comm_get_src_data(smx_synchro_t synchro)
1009 return synchro->comm.src_data;
1013 * \brief Return the user data associated to the receiver of the communication
1014 * \param synchro The communication
1015 * \return the user data
1017 void* SIMIX_comm_get_dst_data(smx_synchro_t synchro)
1019 return synchro->comm.dst_data;
1022 smx_process_t SIMIX_comm_get_src_proc(smx_synchro_t synchro)
1024 return synchro->comm.src_proc;
1027 smx_process_t SIMIX_comm_get_dst_proc(smx_synchro_t synchro)
1029 return synchro->comm.dst_proc;
1032 #ifdef HAVE_LATENCY_BOUND_TRACKING
1034 * \brief verify if communication is latency bounded
1035 * \param comm The communication
1037 int SIMIX_comm_is_latency_bounded(smx_synchro_t synchro)
1042 if (synchro->comm.surf_comm){
1043 XBT_DEBUG("Getting latency limited for surf_action (%p)", synchro->comm.surf_comm);
1044 synchro->latency_limited = surf_network_action_get_latency_limited(synchro->comm.surf_comm);
1045 XBT_DEBUG("synchro limited is %d", synchro->latency_limited);
1047 return synchro->latency_limited;
1051 /******************************************************************************/
1052 /* SIMIX_comm_copy_data callbacks */
1053 /******************************************************************************/
1054 static void (*SIMIX_comm_copy_data_callback) (smx_synchro_t, void*, size_t) =
1055 &SIMIX_comm_copy_pointer_callback;
1058 SIMIX_comm_set_copy_data_callback(void (*callback) (smx_synchro_t, void*, size_t))
1060 SIMIX_comm_copy_data_callback = callback;
1063 void SIMIX_comm_copy_pointer_callback(smx_synchro_t comm, void* buff, size_t buff_size)
1065 xbt_assert((buff_size == sizeof(void *)),
1066 "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
1067 *(void **) (comm->comm.dst_buff) = buff;
1070 void SIMIX_comm_copy_buffer_callback(smx_synchro_t comm, void* buff, size_t buff_size)
1072 XBT_DEBUG("Copy the data over");
1073 memcpy(comm->comm.dst_buff, buff, buff_size);
1074 if (comm->comm.detached) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the original buffer available to the application ASAP
1076 comm->comm.src_buff = NULL;
1082 * \brief Copy the communication data from the sender's buffer to the receiver's one
1083 * \param comm The communication
1085 void SIMIX_comm_copy_data(smx_synchro_t comm)
1087 size_t buff_size = comm->comm.src_buff_size;
1088 /* If there is no data to be copy then return */
1089 if (!comm->comm.src_buff || !comm->comm.dst_buff || comm->comm.copied)
1092 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
1094 comm->comm.src_proc ? sg_host_name(comm->comm.src_proc->host) : "a finished process",
1095 comm->comm.src_buff,
1096 comm->comm.dst_proc ? sg_host_name(comm->comm.dst_proc->host) : "a finished process",
1097 comm->comm.dst_buff, buff_size);
1099 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
1100 if (comm->comm.dst_buff_size)
1101 buff_size = MIN(buff_size, *(comm->comm.dst_buff_size));
1103 /* Update the receiver's buffer size to the copied amount */
1104 if (comm->comm.dst_buff_size)
1105 *comm->comm.dst_buff_size = buff_size;
1108 if(comm->comm.copy_data_fun)
1109 comm->comm.copy_data_fun (comm, comm->comm.src_buff, buff_size);
1111 SIMIX_comm_copy_data_callback (comm, comm->comm.src_buff, buff_size);
1115 /* Set the copied flag so we copy data only once */
1116 /* (this function might be called from both communication ends) */
1117 comm->comm.copied = 1;