-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathSupport.h
213 lines (185 loc) · 4.79 KB
/
MathSupport.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef MATHSUPPORT_H
#define MATHSUPPORT_H
#ifdef _CRAYC
#undef __SSE2__
#endif
#include <iostream>
#include <stdio.h>
#ifdef __SSE2__
#include <emmintrin.h>
#endif
#include "MatrixSize.h"
#include "CompilerHints.h"
#ifdef USE_LAPACK
#include "blas.h"
#else
#include <cmath>
#endif
// Codeml random number generator (setting seed, returning random number)
static unsigned int z_rndu = 1237;
static int w_rndu = 1237;
inline void SetSeedCodeml(int seed, int PrintSeed) {
int i;
FILE *frand, *fseed;
if (sizeof(unsigned int) != 4)
std::cout << "oh-oh, we are in trouble. int not 32-bit?";
if (seed <= 0) {
frand = fopen("/dev/urandom", "r");
if (frand) {
for (i = 0, seed = 0; i < sizeof(unsigned int); i++)
seed += (seed << 8) + getc(frand);
seed = 2 * seed + 1;
fclose(frand);
} else {
seed = 1234567891 * (int)time(NULL) + 1;
}
//seed = abs(seed);
if (seed < 0) seed=-seed;
// if(PrintSeed) {
// fseed = fopen("SeedUsed", "w");
// if(fseed == NULL) std::cout << "can't open file SeedUsed.";
// fprintf(fseed, "%d\n", seed);
// fclose(fseed);
//}
}
z_rndu = (unsigned int)seed;
w_rndu = seed;
}
inline double rnduCodeml(void) {
// 32-bit integer assumed.
// From Ripley (1987) p. 46 or table 2.4 line 2.
// This may return 0 or 1, which can be a problem.
z_rndu = z_rndu * 69069 + 1;
if (z_rndu == 0 || z_rndu == 4294967295)
z_rndu = 13;
return z_rndu / 4294967295.0;
}
inline double rndu2Codeml(void) {
// 32-bit integer assumed.
// From Ripley (1987) table 2.4 line 4.
//w_rndu = abs(w_rndu * 16807) % 2147483647;
if (w_rndu >= 0)
w_rndu = (w_rndu * 16807) % 2147483647;
else
w_rndu = ((-w_rndu) * 16807) % 2147483647;
if (w_rndu == 0)
w_rndu = 13;
return w_rndu / 2147483647.0;
}
//#ifdef USE_MKL_VML
//#include <mkl_vml_functions.h>
//#endif
/// Dot product specialized for 61 elements vectors.
///
/// @param[in] aV1 First vector
/// @param[in] aV2 Second vector
///
/// @return The dot product
///
inline double dot(const double *RESTRICT aV1, const double *RESTRICT aV2) {
#if 0
double result;
__m128d num1, num2, num3, num4;
num4 = _mm_setzero_pd();
for(int i=0; i < N-1; i += 2)
{
num1 = _mm_load_pd(aV1+i);
num2 = _mm_load_pd(aV2+i);
num3 = _mm_mul_pd(num1, num2);
num4 = _mm_add_pd(num4, num3);
}
num4 = _mm_hadd_pd(num4, num4);
_mm_store_sd(&result, num4);
result += aV1[60]*aV2[60];
return result;
#endif
#ifdef USE_LAPACK
return ddot_(&N, aV1, &I1, aV2, &I1);
#else
double tot = 0.;
for (int i = 0; i < N; ++i)
tot += aV1[i] * aV2[i];
return tot;
#endif
}
/// Element-wise vector-vector multiplication (specialized to 61 elements
/// vectors)
///
/// @param[in,out] aVres Vector that should be multiplied by the aV one
/// @param[in] aV Multiplicand (that is: for(i=0; i < N; ++i) aVres[i] *= aV[i])
///
inline void elementWiseMult(double *RESTRICT aVres, const double *RESTRICT aV) {
//#ifdef USE_MKL_VML
// vdMul(N, aVres, aV, aVres);
//#elif defined(__SSE2__)
// __m128d num1, num2, num3;
//
// for(int i=0; i < N-1; )
// {
// num1 = _mm_load_pd(aVres+i);
// num2 = _mm_load_pd(aV+i);
// num3 = _mm_mul_pd(num1, num2);
// _mm_store_pd(aVres+i, num3);
// i += 2;
//
// num1 = _mm_load_pd(aVres+i);
// num2 = _mm_load_pd(aV+i);
// num3 = _mm_mul_pd(num1, num2);
// _mm_store_pd(aVres+i, num3);
// i += 2;
// }
// aVres[N-1] *= aV[N-1];
//#else
// Manual unrolling gives the best results here
for (int i = 0; i < 61; ++i)
aVres[i] *= aV[i];
#if 0
for(int i=0; i < 60; )
{
aVres[i] *= aV[i]; ++i;
aVres[i] *= aV[i]; ++i;
aVres[i] *= aV[i]; ++i;
aVres[i] *= aV[i]; ++i;
aVres[i] *= aV[i]; ++i;
aVres[i] *= aV[i]; ++i;
}
aVres[60] *= aV[60];
#endif
//#endif
}
/// Check if two values are sufficiently different
///
/// @param[in] aFirst First number to compare
/// @param[in] aSecond Second term to compare
///
/// @return True if the two parameters differs more than (hardcoded) TOL
///
inline bool isDifferent(double aFirst, double aSecond) {
static const double TOL = 1e-8;
const double diff = aFirst - aSecond;
return (diff > TOL || diff < -TOL);
}
#ifdef USE_CPV_SCALING
/// Normalize a vector (length 61).
///
/// @param[in,out] aVector The vector to be scaled
///
/// @return The length of the vector
///
inline double normalizeVector(double *RESTRICT aVector) {
#ifdef USE_LAPACK
double norm = dnrm2_(&N, aVector, &I1);
double inv_norm = 1. / norm;
dscal_(&N, &inv_norm, aVector, &I1);
#else
double norm = 0.;
for (int i = 0; i < N; ++i)
norm += aVector[i] * aVector[i];
norm = sqrt(norm);
for (int i = 0; i < N; ++i)
aVector[i] /= norm;
#endif
return norm;
}
#endif
#endif