Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / smpi / colls / allgather / allgather-spreading-simple.cpp
1 /* Copyright (c) 2013-2019. 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
9 /*****************************************************************************
10
11 Copyright (c) 2006, Ahmad Faraj & Xin Yuan,
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16
17   * Redistributions of source code must retain the above copyright notice,
18     this list of conditions and the following disclaimer.
19
20   * Redistributions in binary form must reproduce the above copyright notice,
21     this list of conditions and the following disclaimer in the documentation
22     and/or other materials provided with the distribution.
23
24   * Neither the name of the Florida State University nor the names of its
25     contributors may be used to endorse or promote products derived from this
26     software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
32 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
35 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39   *************************************************************************
40   *     Any results obtained from executing this software require the     *
41   *     acknowledgment and citation of the software and its owners.       *
42   *     The full citation is given below:                                 *
43   *                                                                       *
44   *     A. Faraj and X. Yuan. "Automatic Generation and Tuning of MPI     *
45   *     Collective Communication Routines." The 19th ACM International    *
46   *     Conference on Supercomputing (ICS), Cambridge, Massachusetts,     *
47   *     June 20-22, 2005.                                                 *
48   *************************************************************************
49
50 *****************************************************************************/
51
52 /*****************************************************************************
53  * Function: allgather_spreading_simple
54  * return: int
55  *  inputs:
56  *   send_buff: send input buffer
57  *   send_count: number of elements to send
58  *   send_type: data type of elements being sent
59  *   recv_buff: receive output buffer
60  *   recv_count: number of elements to received
61  *   recv_type: data type of elements being received
62  *   comm: communication
63  * Descrp: Let i -> j denote the communication from node i to node j. The
64  *         order of communications for node i is i -> i + 1, i -> i + 2, ...,
65  *         i -> (i + p -1) % P.
66  *
67  * Auther: Ahmad Faraj
68  ****************************************************************************/
69
70 namespace simgrid{
71 namespace smpi{
72
73
74 int
75 Coll_allgather_spreading_simple::allgather(void *send_buff, int send_count,
76                                            MPI_Datatype send_type,
77                                            void *recv_buff, int recv_count,
78                                            MPI_Datatype recv_type,
79                                            MPI_Comm comm)
80 {
81   MPI_Request *reqs, *req_ptr;
82   MPI_Aint extent;
83   int i, src, dst, rank, num_procs, num_reqs;
84   int tag = COLL_TAG_ALLGATHER;
85   MPI_Status status;
86   char *recv_ptr = (char *) recv_buff;
87
88   rank = comm->rank();
89   num_procs = comm->size();
90   extent = send_type->get_extent();
91
92   num_reqs = (2 * num_procs) - 2;
93   reqs = (MPI_Request *) xbt_malloc(num_reqs * sizeof(MPI_Request));
94   if (not reqs) {
95     printf("allgather-spreading-simple.c:40: cannot allocate memory\n");
96     MPI_Finalize();
97     exit(0);
98   }
99
100   req_ptr = reqs;
101   Request::sendrecv(send_buff, send_count, send_type, rank, tag,
102                (char *) recv_buff + rank * recv_count * extent, recv_count,
103                recv_type, rank, tag, comm, &status);
104
105   for (i = 0; i < num_procs; i++) {
106     src = (rank + i) % num_procs;
107     if (src == rank)
108       continue;
109     *(req_ptr++) = Request::irecv(recv_ptr + src * recv_count * extent, recv_count, recv_type,
110               src, tag, comm);
111   }
112
113   for (i = 0; i < num_procs; i++) {
114     dst = (rank + i) % num_procs;
115     if (dst == rank)
116       continue;
117     *(req_ptr++) = Request::isend(send_buff, send_count, send_type, dst, tag, comm);
118   }
119
120   Request::waitall(num_reqs, reqs, MPI_STATUSES_IGNORE);
121   free(reqs);
122
123   return MPI_SUCCESS;
124 }
125
126 }
127 }