Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mem leak
[simgrid.git] / src / smpi / colls / allreduce-rab1.c
1 #include "colls_private.h"
2 //#include <star-reduction.c>
3
4 // NP pow of 2 for now
5 int smpi_coll_tuned_allreduce_rab1(void *sbuff, void *rbuff,
6                                    int count, MPI_Datatype dtype,
7                                    MPI_Op op, MPI_Comm comm)
8 {
9   MPI_Status status;
10   MPI_Aint extent;
11   int tag = 4321, rank, nprocs, send_size, newcnt, share;
12   int pof2 = 1, mask, send_idx, recv_idx, dst, send_cnt, recv_cnt;
13
14   void *recv, *tmp_buf;
15
16   rank = smpi_comm_rank(comm);
17   nprocs = smpi_comm_size(comm);
18
19   extent = smpi_datatype_get_extent(dtype);
20
21   pof2 = 1;
22   while (pof2 <= nprocs)
23     pof2 <<= 1;
24   pof2 >>= 1;
25
26   mask = 1;
27   send_idx = recv_idx = 0;
28
29   // uneven count
30   if ((count % nprocs)) {
31     send_size = (count + nprocs) / nprocs;
32     newcnt = send_size * nprocs;
33
34     recv = (void *) xbt_malloc(extent * newcnt);
35     tmp_buf = (void *) xbt_malloc(extent * newcnt);
36     memcpy(recv, sbuff, extent * count);
37
38
39     mask = pof2 / 2;
40     share = newcnt / pof2;
41     while (mask > 0) {
42       dst = rank ^ mask;
43       send_cnt = recv_cnt = newcnt / (pof2 / mask);
44
45       if (rank < dst)
46         send_idx = recv_idx + (mask * share);
47       else
48         recv_idx = send_idx + (mask * share);
49
50       smpi_mpi_sendrecv((char *) recv + send_idx * extent, send_cnt, dtype, dst, tag,
51                    tmp_buf, recv_cnt, dtype, dst, tag, comm, &status);
52
53       smpi_op_apply(op, tmp_buf, (char *) recv + recv_idx * extent, &recv_cnt,
54                      &dtype);
55
56       // update send_idx for next iteration 
57       send_idx = recv_idx;
58       mask >>= 1;
59     }
60
61     memcpy(tmp_buf, (char *) recv + recv_idx * extent, recv_cnt * extent);
62     mpi_coll_allgather_fun(tmp_buf, recv_cnt, dtype, recv, recv_cnt, dtype, comm);
63
64     memcpy(rbuff, recv, count * extent);
65     free(recv);
66     free(tmp_buf);
67
68   }
69
70   else {
71     tmp_buf = (void *) xbt_malloc(extent * count);
72     memcpy(rbuff, sbuff, count * extent);
73     mask = pof2 / 2;
74     share = count / pof2;
75     while (mask > 0) {
76       dst = rank ^ mask;
77       send_cnt = recv_cnt = count / (pof2 / mask);
78
79       if (rank < dst)
80         send_idx = recv_idx + (mask * share);
81       else
82         recv_idx = send_idx + (mask * share);
83
84       smpi_mpi_sendrecv((char *) rbuff + send_idx * extent, send_cnt, dtype, dst,
85                    tag, tmp_buf, recv_cnt, dtype, dst, tag, comm, &status);
86
87       smpi_op_apply(op, tmp_buf, (char *) rbuff + recv_idx * extent, &recv_cnt,
88                      &dtype);
89
90       // update send_idx for next iteration 
91       send_idx = recv_idx;
92       mask >>= 1;
93     }
94
95     memcpy(tmp_buf, (char *) rbuff + recv_idx * extent, recv_cnt * extent);
96     mpi_coll_allgather_fun(tmp_buf, recv_cnt, dtype, rbuff, recv_cnt, dtype, comm);
97     free(tmp_buf);
98   }
99
100   return MPI_SUCCESS;
101 }