Logo AND Algorithmique Numérique Distribuée

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