3 /* smpi_coll.c -- various optimized routing for collectives */
5 /* Copyright (c) 2009 Stephane Genaud. */
6 /* All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * * under the terms of the license (GNU LGPL) which comes with this package. */
17 #include "smpi_coll_private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
20 "Logging specific to SMPI (coll)");
22 /* proc_tree taken and translated from P2P-MPI */
33 typedef struct proc_tree * proc_tree_t;
38 void build_tree( int index, int extent, proc_tree_t *tree);
39 proc_tree_t alloc_tree( int arity );
40 void free_tree( proc_tree_t tree);
41 void print_tree(proc_tree_t tree);
49 proc_tree_t alloc_tree( int arity ) {
50 proc_tree_t tree = malloc(1*sizeof(struct proc_tree));
53 tree->PROCTREE_A = arity;
55 tree->numChildren = 0;
56 tree->child = malloc(arity*sizeof(int));
57 for (i=0; i < arity; i++) {
68 void free_tree( proc_tree_t tree) {
76 * Build the tree depending on a process rank (index) and the group size (extent)
77 * @param index the rank of the calling process
78 * @param extent the total number of processes
80 void build_tree( int index, int extent, proc_tree_t *tree) {
81 int places = (*tree)->PROCTREE_A * index;
89 for (i = 1; i <= (*tree)->PROCTREE_A; i++) {
91 ch = (*tree)->PROCTREE_A * index + i + (*tree)->root;
92 //printf("places %d\n",places);
94 if (places < extent) {
95 //printf("ch <%d> = <%d>\n",i,ch);
96 //printf("adding to the tree at index <%d>\n\n",i-1);
97 (*tree)->child[i - 1] = ch;
98 (*tree)->numChildren++;
101 //fprintf(stderr,"not adding to the tree\n");
104 //fprintf(stderr,"procTree.numChildren <%d>\n",(*tree)->numChildren);
106 if (index == (*tree)->root) {
111 pr = (index - 1) / (*tree)->PROCTREE_A;
112 (*tree)->parent = pr;
117 * prints the tree as a graphical representation
119 void print_tree(proc_tree_t tree) {
122 if (-1 != tree->parent ) {
123 printf("[%d]\n +---",tree->parent);
129 printf("<%d>\n",tree->me);
130 for (i=0;i < tree->numChildren; i++) {
131 printf("%s +--- %d\n", spacer,tree->child[i]);
139 int tree_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree);
140 int tree_bcast( void *buf, int count, MPI_Datatype datatype, int root,
141 MPI_Comm comm, proc_tree_t tree)
143 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
145 int retval = MPI_SUCCESS;
147 smpi_mpi_request_t request;
148 smpi_mpi_request_t * requests;
150 rank = smpi_mpi_comm_rank(comm);
152 /* wait for data from my parent in the tree */
154 DEBUG4("[%d] recv(%d from %d, tag=%d)\n",rank,rank, tree->parent, system_tag+rank);
155 retval = smpi_create_request(buf, count, datatype,
159 if (MPI_SUCCESS != retval) {
160 printf("** internal error: smpi_create_request() rank=%d returned retval=%d, %s:%d\n",
161 rank,retval,__FILE__,__LINE__);
163 smpi_mpi_irecv(request);
164 DEBUG2("[%d] waiting on irecv from %d\n",rank , tree->parent);
165 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
166 xbt_mallocator_release(smpi_global->request_mallocator, request);
169 requests = xbt_malloc( tree->numChildren * sizeof(smpi_mpi_request_t));
170 DEBUG2("[%d] creates %d requests\n",rank,tree->numChildren);
172 /* iniates sends to ranks lower in the tree */
173 for (i=0; i < tree->numChildren; i++) {
174 if (tree->child[i] != -1) {
175 DEBUG4("[%d] send(%d->%d, tag=%d)\n",rank,rank, tree->child[i], system_tag+tree->child[i]);
176 retval = smpi_create_request(buf, count, datatype,
177 rank, tree->child[i],
178 system_tag + tree->child[i],
179 comm, &(requests[i]));
180 DEBUG5("[%d] after create req[%d]=%p req->(src=%d,dst=%d)\n",rank,i,requests[i],requests[i]->src,requests[i]->dst );
181 if (MPI_SUCCESS != retval) {
182 printf("** internal error: smpi_create_request() rank=%d returned retval=%d, %s:%d\n",
183 rank,retval,__FILE__,__LINE__);
185 smpi_mpi_isend(requests[i]);
186 /* FIXME : we should not wait immediately here. See next FIXME. */
187 smpi_mpi_wait( requests[i], MPI_STATUS_IGNORE);
188 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
191 /* FIXME : normally, we sould wait only once all isend have been issued:
192 * this is the following commented code. It deadlocks, probably because
193 * of a bug in the sender process */
195 /* wait for completion of sends */
196 /* printf("[%d] wait for %d send completions\n",rank,tree->numChildren);
197 smpi_mpi_waitall( tree->numChildren, requests, MPI_STATUS_IGNORE);
198 printf("[%d] reqs completed\n)",rank);
203 /* checked ok with valgrind --leak-check=full*/
210 int tree_antibcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree);
211 int tree_antibcast( void *buf, int count, MPI_Datatype datatype, int root,
212 MPI_Comm comm, proc_tree_t tree)
214 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
216 int retval = MPI_SUCCESS;
218 smpi_mpi_request_t request;
219 smpi_mpi_request_t * requests;
221 rank = smpi_mpi_comm_rank(comm);
223 //------------------anti-bcast-------------------
225 // everyone sends to its parent, except root.
227 retval = smpi_create_request(buf, count, datatype,
231 if (MPI_SUCCESS != retval) {
232 printf("** internal error: smpi_create_request() rank=%d returned retval=%d, %s:%d\n",
233 rank,retval,__FILE__,__LINE__);
235 smpi_mpi_isend(request);
236 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
237 xbt_mallocator_release(smpi_global->request_mallocator, request);
240 //every one receives as many messages as it has children
241 requests = xbt_malloc( tree->numChildren * sizeof(smpi_mpi_request_t));
242 for (i=0; i < tree->numChildren; i++) {
243 if (tree->child[i] != -1) {
244 retval = smpi_create_request(buf, count, datatype,
245 tree->child[i], rank,
246 system_tag + tree->child[i],
247 comm, &(requests[i]));
248 if (MPI_SUCCESS != retval) {
249 printf("** internal error: smpi_create_request() rank=%d returned retval=%d, %s:%d\n",
250 rank,retval,__FILE__,__LINE__);
252 smpi_mpi_irecv(requests[i]);
253 /* FIXME : we should not wait immediately here. See next FIXME. */
254 smpi_mpi_wait( requests[i], MPI_STATUS_IGNORE);
255 xbt_mallocator_release(smpi_global->request_mallocator, requests[i]);
261 /* checked ok with valgrind --leak-check=full*/
265 * bcast with a binary, ternary, or whatever tree ..
267 int nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
268 MPI_Comm comm, int arity)
273 rank = smpi_mpi_comm_rank( comm );
274 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
275 proc_tree_t tree = alloc_tree( arity );
276 build_tree( rank, comm->size, &tree );
278 retval = tree_bcast( buf, count, datatype, root, comm, tree );
288 int nary_tree_barrier( MPI_Comm comm , int arity)
291 int retval = MPI_SUCCESS;
294 rank = smpi_mpi_comm_rank(comm);
295 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
296 proc_tree_t tree = alloc_tree( arity );
298 build_tree( rank, comm->size, &tree );
300 retval = tree_antibcast( &dummy, 1, MPI_CHAR, 0, comm, tree);
301 if (MPI_SUCCESS != retval) {
302 printf("[%s:%d] ** Error: tree_antibcast() returned retval=%d\n",__FILE__,__LINE__,retval);
305 retval = tree_bcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
311 /* checked ok with valgrind --leak-check=full*/
318 * this algorithm performs size steps (1<=s<=size) and
319 * at each step s, a process p sends iand receive to.from a unique distinct remote process
320 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
321 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
323 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
326 int smpi_coll_tuned_alltoall_pairwise (void *sendbuf, int sendcount, MPI_Datatype datatype,
327 void* recvbuf, int recvcount, MPI_Datatype recvdatatype, MPI_Comm comm)
329 int retval = MPI_SUCCESS;
331 int size = comm->size;
333 int sendto, recvfrom;
334 int tag_alltoall=999;
335 void * tmpsend, *tmprecv;
337 rank = smpi_mpi_comm_rank(comm);
338 INFO1("[%d] algorithm alltoall_pairwise() called.\n",rank);
341 /* Perform pairwise exchange - starting from 1 so the local copy is last */
342 for (step = 1; step < size+1; step++) {
344 /* who do we talk to in this step? */
345 sendto = (rank+step)%size;
346 recvfrom = (rank+size-step)%size;
348 /* where from are we sending and where from are we receiving actual data ? */
349 tmpsend = (char*)sendbuf+sendto*datatype->size*sendcount;
350 tmprecv = (char*)recvbuf+recvfrom*recvdatatype->size*recvcount;
352 /* send and receive */
353 /* in OpenMPI, they use :
354 err = ompi_coll_tuned_sendrecv( tmpsend, scount, sdtype, sendto, MCA_COLL_BASE_TAG_ALLTOALL,
355 tmprecv, rcount, rdtype, recvfrom, MCA_COLL_BASE_TAG_ALLTOALL,
356 comm, MPI_STATUS_IGNORE, rank);
358 retval = MPI_Sendrecv(tmpsend, sendcount, datatype, sendto, tag_alltoall,
359 tmprecv, recvcount, recvdatatype, recvfrom, tag_alltoall,
360 comm, MPI_STATUS_IGNORE);
366 * helper: copy a datatype into another (in the simple case dt1=dt2)
368 int copy_dt( void *sbuf, int scount, const MPI_Datatype sdtype, void *rbuf, int rcount, const MPI_Datatype rdtype);
369 int copy_dt( void *sbuf, int scount, const MPI_Datatype sdtype,
370 void *rbuf, int rcount, const MPI_Datatype rdtype)
372 /* First check if we really have something to do */
374 return ((0 == scount) ? MPI_SUCCESS : MPI_ERR_TRUNCATE);
376 /* If same datatypes used, just copy. */
377 if (sdtype == rdtype) {
378 int count = ( scount < rcount ? scount : rcount );
379 memcpy( rbuf, sbuf, sdtype->size*count);
380 return ((scount > rcount) ? MPI_ERR_TRUNCATE : MPI_SUCCESS);
383 * - If receive packed.
385 * to be treated once we have the MPI_Pack things ...
387 return( MPI_SUCCESS );
393 int smpi_coll_tuned_alltoall_basic_linear(void *sbuf, int scount, MPI_Datatype sdtype,
394 void* rbuf, int rcount, MPI_Datatype rdtype, MPI_Comm comm)
397 int system_alltoall_tag = 888;
399 int size = comm->size;
410 rank = smpi_mpi_comm_rank(comm);
411 INFO1("[%d] algorithm alltoall_basic_linear() called.\n",rank);
413 err = smpi_mpi_type_get_extent(sdtype, &lb, &sndinc);
414 err = smpi_mpi_type_get_extent(rdtype, &lb, &rcvinc);
415 /* simple optimization */
416 psnd = ((char *) sbuf) + (rank * sndinc);
417 prcv = ((char *) rbuf) + (rank * rcvinc);
419 err = copy_dt( psnd, scount, sdtype, prcv, rcount, rdtype );
420 if (MPI_SUCCESS != err) {
424 /* If only one process, we're done. */
429 /* Initiate all send/recv to/from others. */
430 reqs = xbt_malloc(2*(size-1) * sizeof(smpi_mpi_request_t));
432 prcv = (char *) rbuf;
433 psnd = (char *) sbuf;
435 /* Post all receives first -- a simple optimization */
436 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
437 err = smpi_create_request( prcv + (i * rcvinc), rcount, rdtype,
440 comm, &(reqs[nreq]));
441 if (MPI_SUCCESS != err) {
442 DEBUG2("[%d] failed to create request for rank %d\n",rank,i);
443 for (i=0;i< nreq;i++)
444 xbt_mallocator_release(smpi_global->request_mallocator, reqs[i]);
449 /* Now post all sends in reverse order
450 * - We would like to minimize the search time through message queue
451 * when messages actually arrive in the order in which they were posted.
453 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size ) {
454 err = smpi_create_request (psnd + (i * sndinc), scount, sdtype,
457 comm, &(reqs[nreq]));
458 if (MPI_SUCCESS != err) {
459 DEBUG2("[%d] failed to create request for rank %d\n",rank,i);
460 for (i=0;i< nreq;i++)
461 xbt_mallocator_release(smpi_global->request_mallocator, reqs[i]);
467 /* Start your engines. This will never return an error. */
468 for ( i=0; i< nreq/2; i++ ) {
469 smpi_mpi_irecv( reqs[i] );
471 for ( i= nreq/2; i<nreq; i++ ) {
472 smpi_mpi_isend( reqs[i] );
476 /* Wait for them all. If there's an error, note that we don't
477 * care what the error was -- just that there *was* an error. The
478 * PML will finish all requests, even if one or more of them fail.
479 * i.e., by the end of this call, all the requests are free-able.
480 * So free them anyway -- even if there was an error, and return
481 * the error after we free everything. */
483 err = smpi_mpi_waitall(nreq, reqs, MPI_STATUS_IGNORE);
486 for (i=0;i< 2*(size-1);i++) {
487 xbt_mallocator_release(smpi_global->request_mallocator, reqs[i]);
496 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
500 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount, MPI_Datatype sdtype,
501 void* recvbuf, int recvcount, MPI_Datatype rdtype, MPI_Comm comm)
503 /* int size = comm->size;
505 int sendto, recvfrom, distance, *displs=NULL, *blen=NULL;
506 int maxpacksize, packsize, position;
507 char * tmpbuf=NULL, *packbuf=NULL;
508 ptrdiff_t lb, sext, rext;
513 rank = smpi_mpi_comm_rank(comm);
515 INFO0("coll:tuned:alltoall_intra_bruck ** NOT IMPLEMENTED YET**");
517 displs = xbt_malloc(size*sizeof(int));
518 blen = xbt_malloc(size*sizeof(int));
522 /* Prepare for packing data */
524 err = MPI_Pack_size( scount*size, sdtype, comm, &maxpacksize );
525 if (err != MPI_SUCCESS) { }
527 /* pack buffer allocation */
528 /* packbuf = (char*) malloc((unsigned) maxpacksize);
529 if (packbuf == NULL) { }
531 /* tmp buffer allocation for message data */
532 /* tmpbuf = xbt_malloc(scount*size*sext);
533 if (tmpbuf == NULL) { }
536 /* Step 1 - local rotation - shift up by rank */
537 /* err = ompi_ddt_copy_content_same_ddt (sdtype, (int32_t) ((size-rank)*scount),
538 tmpbuf, ((char*)sbuf)+rank*scount*sext);
540 line = __LINE__; err = -1; goto err_hndl;
544 err = ompi_ddt_copy_content_same_ddt (sdtype, (int32_t) (rank*scount),
545 tmpbuf+(size-rank)*scount*sext, (char*)sbuf);
547 line = __LINE__; err = -1; goto err_hndl;
551 /* perform communication step */
552 /* for (distance = 1; distance < size; distance<<=1) {
554 /* send data to "sendto" */
555 /* sendto = (rank+distance)%size;
556 recvfrom = (rank-distance+size)%size;
560 /* create indexed datatype */
561 // for (i = 1; i < size; i++) {
562 // if ((i&distance) == distance) {
563 // displs[k] = i*scount; blen[k] = scount;
567 /* Set indexes and displacements */
568 // err = MPI_Type_indexed(k, blen, displs, sdtype, &iddt);
569 // if (err != MPI_SUCCESS) { line = __LINE__; goto err_hndl; }
570 // /* Commit the new datatype */
571 /// err = MPI_Type_commit(&iddt);
572 // if (err != MPI_SUCCESS) { line = __LINE__; goto err_hndl; }
574 /* have the new distribution ddt, pack and exchange data */
575 // err = MPI_Pack(tmpbuf, 1, iddt, packbuf, maxpacksize, &packsize, comm);
576 // if (err != MPI_SUCCESS) { line = __LINE__; goto err_hndl; }
579 // err = ompi_coll_tuned_sendrecv ( packbuf, packsize, MPI_PACKED, sendto,
580 // MCA_COLL_BASE_TAG_ALLTOALL,
581 // rbuf, packsize, MPI_PACKED, recvfrom,
582 // MCA_COLL_BASE_TAG_ALLTOALL,
583 // comm, MPI_STATUS_IGNORE, rank);
584 // if (err != MPI_SUCCESS) { line = __LINE__; goto err_hndl; }
586 /* Unpack data from rbuf to tmpbuf */
588 // err = MPI_Unpack(rbuf, packsize, &position,
589 // tmpbuf, 1, iddt, comm);
590 // if (err != MPI_SUCCESS) { line = __LINE__; goto err_hndl; }
593 // err = MPI_Type_free(&iddt);
594 // if (err != MPI_SUCCESS) { line = __LINE__; goto err_hndl; }
595 // } /* end of for (distance = 1... */
597 /* Step 3 - local rotation - */
598 // for (i = 0; i < size; i++) {
600 // err = ompi_ddt_copy_content_same_ddt (rdtype, (int32_t) rcount,
601 // ((char*)rbuf)+(((rank-i+size)%size)*rcount*rext),
602 // tmpbuf+i*rcount*rext);
605 // line = __LINE__; err = -1; goto err_hndl;
609 /* Step 4 - clean up */
610 /* if (tmpbuf != NULL) free(tmpbuf);
611 if (packbuf != NULL) free(packbuf);
613 if (displs != NULL) free(displs);
614 if (blen != NULL) free(blen);
619 OPAL_OUTPUT((ompi_coll_tuned_stream,"%s:%4d\tError occurred %d, rank %2d", __FILE__,line,err,rank));
620 if (tmpbuf != NULL) free(tmpbuf);
621 if (packbuf != NULL) free(packbuf);
623 if (displs != NULL) free(displs);
624 if (blen != NULL) free(blen);
628 return -1; /* FIXME: to be changed*/
633 * -----------------------------------------------------------------------------------------------------
635 * -----------------------------------------------------------------------------------------------------
644 for (rank=0;rank<size;rank++) {
645 printf("--------------tree for rank %d ----------\n",rank);
646 tree = alloc_tree( 2 );
647 build_tree( rank, size, &tree );
652 printf("-------------- bcast ----------\n");
653 for (rank=0;rank<size;rank++) {