Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / colls / alltoall / alltoall-bruck.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 /*****************************************************************************
8
9  * Function: alltoall_bruck
10
11  * Return: int
12
13  * inputs:
14     send_buff: send input buffer
15     send_count: number of elements to send
16     send_type: data type of elements being sent
17     recv_buff: receive output buffer
18     recv_count: number of elements to received
19     recv_type: data type of elements being received
20     comm: communicator
21
22  * Descrp: Function realizes the alltoall operation using the bruck algorithm.
23
24  * Author: MPICH / modified by Ahmad Faraj
25
26  ****************************************************************************/
27
28 #include "../colls_private.hpp"
29
30 namespace simgrid::smpi {
31
32 int
33 alltoall__bruck(const void *send_buff, int send_count,
34                 MPI_Datatype send_type, void *recv_buff,
35                 int recv_count, MPI_Datatype recv_type,
36                 MPI_Comm comm)
37 {
38   MPI_Status status;
39   MPI_Aint extent;
40   MPI_Datatype new_type;
41
42   int i, src, dst, rank, num_procs, count, block, position;
43   int pack_size, tag = COLL_TAG_ALLTOALL, pof2 = 1;
44
45   char *send_ptr = (char *) send_buff;
46   char *recv_ptr = (char *) recv_buff;
47
48   num_procs = comm->size();
49   rank = comm->rank();
50
51   extent = recv_type->get_extent();
52
53   unsigned char* tmp_buff = smpi_get_tmp_sendbuffer(num_procs * recv_count * extent);
54   int* disps         = new int[num_procs];
55   int* blocks_length = new int[num_procs];
56
57   Request::sendrecv(send_ptr + rank * send_count * extent,
58                (num_procs - rank) * send_count, send_type, rank, tag,
59                recv_ptr, (num_procs - rank) * recv_count, recv_type, rank,
60                tag, comm, &status);
61
62   Request::sendrecv(send_ptr, rank * send_count, send_type, rank, tag,
63                recv_ptr + (num_procs - rank) * recv_count * extent,
64                rank * recv_count, recv_type, rank, tag, comm, &status);
65
66
67
68   MPI_Pack_size(send_count * num_procs, send_type, comm, &pack_size);
69
70   while (pof2 < num_procs) {
71     dst = (rank + pof2) % num_procs;
72     src = (rank - pof2 + num_procs) % num_procs;
73
74
75     count = 0;
76     for (block = 1; block < num_procs; block++)
77       if (block & pof2) {
78         blocks_length[count] = send_count;
79         disps[count] = block * send_count;
80         count++;
81       }
82
83     MPI_Type_indexed(count, blocks_length, disps, recv_type, &new_type);
84     new_type->commit();
85
86     position = 0;
87     MPI_Pack(recv_buff, 1, new_type, tmp_buff, pack_size, &position, comm);
88
89     Request::sendrecv(tmp_buff, position, MPI_PACKED, dst, tag, recv_buff, 1,
90                  new_type, src, tag, comm, &status);
91     Datatype::unref(new_type);
92
93     pof2 *= 2;
94   }
95
96   delete[] disps;
97   delete[] blocks_length;
98
99   Request::sendrecv(recv_ptr + (rank + 1) * recv_count * extent,
100                (num_procs - rank - 1) * recv_count, send_type,
101                rank, tag, tmp_buff, (num_procs - rank - 1) * recv_count,
102                recv_type, rank, tag, comm, &status);
103
104   Request::sendrecv(recv_ptr, (rank + 1) * recv_count, send_type, rank, tag,
105                tmp_buff + (num_procs - rank - 1) * recv_count * extent,
106                (rank + 1) * recv_count, recv_type, rank, tag, comm, &status);
107
108
109   for (i = 0; i < num_procs; i++)
110     Request::sendrecv(tmp_buff + i * recv_count * extent, recv_count, send_type,
111                  rank, tag,
112                  recv_ptr + (num_procs - i - 1) * recv_count * extent,
113                  recv_count, recv_type, rank, tag, comm, &status);
114
115   smpi_free_tmp_buffer(tmp_buff);
116   return MPI_SUCCESS;
117 }
118
119 } // namespace simgrid::smpi