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);
196 * MPI_Sendrecv internal level
198 int smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
199 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
200 MPI_Comm comm, MPI_Status *status)
203 int retval = MPI_SUCCESS;
204 smpi_mpi_request_t srequest;
205 smpi_mpi_request_t rrequest;
207 rank = smpi_mpi_comm_rank(comm);
210 retval = smpi_create_request(sendbuf, sendcount, sendtype,
213 smpi_mpi_isend(srequest);
216 retval = smpi_create_request(recvbuf, recvcount, recvtype,
217 source, rank,recvtag,
219 smpi_mpi_irecv(rrequest);
221 smpi_mpi_wait(srequest, MPI_STATUS_IGNORE);
222 //printf("[%d] isend request src=%d dst=%d tag=%d COMPLETED (retval=%d) \n",rank,rank,dest,sendtag,retval);
223 smpi_mpi_wait(rrequest, MPI_STATUS_IGNORE);
224 //printf("[%d] irecv request src=%d -> dst=%d tag=%d COMPLETED (retval=%d)\n",rank,source,rank,recvtag,retval);
229 * MPI_Sendrecv user entry point
231 int SMPI_MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
232 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
233 MPI_Comm comm, MPI_Status *status)
235 int retval = MPI_SUCCESS;
238 smpi_mpi_sendrecv( sendbuf, sendcount, sendtype, dest, sendtag,
239 recvbuf, recvcount, recvtype, source, recvtag,
249 * MPI_Wait and friends
251 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
253 return smpi_mpi_wait(*request, status);
256 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
258 return smpi_mpi_waitall(count, requests, status);
261 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index,
264 return smpi_mpi_waitany(count, requests, index, status);
274 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm);
275 int flat_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
279 int retval = MPI_SUCCESS;
280 smpi_mpi_request_t request;
282 rank = smpi_mpi_comm_rank(comm);
284 retval = smpi_create_request(buf, count, datatype, root,
285 (root + 1) % comm->size, 0, comm, &request);
286 request->forward = comm->size - 1;
287 smpi_mpi_isend(request);
289 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
291 smpi_mpi_irecv(request);
294 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
295 xbt_mallocator_release(smpi_global->request_mallocator, request);
301 * Bcast internal level
303 int smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
306 int retval = MPI_SUCCESS;
307 //retval = flat_tree_bcast(buf, count, datatype, root, comm);
308 retval = nary_tree_bcast(buf, count, datatype, root, comm, 2 );
313 * Bcast user entry point
315 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
318 int retval = MPI_SUCCESS;
321 smpi_mpi_bcast(buf,count,datatype,root,comm);
331 * debugging helper function
333 static void print_buffer_int(void *buf, int len, char *msg, int rank)
336 printf("**[%d] %s: ", rank, msg);
337 for (tmp = 0; tmp < len; tmp++) {
339 printf("[%d]", v[tmp]);
344 static void print_buffer_double(void *buf, int len, char *msg, int rank)
348 printf("**[%d] %s: ", rank, msg);
349 for (tmp = 0; tmp < len; tmp++) {
351 printf("[%lf]", v[tmp]);
360 * MPI_Reduce internal level
362 int smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
363 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
365 int retval = MPI_SUCCESS;
370 smpi_mpi_request_t *requests;
371 smpi_mpi_request_t request;
375 rank = smpi_mpi_comm_rank(comm);
378 if (rank != root) { // if i am not ROOT, simply send my buffer to root
381 print_buffer_int(sendbuf, count, xbt_strdup("sndbuf"), rank);
383 retval = smpi_create_request(sendbuf, count, datatype, rank, root, tag, comm,
385 smpi_mpi_isend(request);
386 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
387 xbt_mallocator_release(smpi_global->request_mallocator, request);
390 // i am the ROOT: wait for all buffers by creating one request by sender
392 requests = xbt_malloc((size-1) * sizeof(smpi_mpi_request_t));
394 void **tmpbufs = xbt_malloc((size-1) * sizeof(void *));
395 for (i = 0; i < size-1; i++) {
396 // we need 1 buffer per request to store intermediate receptions
397 tmpbufs[i] = xbt_malloc(count * datatype->size);
399 // root: initiliaze recv buf with my own snd buf
400 memcpy(recvbuf, sendbuf, count * datatype->size * sizeof(char));
402 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
403 // since we should op values as soon as one receiving request matches.
404 for (i = 0; i < size-1; i++) {
405 // reminder: for smpi_create_request() the src is always the process sending.
406 src = i < root ? i : i + 1;
407 retval = smpi_create_request(tmpbufs[i], count, datatype,
408 src, root, tag, comm, &(requests[i]));
409 if (NULL != requests[i] && MPI_SUCCESS == retval) {
410 if (MPI_SUCCESS == retval) {
411 smpi_mpi_irecv(requests[i]);
415 // now, wait for completion of all irecv's.
416 for (i = 0; i < size-1; i++) {
417 int index = MPI_UNDEFINED;
418 smpi_mpi_waitany( size-1, requests, &index, MPI_STATUS_IGNORE);
420 printf ("MPI_Waitany() unblocked: root received (completes req[index=%d])\n",index);
421 print_buffer_int(tmpbufs[index], count, bprintf("tmpbufs[index=%d] (value received)", index),
426 op->func(tmpbufs[index], recvbuf, &count, &datatype);
428 print_buffer_int(recvbuf, count, xbt_strdup("rcvbuf"), rank);
430 xbt_free(tmpbufs[index]);
431 /* FIXME: with the following line, it generates an
432 * [xbt_ex/CRITICAL] Conditional list not empty 162518800.
434 // xbt_mallocator_release(smpi_global->request_mallocator, requests[index]);
443 * MPI_Reduce user entry point
445 int SMPI_MPI_Reduce(void *sendbuf, void *recvbuf, int count,
446 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
448 int retval = MPI_SUCCESS;
452 retval = smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
463 * Same as MPI_REDUCE except that the result appears in the receive buffer of all the group members.
465 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
466 MPI_Op op, MPI_Comm comm )
468 int retval = MPI_SUCCESS;
469 int root=0; // arbitrary choice
473 retval = smpi_mpi_reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
474 if (MPI_SUCCESS != retval)
477 retval = smpi_mpi_bcast( sendbuf, count, datatype, root, comm);
485 * MPI_Scatter user entry point
487 int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
488 void *recvbuf, int recvcount, MPI_Datatype recvtype,
489 int root, MPI_Comm comm)
491 int retval = MPI_SUCCESS;
496 char *cptr; // to manipulate the void * buffers
497 smpi_mpi_request_t *requests;
498 smpi_mpi_request_t request;
499 smpi_mpi_status_t status;
504 rank = smpi_mpi_comm_rank(comm);
506 requests = xbt_malloc((comm->size-1) * sizeof(smpi_mpi_request_t));
508 // i am the root: distribute my sendbuf
509 //print_buffer_int(sendbuf, comm->size, xbt_strdup("rcvbuf"), rank);
511 for (i=0; i < comm->size; i++) {
512 if ( i!=root ) { // send to processes ...
514 retval = smpi_create_request((void *)cptr, sendcount,
515 datatype, root, i, tag, comm, &(requests[cnt]));
516 if (NULL != requests[cnt] && MPI_SUCCESS == retval) {
517 if (MPI_SUCCESS == retval) {
518 smpi_mpi_isend(requests[cnt]);
523 else { // ... except if it's me.
524 memcpy(recvbuf, (void *)cptr, recvcount*recvtype->size*sizeof(char));
526 cptr += sendcount*datatype->size;
528 for(i=0; i<cnt; i++) { // wait for send to complete
529 /* FIXME: waitall() should be slightly better */
530 smpi_mpi_wait(requests[i], &status);
531 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
535 else { // i am a non-root process: wait data from the root
536 retval = smpi_create_request(recvbuf,recvcount,
537 recvtype, root, rank, tag, comm, &request);
538 if (NULL != request && MPI_SUCCESS == retval) {
539 if (MPI_SUCCESS == retval) {
540 smpi_mpi_irecv(request);
543 smpi_mpi_wait(request, &status);
544 xbt_mallocator_release(smpi_global->request_mallocator, request);
555 * MPI_Alltoall user entry point
557 * Uses the logic of OpenMPI (upto 1.2.7 or greater) for the optimizations
558 * ompi/mca/coll/tuned/coll_tuned_module.c
560 int SMPI_MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype datatype,
561 void *recvbuf, int recvcount, MPI_Datatype recvtype,
564 int retval = MPI_SUCCESS;
570 rank = smpi_mpi_comm_rank(comm);
571 block_dsize = datatype->size * sendcount;
573 if ((block_dsize < 200) && (comm->size > 12)) {
574 retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, datatype,
575 recvbuf, recvcount, recvtype, comm);
577 } else if (block_dsize < 3000) {
578 /* use this one !! retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, datatype,
579 recvbuf, recvcount, recvtype, comm);
581 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
582 recvbuf, recvcount, recvtype, comm);
585 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
586 recvbuf, recvcount, recvtype, comm);
597 // used by comm_split to sort ranks based on key values
598 int smpi_compare_rankkeys(const void *a, const void *b);
599 int smpi_compare_rankkeys(const void *a, const void *b)
618 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
621 int retval = MPI_SUCCESS;
624 smpi_mpi_request_t request;
626 smpi_mpi_status_t status;
630 // FIXME: need to test parameters
632 index = smpi_process_index();
633 rank = comm->index_to_rank_map[index];
638 // root node does most of the real work
640 int colormap[comm->size];
641 int keymap[comm->size];
642 int rankkeymap[comm->size * 2];
644 smpi_mpi_communicator_t tempcomm = NULL;
651 // FIXME: use scatter/gather or similar instead of individual comms
652 for (i = 1; i < comm->size; i++) {
653 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
654 rank, MPI_ANY_TAG, comm, &request);
655 smpi_mpi_irecv(request);
656 smpi_mpi_wait(request, &status);
657 colormap[status.MPI_SOURCE] = colorkey[0];
658 keymap[status.MPI_SOURCE] = colorkey[1];
659 xbt_mallocator_release(smpi_global->request_mallocator, request);
662 for (i = 0; i < comm->size; i++) {
663 if (MPI_UNDEFINED == colormap[i]) {
666 // make a list of nodes with current color and sort by keys
668 for (j = i; j < comm->size; j++) {
669 if (colormap[i] == colormap[j]) {
670 colormap[j] = MPI_UNDEFINED;
671 rankkeymap[count * 2] = j;
672 rankkeymap[count * 2 + 1] = keymap[j];
676 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
679 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
680 tempcomm->barrier_count = 0;
681 tempcomm->size = count;
682 tempcomm->barrier_mutex = SIMIX_mutex_init();
683 tempcomm->barrier_cond = SIMIX_cond_init();
684 tempcomm->rank_to_index_map = xbt_new(int, count);
685 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
686 for (j = 0; j < smpi_global->process_count; j++) {
687 tempcomm->index_to_rank_map[j] = -1;
689 for (j = 0; j < count; j++) {
690 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
691 tempcomm->rank_to_index_map[j] = indextmp;
692 tempcomm->index_to_rank_map[indextmp] = j;
694 for (j = 0; j < count; j++) {
695 if (rankkeymap[j * 2]) {
696 retval = smpi_create_request(&j, 1, MPI_INT, 0,
697 rankkeymap[j * 2], 0, comm, &request);
698 request->data = tempcomm;
699 smpi_mpi_isend(request);
700 smpi_mpi_wait(request, &status);
701 xbt_mallocator_release(smpi_global->request_mallocator, request);
703 *comm_out = tempcomm;
710 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
712 smpi_mpi_isend(request);
713 smpi_mpi_wait(request, &status);
714 xbt_mallocator_release(smpi_global->request_mallocator, request);
715 if (MPI_UNDEFINED != color) {
716 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
718 smpi_mpi_irecv(request);
719 smpi_mpi_wait(request, &status);
720 *comm_out = request->data;
729 double SMPI_MPI_Wtime(void)
731 return (SIMIX_get_clock());