Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / colls / alltoall / alltoall-2dmesh.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 <cmath>
9
10 /*****************************************************************************
11
12  * Function: alltoall_2dmesh_shoot
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 alltoall operation using the 2dmesh
26            algorithm. It actually performs allgather operation in x dimension
27            then in the y dimension. Each node then extracts the needed data.
28            The communication in each dimension follows "simple."
29
30  * Author: Ahmad Faraj
31
32 ****************************************************************************/
33 static int alltoall_check_is_2dmesh(int num, int *i, int *j)
34 {
35   int x, max = num / 2;
36   x = sqrt(double(num));
37
38   while (x <= max) {
39     if ((num % x) == 0) {
40       *i = x;
41       *j = num / x;
42
43       if (*i > *j) {
44         x = *i;
45         *i = *j;
46         *j = x;
47       }
48
49       return 1;
50     }
51     x++;
52   }
53   return 0;
54 }
55 namespace simgrid::smpi {
56
57 int alltoall__2dmesh(const void *send_buff, int send_count,
58                      MPI_Datatype send_type,
59                      void *recv_buff, int recv_count,
60                      MPI_Datatype recv_type, MPI_Comm comm)
61 {
62   MPI_Status s;
63   MPI_Aint extent;
64
65   int i, j, src, dst, rank, num_procs, count, num_reqs;
66   int X, Y, send_offset, recv_offset;
67   int my_row_base, my_col_base, src_row_base, block_size;
68   int tag = COLL_TAG_ALLTOALL;
69
70   rank = comm->rank();
71   num_procs = comm->size();
72   extent = send_type->get_extent();
73
74   if (not alltoall_check_is_2dmesh(num_procs, &X, &Y))
75     return MPI_ERR_OTHER;
76
77   my_row_base = (rank / Y) * Y;
78   my_col_base = rank % Y;
79
80   block_size = extent * send_count;
81
82   unsigned char* tmp_buff1 = smpi_get_tmp_sendbuffer(block_size * num_procs * Y);
83   unsigned char* tmp_buff2 = smpi_get_tmp_recvbuffer(block_size * Y);
84
85   num_reqs = X;
86   if (Y > X)
87     num_reqs = Y;
88
89   auto* statuses       = new MPI_Status[num_reqs];
90   auto* reqs           = new MPI_Request[num_reqs];
91   MPI_Request* req_ptr = reqs;
92
93   count = send_count * num_procs;
94
95   for (i = 0; i < Y; i++) {
96     src = i + my_row_base;
97     if (src == rank)
98       continue;
99
100     recv_offset = (src % Y) * block_size * num_procs;
101     *(req_ptr++) = Request::irecv(tmp_buff1 + recv_offset, count, recv_type, src, tag, comm);
102   }
103
104   for (i = 0; i < Y; i++) {
105     dst = i + my_row_base;
106     if (dst == rank)
107       continue;
108     Request::send(send_buff, count, send_type, dst, tag, comm);
109   }
110
111   Request::waitall(Y - 1, reqs, statuses);
112   req_ptr = reqs;
113
114   for (i = 0; i < Y; i++) {
115     send_offset = (rank * block_size) + (i * block_size * num_procs);
116     recv_offset = (my_row_base * block_size) + (i * block_size);
117
118     if (i + my_row_base == rank)
119       Request::sendrecv((char *) send_buff + recv_offset, send_count, send_type,
120                    rank, tag,
121                    (char *) recv_buff + recv_offset, recv_count, recv_type,
122                    rank, tag, comm, &s);
123
124     else
125       Request::sendrecv(tmp_buff1 + send_offset, send_count, send_type,
126                    rank, tag,
127                    (char *) recv_buff + recv_offset, recv_count, recv_type,
128                    rank, tag, comm, &s);
129   }
130
131
132   for (i = 0; i < X; i++) {
133     src = (i * Y + my_col_base);
134     if (src == rank)
135       continue;
136     src_row_base = (src / Y) * Y;
137
138     *(req_ptr++) = Request::irecv((char *) recv_buff + src_row_base * block_size, recv_count * Y,
139               recv_type, src, tag, comm);
140   }
141
142   for (i = 0; i < X; i++) {
143     dst = (i * Y + my_col_base);
144     if (dst == rank)
145       continue;
146
147     recv_offset = 0;
148     for (j = 0; j < Y; j++) {
149       send_offset = (dst + j * num_procs) * block_size;
150
151       if (j + my_row_base == rank)
152         Request::sendrecv((char *) send_buff + dst * block_size, send_count,
153                      send_type, rank, tag, tmp_buff2 + recv_offset, recv_count,
154                      recv_type, rank, tag, comm, &s);
155       else
156         Request::sendrecv(tmp_buff1 + send_offset, send_count, send_type,
157                      rank, tag,
158                      tmp_buff2 + recv_offset, recv_count, recv_type,
159                      rank, tag, comm, &s);
160
161       recv_offset += block_size;
162     }
163
164     Request::send(tmp_buff2, send_count * Y, send_type, dst, tag, comm);
165   }
166   Request::waitall(X - 1, reqs, statuses);
167   delete[] reqs;
168   delete[] statuses;
169   smpi_free_tmp_buffer(tmp_buff1);
170   smpi_free_tmp_buffer(tmp_buff2);
171   return MPI_SUCCESS;
172 }
173 } // namespace simgrid::smpi