Logo AND Algorithmique Numérique Distribuée

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