Move thread_mpi to src/external/
[alexxy/gromacs.git] / src / external / thread_mpi / src / tmpi_ops.h
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
39 #ifdef THREAD_MPI_OPS
40
41 /* cpp wizardry follows...
42
43    This file is #included directly from thread_mpi.c, and constructs
44    MPI_Reduce operators.
45
46    What this does is create the min, max, sum, prod, etc. functions for a given
47    datatype (pre-defined as TYPE, with identifier name TYPENM) and puts pointers
48    to these functions in an array called oplist_TYPENM.
49
50    gmx_thread_mpi_reduce.c includes this file once for each type used by MPI,
51    and thus builds up a set of arrays of function pointers, that then get used
52    in the mpi_datatype_ structure. This way, each operation/datatype entry
53    that makes sense can be extracted easily. Note that we don't (yet) support
54    user-defined ops */
55
56 #define FNAMEr(tp, fn) tMPI_ ## tp ## _ ## fn
57 #define FNAME(tp, fn) FNAMEr(tp, fn)
58
59 /* macros to define functions and prototypes based on a name and an operation */
60 #define FNr(tp, fname, fn) \
61     static void tMPI_ ## tp ## _ ## fname  (void *dest, void *src_a, void *src_b, \
62                                             int count) \
63     { \
64         /*printf("in function %s, count=%d\n", __FUNCTION__, count);*/ \
65         TYPE *a = (TYPE*)src_a; \
66         TYPE *b = (TYPE*)src_b; \
67         TYPE *d = (TYPE*)dest; \
68         int   i; \
69         for (i = 0; i < count; i++) { \
70             d[i] = (TYPE)(fn(a[i], b[i])); } \
71     }
72
73 #define FN(tp, fname, fn) FNr(tp, fname, fn)
74
75 #define OPFNr(tp, fname, operator)  \
76               static void tMPI_ ## tp ## _ ## fname  (void *dest, void *src_a, void *src_b, \
77                                                       int count) \
78               { \
79                   /*printf("in function %s, count=%d\n", __FUNCTION__, count);*/ \
80                   TYPE *a = (TYPE*)src_a; \
81                   TYPE *b = (TYPE*)src_b; \
82                   TYPE *d = (TYPE*)dest; \
83                   int i; \
84                   for (i = 0; i < count; i++) { \
85                       d[i] = (TYPE)(a[i] operator b[i]); } \
86               }
87
88 #define OPFN(tp, fname, operator) OPFNr(tp, fname, operator)
89
90
91 /* these are the function prototypes + definitions: */
92 #define MAX(a, b)  (( (a) > (b) ) ? (a) : (b))
93 FN(TYPENM, max, MAX)
94 #undef MAX
95 #define MIN(a, b)  (( (a) < (b) ) ? (a) : (b))
96 FN(TYPENM, min, MIN)
97 #undef MIN
98 OPFN(TYPENM, sum, +)
99 OPFN(TYPENM, prod, *)
100 #if INTTYPE != 0
101 OPFN(TYPENM, land, &&)
102 OPFN(TYPENM, band, &)
103 OPFN(TYPENM, lor, ||)
104 OPFN(TYPENM, bor, |)
105 OPFN(TYPENM, bxor, ^)
106 #define XOR(a, b)  ( (!a) ^ (!b) )
107 FN(TYPENM, lxor, XOR)
108 #undef XOR
109 #endif
110
111 #define OPARRAYr(tp) oplist_ ## tp
112 #define OPARRAY(tp) OPARRAYr(tp)
113
114 tMPI_Op_fn OPARRAY(TYPENM)[] =
115 {
116     FNAME(TYPENM, max),
117     FNAME(TYPENM, min),
118     FNAME(TYPENM, sum),
119     FNAME(TYPENM, prod),
120 #if INTTYPE
121     FNAME(TYPENM, land),
122     FNAME(TYPENM, band),
123     FNAME(TYPENM, lor),
124     FNAME(TYPENM, bor),
125     FNAME(TYPENM, lxor),
126     FNAME(TYPENM, bxor)
127 #else
128     0,
129     0,
130     0,
131     0,
132     0,
133     0
134 #endif
135 };
136
137
138 #undef FNAME
139 #undef FNAMEr
140 #undef OPARRAYr
141 #undef OPARRAY
142 #undef FN
143 #undef FNr
144 #undef OPFN
145 #undef OPFNr
146
147 #undef TYPE
148 #undef TYPENM
149 #undef INTTYPE
150
151 #endif