cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vectorize_reduce.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 #include "cddefines.h"
4 #include "vectorize.h"
5 
6 //
7 // This file should ONLY contain simple routines that implement vectorizable loops.
8 // These loops could then also be targets for parallelization, e.g. with openMP.
9 // It is imperative to check that the loops are written up in such a way that the
10 // compiler allows the vectorization to take place. With g++ this can be done with
11 // g++ [ other options ] -ftree-vectorizer-verbose=1 <somefile.cpp>
12 // This will generate a report about loop vectorization on stderr.
13 //
14 // Note that for the reduction loops more aggressive optimization options are needed
15 // in order for them to be vectorized. Most importantly, the compiler assumes that
16 // -fno-trapping-math is in effect in this file. This can cause problems with
17 // conditional statements, e.g. like this one
18 // y = ( x > 0. ) ? log(x) : -70.;
19 // These sort of conditionals should be avoided here.
20 //
21 
22 // calculate Sum( a[i] )
23 double reduce_a(const double* a, long ilo, long ihi)
24 {
25  DEBUG_ENTRY( "reduce_a()" );
26 
27  double sum = 0.;
28  for( long i=ilo; i < ihi; ++i )
29  sum += a[i];
30  return sum;
31 }
32 
33 // calculate Sum( a[i] )
34 sys_float reduce_a(const sys_float* a, long ilo, long ihi)
35 {
36  DEBUG_ENTRY( "reduce_a()" );
37 
38  sys_float sum = 0.f;
39  for( long i=ilo; i < ihi; ++i )
40  sum += a[i];
41  return sum;
42 }
43 
44 // calculate Sum( a[i]*b[i] )
45 double reduce_ab(const double* a, const double* b, long ilo, long ihi)
46 {
47  DEBUG_ENTRY( "reduce_ab()" );
48 
49  double sum = 0.;
50  for( long i=ilo; i < ihi; ++i )
51  sum += a[i]*b[i];
52  return sum;
53 }
54 
55 // calculate Sum( a[i]*double(b[i]) )
56 double reduce_ab(const double* a, const sys_float* b, long ilo, long ihi)
57 {
58  DEBUG_ENTRY( "reduce_ab()" );
59 
60  double sum = 0.;
61  for( long i=ilo; i < ihi; ++i )
62  sum += a[i]*double(b[i]);
63  return sum;
64 }
65 
66 // calculate Sum( a[i]*b[i] )
67 sys_float reduce_ab(const sys_float* a, const sys_float* b, long ilo, long ihi)
68 {
69  DEBUG_ENTRY( "reduce_ab()" );
70 
71  sys_float sum = 0.f;
72  for( long i=ilo; i < ihi; ++i )
73  sum += a[i]*b[i];
74  return sum;
75 }
76 
77 // calculate Sum( a[i]*b[i]*c[i] )
78 double reduce_abc(const double* a, const double* b, const double* c, long ilo, long ihi)
79 {
80  DEBUG_ENTRY( "reduce_abc()" );
81 
82  double sum = 0.;
83  for( long i=ilo; i < ihi; ++i )
84  sum += a[i]*b[i]*c[i];
85  return sum;
86 }
87 
88 // calculate Sum( a[i]*b[i]*double(c[i]) )
89 double reduce_abc(const double* a, const double* b, const sys_float* c, long ilo, long ihi)
90 {
91  DEBUG_ENTRY( "reduce_abc()" );
92 
93  double sum = 0.;
94  for( long i=ilo; i < ihi; ++i )
95  sum += a[i]*b[i]*double(c[i]);
96  return sum;
97 }
98 
99 // calculate Sum( a[i]*double(b[i])*double(c[i]) )
100 double reduce_abc(const double* a, const sys_float* b, const sys_float* c, long ilo, long ihi)
101 {
102  DEBUG_ENTRY( "reduce_abc()" );
103 
104  double sum = 0.;
105  for( long i=ilo; i < ihi; ++i )
106  sum += a[i]*double(b[i])*double(c[i]);
107  return sum;
108 }
109 
110 // calculate Sum( a[i]*b[i]*c[i] )
111 sys_float reduce_abc(const sys_float* a, const sys_float* b, const sys_float* c, long ilo, long ihi)
112 {
113  DEBUG_ENTRY( "reduce_abc()" );
114 
115  sys_float sum = 0.f;
116  for( long i=ilo; i < ihi; ++i )
117  sum += a[i]*b[i]*c[i];
118  return sum;
119 }
120 
121 // calculate Sum( a[i]*b[i] ), sum_a = Sum( a[i] )
122 double reduce_ab_a(const double* a, const double* b, long ilo, long ihi, double* sum_a)
123 {
124  DEBUG_ENTRY( "reduce_ab_a()" );
125 
126  double sum1 = 0.;
127  double sum2 = 0.;
128  for( long i=ilo; i < ihi; ++i )
129  {
130  double one = a[i];
131  sum1 += one;
132  sum2 += one*b[i];
133  }
134  *sum_a = sum1;
135  return sum2;
136 }
137 
138 // calculate Sum( double(a[i])*b[i] ), sum_a = Sum( double(a[i]) )
139 double reduce_ab_a(const sys_float* a, const double* b, long ilo, long ihi, double* sum_a)
140 {
141  DEBUG_ENTRY( "reduce_ab_a()" );
142 
143  double sum1 = 0.;
144  double sum2 = 0.;
145  for( long i=ilo; i < ihi; ++i )
146  {
147  double one = double(a[i]);
148  sum1 += one;
149  sum2 += one*b[i];
150  }
151  *sum_a = sum1;
152  return sum2;
153 }
154 
155 // calculate Sum( a[i]*double(b[i]) ), sum_a = Sum( a[i] )
156 double reduce_ab_a(const double* a, const sys_float* b, long ilo, long ihi, double* sum_a)
157 {
158  DEBUG_ENTRY( "reduce_ab_a()" );
159 
160  double sum1 = 0.;
161  double sum2 = 0.;
162  for( long i=ilo; i < ihi; ++i )
163  {
164  double one = a[i];
165  sum1 += one;
166  sum2 += one*double(b[i]);
167  }
168  *sum_a = sum1;
169  return sum2;
170 }
171 
172 // calculate Sum( a[i]*b[i] ), sum_a = Sum( a[i] )
173 sys_float reduce_ab_a(const sys_float* a, const sys_float* b, long ilo, long ihi, sys_float* sum_a)
174 {
175  DEBUG_ENTRY( "reduce_ab_a()" );
176 
177  sys_float sum1 = 0.f;
178  sys_float sum2 = 0.f;
179  for( long i=ilo; i < ihi; ++i )
180  {
181  sys_float one = a[i];
182  sum1 += one;
183  sum2 += one*b[i];
184  }
185  *sum_a = sum1;
186  return sum2;
187 }
188 
189 // calculate Sum( a[i]*b[i]*c[i] ), sum_ab = Sum( a[i]*b[i] )
190 double reduce_abc_ab(const double* a, const double* b, const double* c, long ilo, long ihi, double* sum_ab)
191 {
192  DEBUG_ENTRY( "reduce_abc_ab()" );
193 
194  double sum2 = 0.;
195  double sum3 = 0.;
196  for( long i=ilo; i < ihi; ++i )
197  {
198  double one = a[i]*b[i];
199  sum2 += one;
200  sum3 += one*c[i];
201  }
202  *sum_ab = sum2;
203  return sum3;
204 }
double reduce_a(const double *a, long ilo, long ihi)
float sys_float
Definition: cddefines.h:127
double reduce_abc_ab(const double *a, const double *b, const double *c, long ilo, long ihi, double *sum_ab)
double reduce_ab(const double *a, const double *b, long ilo, long ihi)
double reduce_ab_a(const double *a, const double *b, long ilo, long ihi, double *sum_a)
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:723
double reduce_abc(const double *a, const double *b, const double *c, long ilo, long ihi)