Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define buffer on the stack here.
[simgrid.git] / src / smpi / colls / allgather-rhv.c
1 #include "colls_private.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 = COLL_TAG_ALLGATHER;
19   int mask;
20   int curr_count;
21
22   // get size of the communicator, followed by rank 
23   num_procs = smpi_comm_size(comm);
24   rank = smpi_comm_rank(comm);
25
26   // get size of single element's type for send buffer and recv buffer
27   s_extent = smpi_datatype_get_extent(send_type);
28   r_extent = smpi_datatype_get_extent(recv_type);
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     XBT_WARN("MPI_allgather_rhv use default MPI_allgather.");  
36     smpi_mpi_allgather(sbuf, send_count, send_type, rbuf, recv_count,
37                               recv_type, comm);
38     return MPI_SUCCESS;        
39   }
40
41   // compute starting offset location to perform local copy
42   int size = num_procs / 2;
43   int base_offset = 0;
44   mask = 1;
45   while (mask < num_procs) {
46     if (rank & mask) {
47       base_offset += size;
48     }
49     mask <<= 1;
50     size /= 2;
51   }
52
53   //  printf("node %d base_offset %d\n",rank,base_offset);
54
55   //perform a remote copy
56
57   dst = base_offset;
58   smpi_mpi_sendrecv(sbuf, send_count, send_type, dst, tag,
59                (char *)rbuf + base_offset * recv_chunk, recv_count, recv_type, dst, tag,
60                comm, &status);
61
62
63   mask >>= 1;
64   i = 1;
65   int phase = 0;
66   curr_count = recv_count;
67   while (mask >= 1) {
68     // destination pair for both send and recv
69     dst = rank ^ mask;
70
71     // compute offsets
72     send_base_offset = base_offset;
73     if (rank & mask) {
74       recv_base_offset = base_offset - i;
75       base_offset -= i;
76     } else {
77       recv_base_offset = base_offset + i;
78     }
79     send_offset = send_base_offset * recv_chunk;
80     recv_offset = recv_base_offset * recv_chunk;
81
82     //  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);
83
84     smpi_mpi_sendrecv((char *)rbuf + send_offset, curr_count, recv_type, dst, tag,
85                  (char *)rbuf + recv_offset, curr_count, recv_type, dst, tag,
86                  comm, &status);
87
88
89     curr_count *= 2;
90     i *= 2;
91     mask >>= 1;
92     phase++;
93   }
94
95   return MPI_SUCCESS;
96 }