Logo AND Algorithmique Numérique Distribuée

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