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);
197 int SMPI_MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
198 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
199 MPI_Comm comm, MPI_Status *status)
202 int retval = MPI_SUCCESS;
203 smpi_mpi_request_t srequest;
204 smpi_mpi_request_t rrequest;
206 rank = smpi_mpi_comm_rank(comm);
210 retval = smpi_create_request(sendbuf, sendcount, sendtype,
213 printf("[%d] isend request src=%d -> dst=%d (retval=%d)\n",rank,rank,dest,retval);
214 smpi_mpi_isend(srequest);
217 //retval = MPI_Isend( sendbuf, sendcount, sendtype, dest, sendtag, MPI_COMM_WORLD, &srequest);
221 retval = smpi_create_request(recvbuf, recvcount, recvtype,
222 source, rank,recvtag,
224 printf("[%d] irecv request src=%d -> dst=%d (retval=%d)\n",rank,source,rank,retval);
225 smpi_mpi_irecv(rrequest);
227 //retval = MPI_Irecv( recvbuf, recvcount, recvtype, source, recvtag, MPI_COMM_WORLD, &rrequest);
230 smpi_mpi_wait(srequest, MPI_STATUS_IGNORE);
231 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);
241 * MPI_Wait and friends
243 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
245 return smpi_mpi_wait(*request, status);
248 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
250 return smpi_mpi_waitall(count, requests, status);
253 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index,
256 return smpi_mpi_waitany(count, requests, index, status);
266 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm);
267 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
271 int retval = MPI_SUCCESS;
272 smpi_mpi_request_t request;
274 rank = smpi_mpi_comm_rank(comm);
276 retval = smpi_create_request(buf, count, datatype, root,
277 (root + 1) % comm->size, 0, comm, &request);
278 request->forward = comm->size - 1;
279 smpi_mpi_isend(request);
281 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
283 smpi_mpi_irecv(request);
286 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
287 xbt_mallocator_release(smpi_global->request_mallocator, request);
294 * Bcast user entry point
296 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
299 int retval = MPI_SUCCESS;
303 //retval = flat_tree_bcast(buf, count, datatype, root, comm);
304 retval = nary_tree_bcast(buf, count, datatype, root, comm, 2 );
313 //#ifdef DEBUG_REDUCE
315 * debugging helper function
317 static void print_buffer_int(void *buf, int len, char *msg, int rank)
320 printf("**[%d] %s: ", rank, msg);
321 for (tmp = 0; tmp < len; tmp++) {
323 printf("[%d]", v[tmp]);
328 static void print_buffer_double(void *buf, int len, char *msg, int rank)
332 printf("**[%d] %s: ", rank, msg);
333 for (tmp = 0; tmp < len; tmp++) {
335 printf("[%lf]", v[tmp]);
346 int SMPI_MPI_Reduce(void *sendbuf, void *recvbuf, int count,
347 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
349 int retval = MPI_SUCCESS;
354 smpi_mpi_request_t *requests;
355 smpi_mpi_request_t request;
359 rank = smpi_mpi_comm_rank(comm);
362 if (rank != root) { // if i am not ROOT, simply send my buffer to root
365 print_buffer_int(sendbuf, count, xbt_strdup("sndbuf"), rank);
368 smpi_create_request(sendbuf, count, datatype, rank, root, tag, comm,
370 smpi_mpi_isend(request);
371 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
372 xbt_mallocator_release(smpi_global->request_mallocator, request);
375 // i am the ROOT: wait for all buffers by creating one request by sender
377 requests = xbt_malloc((size-1) * sizeof(smpi_mpi_request_t));
379 void **tmpbufs = xbt_malloc((size-1) * sizeof(void *));
380 for (i = 0; i < size-1; i++) {
381 // we need 1 buffer per request to store intermediate receptions
382 tmpbufs[i] = xbt_malloc(count * datatype->size);
384 // root: initiliaze recv buf with my own snd buf
385 memcpy(recvbuf, sendbuf, count * datatype->size * sizeof(char));
387 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
388 // since we should op values as soon as one receiving request matches.
389 for (i = 0; i < size-1; i++) {
390 // reminder: for smpi_create_request() the src is always the process sending.
391 src = i < root ? i : i + 1;
392 retval = smpi_create_request(tmpbufs[i], count, datatype,
393 src, root, tag, comm, &(requests[i]));
394 if (NULL != requests[i] && MPI_SUCCESS == retval) {
395 if (MPI_SUCCESS == retval) {
396 smpi_mpi_irecv(requests[i]);
400 // now, wait for completion of all irecv's.
401 for (i = 0; i < size-1; i++) {
402 int index = MPI_UNDEFINED;
403 smpi_mpi_waitany( size-1, requests, &index, MPI_STATUS_IGNORE);
405 printf ("MPI_Waitany() unblocked: root received (completes req[index=%d])\n",index);
406 print_buffer_int(tmpbufs[index], count, bprintf("tmpbufs[index=%d] (value received)", index),
411 op->func(tmpbufs[index], recvbuf, &count, &datatype);
413 print_buffer_int(recvbuf, count, xbt_strdup("rcvbuf"), rank);
415 xbt_free(tmpbufs[index]);
416 /* FIXME: with the following line, it generates an
417 * [xbt_ex/CRITICAL] Conditional list not empty 162518800.
419 // xbt_mallocator_release(smpi_global->request_mallocator, requests[index]);
431 * Same as MPI_REDUCE except that the result appears in the receive buffer of all the group members.
433 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
434 MPI_Op op, MPI_Comm comm );
435 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
436 MPI_Op op, MPI_Comm comm )
438 int retval = MPI_SUCCESS;
439 int root=1; // arbitrary choice
443 retval = SMPI_MPI_Reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
444 if (MPI_SUCCESS != retval)
447 retval = SMPI_MPI_Bcast( sendbuf, count, datatype, root, comm);
454 * MPI_Scatter user entry point
456 //int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
457 // void *recvbuf, int recvcount, MPI_Datatype recvtype,int root,
459 int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
460 void *recvbuf, int recvcount, MPI_Datatype recvtype,
461 int root, MPI_Comm comm)
463 int retval = MPI_SUCCESS;
468 char *cptr; // to manipulate the void * buffers
469 smpi_mpi_request_t *requests;
470 smpi_mpi_request_t request;
471 smpi_mpi_status_t status;
476 rank = smpi_mpi_comm_rank(comm);
478 requests = xbt_malloc((comm->size-1) * sizeof(smpi_mpi_request_t));
480 // i am the root: distribute my sendbuf
481 //print_buffer_int(sendbuf, comm->size, xbt_strdup("rcvbuf"), rank);
483 for (i=0; i < comm->size; i++) {
484 if ( i!=root ) { // send to processes ...
486 retval = smpi_create_request((void *)cptr, sendcount,
487 datatype, root, i, tag, comm, &(requests[cnt]));
488 if (NULL != requests[cnt] && MPI_SUCCESS == retval) {
489 if (MPI_SUCCESS == retval) {
490 smpi_mpi_isend(requests[cnt]);
495 else { // ... except if it's me.
496 memcpy(recvbuf, (void *)cptr, recvcount*recvtype->size*sizeof(char));
498 cptr += sendcount*datatype->size;
500 for(i=0; i<cnt; i++) { // wait for send to complete
501 /* FIXME: waitall() should be slightly better */
502 smpi_mpi_wait(requests[i], &status);
503 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
507 else { // i am a non-root process: wait data from the root
508 retval = smpi_create_request(recvbuf,recvcount,
509 recvtype, root, rank, tag, comm, &request);
510 if (NULL != request && MPI_SUCCESS == retval) {
511 if (MPI_SUCCESS == retval) {
512 smpi_mpi_irecv(request);
515 smpi_mpi_wait(request, &status);
516 xbt_mallocator_release(smpi_global->request_mallocator, request);
527 * MPI_Alltoall user entry point
529 * Uses the logic of OpenMPI (upto 1.2.7 or greater) for the optimizations
530 * ompi/mca/coll/tuned/coll_tuned_module.c
532 int SMPI_MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype datatype,
533 void *recvbuf, int recvcount, MPI_Datatype recvtype,
536 int retval = MPI_SUCCESS;
542 rank = smpi_mpi_comm_rank(comm);
543 block_dsize = datatype->size * sendcount;
545 if ((block_dsize < 200) && (comm->size > 12)) {
546 retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, datatype,
547 recvbuf, recvcount, recvtype, comm);
549 } else if (block_dsize < 3000) {
550 /* use this one !! retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, datatype,
551 recvbuf, recvcount, recvtype, comm);
553 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
554 recvbuf, recvcount, recvtype, comm);
557 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
558 recvbuf, recvcount, recvtype, comm);
569 // used by comm_split to sort ranks based on key values
570 int smpi_compare_rankkeys(const void *a, const void *b);
571 int smpi_compare_rankkeys(const void *a, const void *b)
590 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
593 int retval = MPI_SUCCESS;
596 smpi_mpi_request_t request;
598 smpi_mpi_status_t status;
602 // FIXME: need to test parameters
604 index = smpi_process_index();
605 rank = comm->index_to_rank_map[index];
610 // root node does most of the real work
612 int colormap[comm->size];
613 int keymap[comm->size];
614 int rankkeymap[comm->size * 2];
616 smpi_mpi_communicator_t tempcomm = NULL;
623 // FIXME: use scatter/gather or similar instead of individual comms
624 for (i = 1; i < comm->size; i++) {
625 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
626 rank, MPI_ANY_TAG, comm, &request);
627 smpi_mpi_irecv(request);
628 smpi_mpi_wait(request, &status);
629 colormap[status.MPI_SOURCE] = colorkey[0];
630 keymap[status.MPI_SOURCE] = colorkey[1];
631 xbt_mallocator_release(smpi_global->request_mallocator, request);
634 for (i = 0; i < comm->size; i++) {
635 if (MPI_UNDEFINED == colormap[i]) {
638 // make a list of nodes with current color and sort by keys
640 for (j = i; j < comm->size; j++) {
641 if (colormap[i] == colormap[j]) {
642 colormap[j] = MPI_UNDEFINED;
643 rankkeymap[count * 2] = j;
644 rankkeymap[count * 2 + 1] = keymap[j];
648 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
651 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
652 tempcomm->barrier_count = 0;
653 tempcomm->size = count;
654 tempcomm->barrier_mutex = SIMIX_mutex_init();
655 tempcomm->barrier_cond = SIMIX_cond_init();
656 tempcomm->rank_to_index_map = xbt_new(int, count);
657 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
658 for (j = 0; j < smpi_global->process_count; j++) {
659 tempcomm->index_to_rank_map[j] = -1;
661 for (j = 0; j < count; j++) {
662 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
663 tempcomm->rank_to_index_map[j] = indextmp;
664 tempcomm->index_to_rank_map[indextmp] = j;
666 for (j = 0; j < count; j++) {
667 if (rankkeymap[j * 2]) {
668 retval = smpi_create_request(&j, 1, MPI_INT, 0,
669 rankkeymap[j * 2], 0, comm, &request);
670 request->data = tempcomm;
671 smpi_mpi_isend(request);
672 smpi_mpi_wait(request, &status);
673 xbt_mallocator_release(smpi_global->request_mallocator, request);
675 *comm_out = tempcomm;
682 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
684 smpi_mpi_isend(request);
685 smpi_mpi_wait(request, &status);
686 xbt_mallocator_release(smpi_global->request_mallocator, request);
687 if (MPI_UNDEFINED != color) {
688 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
690 smpi_mpi_irecv(request);
691 smpi_mpi_wait(request, &status);
692 *comm_out = request->data;
701 double SMPI_MPI_Wtime(void)
703 return (SIMIX_get_clock());