Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add collectives for allgather, allreduce, bcast and reduce
[simgrid.git] / src / smpi / colls / allgather-rhv.c
1 #include "colls.h"
2
3 // now only work with power of two processes
4
5 int
6 smpi_coll_tuned_allgather_rhv(void *sbuf, int send_count,
7                               MPI_Datatype send_type, void *rbuf,
8                               int recv_count, MPI_Datatype recv_type,
9                               MPI_Comm comm)
10 {
11   MPI_Status status;
12   MPI_Aint s_extent, r_extent;
13
14   // local int variables
15   int i, dst, send_base_offset, recv_base_offset, send_chunk, recv_chunk,
16       send_offset, recv_offset;
17   int rank, num_procs;
18   int tag = 50;
19   int mask;
20   int curr_count;
21
22   // get size of the communicator, followed by rank 
23   MPI_Comm_size(comm, &num_procs);
24   MPI_Comm_rank(comm, &rank);
25
26   // get size of single element's type for send buffer and recv buffer
27   MPI_Type_extent(send_type, &s_extent);
28   MPI_Type_extent(recv_type, &r_extent);
29
30   // multiply size of each element by number of elements to send or recv
31   send_chunk = s_extent * send_count;
32   recv_chunk = r_extent * recv_count;
33
34   if (send_chunk != recv_chunk)
35     return MPI_Allgather(sbuf, send_count, send_type, rbuf, recv_count,
36                          recv_type, comm);
37
38   // compute starting offset location to perform local copy
39   int size = num_procs / 2;
40   int base_offset = 0;
41   mask = 1;
42   while (mask < num_procs) {
43     if (rank & mask) {
44       base_offset += size;
45     }
46     mask <<= 1;
47     size /= 2;
48   }
49
50   //  printf("node %d base_offset %d\n",rank,base_offset);
51
52   //perform a remote copy
53
54   dst = base_offset;
55   MPI_Sendrecv(sbuf, send_count, send_type, dst, tag,
56                (char *)rbuf + base_offset * recv_chunk, recv_count, recv_type, dst, tag,
57                comm, &status);
58
59
60   mask >>= 1;
61   i = 1;
62   int phase = 0;
63   curr_count = recv_count;
64   while (mask >= 1) {
65     // destination pair for both send and recv
66     dst = rank ^ mask;
67
68     // compute offsets
69     send_base_offset = base_offset;
70     if (rank & mask) {
71       recv_base_offset = base_offset - i;
72       base_offset -= i;
73     } else {
74       recv_base_offset = base_offset + i;
75     }
76     send_offset = send_base_offset * recv_chunk;
77     recv_offset = recv_base_offset * recv_chunk;
78
79     //  printf("node %d send to %d in phase %d s_offset = %d r_offset = %d count = %d\n",rank,dst,phase, send_base_offset, recv_base_offset, curr_count);
80
81     MPI_Sendrecv((char *)rbuf + send_offset, curr_count, recv_type, dst, tag,
82                  (char *)rbuf + recv_offset, curr_count, recv_type, dst, tag,
83                  comm, &status);
84
85
86     curr_count *= 2;
87     i *= 2;
88     mask >>= 1;
89     phase++;
90   }
91
92   return MPI_SUCCESS;
93 }