Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / colls / alltoall / alltoall-rdb.cpp
1 /* Copyright (c) 2013-2023. 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.hpp"
8 #include "smpi_status.hpp"
9
10 /*****************************************************************************
11
12  * Function: alltoall_rdb
13
14  * Return: int
15
16  * inputs:
17     send_buff: send input buffer
18     send_count: number of elements to send
19     send_type: data type of elements being sent
20     recv_buff: receive output buffer
21     recv_count: number of elements to received
22     recv_type: data type of elements being received
23     comm: communicator
24
25  * Descrp: Function realizes the allgather operation using the recursive
26            doubling algorithm.
27
28  * Author: MPICH / slightly modified by Ahmad Faraj.
29
30  ****************************************************************************/
31 namespace simgrid::smpi {
32 int alltoall__rdb(const void *send_buff, int send_count,
33                   MPI_Datatype send_type,
34                   void *recv_buff, int recv_count,
35                   MPI_Datatype recv_type, MPI_Comm comm)
36 {
37   /* MPI variables */
38   MPI_Status status;
39   MPI_Aint send_increment, recv_increment, extent;
40
41   int dst_tree_root, rank_tree_root, send_offset, recv_offset;
42   int rank, num_procs, j, k, dst, curr_size, max_size;
43   int last_recv_count = 0, tmp_mask, tree_root, num_procs_completed;
44   int tag = COLL_TAG_ALLTOALL, mask = 1, i = 0;
45
46   char *send_ptr = (char *) send_buff;
47   char *recv_ptr = (char *) recv_buff;
48
49   num_procs = comm->size();
50   rank = comm->rank();
51   send_increment = send_type->get_extent();
52   recv_increment = recv_type->get_extent();
53   extent = recv_type->get_extent();
54
55   send_increment *= (send_count * num_procs);
56   recv_increment *= (recv_count * num_procs);
57
58   max_size = num_procs * recv_increment;
59
60   unsigned char* tmp_buff = smpi_get_tmp_sendbuffer(max_size);
61
62   curr_size = send_count * num_procs;
63
64   Request::sendrecv(send_ptr, curr_size, send_type, rank, tag,
65                tmp_buff + (rank * recv_increment),
66                curr_size, recv_type, rank, tag, comm, &status);
67
68   while (mask < num_procs) {
69     dst = rank ^ mask;
70     dst_tree_root = dst >> i;
71     dst_tree_root <<= i;
72     rank_tree_root = rank >> i;
73     rank_tree_root <<= i;
74     send_offset = rank_tree_root * send_increment;
75     recv_offset = dst_tree_root * recv_increment;
76
77     if (dst < num_procs) {
78       Request::sendrecv(tmp_buff + send_offset, curr_size, send_type, dst, tag,
79                    tmp_buff + recv_offset, mask * recv_count * num_procs,
80                    recv_type, dst, tag, comm, &status);
81
82       last_recv_count = Status::get_count(&status, recv_type);
83       curr_size += last_recv_count;
84     }
85
86
87     if (dst_tree_root + mask > num_procs) {
88
89       num_procs_completed = num_procs - rank_tree_root - mask;
90       /* num_procs_completed is the number of processes in this
91          subtree that have all the data. Send data to others
92          in a tree fashion. First find root of current tree
93          that is being divided into two. k is the number of
94          least-significant bits in this process's rank that
95          must be zeroed out to find the rank of the root */
96
97       j = mask;
98       k = 0;
99       while (j) {
100         j >>= 1;
101         k++;
102       }
103       k--;
104
105       tmp_mask = mask >> 1;
106
107       while (tmp_mask) {
108         dst = rank ^ tmp_mask;
109
110         tree_root = rank >> k;
111         tree_root <<= k;
112
113         /* send only if this proc has data and destination
114            doesn't have data. at any step, multiple processes
115            can send if they have the data */
116
117         if ((dst > rank)
118             && (rank < tree_root + num_procs_completed)
119             && (dst >= tree_root + num_procs_completed)) {
120           Request::send(tmp_buff + dst_tree_root * send_increment,
121                    last_recv_count, send_type, dst, tag, comm);
122
123         }
124
125         /* recv only if this proc. doesn't have data and sender
126            has data */
127
128         else if ((dst < rank)
129                  && (dst < tree_root + num_procs_completed)
130                  && (rank >= tree_root + num_procs_completed)) {
131           Request::recv(tmp_buff + dst_tree_root * send_increment,
132                    mask * num_procs * send_count, send_type, dst,
133                    tag, comm, &status);
134
135           last_recv_count = Status::get_count(&status, send_type);
136           curr_size += last_recv_count;
137         }
138
139         tmp_mask >>= 1;
140         k--;
141       }
142     }
143
144     mask <<= 1;
145     i++;
146   }
147
148   for (i = 0; i < num_procs; i++)
149     Request::sendrecv(tmp_buff + (rank + i * num_procs) * send_count * extent,
150                  send_count, send_type, rank, tag,
151                  recv_ptr + (i * recv_count * extent),
152                  recv_count, recv_type, rank, tag, comm, &status);
153   smpi_free_tmp_buffer(tmp_buff);
154   return MPI_SUCCESS;
155 }
156 } // namespace simgrid::smpi