source: sasmodels/sasmodels/kernel_header.c @ d5ce7fa

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since d5ce7fa was 74e9b5f, checked in by pkienzle, 6 years ago

autotag functions as device functions for cuda. Refs #1076.

  • Property mode set to 100644
File size: 9.0 KB
Line 
1#ifdef __OPENCL_VERSION__
2# define USE_OPENCL
3#elif defined(__CUDACC__)
4# define USE_CUDA
5#elif defined(_OPENMP)
6# define USE_OPENMP
7#endif
8
9// Use SAS_DOUBLE to force the use of double even for float kernels
10#define SAS_DOUBLE dou ## ble
11
12// If opencl is not available, then we are compiling a C function
13// Note: if using a C++ compiler, then define kernel as extern "C"
14#ifdef USE_OPENCL
15
16   #define USE_GPU
17   #define pglobal global
18   #define pconstant constant
19
20   typedef int int32_t;
21
22   #if defined(USE_SINCOS)
23   #  define SINCOS(angle,svar,cvar) svar=sincos(angle,&cvar)
24   #else
25   #  define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0)
26   #endif
27   // Intel CPU on Mac gives strange values for erf(); on the verified
28   // platforms (intel, nvidia, amd), the cephes erf() is significantly
29   // faster than that available in the native OpenCL.
30   #define NEED_ERF
31   // OpenCL only has type generic math
32   #define expf exp
33   #ifndef NEED_ERF
34   #  define erff erf
35   #  define erfcf erfc
36   #endif
37
38#elif defined(USE_CUDA)
39
40   #define USE_GPU
41   #define local __shared__
42   #define pglobal
43   #define constant __constant__
44   #define pconstant const
45   #define kernel extern "C" __global__
46
47   // OpenCL powr(a,b) = C99 pow(a,b), b >= 0
48   // OpenCL pown(a,b) = C99 pow(a,b), b integer
49   #define powr(a,b) pow(a,b)
50   #define pown(a,b) pow(a,b)
51   //typedef int int32_t;
52   #if defined(USE_SINCOS)
53   #  define SINCOS(angle,svar,cvar) sincos(angle,&svar,&cvar)
54   #else
55   #  define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0)
56   #endif
57
58#else // !USE_OPENCL && !USE_CUDA
59
60   #define local
61   #define pglobal
62   #define constant const
63   #define pconstant const
64
65   #ifdef __cplusplus
66      #include <cstdio>
67      #include <cmath>
68      using namespace std;
69      #if defined(_MSC_VER)
70         #include <limits>
71         #include <float.h>
72         #define kernel extern "C" __declspec( dllexport )
73         inline double trunc(double x) { return x>=0?floor(x):-floor(-x); }
74         inline double fmin(double x, double y) { return x>y ? y : x; }
75         inline double fmax(double x, double y) { return x<y ? y : x; }
76         #define isnan(x) _isnan(x)
77         #define isinf(x) (!_finite(x))
78         #define isfinite(x) _finite(x)
79         #define NAN (std::numeric_limits<double>::quiet_NaN()) // non-signalling NaN
80         #define INFINITY (std::numeric_limits<double>::infinity())
81         #define NEED_ERF
82         #define NEED_EXPM1
83         #define NEED_TGAMMA
84     #else
85         #define kernel extern "C"
86         #include <cstdint>
87     #endif
88     inline void SINCOS(double angle, double &svar, double &cvar) { svar=sin(angle); cvar=cos(angle); }
89   #else // !__cplusplus
90     #include <inttypes.h>  // C99 guarantees that int32_t types is here
91     #include <stdio.h>
92     #if defined(__TINYC__)
93         typedef int int32_t;
94         #include <math.h>
95         // TODO: check isnan is correct
96         inline double _isnan(double x) { return x != x; } // hope this doesn't optimize away!
97         #undef isnan
98         #define isnan(x) _isnan(x)
99         // Defeat the double->float conversion since we don't have tgmath
100         inline SAS_DOUBLE trunc(SAS_DOUBLE x) { return x>=0?floor(x):-floor(-x); }
101         inline SAS_DOUBLE fmin(SAS_DOUBLE x, SAS_DOUBLE y) { return x>y ? y : x; }
102         inline SAS_DOUBLE fmax(SAS_DOUBLE x, SAS_DOUBLE y) { return x<y ? y : x; }
103         #define NEED_ERF
104         #define NEED_EXPM1
105         #define NEED_TGAMMA
106         // expf missing from windows?
107         #define expf exp
108     #else
109         #include <tgmath.h> // C99 type-generic math, so sin(float) => sinf
110     #endif
111     // MSVC doesn't support C99, so no need for dllexport on C99 branch
112     #define kernel
113     #define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0)
114   #endif  // !__cplusplus
115   // OpenCL powr(a,b) = C99 pow(a,b), b >= 0
116   // OpenCL pown(a,b) = C99 pow(a,b), b integer
117   #define powr(a,b) pow(a,b)
118   #define pown(a,b) pow(a,b)
119
120#endif // !USE_OPENCL
121
122#if defined(NEED_EXPM1)
123   // TODO: precision is a half digit lower than numpy on mac in [1e-7, 0.5]
124   // Run "explore/precision.py sas_expm1" to see this (may have to fiddle
125   // the xrange for log to see the complete range).
126   static SAS_DOUBLE expm1(SAS_DOUBLE x_in) {
127      double x = (double)x_in;  // go back to float for single precision kernels
128      // Adapted from the cephes math library.
129      // Copyright 1984 - 1992 by Stephen L. Moshier
130      if (x != x || x == 0.0) {
131         return x; // NaN and +/- 0
132      } else if (x < -0.5 || x > 0.5) {
133         return exp(x) - 1.0;
134      } else {
135         const double xsq = x*x;
136         const double p = (((
137            +1.2617719307481059087798E-4)*xsq
138            +3.0299440770744196129956E-2)*xsq
139            +9.9999999999999999991025E-1);
140         const double q = ((((
141            +3.0019850513866445504159E-6)*xsq
142            +2.5244834034968410419224E-3)*xsq
143            +2.2726554820815502876593E-1)*xsq
144            +2.0000000000000000000897E0);
145         double r = x * p;
146         r =  r / (q - r);
147         return r+r;
148       }
149   }
150#endif
151
152// Standard mathematical constants:
153//   M_E, M_LOG2E, M_LOG10E, M_LN2, M_LN10, M_PI, M_PI_2=pi/2, M_PI_4=pi/4,
154//   M_1_PI=1/pi, M_2_PI=2/pi, M_2_SQRTPI=2/sqrt(pi), SQRT2, SQRT1_2=sqrt(1/2)
155// OpenCL defines M_constant_F for float constants, and nothing if double
156// is not enabled on the card, which is why these constants may be missing
157#ifndef M_PI
158#  define M_PI 3.141592653589793
159#endif
160#ifndef M_PI_2
161#  define M_PI_2 1.570796326794897
162#endif
163#ifndef M_PI_4
164#  define M_PI_4 0.7853981633974483
165#endif
166#ifndef M_E
167#  define M_E 2.718281828459045091
168#endif
169#ifndef M_SQRT1_2
170#  define M_SQRT1_2 0.70710678118654746
171#endif
172
173// Non-standard function library
174// pi/180, used for converting between degrees and radians
175// 4/3 pi for computing sphere volumes
176// square and cube for computing squares and cubes
177#ifndef M_PI_180
178#  define M_PI_180 0.017453292519943295
179#endif
180#ifndef M_4PI_3
181#  define M_4PI_3 4.18879020478639
182#endif
183inline double square(double x) { return x*x; }
184inline double cube(double x) { return x*x*x; }
185inline double sas_sinx_x(double x) { return x==0 ? 1.0 : sin(x)/x; }
186
187// CRUFT: support old style models with orientation received qx, qy and angles
188
189// To rotate from the canonical position to theta, phi, psi, first rotate by
190// psi about the major axis, oriented along z, which is a rotation in the
191// detector plane xy. Next rotate by theta about the y axis, aligning the major
192// axis in the xz plane. Finally, rotate by phi in the detector plane xy.
193// To compute the scattering, undo these rotations in reverse order:
194//     rotate in xy by -phi, rotate in xz by -theta, rotate in xy by -psi
195// The returned q is the length of the q vector and (xhat, yhat, zhat) is a unit
196// vector in the q direction.
197// To change between counterclockwise and clockwise rotation, change the
198// sign of phi and psi.
199
200#if 1
201//think cos(theta) should be sin(theta) in new coords, RKH 11Jan2017
202#define ORIENT_SYMMETRIC(qx, qy, theta, phi, q, sn, cn) do { \
203    SINCOS(phi*M_PI_180, sn, cn); \
204    q = sqrt(qx*qx + qy*qy); \
205    cn  = (q==0. ? 1.0 : (cn*qx + sn*qy)/q * sin(theta*M_PI_180));  \
206    sn = sqrt(1 - cn*cn); \
207    } while (0)
208#else
209// SasView 3.x definition of orientation
210#define ORIENT_SYMMETRIC(qx, qy, theta, phi, q, sn, cn) do { \
211    SINCOS(theta*M_PI_180, sn, cn); \
212    q = sqrt(qx*qx + qy*qy);\
213    cn = (q==0. ? 1.0 : (cn*cos(phi*M_PI_180)*qx + sn*qy)/q); \
214    sn = sqrt(1 - cn*cn); \
215    } while (0)
216#endif
217
218#if 1
219#define ORIENT_ASYMMETRIC(qx, qy, theta, phi, psi, q, xhat, yhat, zhat) do { \
220    q = sqrt(qx*qx + qy*qy); \
221    const double qxhat = qx/q; \
222    const double qyhat = qy/q; \
223    double sin_theta, cos_theta; \
224    double sin_phi, cos_phi; \
225    double sin_psi, cos_psi; \
226    SINCOS(theta*M_PI_180, sin_theta, cos_theta); \
227    SINCOS(phi*M_PI_180, sin_phi, cos_phi); \
228    SINCOS(psi*M_PI_180, sin_psi, cos_psi); \
229    xhat = qxhat*(-sin_phi*sin_psi + cos_theta*cos_phi*cos_psi) \
230         + qyhat*( cos_phi*sin_psi + cos_theta*sin_phi*cos_psi); \
231    yhat = qxhat*(-sin_phi*cos_psi - cos_theta*cos_phi*sin_psi) \
232         + qyhat*( cos_phi*cos_psi - cos_theta*sin_phi*sin_psi); \
233    zhat = qxhat*(-sin_theta*cos_phi) \
234         + qyhat*(-sin_theta*sin_phi); \
235    } while (0)
236#else
237// SasView 3.x definition of orientation
238#define ORIENT_ASYMMETRIC(qx, qy, theta, phi, psi, q, cos_alpha, cos_mu, cos_nu) do { \
239    q = sqrt(qx*qx + qy*qy); \
240    const double qxhat = qx/q; \
241    const double qyhat = qy/q; \
242    double sin_theta, cos_theta; \
243    double sin_phi, cos_phi; \
244    double sin_psi, cos_psi; \
245    SINCOS(theta*M_PI_180, sin_theta, cos_theta); \
246    SINCOS(phi*M_PI_180, sin_phi, cos_phi); \
247    SINCOS(psi*M_PI_180, sin_psi, cos_psi); \
248    cos_alpha = cos_theta*cos_phi*qxhat + sin_theta*qyhat; \
249    cos_mu = (-sin_theta*cos_psi*cos_phi - sin_psi*sin_phi)*qxhat + cos_theta*cos_psi*qyhat; \
250    cos_nu = (-cos_phi*sin_psi*sin_theta + sin_phi*cos_psi)*qxhat + sin_psi*cos_theta*qyhat; \
251    } while (0)
252#endif
Note: See TracBrowser for help on using the repository browser.