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
441 //smpi_bench_end(); //FIXME: restaure after calling smpi_mpi_reduce instead
444 retval = SMPI_MPI_Reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
445 if (MPI_SUCCESS != retval)
448 DEBUG0("Reduce done, time to bcast");
449 retval = SMPI_MPI_Bcast( sendbuf, count, datatype, root, comm);
450 // smpi_bench_begin();
456 * MPI_Scatter user entry point
458 //int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
459 // void *recvbuf, int recvcount, MPI_Datatype recvtype,int root,
461 int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
462 void *recvbuf, int recvcount, MPI_Datatype recvtype,
463 int root, MPI_Comm comm)
465 int retval = MPI_SUCCESS;
470 char *cptr; // to manipulate the void * buffers
471 smpi_mpi_request_t *requests;
472 smpi_mpi_request_t request;
473 smpi_mpi_status_t status;
478 rank = smpi_mpi_comm_rank(comm);
480 requests = xbt_malloc((comm->size-1) * sizeof(smpi_mpi_request_t));
482 // i am the root: distribute my sendbuf
483 //print_buffer_int(sendbuf, comm->size, xbt_strdup("rcvbuf"), rank);
485 for (i=0; i < comm->size; i++) {
486 if ( i!=root ) { // send to processes ...
488 retval = smpi_create_request((void *)cptr, sendcount,
489 datatype, root, i, tag, comm, &(requests[cnt]));
490 if (NULL != requests[cnt] && MPI_SUCCESS == retval) {
491 if (MPI_SUCCESS == retval) {
492 smpi_mpi_isend(requests[cnt]);
497 else { // ... except if it's me.
498 memcpy(recvbuf, (void *)cptr, recvcount*recvtype->size*sizeof(char));
500 cptr += sendcount*datatype->size;
502 for(i=0; i<cnt; i++) { // wait for send to complete
503 /* FIXME: waitall() should be slightly better */
504 smpi_mpi_wait(requests[i], &status);
505 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
509 else { // i am a non-root process: wait data from the root
510 retval = smpi_create_request(recvbuf,recvcount,
511 recvtype, root, rank, tag, comm, &request);
512 if (NULL != request && MPI_SUCCESS == retval) {
513 if (MPI_SUCCESS == retval) {
514 smpi_mpi_irecv(request);
517 smpi_mpi_wait(request, &status);
518 xbt_mallocator_release(smpi_global->request_mallocator, request);
529 * MPI_Alltoall user entry point
531 * Uses the logic of OpenMPI (upto 1.2.7 or greater) for the optimizations
532 * ompi/mca/coll/tuned/coll_tuned_module.c
534 int SMPI_MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype datatype,
535 void *recvbuf, int recvcount, MPI_Datatype recvtype,
538 int retval = MPI_SUCCESS;
544 rank = smpi_mpi_comm_rank(comm);
545 block_dsize = datatype->size * sendcount;
547 if ((block_dsize < 200) && (comm->size > 12)) {
548 retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, datatype,
549 recvbuf, recvcount, recvtype, comm);
551 } else if (block_dsize < 3000) {
552 /* use this one !! retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, datatype,
553 recvbuf, recvcount, recvtype, comm);
555 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
556 recvbuf, recvcount, recvtype, comm);
559 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
560 recvbuf, recvcount, recvtype, comm);
571 // used by comm_split to sort ranks based on key values
572 int smpi_compare_rankkeys(const void *a, const void *b);
573 int smpi_compare_rankkeys(const void *a, const void *b)
592 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
595 int retval = MPI_SUCCESS;
598 smpi_mpi_request_t request;
600 smpi_mpi_status_t status;
604 // FIXME: need to test parameters
606 index = smpi_process_index();
607 rank = comm->index_to_rank_map[index];
612 // root node does most of the real work
614 int colormap[comm->size];
615 int keymap[comm->size];
616 int rankkeymap[comm->size * 2];
618 smpi_mpi_communicator_t tempcomm = NULL;
625 // FIXME: use scatter/gather or similar instead of individual comms
626 for (i = 1; i < comm->size; i++) {
627 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
628 rank, MPI_ANY_TAG, comm, &request);
629 smpi_mpi_irecv(request);
630 smpi_mpi_wait(request, &status);
631 colormap[status.MPI_SOURCE] = colorkey[0];
632 keymap[status.MPI_SOURCE] = colorkey[1];
633 xbt_mallocator_release(smpi_global->request_mallocator, request);
636 for (i = 0; i < comm->size; i++) {
637 if (MPI_UNDEFINED == colormap[i]) {
640 // make a list of nodes with current color and sort by keys
642 for (j = i; j < comm->size; j++) {
643 if (colormap[i] == colormap[j]) {
644 colormap[j] = MPI_UNDEFINED;
645 rankkeymap[count * 2] = j;
646 rankkeymap[count * 2 + 1] = keymap[j];
650 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
653 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
654 tempcomm->barrier_count = 0;
655 tempcomm->size = count;
656 tempcomm->barrier_mutex = SIMIX_mutex_init();
657 tempcomm->barrier_cond = SIMIX_cond_init();
658 tempcomm->rank_to_index_map = xbt_new(int, count);
659 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
660 for (j = 0; j < smpi_global->process_count; j++) {
661 tempcomm->index_to_rank_map[j] = -1;
663 for (j = 0; j < count; j++) {
664 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
665 tempcomm->rank_to_index_map[j] = indextmp;
666 tempcomm->index_to_rank_map[indextmp] = j;
668 for (j = 0; j < count; j++) {
669 if (rankkeymap[j * 2]) {
670 retval = smpi_create_request(&j, 1, MPI_INT, 0,
671 rankkeymap[j * 2], 0, comm, &request);
672 request->data = tempcomm;
673 smpi_mpi_isend(request);
674 smpi_mpi_wait(request, &status);
675 xbt_mallocator_release(smpi_global->request_mallocator, request);
677 *comm_out = tempcomm;
684 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
686 smpi_mpi_isend(request);
687 smpi_mpi_wait(request, &status);
688 xbt_mallocator_release(smpi_global->request_mallocator, request);
689 if (MPI_UNDEFINED != color) {
690 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
692 smpi_mpi_irecv(request);
693 smpi_mpi_wait(request, &status);
694 *comm_out = request->data;
703 double SMPI_MPI_Wtime(void)
705 return (SIMIX_get_clock());