Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add smpi_status.cpp and hpp.
[simgrid.git] / src / smpi / colls / allgather / allgather-rdb.cpp
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "../colls_private.h"
8
9 namespace simgrid{
10 namespace smpi{
11
12
13 int
14 Coll_allgather_rdb::allgather(void *sbuf, int send_count,
15                               MPI_Datatype send_type, void *rbuf,
16                               int recv_count, MPI_Datatype recv_type,
17                               MPI_Comm comm)
18 {
19   // MPI variables
20   MPI_Status status;
21   MPI_Aint send_chunk, recv_chunk;
22
23   // local int variables
24   unsigned int i, j, k, dst, send_offset, recv_offset, tree_root;
25   int dst_tree_root, rank_tree_root, last_recv_count = 0, num_procs_completed;
26   int offset, tmp_mask;
27   int tag = COLL_TAG_ALLGATHER;
28   unsigned int mask = 1;
29   int success = 0;
30   int curr_count = recv_count;
31
32   // local string variables
33   char *send_ptr = (char *) sbuf;
34   char *recv_ptr = (char *) rbuf;
35
36   // get size of the communicator, followed by rank 
37   unsigned int num_procs = comm->size();
38   unsigned int rank = comm->rank();
39
40   // get size of single element's type for send buffer and recv buffer
41   send_chunk = send_type->get_extent();
42   recv_chunk = recv_type->get_extent();
43
44   // multiply size of each element by number of elements to send or recv
45   send_chunk *= send_count;
46   recv_chunk *= recv_count;
47
48   // perform a local copy
49   Request::sendrecv(send_ptr, send_count, send_type, rank, tag,
50                recv_ptr + rank * recv_chunk, recv_count, recv_type, rank, tag,
51                comm, &status);
52
53   i = 0;
54   while (mask < num_procs) {
55     dst = rank ^ mask;
56     dst_tree_root = dst >> i;
57     dst_tree_root <<= i;
58     rank_tree_root = rank >> i;
59     rank_tree_root <<= i;
60     send_offset = rank_tree_root * send_chunk;
61     recv_offset = dst_tree_root * recv_chunk;
62
63     if (dst < num_procs) {
64       Request::sendrecv(recv_ptr + send_offset, curr_count, send_type, dst,
65                    tag, recv_ptr + recv_offset, mask * recv_count,
66                    recv_type, dst, tag, comm, &status);
67       last_recv_count = Status::get_count(&status, recv_type);
68       curr_count += last_recv_count;
69     }
70
71     if (dst_tree_root + mask > num_procs) {
72       num_procs_completed = num_procs - rank_tree_root - mask;
73       /* num_procs_completed is the number of processes in this
74          subtree that have all the data. Send data to others
75          in a tree fashion. First find root of current tree
76          that is being divided into two. k is the number of
77          least-significant bits in this process's rank that
78          must be zeroed out to find the rank of the root */
79
80       j = mask;
81       k = 0;
82       while (j) {
83         j >>= 1;
84         k++;
85       }
86       k--;
87
88       offset = recv_chunk * (rank_tree_root + mask);
89       tmp_mask = mask >> 1;
90
91       while (tmp_mask) {
92         dst = rank ^ tmp_mask;
93
94         tree_root = rank >> k;
95         tree_root <<= k;
96
97         /* send only if this proc has data and destination
98            doesn't have data. at any step, multiple processes
99            can send if they have the data */
100         if ((dst > rank)
101             && (rank < tree_root + num_procs_completed)
102             && (dst >= tree_root + num_procs_completed)) {
103           Request::send(recv_ptr + offset, last_recv_count, recv_type, dst,
104                    tag, comm);
105
106           /* last_recv_cnt was set in the previous
107              receive. that's the amount of data to be
108              sent now. */
109         }
110         /* recv only if this proc. doesn't have data and sender
111            has data */
112         else if ((dst < rank)
113                  && (dst < tree_root + num_procs_completed)
114                  && (rank >= tree_root + num_procs_completed)) {
115           Request::recv(recv_ptr + offset,
116                    recv_count * num_procs_completed,
117                    recv_type, dst, tag, comm, &status);
118           // num_procs_completed is also equal to the no. of processes
119           // whose data we don't have
120           last_recv_count = Status::get_count(&status, recv_type);
121           curr_count += last_recv_count;
122         }
123         tmp_mask >>= 1;
124         k--;
125       }
126     }
127
128     mask <<= 1;
129     i++;
130   }
131
132   return success;
133 }
134
135
136 }
137 }