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);
70 //------------------------------- Datatypes ---------------------------------------
72 * query the size of the type
74 int SMPI_MPI_Type_size(MPI_Datatype datatype, size_t * size)
76 int retval = MPI_SUCCESS;
80 if (NULL == datatype) {
81 retval = MPI_ERR_TYPE;
82 } else if (NULL == size) {
85 *size = datatype->size;
95 * query extent and lower bound of the type
97 int SMPI_MPI_Type_get_extent( MPI_Datatype datatype, int *lb, int *extent)
99 return( smpi_mpi_type_get_extent( datatype, lb, extent));
105 int SMPI_MPI_Barrier(MPI_Comm comm)
107 int retval = MPI_SUCCESS;
113 retval = MPI_ERR_COMM;
117 * original implemantation:
118 * retval = smpi_mpi_barrier(comm);
119 * this one is unrealistic: it just cond_waits, means no time.
121 retval = nary_tree_barrier( comm, arity );
129 int SMPI_MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
130 int tag, MPI_Comm comm, MPI_Request * request)
132 int retval = MPI_SUCCESS;
136 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
138 if (NULL != *request && MPI_SUCCESS == retval) {
139 retval = smpi_mpi_irecv(*request);
147 int SMPI_MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src,
148 int tag, MPI_Comm comm, MPI_Status * status)
150 int retval = MPI_SUCCESS;
151 smpi_mpi_request_t request;
155 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
157 if (NULL != request && MPI_SUCCESS == retval) {
158 retval = smpi_mpi_irecv(request);
159 if (MPI_SUCCESS == retval) {
160 retval = smpi_mpi_wait(request, status);
162 xbt_mallocator_release(smpi_global->request_mallocator, request);
170 int SMPI_MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
171 int tag, MPI_Comm comm, MPI_Request * request)
173 int retval = MPI_SUCCESS;
177 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
179 if (NULL != *request && MPI_SUCCESS == retval) {
180 retval = smpi_mpi_isend(*request);
188 int SMPI_MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst,
189 int tag, MPI_Comm comm)
191 int retval = MPI_SUCCESS;
192 smpi_mpi_request_t request;
196 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
198 if (NULL != request && MPI_SUCCESS == retval) {
199 retval = smpi_mpi_isend(request);
200 if (MPI_SUCCESS == retval) {
201 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
203 xbt_mallocator_release(smpi_global->request_mallocator, request);
213 * MPI_Sendrecv internal level
215 int smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
216 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
217 MPI_Comm comm, MPI_Status *status)
220 int retval = MPI_SUCCESS;
221 smpi_mpi_request_t srequest;
222 smpi_mpi_request_t rrequest;
224 rank = smpi_mpi_comm_rank(comm);
227 retval = smpi_create_request(sendbuf, sendcount, sendtype,
230 smpi_mpi_isend(srequest);
233 retval = smpi_create_request(recvbuf, recvcount, recvtype,
234 source, rank,recvtag,
236 smpi_mpi_irecv(rrequest);
238 smpi_mpi_wait(srequest, MPI_STATUS_IGNORE);
239 //printf("[%d] isend request src=%d dst=%d tag=%d COMPLETED (retval=%d) \n",rank,rank,dest,sendtag,retval);
240 smpi_mpi_wait(rrequest, MPI_STATUS_IGNORE);
241 //printf("[%d] irecv request src=%d -> dst=%d tag=%d COMPLETED (retval=%d)\n",rank,source,rank,recvtag,retval);
246 * MPI_Sendrecv user entry point
248 int SMPI_MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
249 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
250 MPI_Comm comm, MPI_Status *status)
252 int retval = MPI_SUCCESS;
255 smpi_mpi_sendrecv( sendbuf, sendcount, sendtype, dest, sendtag,
256 recvbuf, recvcount, recvtype, source, recvtag,
266 * MPI_Wait and friends
268 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
270 return smpi_mpi_wait(*request, status);
273 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
275 return smpi_mpi_waitall(count, requests, status);
278 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index,
281 return smpi_mpi_waitany(count, requests, index, status);
291 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm);
292 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
296 int retval = MPI_SUCCESS;
297 smpi_mpi_request_t request;
299 rank = smpi_mpi_comm_rank(comm);
301 retval = smpi_create_request(buf, count, datatype, root,
302 (root + 1) % comm->size, 0, comm, &request);
303 request->forward = comm->size - 1;
304 smpi_mpi_isend(request);
306 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
308 smpi_mpi_irecv(request);
311 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
312 xbt_mallocator_release(smpi_global->request_mallocator, request);
318 * Bcast internal level
320 int smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
323 int retval = MPI_SUCCESS;
324 //retval = flat_tree_bcast(buf, count, datatype, root, comm);
325 retval = nary_tree_bcast(buf, count, datatype, root, comm, 2 );
330 * Bcast user entry point
332 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
335 int retval = MPI_SUCCESS;
338 smpi_mpi_bcast(buf,count,datatype,root,comm);
348 * debugging helper function
350 static void print_buffer_int(void *buf, int len, char *msg, int rank)
353 printf("**[%d] %s: ", rank, msg);
354 for (tmp = 0; tmp < len; tmp++) {
356 printf("[%d]", v[tmp]);
361 static void print_buffer_double(void *buf, int len, char *msg, int rank)
365 printf("**[%d] %s: ", rank, msg);
366 for (tmp = 0; tmp < len; tmp++) {
368 printf("[%lf]", v[tmp]);
377 * MPI_Reduce internal level
379 int smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
380 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
382 int retval = MPI_SUCCESS;
387 smpi_mpi_request_t *requests;
388 smpi_mpi_request_t request;
392 rank = smpi_mpi_comm_rank(comm);
395 if (rank != root) { // if i am not ROOT, simply send my buffer to root
398 print_buffer_int(sendbuf, count, xbt_strdup("sndbuf"), rank);
400 retval = smpi_create_request(sendbuf, count, datatype, rank, root, tag, comm,
402 smpi_mpi_isend(request);
403 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
404 xbt_mallocator_release(smpi_global->request_mallocator, request);
407 // i am the ROOT: wait for all buffers by creating one request by sender
409 requests = xbt_malloc((size-1) * sizeof(smpi_mpi_request_t));
411 void **tmpbufs = xbt_malloc((size-1) * sizeof(void *));
412 for (i = 0; i < size-1; i++) {
413 // we need 1 buffer per request to store intermediate receptions
414 tmpbufs[i] = xbt_malloc(count * datatype->size);
416 // root: initiliaze recv buf with my own snd buf
417 memcpy(recvbuf, sendbuf, count * datatype->size * sizeof(char));
419 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
420 // since we should op values as soon as one receiving request matches.
421 for (i = 0; i < size-1; i++) {
422 // reminder: for smpi_create_request() the src is always the process sending.
423 src = i < root ? i : i + 1;
424 retval = smpi_create_request(tmpbufs[i], count, datatype,
425 src, root, tag, comm, &(requests[i]));
426 if (NULL != requests[i] && MPI_SUCCESS == retval) {
427 if (MPI_SUCCESS == retval) {
428 smpi_mpi_irecv(requests[i]);
432 // now, wait for completion of all irecv's.
433 for (i = 0; i < size-1; i++) {
434 int index = MPI_UNDEFINED;
435 smpi_mpi_waitany( size-1, requests, &index, MPI_STATUS_IGNORE);
437 printf ("MPI_Waitany() unblocked: root received (completes req[index=%d])\n",index);
438 print_buffer_int(tmpbufs[index], count, bprintf("tmpbufs[index=%d] (value received)", index),
443 op->func(tmpbufs[index], recvbuf, &count, &datatype);
445 print_buffer_int(recvbuf, count, xbt_strdup("rcvbuf"), rank);
447 xbt_free(tmpbufs[index]);
448 /* FIXME: with the following line, it generates an
449 * [xbt_ex/CRITICAL] Conditional list not empty 162518800.
451 // xbt_mallocator_release(smpi_global->request_mallocator, requests[index]);
460 * MPI_Reduce user entry point
462 int SMPI_MPI_Reduce(void *sendbuf, void *recvbuf, int count,
463 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
465 int retval = MPI_SUCCESS;
469 retval = smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
480 * Same as MPI_REDUCE except that the result appears in the receive buffer of all the group members.
482 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
483 MPI_Op op, MPI_Comm comm )
485 int retval = MPI_SUCCESS;
486 int root=0; // arbitrary choice
490 retval = smpi_mpi_reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
491 if (MPI_SUCCESS != retval)
494 retval = smpi_mpi_bcast( sendbuf, count, datatype, root, comm);
502 * MPI_Scatter user entry point
504 int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
505 void *recvbuf, int recvcount, MPI_Datatype recvtype,
506 int root, MPI_Comm comm)
508 int retval = MPI_SUCCESS;
513 char *cptr; // to manipulate the void * buffers
514 smpi_mpi_request_t *requests;
515 smpi_mpi_request_t request;
516 smpi_mpi_status_t status;
521 rank = smpi_mpi_comm_rank(comm);
523 requests = xbt_malloc((comm->size-1) * sizeof(smpi_mpi_request_t));
525 // i am the root: distribute my sendbuf
526 //print_buffer_int(sendbuf, comm->size, xbt_strdup("rcvbuf"), rank);
528 for (i=0; i < comm->size; i++) {
529 if ( i!=root ) { // send to processes ...
531 retval = smpi_create_request((void *)cptr, sendcount,
532 datatype, root, i, tag, comm, &(requests[cnt]));
533 if (NULL != requests[cnt] && MPI_SUCCESS == retval) {
534 if (MPI_SUCCESS == retval) {
535 smpi_mpi_isend(requests[cnt]);
540 else { // ... except if it's me.
541 memcpy(recvbuf, (void *)cptr, recvcount*recvtype->size*sizeof(char));
543 cptr += sendcount*datatype->size;
545 for(i=0; i<cnt; i++) { // wait for send to complete
546 /* FIXME: waitall() should be slightly better */
547 smpi_mpi_wait(requests[i], &status);
548 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
552 else { // i am a non-root process: wait data from the root
553 retval = smpi_create_request(recvbuf,recvcount,
554 recvtype, root, rank, tag, comm, &request);
555 if (NULL != request && MPI_SUCCESS == retval) {
556 if (MPI_SUCCESS == retval) {
557 smpi_mpi_irecv(request);
560 smpi_mpi_wait(request, &status);
561 xbt_mallocator_release(smpi_global->request_mallocator, request);
572 * MPI_Alltoall user entry point
574 * Uses the logic of OpenMPI (upto 1.2.7 or greater) for the optimizations
575 * ompi/mca/coll/tuned/coll_tuned_module.c
577 int SMPI_MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype datatype,
578 void *recvbuf, int recvcount, MPI_Datatype recvtype,
581 int retval = MPI_SUCCESS;
587 rank = smpi_mpi_comm_rank(comm);
588 block_dsize = datatype->size * sendcount;
590 if ((block_dsize < 200) && (comm->size > 12)) {
591 retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, datatype,
592 recvbuf, recvcount, recvtype, comm);
594 } else if (block_dsize < 3000) {
595 retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, datatype,
596 recvbuf, recvcount, recvtype, comm);
599 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
600 recvbuf, recvcount, recvtype, comm);
611 // used by comm_split to sort ranks based on key values
612 int smpi_compare_rankkeys(const void *a, const void *b);
613 int smpi_compare_rankkeys(const void *a, const void *b)
632 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
635 int retval = MPI_SUCCESS;
638 smpi_mpi_request_t request;
640 smpi_mpi_status_t status;
644 // FIXME: need to test parameters
646 index = smpi_process_index();
647 rank = comm->index_to_rank_map[index];
652 // root node does most of the real work
654 int colormap[comm->size];
655 int keymap[comm->size];
656 int rankkeymap[comm->size * 2];
658 smpi_mpi_communicator_t tempcomm = NULL;
665 // FIXME: use scatter/gather or similar instead of individual comms
666 for (i = 1; i < comm->size; i++) {
667 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
668 rank, MPI_ANY_TAG, comm, &request);
669 smpi_mpi_irecv(request);
670 smpi_mpi_wait(request, &status);
671 colormap[status.MPI_SOURCE] = colorkey[0];
672 keymap[status.MPI_SOURCE] = colorkey[1];
673 xbt_mallocator_release(smpi_global->request_mallocator, request);
676 for (i = 0; i < comm->size; i++) {
677 if (MPI_UNDEFINED == colormap[i]) {
680 // make a list of nodes with current color and sort by keys
682 for (j = i; j < comm->size; j++) {
683 if (colormap[i] == colormap[j]) {
684 colormap[j] = MPI_UNDEFINED;
685 rankkeymap[count * 2] = j;
686 rankkeymap[count * 2 + 1] = keymap[j];
690 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
693 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
694 tempcomm->barrier_count = 0;
695 tempcomm->size = count;
696 tempcomm->barrier_mutex = SIMIX_mutex_init();
697 tempcomm->barrier_cond = SIMIX_cond_init();
698 tempcomm->rank_to_index_map = xbt_new(int, count);
699 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
700 for (j = 0; j < smpi_global->process_count; j++) {
701 tempcomm->index_to_rank_map[j] = -1;
703 for (j = 0; j < count; j++) {
704 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
705 tempcomm->rank_to_index_map[j] = indextmp;
706 tempcomm->index_to_rank_map[indextmp] = j;
708 for (j = 0; j < count; j++) {
709 if (rankkeymap[j * 2]) {
710 retval = smpi_create_request(&j, 1, MPI_INT, 0,
711 rankkeymap[j * 2], 0, comm, &request);
712 request->data = tempcomm;
713 smpi_mpi_isend(request);
714 smpi_mpi_wait(request, &status);
715 xbt_mallocator_release(smpi_global->request_mallocator, request);
717 *comm_out = tempcomm;
724 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
726 smpi_mpi_isend(request);
727 smpi_mpi_wait(request, &status);
728 xbt_mallocator_release(smpi_global->request_mallocator, request);
729 if (MPI_UNDEFINED != color) {
730 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
732 smpi_mpi_irecv(request);
733 smpi_mpi_wait(request, &status);
734 *comm_out = request->data;
743 double SMPI_MPI_Wtime(void)
745 return (SIMIX_get_clock());