3 SMPI_MPI_Global_t smpi_mpi_global = NULL;
5 void smpi_mpi_land_func(void *x, void *y, void *z)
7 *(int *)z = *(int *)x && *(int *)y;
10 void smpi_mpi_sum_func(void *x, void *y, void *z)
12 *(int *)z = *(int *)x + *(int *)y;
15 int inline smpi_mpi_comm_size(smpi_mpi_communicator_t comm)
20 // FIXME: smarter algorithm?
21 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm, smx_host_t host)
25 for(i = comm->size - 1; i > 0 && host != comm->hosts[i]; i--);
30 int inline smpi_mpi_comm_rank_self(smpi_mpi_communicator_t comm)
32 return smpi_mpi_comm_rank(comm, SIMIX_host_self());
37 smx_process_t process;
42 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
43 smpi_global->running_hosts_count++;
44 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
46 // initialize some local variables
47 process = SIMIX_process_self();
48 host = SIMIX_host_self();
49 hosts = SIMIX_host_get_table();
50 size = SIMIX_host_get_number();
52 // node 0 sets the globals
53 if (host == hosts[0]) {
55 smpi_mpi_global = xbt_new(s_SMPI_MPI_Global_t, 1);
57 // global communicator
58 smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
59 smpi_mpi_global->mpi_comm_world->size = size;
60 smpi_mpi_global->mpi_comm_world->hosts = hosts;
61 smpi_mpi_global->mpi_comm_world->processes = xbt_new(smx_process_t, size);
62 smpi_mpi_global->mpi_comm_world->processes[0] = process;
63 smpi_mpi_global->mpi_comm_world->barrier_count = 0;
64 smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
65 smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
68 smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1);
69 smpi_mpi_global->mpi_byte->size = (size_t)1;
70 smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
71 smpi_mpi_global->mpi_int->size = sizeof(int);
72 smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
73 smpi_mpi_global->mpi_double->size = sizeof(double);
76 smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
77 smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
78 smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
79 smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
81 // signal all nodes to perform initialization
82 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
83 smpi_global->root_ready = 1;
84 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
85 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
89 // make sure root is done before own initialization
90 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
91 if (!smpi_global->root_ready) {
92 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
94 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
96 smpi_mpi_global->mpi_comm_world->processes[smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world)] = process;
99 // wait for all nodes to signal initializatin complete
100 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
101 smpi_global->ready_process_count++;
102 if (smpi_global->ready_process_count < 3 * size) {
103 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
105 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
107 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
112 void smpi_mpi_finalize()
115 int rank = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
117 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
118 i = --smpi_global->running_hosts_count;
119 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
121 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
122 smpi_global->ready_process_count--;
123 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
127 // wake up senders/receivers
128 for (i = 0; i < smpi_mpi_global->mpi_comm_world->size; i++) {
129 if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
130 SIMIX_process_resume(smpi_global->sender_processes[i]);
132 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
133 SIMIX_process_resume(smpi_global->receiver_processes[i]);
137 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
138 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
139 xbt_free(smpi_mpi_global->mpi_comm_world->processes);
140 xbt_free(smpi_mpi_global->mpi_comm_world);
142 xbt_free(smpi_mpi_global->mpi_byte);
143 xbt_free(smpi_mpi_global->mpi_int);
144 xbt_free(smpi_mpi_global->mpi_double);
146 xbt_free(smpi_mpi_global->mpi_land);
147 xbt_free(smpi_mpi_global->mpi_sum);
149 xbt_free(smpi_mpi_global);
155 void smpi_barrier(smpi_mpi_communicator_t comm)
158 SIMIX_mutex_lock(comm->barrier_mutex);
159 if(++comm->barrier_count < comm->size) {
160 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
162 comm->barrier_count = 0;
163 SIMIX_cond_broadcast(comm->barrier_cond);
165 SIMIX_mutex_unlock(comm->barrier_mutex);
170 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t datatype,
171 int src, int dst, int tag, smpi_mpi_communicator_t comm, smpi_mpi_request_t *request)
173 int retval = MPI_SUCCESS;
178 retval = MPI_ERR_COUNT;
179 } else if (NULL == buf) {
180 retval = MPI_ERR_INTERN;
181 } else if (NULL == datatype) {
182 retval = MPI_ERR_TYPE;
183 } else if (NULL == comm) {
184 retval = MPI_ERR_COMM;
185 } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
186 retval = MPI_ERR_RANK;
187 } else if (0 > dst || comm->size <= dst) {
188 retval = MPI_ERR_RANK;
189 } else if (0 > tag) {
190 retval = MPI_ERR_TAG;
192 *request = xbt_mallocator_get(smpi_global->request_mallocator);
193 (*request)->comm = comm;
194 (*request)->src = src;
195 (*request)->dst = dst;
196 (*request)->tag = tag;
197 (*request)->buf = buf;
198 (*request)->count = count;
199 (*request)->datatype = datatype;
204 int smpi_isend(smpi_mpi_request_t request)
206 int retval = MPI_SUCCESS;
207 int rank = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
209 if (NULL != request) {
210 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[rank]);
211 xbt_fifo_push(smpi_global->pending_send_request_queues[rank], request);
212 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[rank]);
215 if (SIMIX_process_is_suspended(smpi_global->sender_processes[rank])) {
216 SIMIX_process_resume(smpi_global->sender_processes[rank]);
222 int smpi_irecv(smpi_mpi_request_t request)
224 int retval = MPI_SUCCESS;
225 int rank = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
227 if (NULL != request) {
228 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[rank]);
229 xbt_fifo_push(smpi_global->pending_recv_request_queues[rank], request);
230 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[rank]);
233 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[rank])) {
234 SIMIX_process_resume(smpi_global->receiver_processes[rank]);
240 int smpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
242 if (NULL != request) {
243 SIMIX_mutex_lock(request->mutex);
244 if (!request->completed) {
245 SIMIX_cond_wait(request->cond, request->mutex);
247 if (NULL != status) {
248 status->MPI_SOURCE = request->src;
250 SIMIX_mutex_unlock(request->mutex);