Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / gmxlib / thread_mpi / gather.c
1 /*
2    This source code file is part of thread_mpi.
3    Written by Sander Pronk, Erik Lindahl, and possibly others.
4
5    Copyright (c) 2009, Sander Pronk, Erik Lindahl.
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    1) Redistributions of source code must retain the above copyright
11    notice, this list of conditions and the following disclaimer.
12    2) Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15    3) Neither the name of the copyright holders nor the
16    names of its contributors may be used to endorse or promote products
17    derived from this software without specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
20    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22    DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
23    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30    If you want to redistribute modifications, please consider that
31    scientific software is very special. Version control is crucial -
32    bugs must be traceable. We will be happy to consider code for
33    inclusion in the official distribution, but derived work should not
34    be called official thread_mpi. Details are found in the README & COPYING
35    files.
36  */
37
38 #ifdef HAVE_TMPI_CONFIG_H
39 #include "tmpi_config.h"
40 #endif
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <stdarg.h>
55 #include <string.h>
56
57 #include "impl.h"
58 #include "collective.h"
59
60
61 int tMPI_Gather(void* sendbuf, int sendcount, tMPI_Datatype sendtype,
62                 void* recvbuf, int recvcount, tMPI_Datatype recvtype,
63                 int root, tMPI_Comm comm)
64 {
65     int                 synct;
66     struct coll_env    *cev;
67     int                 myrank;
68     int                 ret = TMPI_SUCCESS;
69     struct tmpi_thread *cur = tMPI_Get_current();
70
71 #ifdef TMPI_PROFILE
72     tMPI_Profile_count_start(cur);
73 #endif
74 #ifdef TMPI_TRACE
75     tMPI_Trace_print("tMPI_Gather(%p, %d, %p, %p, %d, %p, %d, %p)",
76                      sendbuf, sendcount, sendtype,
77                      recvbuf, recvcount, recvtype, root, comm);
78 #endif
79
80     if (!comm)
81     {
82         return tMPI_Error(TMPI_COMM_WORLD, TMPI_ERR_COMM);
83     }
84     myrank = tMPI_Comm_seek_rank(comm, cur);
85
86     /* we increase our counter, and determine which coll_env we get */
87     cev = tMPI_Get_cev(comm, myrank, &synct);
88
89     if (myrank == root)
90     {
91         int i;
92         int n_remaining = comm->grp.N-1;
93         /* do root transfer */
94         if (sendbuf != TMPI_IN_PLACE)
95         {
96             tMPI_Coll_root_xfer(comm, sendtype, recvtype,
97                                 sendtype->size*sendcount,
98                                 recvtype->size*recvcount,
99                                 sendbuf,
100                                 (char*)recvbuf+myrank*recvcount*recvtype->size,
101                                 &ret);
102         }
103         for (i = 0; i < comm->grp.N; i++)
104         {
105             cev->met[myrank].read_data[i] = FALSE;
106         }
107         cev->met[myrank].read_data[myrank] = TRUE;
108
109         /* wait for data availability as long as there are xfers to be done */
110         while (n_remaining > 0)
111         {
112 #if defined(TMPI_PROFILE) && defined(TMPI_CYCLE_COUNT)
113             tMPI_Profile_wait_start(cur);
114 #endif
115             tMPI_Event_wait( &(cev->met[myrank]).recv_ev );
116 #if defined(TMPI_PROFILE) && defined(TMPI_CYCLE_COUNT)
117             tMPI_Profile_wait_stop(cur, TMPIWAIT_Coll_recv);
118 #endif
119             /* now check all of them */
120             for (i = 0; i < comm->grp.N; i++)
121             {
122                 if (!cev->met[myrank].read_data[i] &&
123                     (tMPI_Atomic_get(&(cev->met[i].current_sync)) == synct))
124                 {
125                     tMPI_Mult_recv(comm, cev, i, 0, TMPI_GATHER_TAG, recvtype,
126                                    recvcount*recvtype->size,
127                                    (char*)recvbuf+i*recvcount*recvtype->size,
128                                    &ret);
129                     tMPI_Event_process( &(cev->met[myrank]).recv_ev, 1);
130                     if (ret != TMPI_SUCCESS)
131                     {
132                         return ret;
133                     }
134                     cev->met[myrank].read_data[i] = TRUE;
135                     n_remaining--;
136                 }
137             }
138         }
139     }
140     else
141     {
142         if (!sendbuf) /* don't do pointer arithmetic on a NULL ptr */
143         {
144             return tMPI_Error(comm, TMPI_ERR_BUF);
145         }
146
147         /* first set up the data just to root. */
148         tMPI_Post_multi(cev, myrank, 0, TMPI_GATHER_TAG, sendtype,
149                         sendcount*sendtype->size, sendbuf, 1, synct, root);
150         /* and wait until root is done copying */
151         tMPI_Wait_for_others(cev, myrank);
152     }
153 #ifdef TMPI_PROFILE
154     tMPI_Profile_count_stop(cur, TMPIFN_Gather);
155 #endif
156     return ret;
157 }
158
159
160
161
162
163
164 int tMPI_Gatherv(void* sendbuf, int sendcount, tMPI_Datatype sendtype,
165                  void* recvbuf, int *recvcounts, int *displs,
166                  tMPI_Datatype recvtype, int root, tMPI_Comm comm)
167 {
168     int                 synct;
169     struct coll_env    *cev;
170     int                 myrank;
171     int                 ret = TMPI_SUCCESS;
172     struct tmpi_thread *cur = tMPI_Get_current();
173
174 #ifdef TMPI_PROFILE
175     tMPI_Profile_count_start(cur);
176 #endif
177 #ifdef TMPI_TRACE
178     tMPI_Trace_print("tMPI_Gatherv(%p, %d, %p, %p, %p, %p, %p, %d, %p)",
179                      sendbuf, sendcount, sendtype, recvbuf,
180                      recvcounts, displs, recvtype, root, comm);
181 #endif
182
183     if (!comm)
184     {
185         return tMPI_Error(TMPI_COMM_WORLD, TMPI_ERR_COMM);
186     }
187     myrank = tMPI_Comm_seek_rank(comm, cur);
188
189     /* we increase our counter, and determine which coll_env we get */
190     cev = tMPI_Get_cev(comm, myrank, &synct);
191
192     if (myrank == root)
193     {
194         int i;
195         int n_remaining = comm->grp.N-1;
196         /* do root transfer */
197         if (sendbuf != TMPI_IN_PLACE)
198         {
199             tMPI_Coll_root_xfer(comm, sendtype, recvtype,
200                                 sendtype->size*sendcount,
201                                 recvtype->size*recvcounts[myrank],
202                                 sendbuf,
203                                 (char*)recvbuf+displs[myrank]*recvtype->size,
204                                 &ret);
205         }
206         for (i = 0; i < comm->grp.N; i++)
207         {
208             cev->met[myrank].read_data[i] = FALSE;
209         }
210         cev->met[myrank].read_data[myrank] = TRUE;
211
212         /* wait for data availability as long as there are xfers to be done */
213         while (n_remaining > 0)
214         {
215 #if defined(TMPI_PROFILE) && defined(TMPI_CYCLE_COUNT)
216             tMPI_Profile_wait_start(cur);
217 #endif
218             tMPI_Event_wait( &(cev->met[myrank]).recv_ev );
219 #if defined(TMPI_PROFILE) && defined(TMPI_CYCLE_COUNT)
220             tMPI_Profile_wait_stop(cur, TMPIWAIT_Coll_recv);
221 #endif
222             for (i = 0; i < comm->grp.N; i++)
223             {
224                 if (!cev->met[myrank].read_data[i] &&
225                     (tMPI_Atomic_get(&(cev->met[i].current_sync)) == synct) )
226                 {
227                     tMPI_Event_process( &(cev->met[myrank]).recv_ev, 1);
228                     tMPI_Mult_recv(comm, cev, i, 0, TMPI_GATHERV_TAG, recvtype,
229                                    recvcounts[i]*recvtype->size,
230                                    (char*)recvbuf+displs[i]*recvtype->size,
231                                    &ret);
232                     if (ret != TMPI_SUCCESS)
233                     {
234                         return ret;
235                     }
236                     cev->met[myrank].read_data[i] = TRUE;
237                     n_remaining--;
238                 }
239             }
240         }
241     }
242     else
243     {
244         if (!sendbuf) /* don't do pointer arithmetic on a NULL ptr */
245         {
246             return tMPI_Error(comm, TMPI_ERR_BUF);
247         }
248
249         /* first set up the data just to root. */
250         tMPI_Post_multi(cev, myrank, 0, TMPI_GATHERV_TAG, sendtype,
251                         sendcount*sendtype->size, sendbuf, 1, synct, root);
252         /* and wait until root is done copying */
253         tMPI_Wait_for_others(cev, myrank);
254     }
255
256 #ifdef TMPI_PROFILE
257     tMPI_Profile_count_stop(cur, TMPIFN_Gatherv);
258 #endif
259     return ret;
260 }