Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / mdlib / mvxvf.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 /* This file is completely threadsafe - keep it that way! */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <sysstuff.h>
41 #include <string.h>
42 #include "typedefs.h"
43 #include "main.h"
44 #include "mvdata.h"
45 #include "network.h"
46 #include "smalloc.h"
47 #include "gmx_fatal.h"
48 #include "symtab.h"
49 #include "main.h"
50 #include "typedefs.h"
51 #include "vec.h"
52 #include "tgroup.h"
53 #include "nrnb.h"
54 #include "partdec.h"
55
56 void move_rvecs(const t_commrec *cr, gmx_bool bForward, gmx_bool bSum,
57                 int left, int right, rvec vecs[], rvec buf[],
58                 int shift, t_nrnb *nrnb)
59 {
60     int     i, j, j0 = 137, j1 = 391;
61     int     cur, nsum;
62     int    *index;
63 #define next ((cur + 1) % cr->nnodes)
64 #define prev ((cur - 1 + cr->nnodes) % cr->nnodes)
65
66 #define HOMENRI(ind, i) ((ind)[(i)+1] - (ind)[(i)])
67
68     index = pd_index(cr);
69
70     if (bSum)
71     {
72         cur = (cr->nodeid + pd_shift(cr)) % cr->nnodes;
73     }
74     else
75     {
76         cur = cr->nodeid;
77     }
78
79     nsum = 0;
80     for (i = 0; (i < shift); i++)
81     {
82         if (bSum)
83         {
84             if (bForward)
85             {
86                 j0 = index[prev];
87                 j1 = index[prev+1];
88             }
89             else
90             {
91                 j0 = index[next];
92                 j1 = index[next+1];
93             }
94             for (j = j0; (j < j1); j++)
95             {
96                 clear_rvec(buf[j]);
97             }
98         }
99         /* Forward pulse around the ring, to increasing NODE number */
100         if (bForward)
101         {
102             if (bSum)
103             {
104                 gmx_tx_rx_real(cr,
105                                GMX_RIGHT, vecs[index[cur ]], HOMENRI(index, cur )*DIM,
106                                GMX_LEFT, buf [index[prev]], HOMENRI(index, prev)*DIM);
107             }
108             else
109             {
110                 gmx_tx_rx_real(cr,
111                                GMX_RIGHT, vecs[index[cur ]], HOMENRI(index, cur )*DIM,
112                                GMX_LEFT, vecs[index[prev]], HOMENRI(index, prev)*DIM);
113             }
114             /* Wait for communication to end */
115             gmx_wait(cr, right, left);
116         }
117
118         /* Backward pulse around the ring, to decreasing NODE number */
119         else
120         {
121             if (bSum)
122             {
123                 gmx_tx_rx_real(cr,
124                                GMX_LEFT, vecs[index[cur ]], HOMENRI(index, cur )*DIM,
125                                GMX_RIGHT, buf [index[next]], HOMENRI(index, next)*DIM);
126             }
127             else
128             {
129                 gmx_tx_rx_real(cr,
130                                GMX_LEFT, vecs[index[cur ]], HOMENRI(index, cur )*DIM,
131                                GMX_RIGHT, vecs[index[next]], HOMENRI(index, next)*DIM);
132             }
133             /* Wait for communication to end */
134             gmx_wait(cr, left, right);
135         }
136
137         /* Actual summation */
138         if (bSum)
139         {
140             for (j = j0; (j < j1); j++)
141             {
142                 rvec_inc(vecs[j], buf[j]);
143             }
144             nsum += (j1-j0);
145         }
146         if (bForward)
147         {
148             cur = prev;
149         }
150         else
151         {
152             cur = next;
153         }
154     }
155     if (nsum > 0)
156     {
157         inc_nrnb(nrnb, eNR_FSUM, nsum);
158     }
159 #undef next
160 #undef prev
161 }
162
163
164 void move_reals(const t_commrec *cr, gmx_bool bForward, gmx_bool bSum,
165                 int left, int right, real reals[], real buf[],
166                 int shift, t_nrnb *nrnb)
167 {
168     int     i, j, j0 = 137, j1 = 391;
169     int     cur, nsum;
170     int    *index;
171 #define next ((cur + 1) % cr->nnodes)
172 #define prev ((cur - 1 + cr->nnodes) % cr->nnodes)
173
174 #define HOMENRI(ind, i) ((ind)[(i)+1] - (ind)[(i)])
175
176     index = pd_index(cr);
177
178     if (bSum)
179     {
180         cur = (cr->nodeid + pd_shift(cr)) % cr->nnodes;
181     }
182     else
183     {
184         cur = cr->nodeid;
185     }
186
187     nsum = 0;
188     for (i = 0; (i < shift); i++)
189     {
190         if (bSum)
191         {
192             if (bForward)
193             {
194                 j0 = index[prev];
195                 j1 = index[prev+1];
196             }
197             else
198             {
199                 j0 = index[next];
200                 j1 = index[next+1];
201             }
202             for (j = j0; (j < j1); j++)
203             {
204                 buf[j] = 0.0;
205             }
206         }
207         /* Forward pulse around the ring, to increasing NODE number */
208         if (bForward)
209         {
210             if (bSum)
211             {
212                 gmx_tx_rx_real(cr,
213                                GMX_RIGHT, reals+index[cur ], HOMENRI(index, cur ),
214                                GMX_LEFT, buf+index[prev], HOMENRI(index, prev));
215             }
216             else
217             {
218                 gmx_tx_rx_real(cr,
219                                GMX_RIGHT, reals+index[cur ], HOMENRI(index, cur ),
220                                GMX_LEFT, reals+index[prev], HOMENRI(index, prev));
221             }
222             /* Wait for communication to end */
223             gmx_wait(cr, right, left);
224         }
225         else
226         {
227             /* Backward pulse around the ring, to decreasing NODE number */
228             if (bSum)
229             {
230                 gmx_tx_rx_real(cr,
231                                GMX_LEFT, reals+index[cur ], HOMENRI(index, cur ),
232                                GMX_RIGHT, buf+index[next], HOMENRI(index, next));
233             }
234             else
235             {
236                 gmx_tx_rx_real(cr,
237                                GMX_LEFT, reals+index[cur ], HOMENRI(index, cur ),
238                                GMX_RIGHT, reals+index[next], HOMENRI(index, next));
239                 /* Wait for communication to end */
240             }
241             gmx_wait(cr, left, right);
242         }
243
244         /* Actual summation */
245         if (bSum)
246         {
247             for (j = j0; (j < j1); j++)
248             {
249                 reals[j] += buf[j];
250             }
251             nsum += (j1-j0);
252         }
253         if (bForward)
254         {
255             cur = prev;
256         }
257         else
258         {
259             cur = next;
260         }
261     }
262
263     if (nsum > 0)
264     {
265         inc_nrnb(nrnb, eNR_FSUM, nsum/3);
266     }
267 #undef next
268 #undef prev
269 }
270
271 void move_x(FILE *log, const t_commrec *cr,
272             int left, int right, rvec x[],
273             t_nrnb *nrnb)
274 {
275     move_rvecs(cr, FALSE, FALSE, left, right, x, NULL, pd_shift(cr), nrnb);
276     move_rvecs(cr, TRUE, FALSE, left, right, x, NULL, pd_bshift(cr), nrnb);
277
278     where();
279 }
280
281 void move_rborn(FILE *log, const t_commrec *cr,
282                 int left, int right, real rborn[],
283                 t_nrnb *nrnb)
284 {
285     move_reals(cr, FALSE, FALSE, left, right, rborn, NULL, pd_shift(cr), nrnb);
286     move_reals(cr, TRUE, FALSE, left, right, rborn, NULL, pd_bshift(cr), nrnb);
287
288     where();
289 }
290
291 void move_f(FILE *log, const t_commrec *cr,
292             int left, int right, rvec f[], rvec fadd[],
293             t_nrnb *nrnb)
294 {
295     move_rvecs(cr, TRUE, TRUE, left, right, f, fadd, pd_shift(cr), nrnb);
296     move_rvecs(cr, FALSE, TRUE, left, right, f, fadd, pd_bshift(cr), nrnb);
297
298     where();
299 }
300
301 void move_gpol(FILE *log, const t_commrec *cr,
302                int left, int right, real gpol[], real gpol_add[],
303                t_nrnb *nrnb)
304 {
305     move_reals(cr, TRUE, TRUE, left, right, gpol, gpol_add, pd_shift(cr), nrnb);
306     move_reals(cr, FALSE, TRUE, left, right, gpol, gpol_add, pd_bshift(cr), nrnb);
307
308     where();
309 }
310
311
312 void move_cgcm(FILE *log, const t_commrec *cr, rvec cg_cm[])
313 {
314     int  i, start, nr;
315     int  cur = cr->nodeid;
316     int *cgindex;
317
318 #define next ((cur+1) % cr->nnodes)
319
320     cgindex = pd_cgindex(cr);
321
322     for (i = 0; (i < cr->nnodes-1); i++)
323     {
324         start = cgindex[cur];
325         nr    = cgindex[cur+1] - start;
326
327         gmx_tx(cr, GMX_LEFT, cg_cm[start], nr*sizeof(cg_cm[0]));
328 #ifdef DEBUG
329         fprintf(log, "move_cgcm: TX start=%d, nr=%d\n", start, nr);
330 #endif
331         start = cgindex[next];
332         nr    = cgindex[next+1] - start;
333
334         gmx_rx(cr, GMX_RIGHT, cg_cm[start], nr*sizeof(cg_cm[0]));
335 #ifdef DEBUG
336         fprintf(log, "move_cgcm: RX start=%d, nr=%d\n", start, nr);
337 #endif
338         gmx_tx_wait(cr, GMX_LEFT);
339         gmx_rx_wait(cr, GMX_RIGHT);
340
341         if (debug)
342         {
343             fprintf(debug, "cgcm[0][XX] %f\n", cg_cm[0][XX]);
344         }
345
346         cur = next;
347     }
348 #undef next
349 }