Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
unify collective tags
[simgrid.git] / src / smpi / colls / reduce-binomial.c
1 #include "colls_private.h"
2
3 //#include <star-reduction.c>
4
5 int smpi_coll_tuned_reduce_binomial(void *sendbuf, void *recvbuf, int count,
6                                     MPI_Datatype datatype, MPI_Op op, int root,
7                                     MPI_Comm comm)
8 {
9   MPI_Status status;
10   int comm_size, rank;
11   int mask, relrank, source;
12   int dst;
13   int tag = COLL_TAG_REDUCE;
14   MPI_Aint extent;
15   void *tmp_buf;
16   MPI_Aint true_lb, true_extent;
17   if (count == 0)
18     return 0;
19   rank = smpi_comm_rank(comm);
20   comm_size = smpi_comm_size(comm);
21
22   extent = smpi_datatype_get_extent(datatype);
23
24   tmp_buf = (void *) xbt_malloc(count * extent);
25   int is_commutative = smpi_op_is_commute(op);
26   smpi_mpi_sendrecv(sendbuf, count, datatype, rank, tag,
27                recvbuf, count, datatype, rank, tag, comm, &status);
28   mask = 1;
29   
30   int lroot;
31   if (is_commutative) 
32         lroot   = root;
33   else
34         lroot   = 0;
35   relrank = (rank - lroot + comm_size) % comm_size;
36
37   smpi_datatype_extent(datatype, &true_lb, &true_extent);
38
39   /* adjust for potential negative lower bound in datatype */
40   tmp_buf = (void *)((char*)tmp_buf - true_lb);
41     
42   /* If I'm not the root, then my recvbuf may not be valid, therefore
43      I have to allocate a temporary one */
44   if (rank != root) {
45       recvbuf = (void *) malloc(count*(max(extent,true_extent)));
46       recvbuf = (void *)((char*)recvbuf - true_lb);
47   }
48    if ((rank != root) || (sendbuf != MPI_IN_PLACE)) {
49       smpi_datatype_copy(sendbuf, count, datatype, recvbuf,count, datatype);
50   }
51
52   while (mask < comm_size) {
53     /* Receive */
54     if ((mask & relrank) == 0) {
55       source = (relrank | mask);
56       if (source < comm_size) {
57         source = (source + lroot) % comm_size;
58         smpi_mpi_recv(tmp_buf, count, datatype, source, tag, comm, &status);
59         
60         if (is_commutative) {
61           smpi_op_apply(op, tmp_buf, recvbuf, &count, &datatype);
62         } else {
63           smpi_op_apply(op, recvbuf, tmp_buf, &count, &datatype);
64           smpi_datatype_copy(tmp_buf, count, datatype,recvbuf, count, datatype);
65         }
66       }
67     } else {
68       dst = ((relrank & (~mask)) + lroot) % comm_size;
69       smpi_mpi_send(recvbuf, count, datatype, dst, tag, comm);
70       break;
71     }
72     mask <<= 1;
73   }
74
75 if (!is_commutative && (root != 0)){
76   if (rank == 0){
77     smpi_mpi_send(recvbuf, count, datatype, root,tag, comm);
78   }else if (rank == root){
79     smpi_mpi_recv(recvbuf, count, datatype, 0, tag, comm, &status);
80   }
81 }
82
83   free(tmp_buf);
84
85   return 0;
86 }