Logo AND Algorithmique Numérique Distribuée

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