Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I just realized that the changes I made to the cond-wait test would break
[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
5 smpi_mpi_global_t smpi_mpi_global = NULL;
6
7 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype);
8
9 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype)
10 {
11         int i;
12         if (*datatype == smpi_mpi_global->mpi_int) {
13                 int *x = a, *y = b;
14                 for (i = 0; i < *length; i++) {
15                         y[i] = x[i] && y[i];
16                 }
17         }
18 }
19
20 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype);
21
22 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype)
23 {
24         int i;
25         if (*datatype == smpi_mpi_global->mpi_int) {
26                 int *x = a, *y = b;
27                 for (i = 0; i < *length; i++) {
28                         y[i] = x[i] + y[i];
29                 }
30         }
31 }
32
33 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
34 {
35         return comm->index_to_rank_map[smpi_host_index()];
36 }
37
38 void smpi_mpi_init()
39 {
40         smx_host_t host;
41         smx_host_t *hosts;
42         int host_count;
43         int i;
44         smpi_host_data_t hdata;
45
46         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
47         smpi_global->running_hosts_count++;
48         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
49
50         // initialize some local variables
51         host       = SIMIX_host_self();
52         hosts      = SIMIX_host_get_table();
53         host_count = SIMIX_host_get_number();
54
55         hdata      = xbt_new(s_smpi_host_data_t, 1);
56
57         for (i = 0; i < host_count && host != hosts[i]; i ++);
58
59         hdata->index = i;
60         hdata->mutex = SIMIX_mutex_init();
61         hdata->cond  = SIMIX_cond_init();
62
63         SIMIX_host_set_data(host, hdata);
64
65         // node 0 sets the globals
66         if (0 == i) {
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                 while (!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_broadcast(smpi_global->start_stop_cond);
122         }
123         while (smpi_global->ready_process_count < 3 * host_count) {
124                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
125         }
126         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
127
128         return;
129 }
130
131 void smpi_mpi_finalize()
132 {
133         int i;
134
135         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
136         i = --smpi_global->running_hosts_count;
137         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
138
139         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
140         smpi_global->ready_process_count--;
141         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
142
143         SIMIX_mutex_destroy(smpi_host_mutex());
144         SIMIX_cond_destroy(smpi_host_cond());
145
146         if (0 >= i) {
147
148                 // wake up senders/receivers
149                 for (i = 0; i < smpi_global->host_count; i++) {
150                         if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
151                                 SIMIX_process_resume(smpi_global->sender_processes[i]);
152                         }
153                         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
154                                 SIMIX_process_resume(smpi_global->receiver_processes[i]);
155                         }
156                 }
157
158                 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
159                 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
160                 xbt_free(smpi_mpi_global->mpi_comm_world);
161
162                 xbt_free(smpi_mpi_global->mpi_byte);
163                 xbt_free(smpi_mpi_global->mpi_int);
164                 xbt_free(smpi_mpi_global->mpi_double);
165
166                 xbt_free(smpi_mpi_global->mpi_land);
167                 xbt_free(smpi_mpi_global->mpi_sum);
168
169                 xbt_free(smpi_mpi_global);
170
171         }
172
173 }
174
175 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
176 {
177
178         SIMIX_mutex_lock(comm->barrier_mutex);
179         ++comm->barrier_count;
180         if (comm->barrier_count > comm->size) { // only happens on second barrier...
181                 comm->barrier_count = 0;
182         } else if (comm->barrier_count == comm->size) {
183                 SIMIX_cond_broadcast(comm->barrier_cond);
184         }
185         while (comm->barrier_count < comm->size) {
186                 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
187         }
188         SIMIX_mutex_unlock(comm->barrier_mutex);
189
190         return MPI_SUCCESS;
191 }
192
193 int smpi_mpi_isend(smpi_mpi_request_t request)
194 {
195         int retval = MPI_SUCCESS;
196         int index  = smpi_host_index();
197
198         if (NULL == request) {
199                 retval = MPI_ERR_INTERN;
200         } else {
201                 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
202                 xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
203                 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[index]);
204
205                 if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
206                         SIMIX_process_resume(smpi_global->sender_processes[index]);
207                 }
208         }
209
210         return retval;
211 }
212
213 int smpi_mpi_irecv(smpi_mpi_request_t request)
214 {
215         int retval = MPI_SUCCESS;
216         int index = smpi_host_index();
217
218         if (NULL == request) {
219                 retval = MPI_ERR_INTERN;
220         } else {
221                 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
222                 xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
223                 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[index]);
224
225                 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
226                         SIMIX_process_resume(smpi_global->receiver_processes[index]);
227                 }
228         }
229
230         return retval;
231 }
232
233 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
234 {
235         int retval = MPI_SUCCESS;
236
237         if (NULL == request) {
238                 retval = MPI_ERR_INTERN;
239         } else {
240                 SIMIX_mutex_lock(request->mutex);
241                 while (!request->completed) {
242                         SIMIX_cond_wait(request->cond, request->mutex);
243                 }
244                 if (NULL != status) {
245                         status->MPI_SOURCE = request->src;
246                         status->MPI_TAG    = request->tag;
247                         status->MPI_ERROR  = MPI_SUCCESS;
248                 }
249                 SIMIX_mutex_unlock(request->mutex);
250         }
251
252         return retval;
253 }