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 ++);
60 hdata->mutex = SIMIX_mutex_init();
61 hdata->cond = SIMIX_cond_init();
63 SIMIX_host_set_data(host, hdata);
65 // node 0 sets the globals
68 smpi_global->hosts = hosts;
69 smpi_global->host_count = host_count;
71 smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
73 // global communicator
74 smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
75 smpi_mpi_global->mpi_comm_world->size = host_count;
76 smpi_mpi_global->mpi_comm_world->barrier_count = 0;
77 smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
78 smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
79 smpi_mpi_global->mpi_comm_world->rank_to_index_map = xbt_new(int, host_count);
80 smpi_mpi_global->mpi_comm_world->index_to_rank_map = xbt_new(int, host_count);
81 for (i = 0; i < host_count; i++) {
82 smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
83 smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
87 smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1);
88 smpi_mpi_global->mpi_byte->size = (size_t)1;
89 smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
90 smpi_mpi_global->mpi_int->size = sizeof(int);
91 smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
92 smpi_mpi_global->mpi_double->size = sizeof(double);
95 smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
96 smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
97 smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
98 smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
100 // signal all nodes to perform initialization
101 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
102 smpi_global->root_ready = 1;
103 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
104 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
108 // make sure root is done before own initialization
109 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
110 while (!smpi_global->root_ready) {
111 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
113 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
117 // wait for all nodes to signal initializatin complete
118 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
119 smpi_global->ready_process_count++;
120 if (smpi_global->ready_process_count >= 3 * host_count) {
121 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
123 while (smpi_global->ready_process_count < 3 * host_count) {
124 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
126 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
131 void smpi_mpi_finalize()
135 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
136 i = --smpi_global->running_hosts_count;
137 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
139 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
140 smpi_global->ready_process_count--;
141 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
143 SIMIX_mutex_destroy(smpi_host_mutex());
144 SIMIX_cond_destroy(smpi_host_cond());
148 // wake up senders/receivers
149 for (i = 0; i < smpi_global->host_count; i++) {
150 if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
151 SIMIX_process_resume(smpi_global->sender_processes[i]);
153 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
154 SIMIX_process_resume(smpi_global->receiver_processes[i]);
158 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
159 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
160 xbt_free(smpi_mpi_global->mpi_comm_world);
162 xbt_free(smpi_mpi_global->mpi_byte);
163 xbt_free(smpi_mpi_global->mpi_int);
164 xbt_free(smpi_mpi_global->mpi_double);
166 xbt_free(smpi_mpi_global->mpi_land);
167 xbt_free(smpi_mpi_global->mpi_sum);
169 xbt_free(smpi_mpi_global);
175 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
178 SIMIX_mutex_lock(comm->barrier_mutex);
179 if (++comm->barrier_count >= comm->size) {
180 SIMIX_cond_broadcast(comm->barrier_cond);
182 while (comm->barrier_count < comm->size) {
183 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
185 SIMIX_mutex_unlock(comm->barrier_mutex);
190 int smpi_mpi_isend(smpi_mpi_request_t request)
192 int retval = MPI_SUCCESS;
193 int index = smpi_host_index();
195 if (NULL == request) {
196 retval = MPI_ERR_INTERN;
198 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
199 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
200 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
202 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
203 SIMIX_process_resume(smpi_global->sender_processes[index]);
210 int smpi_mpi_irecv(smpi_mpi_request_t request)
212 int retval = MPI_SUCCESS;
213 int index = smpi_host_index();
215 if (NULL == request) {
216 retval = MPI_ERR_INTERN;
218 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
219 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
220 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
222 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
223 SIMIX_process_resume(smpi_global->receiver_processes[index]);
230 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
232 int retval = MPI_SUCCESS;
234 if (NULL == request) {
235 retval = MPI_ERR_INTERN;
237 SIMIX_mutex_lock(request->mutex);
238 while (!request->completed) {
239 SIMIX_cond_wait(request->cond, request->mutex);
241 if (NULL != status) {
242 status->MPI_SOURCE = request->src;
243 status->MPI_TAG = request->tag;
244 status->MPI_ERROR = MPI_SUCCESS;
246 SIMIX_mutex_unlock(request->mutex);