4 #include "smpi_coll_private.h"
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi,
7 "Logging specific to SMPI (mpi)");
9 int SMPI_MPI_Init(int *argc, char ***argv)
11 smpi_process_init(argc, argv);
16 int SMPI_MPI_Finalize()
19 smpi_process_finalize();
23 // right now this just exits the current node, should send abort signal to all
24 // hosts in the communicator (TODO)
25 int SMPI_MPI_Abort(MPI_Comm comm, int errorcode)
31 int SMPI_MPI_Comm_size(MPI_Comm comm, int *size)
33 int retval = MPI_SUCCESS;
38 retval = MPI_ERR_COMM;
39 } else if (NULL == size) {
50 int SMPI_MPI_Comm_rank(MPI_Comm comm, int *rank)
52 int retval = MPI_SUCCESS;
57 retval = MPI_ERR_COMM;
58 } else if (NULL == rank) {
61 *rank = smpi_mpi_comm_rank(comm);
69 int SMPI_MPI_Type_size(MPI_Datatype datatype, size_t * size)
71 int retval = MPI_SUCCESS;
75 if (NULL == datatype) {
76 retval = MPI_ERR_TYPE;
77 } else if (NULL == size) {
80 *size = datatype->size;
88 int SMPI_MPI_Barrier(MPI_Comm comm)
90 int retval = MPI_SUCCESS;
96 retval = MPI_ERR_COMM;
100 * original implemantation:
101 * retval = smpi_mpi_barrier(comm);
102 * this one is unrealistic: it just cond_waits, means no time.
104 retval = nary_tree_barrier( comm, arity );
112 int SMPI_MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
113 int tag, MPI_Comm comm, MPI_Request * request)
115 int retval = MPI_SUCCESS;
119 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
121 if (NULL != *request && MPI_SUCCESS == retval) {
122 retval = smpi_mpi_irecv(*request);
130 int SMPI_MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src,
131 int tag, MPI_Comm comm, MPI_Status * status)
133 int retval = MPI_SUCCESS;
134 smpi_mpi_request_t request;
138 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
140 if (NULL != request && MPI_SUCCESS == retval) {
141 retval = smpi_mpi_irecv(request);
142 if (MPI_SUCCESS == retval) {
143 retval = smpi_mpi_wait(request, status);
145 xbt_mallocator_release(smpi_global->request_mallocator, request);
153 int SMPI_MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
154 int tag, MPI_Comm comm, MPI_Request * request)
156 int retval = MPI_SUCCESS;
160 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
162 if (NULL != *request && MPI_SUCCESS == retval) {
163 retval = smpi_mpi_isend(*request);
171 int SMPI_MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst,
172 int tag, MPI_Comm comm)
174 int retval = MPI_SUCCESS;
175 smpi_mpi_request_t request;
179 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
181 if (NULL != request && MPI_SUCCESS == retval) {
182 retval = smpi_mpi_isend(request);
183 if (MPI_SUCCESS == retval) {
184 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
186 xbt_mallocator_release(smpi_global->request_mallocator, request);
195 * MPI_Wait and friends
197 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
199 return smpi_mpi_wait(*request, status);
202 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
204 return smpi_mpi_waitall(count, requests, status);
207 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index,
210 return smpi_mpi_waitany(count, requests, index, status);
220 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm);
221 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
225 int retval = MPI_SUCCESS;
226 smpi_mpi_request_t request;
228 rank = smpi_mpi_comm_rank(comm);
230 retval = smpi_create_request(buf, count, datatype, root,
231 (root + 1) % comm->size, 0, comm, &request);
232 request->forward = comm->size - 1;
233 smpi_mpi_isend(request);
235 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
237 smpi_mpi_irecv(request);
240 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
241 xbt_mallocator_release(smpi_global->request_mallocator, request);
248 * Bcast user entry point
250 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
253 int retval = MPI_SUCCESS;
257 //retval = flat_tree_bcast(buf, count, datatype, root, comm);
258 retval = nary_tree_bcast(buf, count, datatype, root, comm, 2 );
269 * debugging helper function
271 static void print_buffer_int(void *buf, int len, const char *msg, int rank)
274 printf("**[%d] %s: ", rank, msg);
275 for (tmp = 0; tmp < len; tmp++) {
277 printf("[%d]", v[tmp]);
287 int SMPI_MPI_Reduce(void *sendbuf, void *recvbuf, int count,
288 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
290 int retval = MPI_SUCCESS;
295 smpi_mpi_request_t *requests;
296 smpi_mpi_request_t request;
300 rank = smpi_mpi_comm_rank(comm);
303 if (rank != root) { // if i am not ROOT, simply send my buffer to root
306 print_buffer_int(sendbuf, count, xbt_strdup("sndbuf"), rank);
309 smpi_create_request(sendbuf, count, datatype, rank, root, tag, comm,
311 smpi_mpi_isend(request);
312 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
313 xbt_mallocator_release(smpi_global->request_mallocator, request);
316 // i am the ROOT: wait for all buffers by creating one request by sender
318 requests = xbt_malloc((size-1) * sizeof(smpi_mpi_request_t));
320 void **tmpbufs = xbt_malloc((size-1) * sizeof(void *));
321 for (i = 0; i < size-1; i++) {
322 // we need 1 buffer per request to store intermediate receptions
323 tmpbufs[i] = xbt_malloc(count * datatype->size);
325 // root: initiliaze recv buf with my own snd buf
326 memcpy(recvbuf, sendbuf, count * datatype->size * sizeof(char));
328 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
329 // since we should op values as soon as one receiving request matches.
330 for (i = 0; i < size-1; i++) {
331 // reminder: for smpi_create_request() the src is always the process sending.
332 src = i < root ? i : i + 1;
333 retval = smpi_create_request(tmpbufs[i], count, datatype,
334 src, root, tag, comm, &(requests[i]));
335 if (NULL != requests[i] && MPI_SUCCESS == retval) {
336 if (MPI_SUCCESS == retval) {
337 smpi_mpi_irecv(requests[i]);
341 // now, wait for completion of all irecv's.
342 for (i = 0; i < size-1; i++) {
343 int index = MPI_UNDEFINED;
344 smpi_mpi_waitany( size-1, requests, &index, MPI_STATUS_IGNORE);
346 printf ("MPI_Waitany() unblocked: root received (completes req[index=%d])\n",index);
347 print_buffer_int(tmpbufs[index], count, bprintf("tmpbufs[index=%d] (value received)", index),
352 op->func(tmpbufs[index], recvbuf, &count, &datatype);
354 print_buffer_int(recvbuf, count, xbt_strdup("rcvbuf"), rank);
356 xbt_free(tmpbufs[index]);
357 /* FIXME: with the following line, it generates an
358 * [xbt_ex/CRITICAL] Conditional list not empty 162518800.
360 // xbt_mallocator_release(smpi_global->request_mallocator, requests[index]);
372 * Same as MPI_REDUCE except that the result appears in the receive buffer of all the group members.
374 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
375 MPI_Op op, MPI_Comm comm );
376 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
377 MPI_Op op, MPI_Comm comm )
379 int retval = MPI_SUCCESS;
380 int root=1; // arbitrary choice
384 retval = SMPI_MPI_Reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
385 if (MPI_SUCCESS != retval)
388 retval = SMPI_MPI_Bcast( sendbuf, count, datatype, root, comm);
397 // used by comm_split to sort ranks based on key values
398 int smpi_compare_rankkeys(const void *a, const void *b);
399 int smpi_compare_rankkeys(const void *a, const void *b)
418 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
421 int retval = MPI_SUCCESS;
424 smpi_mpi_request_t request;
426 smpi_mpi_status_t status;
430 // FIXME: need to test parameters
432 index = smpi_process_index();
433 rank = comm->index_to_rank_map[index];
438 // root node does most of the real work
440 int colormap[comm->size];
441 int keymap[comm->size];
442 int rankkeymap[comm->size * 2];
444 smpi_mpi_communicator_t tempcomm = NULL;
451 // FIXME: use scatter/gather or similar instead of individual comms
452 for (i = 1; i < comm->size; i++) {
453 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
454 rank, MPI_ANY_TAG, comm, &request);
455 smpi_mpi_irecv(request);
456 smpi_mpi_wait(request, &status);
457 colormap[status.MPI_SOURCE] = colorkey[0];
458 keymap[status.MPI_SOURCE] = colorkey[1];
459 xbt_mallocator_release(smpi_global->request_mallocator, request);
462 for (i = 0; i < comm->size; i++) {
463 if (MPI_UNDEFINED == colormap[i]) {
466 // make a list of nodes with current color and sort by keys
468 for (j = i; j < comm->size; j++) {
469 if (colormap[i] == colormap[j]) {
470 colormap[j] = MPI_UNDEFINED;
471 rankkeymap[count * 2] = j;
472 rankkeymap[count * 2 + 1] = keymap[j];
476 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
479 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
480 tempcomm->barrier_count = 0;
481 tempcomm->size = count;
482 tempcomm->barrier_mutex = SIMIX_mutex_init();
483 tempcomm->barrier_cond = SIMIX_cond_init();
484 tempcomm->rank_to_index_map = xbt_new(int, count);
485 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
486 for (j = 0; j < smpi_global->process_count; j++) {
487 tempcomm->index_to_rank_map[j] = -1;
489 for (j = 0; j < count; j++) {
490 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
491 tempcomm->rank_to_index_map[j] = indextmp;
492 tempcomm->index_to_rank_map[indextmp] = j;
494 for (j = 0; j < count; j++) {
495 if (rankkeymap[j * 2]) {
496 retval = smpi_create_request(&j, 1, MPI_INT, 0,
497 rankkeymap[j * 2], 0, comm, &request);
498 request->data = tempcomm;
499 smpi_mpi_isend(request);
500 smpi_mpi_wait(request, &status);
501 xbt_mallocator_release(smpi_global->request_mallocator, request);
503 *comm_out = tempcomm;
510 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
512 smpi_mpi_isend(request);
513 smpi_mpi_wait(request, &status);
514 xbt_mallocator_release(smpi_global->request_mallocator, request);
515 if (MPI_UNDEFINED != color) {
516 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
518 smpi_mpi_irecv(request);
519 smpi_mpi_wait(request, &status);
520 *comm_out = request->data;
529 double SMPI_MPI_Wtime(void)
531 return (SIMIX_get_clock());