3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
5 smpi_mpi_global_t smpi_mpi_global = NULL;
7 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype);
9 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype)
12 if (*datatype == smpi_mpi_global->mpi_int) {
14 for (i = 0; i < *length; i++) {
20 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype);
22 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype)
25 if (*datatype == smpi_mpi_global->mpi_int) {
27 for (i = 0; i < *length; i++) {
33 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
35 return comm->index_to_rank_map[smpi_host_index()];
44 smpi_host_data_t hdata;
46 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
47 smpi_global->running_hosts_count++;
48 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
50 // initialize some local variables
51 host = SIMIX_host_self();
52 hosts = SIMIX_host_get_table();
53 host_count = SIMIX_host_get_number();
55 hdata = xbt_new(s_smpi_host_data_t, 1);
57 for (i = 0; i < host_count && host != hosts[i]; i ++);
61 SIMIX_host_set_data(host, hdata);
63 // node 0 sets the globals
66 smpi_global->hosts = hosts;
67 smpi_global->host_count = host_count;
69 smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
71 // global communicator
72 smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
73 smpi_mpi_global->mpi_comm_world->size = host_count;
74 smpi_mpi_global->mpi_comm_world->barrier_count = 0;
75 smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
76 smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
77 smpi_mpi_global->mpi_comm_world->rank_to_index_map = xbt_new(int, host_count);
78 smpi_mpi_global->mpi_comm_world->index_to_rank_map = xbt_new(int, host_count);
79 for (i = 0; i < host_count; i++) {
80 smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
81 smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
85 smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1);
86 smpi_mpi_global->mpi_byte->size = (size_t)1;
87 smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
88 smpi_mpi_global->mpi_int->size = sizeof(int);
89 smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
90 smpi_mpi_global->mpi_double->size = sizeof(double);
93 smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
94 smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
95 smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
96 smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
98 // signal all nodes to perform initialization
99 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
100 smpi_global->root_ready = 1;
101 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
102 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
106 // make sure root is done before own initialization
107 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
108 while (!smpi_global->root_ready) {
109 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
111 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
115 // wait for all nodes to signal initializatin complete
116 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
117 smpi_global->ready_process_count++;
118 if (smpi_global->ready_process_count < 3 * host_count) {
119 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
121 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
123 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
128 void smpi_mpi_finalize()
132 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
133 i = --smpi_global->running_hosts_count;
134 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
136 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
137 smpi_global->ready_process_count--;
138 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
142 // wake up senders/receivers
143 for (i = 0; i < smpi_global->host_count; i++) {
144 if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
145 SIMIX_process_resume(smpi_global->sender_processes[i]);
147 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
148 SIMIX_process_resume(smpi_global->receiver_processes[i]);
152 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
153 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
154 xbt_free(smpi_mpi_global->mpi_comm_world);
156 xbt_free(smpi_mpi_global->mpi_byte);
157 xbt_free(smpi_mpi_global->mpi_int);
158 xbt_free(smpi_mpi_global->mpi_double);
160 xbt_free(smpi_mpi_global->mpi_land);
161 xbt_free(smpi_mpi_global->mpi_sum);
163 xbt_free(smpi_mpi_global);
169 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
172 SIMIX_mutex_lock(comm->barrier_mutex);
173 if(++comm->barrier_count < comm->size) {
174 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
176 comm->barrier_count = 0;
177 SIMIX_cond_broadcast(comm->barrier_cond);
179 SIMIX_mutex_unlock(comm->barrier_mutex);
184 int smpi_mpi_isend(smpi_mpi_request_t request)
186 int retval = MPI_SUCCESS;
187 int index = smpi_host_index();
189 if (NULL == request) {
190 retval = MPI_ERR_INTERN;
192 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
193 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
194 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
196 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
197 SIMIX_process_resume(smpi_global->sender_processes[index]);
204 int smpi_mpi_irecv(smpi_mpi_request_t request)
206 int retval = MPI_SUCCESS;
207 int index = smpi_host_index();
209 if (NULL == request) {
210 retval = MPI_ERR_INTERN;
212 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
213 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
214 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
216 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
217 SIMIX_process_resume(smpi_global->receiver_processes[index]);
224 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
226 int retval = MPI_SUCCESS;
228 if (NULL == request) {
229 retval = MPI_ERR_INTERN;
231 SIMIX_mutex_lock(request->mutex);
232 while (!request->completed) {
233 SIMIX_cond_wait(request->cond, request->mutex);
235 if (NULL != status) {
236 status->MPI_SOURCE = request->src;
237 status->MPI_TAG = request->tag;
238 status->MPI_ERROR = MPI_SUCCESS;
240 SIMIX_mutex_unlock(request->mutex);