1 /* Copyright (c) 2013-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "../colls_private.hpp"
7 #include "xbt/string.hpp"
9 namespace simgrid::smpi {
11 int allgather__loosely_lr(const void *sbuf, int scount,
12 MPI_Datatype stype, void *rbuf,
13 int rcount, MPI_Datatype rtype,
17 int tag = COLL_TAG_ALLGATHER;
18 int i, j, send_offset, recv_offset;
19 int intra_rank, inter_rank, inter_comm_size, intra_comm_size;
20 int inter_dst, inter_src;
22 comm_size = comm->size();
24 if(comm->get_leaders_comm()==MPI_COMM_NULL){
28 if (comm->is_uniform()){
29 num_core = comm->get_intra_comm()->size();
32 if(comm_size%num_core)
33 throw std::invalid_argument(xbt::string_printf(
34 "allgather loosely lr algorithm can't be used with non multiple of NUM_CORE=%d number of processes!",
38 MPI_Aint rextent, sextent;
39 rextent = rtype->get_extent();
40 sextent = stype->get_extent();
41 MPI_Request inter_rrequest;
42 MPI_Request rrequest_array[128];
43 MPI_Request srequest_array[128];
44 MPI_Request inter_srequest_array[128];
47 int rrequest_count = 0;
48 int srequest_count = 0;
49 int inter_srequest_count = 0;
53 intra_rank = rank % num_core;
54 inter_rank = rank / num_core;
55 inter_comm_size = (comm_size + num_core - 1) / num_core;
56 intra_comm_size = num_core;
60 //copy corresponding message from sbuf to rbuf
61 recv_offset = rank * rextent * rcount;
62 Request::sendrecv(sbuf, scount, stype, rank, tag,
63 (char *)rbuf + recv_offset, rcount, rtype, rank, tag, comm, &status);
66 int inter_send_offset, inter_recv_offset;
70 inter_srequest_count = 0;
72 for (i = 0; i < inter_comm_size; i++) {
74 // inter_communication
76 inter_dst = (rank + intra_comm_size) % comm_size;
77 inter_src = (rank - intra_comm_size + comm_size) % comm_size;
80 ((inter_rank - 1 - i +
81 inter_comm_size) % inter_comm_size) * intra_comm_size + intra_rank;
84 inter_comm_size) % inter_comm_size) * intra_comm_size + intra_rank;
86 inter_send_offset = dst_seg * sextent * scount;
87 inter_recv_offset = src_seg * rextent * rcount;
89 for (j = 0; j < intra_comm_size; j++) {
91 // inter communication
92 if (intra_rank == j) {
93 if (i != inter_comm_size - 1) {
95 inter_rrequest = Request::irecv((char*)rbuf + inter_recv_offset, rcount, rtype, inter_src, tag, comm);
96 inter_srequest_array[inter_srequest_count++] =
97 Request::isend((char*)rbuf + inter_send_offset, scount, stype, inter_dst, tag, comm);
100 //intra_communication
101 src = inter_rank * intra_comm_size + j;
102 dst = inter_rank * intra_comm_size + j;
106 inter_comm_size) % inter_comm_size) * intra_comm_size + j;
109 inter_comm_size) % inter_comm_size) * intra_comm_size + intra_rank;
111 send_offset = dst_seg * sextent * scount;
112 recv_offset = src_seg * rextent * rcount;
115 if (j != intra_rank) {
117 rrequest_array[rrequest_count++] = Request::irecv((char *)rbuf + recv_offset, rcount, rtype, src, tag, comm);
118 srequest_array[srequest_count++] = Request::isend((char *)rbuf + send_offset, scount, stype, dst, tag, comm);
124 // wait for inter communication to finish for these rounds (# of round equals num_core)
125 if (i != inter_comm_size - 1) {
126 Request::wait(&inter_rrequest, &status);
131 Request::waitall(rrequest_count, rrequest_array, MPI_STATUSES_IGNORE);
132 Request::waitall(srequest_count, srequest_array, MPI_STATUSES_IGNORE);
133 Request::waitall(inter_srequest_count, inter_srequest_array, MPI_STATUSES_IGNORE);
138 } // namespace simgrid::smpi