3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_sender, smpi, "Logging specific to SMPI (sender)");
5 int smpi_sender(int argc, char **argv)
12 xbt_fifo_t request_queue;
13 smx_mutex_t request_queue_mutex;
15 int running_hosts_count;
17 smpi_mpi_request_t request;
23 smpi_received_message_t message;
27 smx_process_t receiver_process;
29 self = SIMIX_process_self();
30 shost = SIMIX_host_self();
32 // make sure root is done before own initialization
33 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
34 if (!smpi_global->root_ready) {
35 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
37 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
39 index = smpi_host_index();
41 request_queue = smpi_global->pending_send_request_queues[index];
42 request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[index];
44 smpi_global->sender_processes[index] = self;
46 // wait for all nodes to signal initializatin complete
47 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
48 smpi_global->ready_process_count++;
49 if (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
50 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
52 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
54 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
58 SIMIX_mutex_lock(request_queue_mutex);
59 request = xbt_fifo_shift(request_queue);
60 SIMIX_mutex_unlock(request_queue_mutex);
62 if (NULL == request) {
63 SIMIX_process_suspend(self);
66 message = xbt_mallocator_get(smpi_global->message_mallocator);
68 SIMIX_mutex_lock(request->mutex);
70 message->comm = request->comm;
71 message->src = request->comm->index_to_rank_map[index];
72 message->tag = request->tag;
73 message->data = request->data;
74 message->buf = xbt_malloc(request->datatype->size * request->count);
75 memcpy(message->buf, request->buf, request->datatype->size * request->count);
77 dindex = request->comm->rank_to_index_map[request->dst];
78 dhost = smpi_global->hosts[dindex];
80 message->forward = (request->forward - 1) / 2;
81 request->forward = request->forward / 2;
83 if (0 < request->forward) {
84 request->dst = (request->dst + message->forward + 1) % request->comm->size;
85 SIMIX_mutex_lock(request_queue_mutex);
86 xbt_fifo_push(request_queue, request);
87 SIMIX_mutex_unlock(request_queue_mutex);
89 request->completed = 1;
92 action = SIMIX_action_communicate(shost, dhost, "communication", request->datatype->size * request->count, -1.0);
94 SIMIX_register_action_to_condition(action, request->cond);
95 SIMIX_cond_wait(request->cond, request->mutex);
97 SIMIX_unregister_action_to_condition(action, request->cond);
98 SIMIX_action_destroy(action);
100 SIMIX_mutex_unlock(request->mutex);
102 SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[dindex]);
103 xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
104 SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[dindex]);
106 // wake up receiver if necessary
107 receiver_process = smpi_global->receiver_processes[dindex];
108 if (SIMIX_process_is_suspended(receiver_process)) {
109 SIMIX_process_resume(receiver_process);
114 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
115 running_hosts_count = smpi_global->running_hosts_count;
116 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
118 } while (0 < running_hosts_count);