4 #include "smpi_coll_private.h"
5 #include "smpi_mpi_dt_private.h"
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi,
8 "Logging specific to SMPI (mpi)");
10 int SMPI_MPI_Init(int *argc, char ***argv)
12 smpi_process_init(argc, argv);
17 int SMPI_MPI_Finalize()
20 smpi_process_finalize();
24 // right now this just exits the current node, should send abort signal to all
25 // hosts in the communicator (TODO)
26 int SMPI_MPI_Abort(MPI_Comm comm, int errorcode)
32 int SMPI_MPI_Comm_size(MPI_Comm comm, int *size)
34 int retval = MPI_SUCCESS;
39 retval = MPI_ERR_COMM;
40 } else if (NULL == size) {
51 int SMPI_MPI_Comm_rank(MPI_Comm comm, int *rank)
53 int retval = MPI_SUCCESS;
58 retval = MPI_ERR_COMM;
59 } else if (NULL == rank) {
62 *rank = smpi_mpi_comm_rank(comm);
75 int SMPI_MPI_Barrier(MPI_Comm comm)
77 int retval = MPI_SUCCESS;
83 retval = MPI_ERR_COMM;
87 * original implemantation:
88 * retval = smpi_mpi_barrier(comm);
89 * this one is unrealistic: it just cond_waits, means no time.
91 retval = nary_tree_barrier( comm, arity );
101 int SMPI_MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
102 int tag, MPI_Comm comm, MPI_Request * request)
104 int retval = MPI_SUCCESS;
108 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
110 if (NULL != *request && MPI_SUCCESS == retval) {
111 retval = smpi_mpi_irecv(*request);
119 int SMPI_MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src,
120 int tag, MPI_Comm comm, MPI_Status * status)
122 int retval = MPI_SUCCESS;
123 smpi_mpi_request_t request;
127 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
129 if (NULL != request && MPI_SUCCESS == retval) {
130 retval = smpi_mpi_irecv(request);
131 if (MPI_SUCCESS == retval) {
132 retval = smpi_mpi_wait(request, status);
134 xbt_mallocator_release(smpi_global->request_mallocator, request);
142 int SMPI_MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
143 int tag, MPI_Comm comm, MPI_Request * request)
145 int retval = MPI_SUCCESS;
149 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
151 if (NULL != *request && MPI_SUCCESS == retval) {
152 retval = smpi_mpi_isend(*request);
161 * MPI_Send user level
163 int SMPI_MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst,
164 int tag, MPI_Comm comm)
166 int retval = MPI_SUCCESS;
167 smpi_mpi_request_t request;
171 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
173 if (NULL != request && MPI_SUCCESS == retval) {
174 retval = smpi_mpi_isend(request);
175 if (MPI_SUCCESS == retval) {
176 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
178 xbt_mallocator_release(smpi_global->request_mallocator, request);
188 * MPI_Sendrecv internal level
190 int smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
191 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
192 MPI_Comm comm, MPI_Status *status)
195 int retval = MPI_SUCCESS;
196 smpi_mpi_request_t srequest;
197 smpi_mpi_request_t rrequest;
199 rank = smpi_mpi_comm_rank(comm);
202 retval = smpi_create_request(sendbuf, sendcount, sendtype,
205 smpi_mpi_isend(srequest);
208 retval = smpi_create_request(recvbuf, recvcount, recvtype,
209 source, rank,recvtag,
211 smpi_mpi_irecv(rrequest);
213 smpi_mpi_wait(srequest, MPI_STATUS_IGNORE);
214 //printf("[%d] isend request src=%d dst=%d tag=%d COMPLETED (retval=%d) \n",rank,rank,dest,sendtag,retval);
215 smpi_mpi_wait(rrequest, MPI_STATUS_IGNORE);
216 //printf("[%d] irecv request src=%d -> dst=%d tag=%d COMPLETED (retval=%d)\n",rank,source,rank,recvtag,retval);
221 * MPI_Sendrecv user entry point
223 int SMPI_MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag,
224 void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag,
225 MPI_Comm comm, MPI_Status *status)
227 int retval = MPI_SUCCESS;
230 smpi_mpi_sendrecv( sendbuf, sendcount, sendtype, dest, sendtag,
231 recvbuf, recvcount, recvtype, source, recvtag,
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);
293 * Bcast internal level
295 int smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
298 int retval = MPI_SUCCESS;
299 int rank = smpi_mpi_comm_rank(comm);
301 DEBUG1("<%d> entered smpi_mpi_bcast(). Calls nary_tree_bcast()",rank);
302 //retval = flat_tree_bcast(buf, count, datatype, root, comm);
303 retval = nary_tree_bcast(buf, count, datatype, root, comm, 2 );
308 * Bcast user entry point
310 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
313 int retval = MPI_SUCCESS;
316 smpi_mpi_bcast(buf,count,datatype,root,comm);
326 * debugging helper function
328 static void print_buffer_int(void *buf, int len, char *msg, int rank)
331 printf("**[%d] %s: ", rank, msg);
332 for (tmp = 0; tmp < len; tmp++) {
334 printf("[%d]", v[tmp]);
339 static void print_buffer_double(void *buf, int len, char *msg, int rank)
343 printf("**[%d] %s: ", rank, msg);
344 for (tmp = 0; tmp < len; tmp++) {
346 printf("[%lf]", v[tmp]);
355 * MPI_Reduce internal level
357 int smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
358 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
360 int retval = MPI_SUCCESS;
364 int system_tag = 666;
365 smpi_mpi_request_t *requests;
366 smpi_mpi_request_t request;
370 rank = smpi_mpi_comm_rank(comm);
372 DEBUG1("<%d> entered smpi_mpi_reduce()",rank);
374 if (rank != root) { // if i am not ROOT, simply send my buffer to root
377 print_buffer_int(sendbuf, count, xbt_strdup("sndbuf"), rank);
379 retval = smpi_create_request(sendbuf, count, datatype, rank, root, system_tag, comm,
381 smpi_mpi_isend(request);
382 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
383 xbt_mallocator_release(smpi_global->request_mallocator, request);
386 // i am the ROOT: wait for all buffers by creating one request by sender
388 requests = xbt_malloc((size-1) * sizeof(smpi_mpi_request_t));
390 void **tmpbufs = xbt_malloc((size-1) * sizeof(void *));
391 for (i = 0; i < size-1; i++) {
392 // we need 1 buffer per request to store intermediate receptions
393 tmpbufs[i] = xbt_malloc(count * datatype->size);
395 // root: initiliaze recv buf with my own snd buf
396 memcpy(recvbuf, sendbuf, count * datatype->size * sizeof(char));
398 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
399 // since we should op values as soon as one receiving request matches.
400 for (i = 0; i < size-1; i++) {
401 // reminder: for smpi_create_request() the src is always the process sending.
402 src = i < root ? i : i + 1;
403 retval = smpi_create_request(tmpbufs[i], count, datatype,
404 src, root, system_tag, comm, &(requests[i]));
405 if (NULL != requests[i] && MPI_SUCCESS == retval) {
406 if (MPI_SUCCESS == retval) {
407 smpi_mpi_irecv(requests[i]);
411 // now, wait for completion of all irecv's.
412 for (i = 0; i < size-1; i++) {
413 int index = MPI_UNDEFINED;
414 smpi_mpi_waitany( size-1, requests, &index, MPI_STATUS_IGNORE);
415 DEBUG3("<%d> waitany() unblocked by reception (completes request[%d]) (%d reqs remaining)",
416 rank,index,size-i-2);
418 print_buffer_int(tmpbufs[index], count, bprintf("tmpbufs[index=%d] (value received)", index),
423 op->func(tmpbufs[index], recvbuf, &count, &datatype);
425 print_buffer_int(recvbuf, count, xbt_strdup("rcvbuf"), rank);
427 xbt_free(tmpbufs[index]);
428 /* FIXME: with the following line, it generates an
429 * [xbt_ex/CRITICAL] Conditional list not empty 162518800.
431 // xbt_mallocator_release(smpi_global->request_mallocator, requests[index]);
440 * MPI_Reduce user entry point
442 int SMPI_MPI_Reduce(void *sendbuf, void *recvbuf, int count,
443 MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
445 int retval = MPI_SUCCESS;
449 retval = smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
460 * Same as MPI_REDUCE except that the result appears in the receive buffer of all the group members.
462 int SMPI_MPI_Allreduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
463 MPI_Op op, MPI_Comm comm )
465 int retval = MPI_SUCCESS;
466 int root=0; // arbitrary choice
470 retval = smpi_mpi_reduce( sendbuf, recvbuf, count, datatype, op, root, comm);
471 if (MPI_SUCCESS != retval)
474 retval = smpi_mpi_bcast( sendbuf, count, datatype, root, comm);
482 * MPI_Scatter user entry point
484 int SMPI_MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype datatype,
485 void *recvbuf, int recvcount, MPI_Datatype recvtype,
486 int root, MPI_Comm comm)
488 int retval = MPI_SUCCESS;
493 char *cptr; // to manipulate the void * buffers
494 smpi_mpi_request_t *requests;
495 smpi_mpi_request_t request;
496 smpi_mpi_status_t status;
501 rank = smpi_mpi_comm_rank(comm);
503 requests = xbt_malloc((comm->size-1) * sizeof(smpi_mpi_request_t));
505 // i am the root: distribute my sendbuf
506 //print_buffer_int(sendbuf, comm->size, xbt_strdup("rcvbuf"), rank);
508 for (i=0; i < comm->size; i++) {
509 if ( i!=root ) { // send to processes ...
511 retval = smpi_create_request((void *)cptr, sendcount,
512 datatype, root, i, tag, comm, &(requests[cnt]));
513 if (NULL != requests[cnt] && MPI_SUCCESS == retval) {
514 if (MPI_SUCCESS == retval) {
515 smpi_mpi_isend(requests[cnt]);
520 else { // ... except if it's me.
521 memcpy(recvbuf, (void *)cptr, recvcount*recvtype->size*sizeof(char));
523 cptr += sendcount*datatype->size;
525 for(i=0; i<cnt; i++) { // wait for send to complete
526 /* FIXME: waitall() should be slightly better */
527 smpi_mpi_wait(requests[i], &status);
528 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
532 else { // i am a non-root process: wait data from the root
533 retval = smpi_create_request(recvbuf,recvcount,
534 recvtype, root, rank, tag, comm, &request);
535 if (NULL != request && MPI_SUCCESS == retval) {
536 if (MPI_SUCCESS == retval) {
537 smpi_mpi_irecv(request);
540 smpi_mpi_wait(request, &status);
541 xbt_mallocator_release(smpi_global->request_mallocator, request);
552 * MPI_Alltoall user entry point
554 * Uses the logic of OpenMPI (upto 1.2.7 or greater) for the optimizations
555 * ompi/mca/coll/tuned/coll_tuned_module.c
557 int SMPI_MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype datatype,
558 void *recvbuf, int recvcount, MPI_Datatype recvtype,
561 int retval = MPI_SUCCESS;
567 rank = smpi_mpi_comm_rank(comm);
568 block_dsize = datatype->size * sendcount;
569 DEBUG2("<%d> optimized alltoall() called. Block size sent to each rank: %d bytes.",rank,block_dsize);
571 if ((block_dsize < 200) && (comm->size > 12)) {
572 retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, datatype,
573 recvbuf, recvcount, recvtype, comm);
575 } else if (block_dsize < 3000) {
576 retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, datatype,
577 recvbuf, recvcount, recvtype, comm);
580 retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, datatype,
581 recvbuf, recvcount, recvtype, comm);
592 // used by comm_split to sort ranks based on key values
593 int smpi_compare_rankkeys(const void *a, const void *b);
594 int smpi_compare_rankkeys(const void *a, const void *b)
613 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
616 int retval = MPI_SUCCESS;
619 smpi_mpi_request_t request;
621 smpi_mpi_status_t status;
625 // FIXME: need to test parameters
627 index = smpi_process_index();
628 rank = comm->index_to_rank_map[index];
633 // root node does most of the real work
635 int colormap[comm->size];
636 int keymap[comm->size];
637 int rankkeymap[comm->size * 2];
639 smpi_mpi_communicator_t tempcomm = NULL;
646 // FIXME: use scatter/gather or similar instead of individual comms
647 for (i = 1; i < comm->size; i++) {
648 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
649 rank, MPI_ANY_TAG, comm, &request);
650 smpi_mpi_irecv(request);
651 smpi_mpi_wait(request, &status);
652 colormap[status.MPI_SOURCE] = colorkey[0];
653 keymap[status.MPI_SOURCE] = colorkey[1];
654 xbt_mallocator_release(smpi_global->request_mallocator, request);
657 for (i = 0; i < comm->size; i++) {
658 if (MPI_UNDEFINED == colormap[i]) {
661 // make a list of nodes with current color and sort by keys
663 for (j = i; j < comm->size; j++) {
664 if (colormap[i] == colormap[j]) {
665 colormap[j] = MPI_UNDEFINED;
666 rankkeymap[count * 2] = j;
667 rankkeymap[count * 2 + 1] = keymap[j];
671 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
674 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
675 tempcomm->barrier_count = 0;
676 tempcomm->size = count;
677 tempcomm->barrier_mutex = SIMIX_mutex_init();
678 tempcomm->barrier_cond = SIMIX_cond_init();
679 tempcomm->rank_to_index_map = xbt_new(int, count);
680 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
681 for (j = 0; j < smpi_global->process_count; j++) {
682 tempcomm->index_to_rank_map[j] = -1;
684 for (j = 0; j < count; j++) {
685 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
686 tempcomm->rank_to_index_map[j] = indextmp;
687 tempcomm->index_to_rank_map[indextmp] = j;
689 for (j = 0; j < count; j++) {
690 if (rankkeymap[j * 2]) {
691 retval = smpi_create_request(&j, 1, MPI_INT, 0,
692 rankkeymap[j * 2], 0, comm, &request);
693 request->data = tempcomm;
694 smpi_mpi_isend(request);
695 smpi_mpi_wait(request, &status);
696 xbt_mallocator_release(smpi_global->request_mallocator, request);
698 *comm_out = tempcomm;
705 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
707 smpi_mpi_isend(request);
708 smpi_mpi_wait(request, &status);
709 xbt_mallocator_release(smpi_global->request_mallocator, request);
710 if (MPI_UNDEFINED != color) {
711 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
713 smpi_mpi_irecv(request);
714 smpi_mpi_wait(request, &status);
715 *comm_out = request->data;
724 double SMPI_MPI_Wtime(void)
726 return (SIMIX_get_clock());