Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
moved XBT_LOG_CONNECT calls into smpi_global so that they will only be called
[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         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
54         smpi_global->running_hosts_count++;
55         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
56
57         // initialize some local variables
58         host       = SIMIX_host_self();
59         hosts      = SIMIX_host_get_table();
60         host_count = SIMIX_host_get_number();
61
62         hdata      = xbt_new(s_smpi_host_data_t, 1);
63
64         for (i = 0; i < host_count && host != hosts[i]; i ++);
65
66         hdata->index = i;
67         hdata->mutex = SIMIX_mutex_init();
68         hdata->cond  = SIMIX_cond_init();
69
70         SIMIX_host_set_data(host, hdata);
71
72         // node 0 sets the globals
73         if (0 == i) {
74
75                 smpi_global->hosts                              = hosts;
76                 smpi_global->host_count                         = host_count;
77
78                 smpi_mpi_global                                 = xbt_new(s_smpi_mpi_global_t, 1);
79
80                 // global communicator
81                 smpi_mpi_global->mpi_comm_world                 = xbt_new(s_smpi_mpi_communicator_t, 1);
82                 smpi_mpi_global->mpi_comm_world->size           = host_count;
83                 smpi_mpi_global->mpi_comm_world->barrier_count  = 0;
84                 smpi_mpi_global->mpi_comm_world->barrier_mutex  = SIMIX_mutex_init();
85                 smpi_mpi_global->mpi_comm_world->barrier_cond   = SIMIX_cond_init();
86                 smpi_mpi_global->mpi_comm_world->rank_to_index_map = xbt_new(int, host_count);
87                 smpi_mpi_global->mpi_comm_world->index_to_rank_map = xbt_new(int, host_count);
88                 for (i = 0; i < host_count; i++) {
89                         smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
90                         smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
91                 }
92
93                 // mpi datatypes
94                 smpi_mpi_global->mpi_byte                       = xbt_new(s_smpi_mpi_datatype_t, 1);
95                 smpi_mpi_global->mpi_byte->size                 = (size_t)1;
96                 smpi_mpi_global->mpi_int                        = xbt_new(s_smpi_mpi_datatype_t, 1);
97                 smpi_mpi_global->mpi_int->size                  = sizeof(int);
98                 smpi_mpi_global->mpi_double                     = xbt_new(s_smpi_mpi_datatype_t, 1);
99                 smpi_mpi_global->mpi_double->size               = sizeof(double);
100
101                 // mpi operations
102                 smpi_mpi_global->mpi_land                       = xbt_new(s_smpi_mpi_op_t, 1);
103                 smpi_mpi_global->mpi_land->func                 = smpi_mpi_land_func;
104                 smpi_mpi_global->mpi_sum                        = xbt_new(s_smpi_mpi_op_t, 1);
105                 smpi_mpi_global->mpi_sum->func                  = smpi_mpi_sum_func;
106
107                 // signal all nodes to perform initialization
108                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
109                 smpi_global->root_ready = 1;
110                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
111                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
112
113         } else {
114
115                 // make sure root is done before own initialization
116                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
117                 while (!smpi_global->root_ready) {
118                         SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
119                 }
120                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
121
122         }
123
124         // wait for all nodes to signal initializatin complete
125         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
126         smpi_global->ready_process_count++;
127         if (smpi_global->ready_process_count >= 3 * host_count) {
128                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
129         }
130         while (smpi_global->ready_process_count < 3 * host_count) {
131                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
132         }
133         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
134
135         return;
136 }
137
138 void smpi_mpi_finalize()
139 {
140         int i;
141
142         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
143         i = --smpi_global->running_hosts_count;
144         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
145
146         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
147         smpi_global->ready_process_count--;
148         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
149
150         SIMIX_mutex_destroy(smpi_host_mutex());
151         SIMIX_cond_destroy(smpi_host_cond());
152
153         if (0 >= i) {
154
155                 // wake up senders/receivers
156                 for (i = 0; i < smpi_global->host_count; i++) {
157                         if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
158                                 SIMIX_process_resume(smpi_global->sender_processes[i]);
159                         }
160                         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
161                                 SIMIX_process_resume(smpi_global->receiver_processes[i]);
162                         }
163                 }
164
165                 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
166                 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
167                 xbt_free(smpi_mpi_global->mpi_comm_world);
168
169                 xbt_free(smpi_mpi_global->mpi_byte);
170                 xbt_free(smpi_mpi_global->mpi_int);
171                 xbt_free(smpi_mpi_global->mpi_double);
172
173                 xbt_free(smpi_mpi_global->mpi_land);
174                 xbt_free(smpi_mpi_global->mpi_sum);
175
176                 xbt_free(smpi_mpi_global);
177
178         }
179
180 }
181
182 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
183 {
184
185         SIMIX_mutex_lock(comm->barrier_mutex);
186         ++comm->barrier_count;
187         if (comm->barrier_count > comm->size) { // only happens on second barrier...
188                 comm->barrier_count = 0;
189         } else if (comm->barrier_count == comm->size) {
190                 SIMIX_cond_broadcast(comm->barrier_cond);
191         }
192         while (comm->barrier_count < comm->size) {
193                 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
194         }
195         SIMIX_mutex_unlock(comm->barrier_mutex);
196
197         return MPI_SUCCESS;
198 }
199
200 int smpi_mpi_isend(smpi_mpi_request_t request)
201 {
202         int retval = MPI_SUCCESS;
203         int index  = smpi_host_index();
204
205         if (NULL == request) {
206                 retval = MPI_ERR_INTERN;
207         } else {
208                 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
209                 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
210                 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
211
212                 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
213                         SIMIX_process_resume(smpi_global->sender_processes[index]);
214                 }
215         }
216
217         return retval;
218 }
219
220 int smpi_mpi_irecv(smpi_mpi_request_t request)
221 {
222         int retval = MPI_SUCCESS;
223         int index = smpi_host_index();
224
225         if (NULL == request) {
226                 retval = MPI_ERR_INTERN;
227         } else {
228                 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
229                 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
230                 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
231
232                 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
233                         SIMIX_process_resume(smpi_global->receiver_processes[index]);
234                 }
235         }
236
237         return retval;
238 }
239
240 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
241 {
242         int retval = MPI_SUCCESS;
243
244         if (NULL == request) {
245                 retval = MPI_ERR_INTERN;
246         } else {
247                 SIMIX_mutex_lock(request->mutex);
248                 while (!request->completed) {
249                         SIMIX_cond_wait(request->cond, request->mutex);
250                 }
251                 if (NULL != status) {
252                         status->MPI_SOURCE = request->src;
253                         status->MPI_TAG    = request->tag;
254                         status->MPI_ERROR  = MPI_SUCCESS;
255                 }
256                 SIMIX_mutex_unlock(request->mutex);
257         }
258
259         return retval;
260 }