3 smpi_mpi_global_t smpi_mpi_global = NULL;
5 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype);
7 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype)
10 if (*datatype == smpi_mpi_global->mpi_int) {
12 for (i = 0; i < *length; i++) {
18 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype);
20 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype)
23 if (*datatype == smpi_mpi_global->mpi_int) {
25 for (i = 0; i < *length; i++) {
31 int inline smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
33 return comm->index_to_rank_map[smpi_host_index()];
42 smpi_host_data_t hdata;
44 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
45 smpi_global->running_hosts_count++;
46 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
48 // initialize some local variables
49 host = SIMIX_host_self();
50 hosts = SIMIX_host_get_table();
51 host_count = SIMIX_host_get_number();
53 hdata = xbt_new(s_smpi_host_data_t, 1);
55 for (i = 0; i < host_count && host != hosts[i]; i ++);
59 SIMIX_host_set_data(host, hdata);
61 // node 0 sets the globals
64 smpi_global->hosts = hosts;
65 smpi_global->host_count = host_count;
67 smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
69 // global communicator
70 smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
71 smpi_mpi_global->mpi_comm_world->size = host_count;
72 smpi_mpi_global->mpi_comm_world->barrier_count = 0;
73 smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
74 smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
75 smpi_mpi_global->mpi_comm_world->rank_to_index_map = xbt_new(int, host_count);
76 smpi_mpi_global->mpi_comm_world->index_to_rank_map = xbt_new(int, host_count);
77 for (i = 0; i < host_count; i++) {
78 smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
79 smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
83 smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1);
84 smpi_mpi_global->mpi_byte->size = (size_t)1;
85 smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
86 smpi_mpi_global->mpi_int->size = sizeof(int);
87 smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
88 smpi_mpi_global->mpi_double->size = sizeof(double);
91 smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
92 smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
93 smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
94 smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
96 // signal all nodes to perform initialization
97 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
98 smpi_global->root_ready = 1;
99 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
100 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
104 // make sure root is done before own initialization
105 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
106 if (!smpi_global->root_ready) {
107 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
109 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
113 // wait for all nodes to signal initializatin complete
114 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
115 smpi_global->ready_process_count++;
116 if (smpi_global->ready_process_count < 3 * host_count) {
117 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
119 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
121 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
126 void smpi_mpi_finalize()
130 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
131 i = --smpi_global->running_hosts_count;
132 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
134 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
135 smpi_global->ready_process_count--;
136 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
140 // wake up senders/receivers
141 for (i = 0; i < smpi_global->host_count; i++) {
142 if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
143 SIMIX_process_resume(smpi_global->sender_processes[i]);
145 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
146 SIMIX_process_resume(smpi_global->receiver_processes[i]);
150 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
151 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
152 xbt_free(smpi_mpi_global->mpi_comm_world);
154 xbt_free(smpi_mpi_global->mpi_byte);
155 xbt_free(smpi_mpi_global->mpi_int);
156 xbt_free(smpi_mpi_global->mpi_double);
158 xbt_free(smpi_mpi_global->mpi_land);
159 xbt_free(smpi_mpi_global->mpi_sum);
161 xbt_free(smpi_mpi_global);
167 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
170 SIMIX_mutex_lock(comm->barrier_mutex);
171 if(++comm->barrier_count < comm->size) {
172 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
174 comm->barrier_count = 0;
175 SIMIX_cond_broadcast(comm->barrier_cond);
177 SIMIX_mutex_unlock(comm->barrier_mutex);
182 int smpi_mpi_isend(smpi_mpi_request_t request)
184 int retval = MPI_SUCCESS;
185 int index = smpi_host_index();
187 if (NULL == request) {
188 retval = MPI_ERR_INTERN;
190 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
191 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
192 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
194 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
195 SIMIX_process_resume(smpi_global->sender_processes[index]);
202 int smpi_mpi_irecv(smpi_mpi_request_t request)
204 int retval = MPI_SUCCESS;
205 int index = smpi_host_index();
207 if (NULL == request) {
208 retval = MPI_ERR_INTERN;
210 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
211 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
212 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
214 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
215 SIMIX_process_resume(smpi_global->receiver_processes[index]);
222 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
224 int retval = MPI_SUCCESS;
226 if (NULL == request) {
227 retval = MPI_ERR_INTERN;
229 SIMIX_mutex_lock(request->mutex);
230 if (!request->completed) {
231 SIMIX_cond_wait(request->cond, request->mutex);
233 if (NULL != status) {
234 status->MPI_SOURCE = request->src;
235 status->MPI_TAG = request->tag;
236 status->MPI_ERROR = MPI_SUCCESS;
238 SIMIX_mutex_unlock(request->mutex);