Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removing references to mpi_comm_world from internal code
[simgrid.git] / src / smpi / smpi_base.c
1 #include "private.h"
2
3 smpi_mpi_global_t smpi_mpi_global = NULL;
4
5 void smpi_mpi_land_func(void *x, void *y, void *z);
6
7 void smpi_mpi_land_func(void *x, void *y, void *z)
8 {
9         *(int *)z = *(int *)x && *(int *)y;
10 }
11
12 void smpi_mpi_sum_func(void *x, void *y, void *z);
13
14 void smpi_mpi_sum_func(void *x, void *y, void *z)
15 {
16         *(int *)z = *(int *)x + *(int *)y;
17 }
18
19 int inline smpi_mpi_comm_size(smpi_mpi_communicator_t comm, int *size)
20 {
21         int retval = MPI_SUCCESS;
22         if (NULL == size) {
23                 retval = MPI_ERR_ARG;
24         } else {
25                 *size = comm->size;
26         }
27         return retval;
28 }
29
30 // FIXME: smarter algorithm?
31 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm, int *rank)
32 {
33         int i;
34         int retval = MPI_SUCCESS;
35
36         if (NULL == rank) {
37                 retval = MPI_ERR_ARG;
38         } else {
39                 smx_host_t host = SIMIX_host_self();
40
41                 for (i = 0; i < comm->size && host != smpi_global->hosts[comm->rank_to_index_map[i]]; i++);
42
43                 *rank = i;
44         }
45
46         return retval;
47 }
48
49 void smpi_mpi_init()
50 {
51         smx_host_t host;
52         smx_host_t *hosts;
53         int host_count;
54         int i;
55
56         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
57         smpi_global->running_hosts_count++;
58         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
59
60         // initialize some local variables
61         host       = SIMIX_host_self();
62         hosts      = SIMIX_host_get_table();
63         host_count = SIMIX_host_get_number();
64
65         // node 0 sets the globals
66         if (host == hosts[0]) {
67
68                 smpi_global->hosts                              = hosts;
69                 smpi_global->host_count                         = host_count;
70
71                 smpi_mpi_global                                 = xbt_new(s_smpi_mpi_global_t, 1);
72
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;
84                 }
85
86                 // mpi datatypes
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);
93
94                 // mpi operations
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;
99
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);
105
106         } else {
107
108                 // make sure root is done before own initialization
109                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
110                 if (!smpi_global->root_ready) {
111                         SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
112                 }
113                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
114
115         }
116
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_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
122         } else {
123                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
124         }
125         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
126
127         return;
128 }
129
130 void smpi_mpi_finalize()
131 {
132         int i;
133
134         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
135         i = --smpi_global->running_hosts_count;
136         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
137
138         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
139         smpi_global->ready_process_count--;
140         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
141
142         if (0 >= i) {
143
144                 // wake up senders/receivers
145                 for (i = 0; i < smpi_global->host_count; i++) {
146                         if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
147                                 SIMIX_process_resume(smpi_global->sender_processes[i]);
148                         }
149                         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
150                                 SIMIX_process_resume(smpi_global->receiver_processes[i]);
151                         }
152                 }
153
154                 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
155                 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
156                 xbt_free(smpi_mpi_global->mpi_comm_world);
157
158                 xbt_free(smpi_mpi_global->mpi_byte);
159                 xbt_free(smpi_mpi_global->mpi_int);
160                 xbt_free(smpi_mpi_global->mpi_double);
161
162                 xbt_free(smpi_mpi_global->mpi_land);
163                 xbt_free(smpi_mpi_global->mpi_sum);
164
165                 xbt_free(smpi_mpi_global);
166
167         }
168
169 }
170
171 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
172 {
173
174         SIMIX_mutex_lock(comm->barrier_mutex);
175         if(++comm->barrier_count < comm->size) {
176                 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
177         } else {
178                 comm->barrier_count = 0;
179                 SIMIX_cond_broadcast(comm->barrier_cond);
180         }
181         SIMIX_mutex_unlock(comm->barrier_mutex);
182
183         return MPI_SUCCESS;
184 }
185
186 int smpi_mpi_isend(smpi_mpi_request_t request)
187 {
188         int retval = MPI_SUCCESS;
189         int index  = smpi_host_index();
190
191         if (NULL == request) {
192                 retval = MPI_ERR_INTERN;
193         } else {
194                 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
195                 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
196                 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
197
198                 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
199                         SIMIX_process_resume(smpi_global->sender_processes[index]);
200                 }
201         }
202
203         return retval;
204 }
205
206 int smpi_mpi_irecv(smpi_mpi_request_t request)
207 {
208         int retval = MPI_SUCCESS;
209         int index = smpi_host_index();
210
211         if (NULL == request) {
212                 retval = MPI_ERR_INTERN;
213         } else {
214                 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
215                 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
216                 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
217
218                 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
219                         SIMIX_process_resume(smpi_global->receiver_processes[index]);
220                 }
221         }
222
223         return retval;
224 }
225
226 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
227 {
228         int retval = MPI_SUCCESS;
229
230         if (NULL == request) {
231                 retval = MPI_ERR_INTERN;
232         } else {
233                 SIMIX_mutex_lock(request->mutex);
234                 if (!request->completed) {
235                         SIMIX_cond_wait(request->cond, request->mutex);
236                 }
237                 if (NULL != status) {
238                         status->MPI_SOURCE = request->src;
239                         status->MPI_TAG    = request->tag;
240                         status->MPI_ERROR  = MPI_SUCCESS;
241                 }
242                 SIMIX_mutex_unlock(request->mutex);
243         }
244
245         return retval;
246 }