Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eb9436f5983160e621a58af6c0ecf193da12c210
[simgrid.git] / src / smpi / smpi_base.c
1 #include "private.h"
2 #include "xbt/time.h"
3
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
5                                 "Logging specific to SMPI (base)");
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
11 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
13
14 smpi_mpi_global_t smpi_mpi_global = NULL;
15
16 void smpi_mpi_land_func(void *a, void *b, int *length,
17                         MPI_Datatype * datatype);
18
19 void smpi_mpi_land_func(void *a, void *b, int *length,
20                         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 void smpi_mpi_sum_func(void *a, void *b, int *length,
32                        MPI_Datatype * datatype);
33
34 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
35 {
36   int i;
37   if (*datatype == smpi_mpi_global->mpi_int) {
38     int *x = a, *y = b;
39     for (i = 0; i < *length; i++) {
40       y[i] = x[i] + y[i];
41     }
42   }
43 }
44
45 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
46 {
47   return comm->index_to_rank_map[smpi_host_index()];
48 }
49
50 void smpi_process_init()
51 {
52   smx_host_t host;
53   int i;
54   smpi_host_data_t hdata;
55
56   smpi_global->running_hosts_count++;
57
58   // initialize some local variables
59   host = SIMIX_host_self();
60
61   hdata = xbt_new(s_smpi_host_data_t, 1);
62   SIMIX_host_set_data(host, hdata);
63   SIMIX_process_set_data(SIMIX_process_self(),hdata);
64
65   for (i = 0; i < smpi_global->host_count && host != smpi_global->hosts[i]; i++);
66
67   hdata->index = i;
68   hdata->mutex = SIMIX_mutex_init();
69   hdata->cond = SIMIX_cond_init();
70   hdata->finalize = 0;
71
72   hdata->pending_recv_request_queue = xbt_fifo_new();
73
74   hdata->main = SIMIX_process_self();
75   hdata->sender = SIMIX_process_create("smpi_sender",
76           smpi_sender, hdata,
77           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
78           /*props */ NULL);
79   hdata->receiver = SIMIX_process_create("smpi_receiver",
80           smpi_receiver, hdata,
81           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
82           /*props */ NULL);
83
84   smpi_global->main_processes[hdata->index] = SIMIX_process_self();
85   return;
86 }
87
88 void smpi_process_finalize()
89 {
90   int i;
91   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
92
93   i = --smpi_global->running_hosts_count;
94
95   hdata->finalize = 2; /* Tell sender and receiver to quit */
96   SIMIX_process_resume(hdata->sender);
97   SIMIX_process_resume(hdata->receiver);
98   while (hdata->finalize>0) { /* wait until it's done */
99           SIMIX_cond_wait(hdata->cond,hdata->mutex);
100   }
101
102   SIMIX_mutex_destroy(hdata->mutex);
103   SIMIX_cond_destroy(hdata->cond);
104   xbt_fifo_free(hdata->pending_recv_request_queue);
105
106
107   if (0 >= i) {
108
109     // wake up senders/receivers
110           /* MQ: (FIXME) Don't do so: it breaks since some hosts are already gone
111     for (i = 0; i < smpi_global->host_count; i++) {
112       smpi_host_data_t remote_hdata =  SIMIX_process_get_data(smpi_global->main_processes[i]);
113
114       if (SIMIX_process_is_suspended(remote_hdata->sender))
115         SIMIX_process_resume(remote_hdata->sender);
116
117       if (SIMIX_process_is_suspended(remote_hdata->receiver))
118         SIMIX_process_resume(remote_hdata->receiver);
119     }*/
120
121     SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
122     SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
123     xbt_free(smpi_mpi_global->mpi_comm_world);
124
125     xbt_free(smpi_mpi_global->mpi_byte);
126     xbt_free(smpi_mpi_global->mpi_int);
127     xbt_free(smpi_mpi_global->mpi_double);
128
129     xbt_free(smpi_mpi_global->mpi_land);
130     xbt_free(smpi_mpi_global->mpi_sum);
131
132     xbt_free(smpi_mpi_global);
133   }
134 }
135
136 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
137 {
138
139   SIMIX_mutex_lock(comm->barrier_mutex);
140   ++comm->barrier_count;
141   if (comm->barrier_count > comm->size) {       // only happens on second barrier...
142     comm->barrier_count = 0;
143   } else if (comm->barrier_count == comm->size) {
144     SIMIX_cond_broadcast(comm->barrier_cond);
145   }
146   while (comm->barrier_count < comm->size) {
147     SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
148   }
149   SIMIX_mutex_unlock(comm->barrier_mutex);
150
151   return MPI_SUCCESS;
152 }
153
154 int smpi_mpi_isend(smpi_mpi_request_t request)
155 {
156         smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
157   int retval = MPI_SUCCESS;
158
159   if (NULL == request) {
160     retval = MPI_ERR_INTERN;
161   } else {
162     xbt_fifo_push(smpi_global->pending_send_request_queues[hdata->index], request);
163
164     if (SIMIX_process_is_suspended(hdata->sender)) {
165       SIMIX_process_resume(hdata->sender);
166     }
167   }
168
169   return retval;
170 }
171
172 int smpi_mpi_irecv(smpi_mpi_request_t request)
173 {
174   int retval = MPI_SUCCESS;
175   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
176
177   if (NULL == request) {
178     retval = MPI_ERR_INTERN;
179   } else {
180     xbt_fifo_push(hdata->pending_recv_request_queue, request);
181
182     if (SIMIX_process_is_suspended(hdata->receiver)) {
183       SIMIX_process_resume(hdata->receiver);
184     }
185   }
186
187   return retval;
188 }
189
190 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
191 {
192   int retval = MPI_SUCCESS;
193
194   if (NULL == request) {
195     retval = MPI_ERR_INTERN;
196   } else {
197     SIMIX_mutex_lock(request->mutex);
198     while (!request->completed) {
199       SIMIX_cond_wait(request->cond, request->mutex);
200     }
201     if (NULL != status) {
202       status->MPI_SOURCE = request->src;
203       status->MPI_TAG = request->tag;
204       status->MPI_ERROR = MPI_SUCCESS;
205     }
206     SIMIX_mutex_unlock(request->mutex);
207   }
208
209   return retval;
210 }