4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
5 "Logging specific to SMPI (base)");
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
11 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
15 smpi_mpi_global_t smpi_mpi_global = NULL;
19 * Get the lower bound and extent for a Datatype
21 * FIXME: this an incomplete implementation as we do not support yet MPI_Type_commit.
22 * Hence, this can be called only for primitive type MPI_INT, MPI_DOUBLE, ...
24 * remark: MPI-1 has also the deprecated
25 * int MPI_Type_extent(MPI_Datatype datatype, *MPI_Aint *extent);
28 int smpi_mpi_type_get_extent(MPI_Datatype datatype, MPI_Aint *lb, MPI_Aint *extent) {
29 *extent = datatype->ub - datatype->lb;
30 return( MPI_SUCCESS );
34 * Operations of MPI_OP : implemented=land,sum,min,max
36 void smpi_mpi_land_func(void *a, void *b, int *length,
37 MPI_Datatype * datatype);
39 void smpi_mpi_land_func(void *a, void *b, int *length,
40 MPI_Datatype * datatype)
43 if (*datatype == smpi_mpi_global->mpi_int) {
45 for (i = 0; i < *length; i++) {
52 * sum two vectors element-wise
54 * @param a the first vectors
55 * @param b the second vectors
56 * @return the second vector is modified and contains the element-wise sums
58 void smpi_mpi_sum_func(void *a, void *b, int *length,
59 MPI_Datatype * datatype);
61 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
64 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
66 for (i = 0; i < *length; i++) {
69 } else if (*datatype == smpi_mpi_global->mpi_int) {
71 for (i = 0; i < *length; i++) {
74 } else if (*datatype == smpi_mpi_global->mpi_float) {
76 for (i = 0; i < *length; i++) {
79 } else if (*datatype == smpi_mpi_global->mpi_double) {
80 double *x = a, *y = b;
81 for (i = 0; i < *length; i++) {
87 *i multiply two vectors element-wise
89 * @param a the first vectors
90 * @param b the second vectors
91 * @return the second vector is modified and contains the element-wise products
93 void smpi_mpi_prod_func(void *a, void *b, int *length,
94 MPI_Datatype * datatype);
96 void smpi_mpi_prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
99 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
101 for (i = 0; i < *length; i++) {
104 } else if (*datatype == smpi_mpi_global->mpi_int) {
106 for (i = 0; i < *length; i++) {
109 } else if (*datatype == smpi_mpi_global->mpi_float) {
110 float *x = a, *y = b;
111 for (i = 0; i < *length; i++) {
114 } else if (*datatype == smpi_mpi_global->mpi_double) {
115 double *x = a, *y = b;
116 for (i = 0; i < *length; i++) {
122 * compute the min of two vectors element-wise
124 void smpi_mpi_min_func(void *a, void *b, int *length,
125 MPI_Datatype * datatype);
127 void smpi_mpi_min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
130 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
132 for (i = 0; i < *length; i++) {
133 y[i] = x[i] < y[i] ? x[i] : y[i];
136 if (*datatype == smpi_mpi_global->mpi_int) {
138 for (i = 0; i < *length; i++) {
139 y[i] = x[i] < y[i] ? x[i] : y[i];
142 if (*datatype == smpi_mpi_global->mpi_float) {
143 float *x = a, *y = b;
144 for (i = 0; i < *length; i++) {
145 y[i] = x[i] < y[i] ? x[i] : y[i];
148 if (*datatype == smpi_mpi_global->mpi_double) {
149 double *x = a, *y = b;
150 for (i = 0; i < *length; i++) {
151 y[i] = x[i] < y[i] ? x[i] : y[i];
161 * compute the max of two vectors element-wise
163 void smpi_mpi_max_func(void *a, void *b, int *length,
164 MPI_Datatype * datatype);
166 void smpi_mpi_max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
169 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
171 for (i = 0; i < *length; i++) {
172 y[i] = x[i] > y[i] ? x[i] : y[i];
174 } else if (*datatype == smpi_mpi_global->mpi_int) {
176 for (i = 0; i < *length; i++) {
177 y[i] = x[i] > y[i] ? x[i] : y[i];
179 } else if (*datatype == smpi_mpi_global->mpi_float) {
180 float *x = a, *y = b;
181 for (i = 0; i < *length; i++) {
182 y[i] = x[i] > y[i] ? x[i] : y[i];
184 } else if (*datatype == smpi_mpi_global->mpi_double) {
185 double *x = a, *y = b;
186 for (i = 0; i < *length; i++) {
187 y[i] = x[i] > y[i] ? x[i] : y[i];
197 * tell the MPI rank of the calling process (from its SIMIX process id)
199 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
201 return comm->index_to_rank_map[smpi_process_index()];
204 void smpi_process_init(int *argc, char ***argv)
206 smpi_process_data_t pdata;
208 // initialize some local variables
210 pdata = xbt_new(s_smpi_process_data_t, 1);
211 SIMIX_process_set_data(SIMIX_process_self(), pdata);
213 /* get rank from command line, and remove it from argv */
214 pdata->index = atoi((*argv)[1]);
215 DEBUG1("I'm rank %d", pdata->index);
217 memmove((*argv)[1], (*argv)[2], sizeof(char *) * (*argc - 2));
218 (*argv)[(*argc) - 1] = NULL;
222 pdata->mutex = SIMIX_mutex_init();
223 pdata->cond = SIMIX_cond_init();
226 pdata->pending_recv_request_queue = xbt_fifo_new();
227 pdata->pending_send_request_queue = xbt_fifo_new();
228 pdata->received_message_queue = xbt_fifo_new();
230 pdata->main = SIMIX_process_self();
231 pdata->sender = SIMIX_process_create("smpi_sender",
233 SIMIX_host_get_name(SIMIX_host_self()),
236 pdata->receiver = SIMIX_process_create("smpi_receiver",
237 smpi_receiver, pdata,
238 SIMIX_host_get_name(SIMIX_host_self
242 smpi_global->main_processes[pdata->index] = SIMIX_process_self();
246 void smpi_process_finalize()
248 smpi_process_data_t pdata = SIMIX_process_get_data(SIMIX_process_self());
250 pdata->finalize = 2; /* Tell sender and receiver to quit */
251 SIMIX_process_resume(pdata->sender);
252 SIMIX_process_resume(pdata->receiver);
253 while (pdata->finalize > 0) { /* wait until it's done */
254 SIMIX_cond_wait(pdata->cond, pdata->mutex);
257 SIMIX_mutex_destroy(pdata->mutex);
258 SIMIX_cond_destroy(pdata->cond);
259 xbt_fifo_free(pdata->pending_recv_request_queue);
260 xbt_fifo_free(pdata->pending_send_request_queue);
261 xbt_fifo_free(pdata->received_message_queue);
266 /*int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
269 SIMIX_mutex_lock(comm->barrier_mutex);
270 ++comm->barrier_count;
271 if (comm->barrier_count > comm->size) { // only happens on second barrier...
272 comm->barrier_count = 0;
273 } else if (comm->barrier_count == comm->size) {
274 SIMIX_cond_broadcast(comm->barrier_cond);
276 while (comm->barrier_count < comm->size) {
277 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
279 SIMIX_mutex_unlock(comm->barrier_mutex);
285 int smpi_mpi_isend(smpi_mpi_request_t request)
287 smpi_process_data_t pdata = SIMIX_process_get_data(SIMIX_process_self());
288 int retval = MPI_SUCCESS;
290 if (NULL == request) {
291 retval = MPI_ERR_INTERN;
293 xbt_fifo_push(pdata->pending_send_request_queue, request);
294 SIMIX_process_resume(pdata->sender);
300 int smpi_mpi_irecv(smpi_mpi_request_t request)
302 int retval = MPI_SUCCESS;
303 smpi_process_data_t pdata = SIMIX_process_get_data(SIMIX_process_self());
305 if (NULL == request) {
306 retval = MPI_ERR_INTERN;
308 xbt_fifo_push(pdata->pending_recv_request_queue, request);
310 if (SIMIX_process_is_suspended(pdata->receiver)) {
311 SIMIX_process_resume(pdata->receiver);
318 void print_req( smpi_mpi_request_t r );
319 void print_req( smpi_mpi_request_t r ) {
320 printf("***req %p-> src=%d dst=%d tag=%d completed=0x%x consumed=0x%x\n",r,r->src,r->dst,r->tag,r->completed,r->consumed);
325 * wait and friends ...
327 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
329 int retval = MPI_SUCCESS;
331 if (NULL == request) {
332 retval = MPI_ERR_INTERN;
334 SIMIX_mutex_lock(request->mutex);
335 //#define DEBUG_STEPH
337 print_req( request ); //@@
339 while (!request->completed) {
340 SIMIX_cond_wait(request->cond, request->mutex);
342 if (NULL != status) {
343 status->MPI_SOURCE = request->src;
344 status->MPI_TAG = request->tag;
345 status->MPI_ERROR = MPI_SUCCESS;
347 SIMIX_mutex_unlock(request->mutex);
356 int smpi_mpi_waitall(int count, smpi_mpi_request_t requests[],
357 smpi_mpi_status_t status[])
362 smpi_mpi_status_t stat;
364 for (cpt = 0; cpt < count; cpt++) {
365 retval = smpi_mpi_waitany(count, requests, &index, &stat);
366 if (retval != MPI_SUCCESS)
368 if (MPI_STATUS_IGNORE != status)
369 memcpy(&(status[index]), &stat, sizeof(stat));
377 int smpi_mpi_waitany(int count, smpi_mpi_request_t * requests, int *index,
378 smpi_mpi_status_t * status)
382 *index = MPI_UNDEFINED;
383 if (NULL == requests) {
384 return MPI_ERR_INTERN;
386 /* First check if one of them is already done */
387 for (cpt = 0; cpt < count; cpt++) {
389 printf("...exam req[%d] of msg from [%d]\n",cpt,requests[cpt]->src);
391 if (requests[cpt]->completed && !requests[cpt]->consumed) { /* got ya */
393 printf("...found match req[%d] of msg from [%d]\n",cpt,requests[cpt]->src);
399 /* If none found, block */
400 /* FIXME: should use a SIMIX_cond_waitany, when implemented. For now, block on the first one */
402 for (cpt = 0; cpt < count; cpt++) {
405 print_req( requests[cpt] );
407 if (!requests[cpt]->completed) { /* this one is not done, wait on it */
409 printf("... blocked waiting a msg %d->%d, tag=%d\n",requests[cpt]->src,requests[cpt]->dst,requests[cpt]->tag);
411 while (!requests[cpt]->completed)
412 SIMIX_cond_wait(requests[cpt]->cond, requests[cpt]->mutex);
418 if (cpt == count) /* they are all done. Damn user */
419 return MPI_ERR_REQUEST;
424 print_req( requests[cpt] );
426 requests[*index]->consumed = 1;
428 print_req( requests[cpt] );
429 printf("...accessing *req[%d]->consumed\n",cpt);
431 if (NULL != status) {
432 status->MPI_SOURCE = requests[*index]->src;
433 status->MPI_TAG = requests[*index]->tag;
434 status->MPI_ERROR = MPI_SUCCESS;