1 /* Copyright (c) 2013-2022. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /*****************************************************************************
9 * Function: alltoall_bruck
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
22 * Descrp: Function realizes the alltoall operation using the bruck algorithm.
24 * Author: MPICH / modified by Ahmad Faraj
26 ****************************************************************************/
28 #include "../colls_private.hpp"
30 namespace simgrid::smpi {
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,
40 MPI_Datatype new_type;
42 int i, src, dst, rank, num_procs, count, block, position;
43 int pack_size, tag = COLL_TAG_ALLTOALL, pof2 = 1;
45 char *send_ptr = (char *) send_buff;
46 char *recv_ptr = (char *) recv_buff;
48 num_procs = comm->size();
51 extent = recv_type->get_extent();
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];
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,
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);
68 MPI_Pack_size(send_count * num_procs, send_type, comm, &pack_size);
70 while (pof2 < num_procs) {
71 dst = (rank + pof2) % num_procs;
72 src = (rank - pof2 + num_procs) % num_procs;
76 for (block = 1; block < num_procs; block++)
78 blocks_length[count] = send_count;
79 disps[count] = block * send_count;
83 MPI_Type_indexed(count, blocks_length, disps, recv_type, &new_type);
87 MPI_Pack(recv_buff, 1, new_type, tmp_buff, pack_size, &position, comm);
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);
97 delete[] blocks_length;
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);
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);
109 for (i = 0; i < num_procs; i++)
110 Request::sendrecv(tmp_buff + i * recv_count * extent, recv_count, send_type,
112 recv_ptr + (num_procs - i - 1) * recv_count * extent,
113 recv_count, recv_type, rank, tag, comm, &status);
115 smpi_free_tmp_buffer(tmp_buff);
119 } // namespace simgrid::smpi