Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
006d60afb23d49d295997228ef09e144fb632d65
[simgrid.git] / src / smpi / smpi_base.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
4 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
5 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
11
12 smpi_mpi_global_t smpi_mpi_global = NULL;
13
14 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype);
15
16 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype)
17 {
18         int i;
19         if (*datatype == smpi_mpi_global->mpi_int) {
20                 int *x = a, *y = b;
21                 for (i = 0; i < *length; i++) {
22                         y[i] = x[i] && y[i];
23                 }
24         }
25 }
26
27 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype);
28
29 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype)
30 {
31         int i;
32         if (*datatype == smpi_mpi_global->mpi_int) {
33                 int *x = a, *y = b;
34                 for (i = 0; i < *length; i++) {
35                         y[i] = x[i] + y[i];
36                 }
37         }
38 }
39
40 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
41 {
42         return comm->index_to_rank_map[smpi_host_index()];
43 }
44
45 void smpi_mpi_init()
46 {
47         smx_host_t host;
48         smx_host_t *hosts;
49         int host_count;
50         int i;
51         smpi_host_data_t hdata;
52
53    /* Connect our log channels: that must be done manually under windows */
54    /* (should be done only once, not for each process) */
55    XBT_LOG_CONNECT(smpi_base, smpi);
56    XBT_LOG_CONNECT(smpi_bench, smpi);
57    XBT_LOG_CONNECT(smpi_kernel, smpi);
58    XBT_LOG_CONNECT(smpi_mpi, smpi);
59    XBT_LOG_CONNECT(smpi_receiver, smpi);
60    XBT_LOG_CONNECT(smpi_sender, smpi);
61    XBT_LOG_CONNECT(smpi_util, smpi);
62    
63         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
64         smpi_global->running_hosts_count++;
65         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
66
67         // initialize some local variables
68         host       = SIMIX_host_self();
69         hosts      = SIMIX_host_get_table();
70         host_count = SIMIX_host_get_number();
71
72         hdata      = xbt_new(s_smpi_host_data_t, 1);
73
74         for (i = 0; i < host_count && host != hosts[i]; i ++);
75
76         hdata->index = i;
77         hdata->mutex = SIMIX_mutex_init();
78         hdata->cond  = SIMIX_cond_init();
79
80         SIMIX_host_set_data(host, hdata);
81
82         // node 0 sets the globals
83         if (0 == i) {
84
85                 smpi_global->hosts                              = hosts;
86                 smpi_global->host_count                         = host_count;
87
88                 smpi_mpi_global                                 = xbt_new(s_smpi_mpi_global_t, 1);
89
90                 // global communicator
91                 smpi_mpi_global->mpi_comm_world                 = xbt_new(s_smpi_mpi_communicator_t, 1);
92                 smpi_mpi_global->mpi_comm_world->size           = host_count;
93                 smpi_mpi_global->mpi_comm_world->barrier_count  = 0;
94                 smpi_mpi_global->mpi_comm_world->barrier_mutex  = SIMIX_mutex_init();
95                 smpi_mpi_global->mpi_comm_world->barrier_cond   = SIMIX_cond_init();
96                 smpi_mpi_global->mpi_comm_world->rank_to_index_map = xbt_new(int, host_count);
97                 smpi_mpi_global->mpi_comm_world->index_to_rank_map = xbt_new(int, host_count);
98                 for (i = 0; i < host_count; i++) {
99                         smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
100                         smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
101                 }
102
103                 // mpi datatypes
104                 smpi_mpi_global->mpi_byte                       = xbt_new(s_smpi_mpi_datatype_t, 1);
105                 smpi_mpi_global->mpi_byte->size                 = (size_t)1;
106                 smpi_mpi_global->mpi_int                        = xbt_new(s_smpi_mpi_datatype_t, 1);
107                 smpi_mpi_global->mpi_int->size                  = sizeof(int);
108                 smpi_mpi_global->mpi_double                     = xbt_new(s_smpi_mpi_datatype_t, 1);
109                 smpi_mpi_global->mpi_double->size               = sizeof(double);
110
111                 // mpi operations
112                 smpi_mpi_global->mpi_land                       = xbt_new(s_smpi_mpi_op_t, 1);
113                 smpi_mpi_global->mpi_land->func                 = smpi_mpi_land_func;
114                 smpi_mpi_global->mpi_sum                        = xbt_new(s_smpi_mpi_op_t, 1);
115                 smpi_mpi_global->mpi_sum->func                  = smpi_mpi_sum_func;
116
117                 // signal all nodes to perform initialization
118                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
119                 smpi_global->root_ready = 1;
120                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
121                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
122
123         } else {
124
125                 // make sure root is done before own initialization
126                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
127                 while (!smpi_global->root_ready) {
128                         SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
129                 }
130                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
131
132         }
133
134         // wait for all nodes to signal initializatin complete
135         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
136         smpi_global->ready_process_count++;
137         if (smpi_global->ready_process_count >= 3 * host_count) {
138                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
139         }
140         while (smpi_global->ready_process_count < 3 * host_count) {
141                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
142         }
143         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
144
145         return;
146 }
147
148 void smpi_mpi_finalize()
149 {
150         int i;
151
152         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
153         i = --smpi_global->running_hosts_count;
154         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
155
156         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
157         smpi_global->ready_process_count--;
158         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
159
160         SIMIX_mutex_destroy(smpi_host_mutex());
161         SIMIX_cond_destroy(smpi_host_cond());
162
163         if (0 >= i) {
164
165                 // wake up senders/receivers
166                 for (i = 0; i < smpi_global->host_count; i++) {
167                         if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
168                                 SIMIX_process_resume(smpi_global->sender_processes[i]);
169                         }
170                         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
171                                 SIMIX_process_resume(smpi_global->receiver_processes[i]);
172                         }
173                 }
174
175                 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
176                 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
177                 xbt_free(smpi_mpi_global->mpi_comm_world);
178
179                 xbt_free(smpi_mpi_global->mpi_byte);
180                 xbt_free(smpi_mpi_global->mpi_int);
181                 xbt_free(smpi_mpi_global->mpi_double);
182
183                 xbt_free(smpi_mpi_global->mpi_land);
184                 xbt_free(smpi_mpi_global->mpi_sum);
185
186                 xbt_free(smpi_mpi_global);
187
188         }
189
190 }
191
192 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
193 {
194
195         SIMIX_mutex_lock(comm->barrier_mutex);
196         ++comm->barrier_count;
197         if (comm->barrier_count > comm->size) { // only happens on second barrier...
198                 comm->barrier_count = 0;
199         } else if (comm->barrier_count == comm->size) {
200                 SIMIX_cond_broadcast(comm->barrier_cond);
201         }
202         while (comm->barrier_count < comm->size) {
203                 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
204         }
205         SIMIX_mutex_unlock(comm->barrier_mutex);
206
207         return MPI_SUCCESS;
208 }
209
210 int smpi_mpi_isend(smpi_mpi_request_t request)
211 {
212         int retval = MPI_SUCCESS;
213         int index  = smpi_host_index();
214
215         if (NULL == request) {
216                 retval = MPI_ERR_INTERN;
217         } else {
218                 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
219                 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
220                 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
221
222                 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
223                         SIMIX_process_resume(smpi_global->sender_processes[index]);
224                 }
225         }
226
227         return retval;
228 }
229
230 int smpi_mpi_irecv(smpi_mpi_request_t request)
231 {
232         int retval = MPI_SUCCESS;
233         int index = smpi_host_index();
234
235         if (NULL == request) {
236                 retval = MPI_ERR_INTERN;
237         } else {
238                 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
239                 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
240                 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
241
242                 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
243                         SIMIX_process_resume(smpi_global->receiver_processes[index]);
244                 }
245         }
246
247         return retval;
248 }
249
250 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
251 {
252         int retval = MPI_SUCCESS;
253
254         if (NULL == request) {
255                 retval = MPI_ERR_INTERN;
256         } else {
257                 SIMIX_mutex_lock(request->mutex);
258                 while (!request->completed) {
259                         SIMIX_cond_wait(request->cond, request->mutex);
260                 }
261                 if (NULL != status) {
262                         status->MPI_SOURCE = request->src;
263                         status->MPI_TAG    = request->tag;
264                         status->MPI_ERROR  = MPI_SUCCESS;
265                 }
266                 SIMIX_mutex_unlock(request->mutex);
267         }
268
269         return retval;
270 }