5 * Eventually will contain the user level MPI primitives and its corresponding
6 * internal wrapper. The implementations of these primitives should go to specific
7 * files. For example, a SMPI_MPI_Bcast() in this file, should call the wrapper
8 * smpi_mpi_bcast(), which decides which implementation to call. Currently, it
9 * calls nary_tree_bcast() in smpi_coll.c. (Stéphane Genaud).
15 #include "smpi_coll_private.h"
16 #include "smpi_mpi_dt_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi,
19 "Logging specific to SMPI (mpi)");
21 int SMPI_MPI_Init(int *argc, char ***argv)
23 smpi_process_init(argc, argv);
28 int SMPI_MPI_Finalize()
31 smpi_process_finalize();
35 // right now this just exits the current node, should send abort signal to all
36 // hosts in the communicator (TODO)
37 int SMPI_MPI_Abort(MPI_Comm comm, int errorcode)
43 int SMPI_MPI_Comm_size(MPI_Comm comm, int *size)
45 int retval = MPI_SUCCESS;
50 retval = MPI_ERR_COMM;
51 } else if (NULL == size) {
62 int SMPI_MPI_Comm_rank(MPI_Comm comm, int *rank)
64 int retval = MPI_SUCCESS;
69 retval = MPI_ERR_COMM;
70 } else if (NULL == rank) {
73 *rank = smpi_mpi_comm_rank(comm);
86 int SMPI_MPI_Barrier(MPI_Comm comm)
88 int retval = MPI_SUCCESS;
94 retval = MPI_ERR_COMM;
98 * original implemantation:
99 * retval = smpi_mpi_barrier(comm);
100 * this one is unrealistic: it just cond_waits, means no time.
102 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 rank = smpi_mpi_comm_rank(comm);
120 retval = smpi_create_request(buf, count, datatype, src, rank, tag, comm,
122 if (NULL != *request && MPI_SUCCESS == retval) {
123 retval = smpi_mpi_irecv(*request);
131 int SMPI_MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src,
132 int tag, MPI_Comm comm, MPI_Status * status)
134 int retval = MPI_SUCCESS;
136 smpi_mpi_request_t request;
140 rank = smpi_mpi_comm_rank(comm);
141 retval = smpi_create_request(buf, count, datatype, src, rank, tag, comm,
143 if (NULL != request && MPI_SUCCESS == retval) {
144 retval = smpi_mpi_irecv(request);
145 if (MPI_SUCCESS == retval) {
146 retval = smpi_mpi_wait(request, status);
148 xbt_mallocator_release(smpi_global->request_mallocator, request);
156 int SMPI_MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
157 int tag, MPI_Comm comm, MPI_Request * request)
159 int retval = MPI_SUCCESS;
164 rank = smpi_mpi_comm_rank(comm);
165 retval = smpi_create_request(buf, count, datatype, rank, dst, tag, comm,
167 if (NULL != *request && MPI_SUCCESS == retval) {
168 retval = smpi_mpi_isend(*request);
177 * MPI_Send user level
179 int SMPI_MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst,
180 int tag, MPI_Comm comm)
182 int retval = MPI_SUCCESS;
184 smpi_mpi_request_t request;
188 rank = smpi_mpi_comm_rank(comm);
189 retval = smpi_create_request(buf, count, datatype, rank, dst, tag, comm,
191 if (NULL != request && MPI_SUCCESS == retval) {
192 retval = smpi_mpi_isend(request);
193 if (MPI_SUCCESS == retval) {
194 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
196 xbt_mallocator_release(smpi_global->request_mallocator, request);
206 * MPI_Sendrecv internal level
208 int smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
209 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
210 MPI_Comm comm, MPI_Status *status)
213 int retval = MPI_SUCCESS;
214 smpi_mpi_request_t srequest;
215 smpi_mpi_request_t rrequest;
217 rank = smpi_mpi_comm_rank(comm);
220 retval = smpi_create_request(sendbuf, sendcount, sendtype,
223 smpi_mpi_isend(srequest);
226 retval = smpi_create_request(recvbuf, recvcount, recvtype,
227 source, rank,recvtag,
229 smpi_mpi_irecv(rrequest);
231 smpi_mpi_wait(srequest, MPI_STATUS_IGNORE);
232 //printf("[%d] isend request src=%d dst=%d tag=%d COMPLETED (retval=%d) \n",rank,rank,dest,sendtag,retval);
233 smpi_mpi_wait(rrequest, MPI_STATUS_IGNORE);
234 //printf("[%d] irecv request src=%d -> dst=%d tag=%d COMPLETED (retval=%d)\n",rank,source,rank,recvtag,retval);
239 * MPI_Sendrecv user entry point
241 int SMPI_MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
242 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
243 MPI_Comm comm, MPI_Status *status)
245 int retval = MPI_SUCCESS;
248 smpi_mpi_sendrecv( sendbuf, sendcount, sendtype, dest, sendtag,
249 recvbuf, recvcount, recvtype, source, recvtag,
259 * MPI_Wait and friends
261 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
266 retval = smpi_mpi_wait(*request, status);
271 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
276 retval = smpi_mpi_waitall(count, requests, status);
281 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index,
287 retval = smpi_mpi_waitany(count, requests, index, status);
299 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm);
300 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
304 int retval = MPI_SUCCESS;
305 smpi_mpi_request_t request;
307 rank = smpi_mpi_comm_rank(comm);
309 retval = smpi_create_request(buf, count, datatype, root,
310 (root + 1) % comm->size, 0, comm, &request);
311 request->forward = comm->size - 1;
312 smpi_mpi_isend(request);
314 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
316 smpi_mpi_irecv(request);
319 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
320 xbt_mallocator_release(smpi_global->request_mallocator, request);
326 * Bcast internal level
328 int smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
331 int retval = MPI_SUCCESS;
332 int rank = smpi_mpi_comm_rank(comm);
334 DEBUG1("<%d> entered smpi_mpi_bcast(). Calls nary_tree_bcast()",rank);
335 //retval = flat_tree_bcast(buf, count, datatype, root, comm);
336 retval = nary_tree_bcast(buf, count, datatype, root, comm, 2 );
341 * Bcast user entry point
343 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
346 int retval = MPI_SUCCESS;
349 smpi_mpi_bcast(buf,count,datatype,root,comm);
359 * debugging helper function
361 static void print_buffer_int(void *buf, int len, char *msg, int rank)
364 printf("**[%d] %s: ", rank, msg);
365 for (tmp = 0; tmp < len; tmp++) {
367 printf("[%d]", v[tmp]);
372 static void print_buffer_double(void *buf, int len, char *msg, int rank)
376 printf("**[%d] %s: ", rank, msg);
377 for (tmp = 0; tmp < len; tmp++) {
379 printf("[%lf]", v[tmp]);
388 * MPI_Reduce internal level
390 int smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
391 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
393 int retval = MPI_SUCCESS;
397 int system_tag = 666;
398 smpi_mpi_request_t *requests;
399 smpi_mpi_request_t request;
403 rank = smpi_mpi_comm_rank(comm);
405 DEBUG1("<%d> entered smpi_mpi_reduce()",rank);
407 if (rank != root) { // if i am not ROOT, simply send my buffer to root
410 print_buffer_int(sendbuf, count, xbt_strdup("sndbuf"), rank);
412 retval = smpi_create_request(sendbuf, count, datatype, rank, root, system_tag, comm,
414 smpi_mpi_isend(request);
415 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
416 xbt_mallocator_release(smpi_global->request_mallocator, request);
419 // i am the ROOT: wait for all buffers by creating one request by sender
421 requests = xbt_malloc((size-1) * sizeof(smpi_mpi_request_t));
423 void **tmpbufs = xbt_malloc((size-1) * sizeof(void *));
424 for (i = 0; i < size-1; i++) {
425 // we need 1 buffer per request to store intermediate receptions
426 tmpbufs[i] = xbt_malloc(count * datatype->size);
428 // root: initiliaze recv buf with my own snd buf
429 memcpy(recvbuf, sendbuf, count * datatype->size * sizeof(char));
431 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
432 // since we should op values as soon as one receiving request matches.
433 for (i = 0; i < size-1; i++) {
434 // reminder: for smpi_create_request() the src is always the process sending.
435 src = i < root ? i : i + 1;
436 retval = smpi_create_request(tmpbufs[i], count, datatype,
437 src, root, system_tag, comm, &(requests[i]));
438 if (NULL != requests[i] && MPI_SUCCESS == retval) {
439 if (MPI_SUCCESS == retval) {
440 smpi_mpi_irecv(requests[i]);
444 // now, wait for completion of all irecv's.
445 for (i = 0; i < size-1; i++) {
446 int index = MPI_UNDEFINED;
447 smpi_mpi_waitany( size-1, requests, &index, MPI_STATUS_IGNORE);
448 DEBUG3("<%d> waitany() unblocked by reception (completes request[%d]) (%d reqs remaining)",
449 rank,index,size-i-2);
451 print_buffer_int(tmpbufs[index], count, bprintf("tmpbufs[index=%d] (value received)", index),
456 op->func(tmpbufs[index], recvbuf, &count, &datatype);
458 print_buffer_int(recvbuf, count, xbt_strdup("rcvbuf"), rank);
460 xbt_free(tmpbufs[index]);
461 /* FIXME: with the following line, it generates an
462 * [xbt_ex/CRITICAL] Conditional list not empty 162518800.
464 // xbt_mallocator_release(smpi_global->request_mallocator, requests[index]);
473 * MPI_Reduce user entry point
475 int SMPI_MPI_Reduce(void *sendbuf, void *recvbuf, int count,
476 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
478 int retval = MPI_SUCCESS;
482 retval = smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
493 * Same as MPI_Reduce except that the result appears in the receive buffer of all the group members.
495 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
496 MPI_Op op, MPI_Comm comm )
498 int retval = MPI_SUCCESS;
499 int root=0; // arbitrary choice
503 retval = smpi_mpi_reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
504 if (MPI_SUCCESS != retval)
507 retval = smpi_mpi_bcast( sendbuf, count, datatype, root, comm);
515 * MPI_Scatter user entry point
517 int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
518 void *recvbuf, int recvcount, MPI_Datatype recvtype,
519 int root, MPI_Comm comm)
521 int retval = MPI_SUCCESS;
526 char *cptr; // to manipulate the void * buffers
527 smpi_mpi_request_t *requests;
528 smpi_mpi_request_t request;
529 smpi_mpi_status_t status;
534 rank = smpi_mpi_comm_rank(comm);
536 requests = xbt_malloc((comm->size-1) * sizeof(smpi_mpi_request_t));
538 // i am the root: distribute my sendbuf
539 //print_buffer_int(sendbuf, comm->size, xbt_strdup("rcvbuf"), rank);
541 for (i=0; i < comm->size; i++) {
542 if ( i!=root ) { // send to processes ...
544 retval = smpi_create_request((void *)cptr, sendcount,
545 datatype, root, i, tag, comm, &(requests[cnt]));
546 if (NULL != requests[cnt] && MPI_SUCCESS == retval) {
547 if (MPI_SUCCESS == retval) {
548 smpi_mpi_isend(requests[cnt]);
553 else { // ... except if it's me.
554 memcpy(recvbuf, (void *)cptr, recvcount*recvtype->size*sizeof(char));
556 cptr += sendcount*datatype->size;
558 for(i=0; i<cnt; i++) { // wait for send to complete
559 /* FIXME: waitall() should be slightly better */
560 smpi_mpi_wait(requests[i], &status);
561 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
565 else { // i am a non-root process: wait data from the root
566 retval = smpi_create_request(recvbuf,recvcount,
567 recvtype, root, rank, tag, comm, &request);
568 if (NULL != request && MPI_SUCCESS == retval) {
569 if (MPI_SUCCESS == retval) {
570 smpi_mpi_irecv(request);
573 smpi_mpi_wait(request, &status);
574 xbt_mallocator_release(smpi_global->request_mallocator, request);
585 * MPI_Alltoall user entry point
587 * Uses the logic of OpenMPI (upto 1.2.7 or greater) for the optimizations
588 * ompi/mca/coll/tuned/coll_tuned_module.c
590 int SMPI_MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype datatype,
591 void *recvbuf, int recvcount, MPI_Datatype recvtype,
594 int retval = MPI_SUCCESS;
600 rank = smpi_mpi_comm_rank(comm);
601 block_dsize = datatype->size * sendcount;
602 DEBUG2("<%d> optimized alltoall() called. Block size sent to each rank: %d bytes.",rank,block_dsize);
604 if ((block_dsize < 200) && (comm->size > 12)) {
605 retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, datatype,
606 recvbuf, recvcount, recvtype, comm);
608 } else if (block_dsize < 3000) {
609 retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, datatype,
610 recvbuf, recvcount, recvtype, comm);
613 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
614 recvbuf, recvcount, recvtype, comm);
623 * MPI_Alltoallv user entry point
625 * As in OpenMPI, alltoallv is not optimized
626 * ompi/mca/coll/basic/coll_basic_alltoallv.c
628 int SMPI_MPI_Alltoallv(void *sendbuf, int *scounts, int *sdisps, MPI_Datatype datatype,
629 void *recvbuf, int *rcounts, int *rdisps, MPI_Datatype recvtype,
632 int retval = MPI_SUCCESS;
636 rank = smpi_mpi_comm_rank(comm);
637 DEBUG1("<%d> basic alltoallv() called.",rank);
639 retval = smpi_coll_basic_alltoallv(sendbuf, scounts, sdisps, datatype,
640 recvbuf, rcounts, rdisps, recvtype,
649 // used by comm_split to sort ranks based on key values
650 int smpi_compare_rankkeys(const void *a, const void *b);
651 int smpi_compare_rankkeys(const void *a, const void *b)
670 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
673 int retval = MPI_SUCCESS;
676 smpi_mpi_request_t request;
678 smpi_mpi_status_t status;
682 // FIXME: need to test parameters
684 index = smpi_process_index();
685 rank = comm->index_to_rank_map[index];
690 // root node does most of the real work
692 int colormap[comm->size];
693 int keymap[comm->size];
694 int rankkeymap[comm->size * 2];
696 smpi_mpi_communicator_t tempcomm = NULL;
703 // FIXME: use scatter/gather or similar instead of individual comms
704 for (i = 1; i < comm->size; i++) {
705 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
706 rank, MPI_ANY_TAG, comm, &request);
707 smpi_mpi_irecv(request);
708 smpi_mpi_wait(request, &status);
709 colormap[status.MPI_SOURCE] = colorkey[0];
710 keymap[status.MPI_SOURCE] = colorkey[1];
711 xbt_mallocator_release(smpi_global->request_mallocator, request);
714 for (i = 0; i < comm->size; i++) {
715 if (MPI_UNDEFINED == colormap[i]) {
718 // make a list of nodes with current color and sort by keys
720 for (j = i; j < comm->size; j++) {
721 if (colormap[i] == colormap[j]) {
722 colormap[j] = MPI_UNDEFINED;
723 rankkeymap[count * 2] = j;
724 rankkeymap[count * 2 + 1] = keymap[j];
728 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
731 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
732 tempcomm->barrier_count = 0;
733 tempcomm->size = count;
734 tempcomm->barrier_mutex = SIMIX_mutex_init();
735 tempcomm->barrier_cond = SIMIX_cond_init();
736 tempcomm->rank_to_index_map = xbt_new(int, count);
737 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
738 for (j = 0; j < smpi_global->process_count; j++) {
739 tempcomm->index_to_rank_map[j] = -1;
741 for (j = 0; j < count; j++) {
742 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
743 tempcomm->rank_to_index_map[j] = indextmp;
744 tempcomm->index_to_rank_map[indextmp] = j;
746 for (j = 0; j < count; j++) {
747 if (rankkeymap[j * 2]) {
748 retval = smpi_create_request(&j, 1, MPI_INT, 0,
749 rankkeymap[j * 2], 0, comm, &request);
750 request->data = tempcomm;
751 smpi_mpi_isend(request);
752 smpi_mpi_wait(request, &status);
753 xbt_mallocator_release(smpi_global->request_mallocator, request);
755 *comm_out = tempcomm;
762 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
764 smpi_mpi_isend(request);
765 smpi_mpi_wait(request, &status);
766 xbt_mallocator_release(smpi_global->request_mallocator, request);
767 if (MPI_UNDEFINED != color) {
768 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
770 smpi_mpi_irecv(request);
771 smpi_mpi_wait(request, &status);
772 *comm_out = request->data;
781 double SMPI_MPI_Wtime(void)
786 time = SIMIX_get_clock();