Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
11439cce8831d1c9bc709eaec1ffd3d3cc747c9c
[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_init_process()
50 {
51   smx_host_t host;
52   int i;
53   smpi_host_data_t hdata;
54
55   SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
56   smpi_global->running_hosts_count++;
57   SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
58
59   // initialize some local variables
60   host = SIMIX_host_self();
61
62   hdata = xbt_new(s_smpi_host_data_t, 1);
63
64   for (i = 0; i < smpi_global->host_count && host != smpi_global->hosts[i]; i++);
65
66   hdata->index = i;
67   hdata->mutex = SIMIX_mutex_init();
68   hdata->cond = SIMIX_cond_init();
69
70   SIMIX_host_set_data(host, hdata);
71
72   // node 0 sets the globals
73   if (0 == i) {
74
75
76     // signal all nodes to perform initialization
77     SIMIX_mutex_lock(smpi_global->start_stop_mutex);
78     smpi_global->root_ready = 1;
79     SIMIX_cond_broadcast(smpi_global->start_stop_cond);
80     SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
81
82   } else {
83
84     // make sure root is done before own initialization
85     SIMIX_mutex_lock(smpi_global->start_stop_mutex);
86     while (!smpi_global->root_ready) {
87       SIMIX_cond_wait(smpi_global->start_stop_cond,
88                       smpi_global->start_stop_mutex);
89     }
90     SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
91
92   }
93
94   // wait for all nodes to signal initializatin complete
95   SIMIX_mutex_lock(smpi_global->start_stop_mutex);
96   smpi_global->ready_process_count++;
97   if (smpi_global->ready_process_count >= 3 * smpi_global->host_count) {
98     SIMIX_cond_broadcast(smpi_global->start_stop_cond);
99   }
100   while (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
101     SIMIX_cond_wait(smpi_global->start_stop_cond,
102                     smpi_global->start_stop_mutex);
103   }
104   SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
105
106   return;
107 }
108
109 void smpi_mpi_finalize()
110 {
111   int i;
112
113   SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
114   i = --smpi_global->running_hosts_count;
115   SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
116
117   SIMIX_mutex_lock(smpi_global->start_stop_mutex);
118   smpi_global->ready_process_count--;
119   SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
120
121   SIMIX_mutex_destroy(smpi_host_mutex());
122   SIMIX_cond_destroy(smpi_host_cond());
123
124   if (0 >= i) {
125
126     // wake up senders/receivers
127     for (i = 0; i < smpi_global->host_count; 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);
139
140     xbt_free(smpi_mpi_global->mpi_byte);
141     xbt_free(smpi_mpi_global->mpi_int);
142     xbt_free(smpi_mpi_global->mpi_double);
143
144     xbt_free(smpi_mpi_global->mpi_land);
145     xbt_free(smpi_mpi_global->mpi_sum);
146
147     xbt_free(smpi_mpi_global);
148
149   }
150
151 }
152
153 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
154 {
155
156   SIMIX_mutex_lock(comm->barrier_mutex);
157   ++comm->barrier_count;
158   if (comm->barrier_count > comm->size) {       // only happens on second barrier...
159     comm->barrier_count = 0;
160   } else if (comm->barrier_count == comm->size) {
161     SIMIX_cond_broadcast(comm->barrier_cond);
162   }
163   while (comm->barrier_count < comm->size) {
164     SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
165   }
166   SIMIX_mutex_unlock(comm->barrier_mutex);
167
168   return MPI_SUCCESS;
169 }
170
171 int smpi_mpi_isend(smpi_mpi_request_t request)
172 {
173   int retval = MPI_SUCCESS;
174   int index = smpi_host_index();
175
176   if (NULL == request) {
177     retval = MPI_ERR_INTERN;
178   } else {
179     SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[index]);
180     xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
181     SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes
182                        [index]);
183
184     if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
185       SIMIX_process_resume(smpi_global->sender_processes[index]);
186     }
187   }
188
189   return retval;
190 }
191
192 int smpi_mpi_irecv(smpi_mpi_request_t request)
193 {
194   int retval = MPI_SUCCESS;
195   int index = smpi_host_index();
196
197   if (NULL == request) {
198     retval = MPI_ERR_INTERN;
199   } else {
200     SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[index]);
201     xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
202     SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes
203                        [index]);
204
205     if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
206       SIMIX_process_resume(smpi_global->receiver_processes[index]);
207     }
208   }
209
210   return retval;
211 }
212
213 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
214 {
215   int retval = MPI_SUCCESS;
216
217   if (NULL == request) {
218     retval = MPI_ERR_INTERN;
219   } else {
220     SIMIX_mutex_lock(request->mutex);
221     while (!request->completed) {
222       SIMIX_cond_wait(request->cond, request->mutex);
223     }
224     if (NULL != status) {
225       status->MPI_SOURCE = request->src;
226       status->MPI_TAG = request->tag;
227       status->MPI_ERROR = MPI_SUCCESS;
228     }
229     SIMIX_mutex_unlock(request->mutex);
230   }
231
232   return retval;
233 }