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 e_surf_action_state_t state;
25 smpi_received_message_t message;
29 smx_process_t receiver_process;
31 self = SIMIX_process_self();
32 shost = SIMIX_host_self();
34 // make sure root is done before own initialization
35 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
36 while (!smpi_global->root_ready) {
37 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
39 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
41 index = smpi_host_index();
43 request_queue = smpi_global->pending_send_request_queues[index];
44 request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[index];
46 smpi_global->sender_processes[index] = self;
48 // wait for all nodes to signal initializatin complete
49 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
50 smpi_global->ready_process_count++;
51 if (smpi_global->ready_process_count >= 3 * smpi_global->host_count) {
52 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
54 while (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
55 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
57 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
61 SIMIX_mutex_lock(request_queue_mutex);
62 request = xbt_fifo_shift(request_queue);
63 SIMIX_mutex_unlock(request_queue_mutex);
65 if (NULL == request) {
66 SIMIX_process_suspend(self);
69 message = xbt_mallocator_get(smpi_global->message_mallocator);
71 SIMIX_mutex_lock(request->mutex);
73 message->comm = request->comm;
74 message->src = request->comm->index_to_rank_map[index];
75 message->tag = request->tag;
76 message->data = request->data;
77 message->buf = xbt_malloc(request->datatype->size * request->count);
78 memcpy(message->buf, request->buf, request->datatype->size * request->count);
80 dindex = request->comm->rank_to_index_map[request->dst];
81 dhost = smpi_global->hosts[dindex];
83 message->forward = (request->forward - 1) / 2;
84 request->forward = request->forward / 2;
86 if (0 < request->forward) {
87 request->dst = (request->dst + message->forward + 1) % request->comm->size;
88 SIMIX_mutex_lock(request_queue_mutex);
89 xbt_fifo_push(request_queue, request);
90 SIMIX_mutex_unlock(request_queue_mutex);
92 request->completed = 1;
95 action = SIMIX_action_communicate(shost, dhost, "communication", request->datatype->size * request->count, -1.0);
97 SIMIX_register_action_to_condition(action, request->cond);
100 state = SIMIX_action_get_state(action);
101 state == SURF_ACTION_READY ||
102 state == SURF_ACTION_RUNNING;
103 state = SIMIX_action_get_state(action)
105 SIMIX_cond_wait(request->cond, request->mutex);
108 SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[dindex]);
109 xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
110 SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[dindex]);
112 SIMIX_unregister_action_to_condition(action, request->cond);
113 SIMIX_action_destroy(action);
115 SIMIX_mutex_unlock(request->mutex);
117 // wake up receiver if necessary
118 receiver_process = smpi_global->receiver_processes[dindex];
119 if (SIMIX_process_is_suspended(receiver_process)) {
120 SIMIX_process_resume(receiver_process);
125 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
126 running_hosts_count = smpi_global->running_hosts_count;
127 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
129 } while (0 < running_hosts_count);