Logo AND Algorithmique Numérique Distribuée

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