3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_sender, smpi,
4 "Logging specific to SMPI (sender)");
6 int smpi_sender(int argc, char **argv)
13 xbt_fifo_t request_queue;
14 smx_mutex_t request_queue_mutex;
16 int running_hosts_count;
18 smpi_mpi_request_t request;
24 e_surf_action_state_t state;
26 smpi_received_message_t message;
30 smx_process_t receiver_process;
32 self = SIMIX_process_self();
33 shost = SIMIX_host_self();
35 // make sure root is done before own initialization
36 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
37 while (!smpi_global->root_ready) {
38 SIMIX_cond_wait(smpi_global->start_stop_cond,
39 smpi_global->start_stop_mutex);
41 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
43 index = smpi_host_index();
45 request_queue = smpi_global->pending_send_request_queues[index];
47 smpi_global->pending_send_request_queues_mutexes[index];
49 smpi_global->sender_processes[index] = self;
51 // wait for all nodes to signal initializatin complete
52 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
53 smpi_global->ready_process_count++;
54 if (smpi_global->ready_process_count >= 3 * smpi_global->host_count) {
55 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
57 while (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
58 SIMIX_cond_wait(smpi_global->start_stop_cond,
59 smpi_global->start_stop_mutex);
61 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
65 SIMIX_mutex_lock(request_queue_mutex);
66 request = xbt_fifo_shift(request_queue);
67 SIMIX_mutex_unlock(request_queue_mutex);
69 if (NULL == request) {
70 SIMIX_process_suspend(self);
73 message = xbt_mallocator_get(smpi_global->message_mallocator);
75 SIMIX_mutex_lock(request->mutex);
77 message->comm = request->comm;
78 message->src = request->comm->index_to_rank_map[index];
79 message->tag = request->tag;
80 message->data = request->data;
81 message->buf = xbt_malloc(request->datatype->size * request->count);
82 memcpy(message->buf, request->buf,
83 request->datatype->size * request->count);
85 dindex = request->comm->rank_to_index_map[request->dst];
86 dhost = smpi_global->hosts[dindex];
88 message->forward = (request->forward - 1) / 2;
89 request->forward = request->forward / 2;
91 if (0 < request->forward) {
93 (request->dst + message->forward + 1) % request->comm->size;
94 SIMIX_mutex_lock(request_queue_mutex);
95 xbt_fifo_push(request_queue, request);
96 SIMIX_mutex_unlock(request_queue_mutex);
98 request->completed = 1;
102 SIMIX_action_communicate(shost, dhost, "communication",
103 request->datatype->size * request->count,
106 SIMIX_register_action_to_condition(action, request->cond);
108 for (state = SIMIX_action_get_state(action);
109 state == SURF_ACTION_READY ||
110 state == SURF_ACTION_RUNNING;
111 state = SIMIX_action_get_state(action)
113 SIMIX_cond_wait(request->cond, request->mutex);
116 SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[dindex]);
117 xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
118 SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes
121 SIMIX_unregister_action_to_condition(action, request->cond);
122 SIMIX_action_destroy(action);
124 SIMIX_mutex_unlock(request->mutex);
126 // wake up receiver if necessary
127 receiver_process = smpi_global->receiver_processes[dindex];
128 if (SIMIX_process_is_suspended(receiver_process)) {
129 SIMIX_process_resume(receiver_process);
134 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
135 running_hosts_count = smpi_global->running_hosts_count;
136 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
138 } while (0 < running_hosts_count);