-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtestKernel.c
192 lines (148 loc) · 6.11 KB
/
testKernel.c
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
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (C) 2016 Matthieu Schaller ([email protected])
* James Willis ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include <config.h>
/* Local includes. */
#include "align.h"
#include "kernel_hydro.h"
#include "vector.h"
/* System includes. */
#include <fenv.h>
#include <stdlib.h>
#include <strings.h>
const int numPoints = (1 << 28);
int main(int argc, char *argv[]) {
/* Initialize CPU frequency, this also starts time. */
unsigned long long cpufreq = 0;
clocks_set_cpufreq(cpufreq);
/* Choke on FPEs */
#ifdef HAVE_FE_ENABLE_EXCEPT
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif
const float h = 1.2348f;
float *u, *W, *dW;
if (posix_memalign((void **)&u, SWIFT_CACHE_ALIGNMENT,
numPoints * sizeof(float)) != 0)
error("Error allocating u");
if (posix_memalign((void **)&W, SWIFT_CACHE_ALIGNMENT,
numPoints * sizeof(float)) != 0)
error("Error allocating W");
if (posix_memalign((void **)&dW, SWIFT_CACHE_ALIGNMENT,
numPoints * sizeof(float)) != 0)
error("Error allocating dW");
message("Serial Output");
message("-------------");
const float numPoints_inv = 1. / numPoints;
for (int i = 0; i < numPoints; ++i)
u[i] = i * 1.2f * kernel_gamma * numPoints_inv / h;
for (int i = 0; i < numPoints; ++i) {
kernel_deval(u[i], &W[i], &dW[i]);
if (W[i] < 0.f) error("Kernel is negative u=%e W=%e", u[i], W[i]);
if (dW[i] > 0.f)
error("Kernel derivatibe is positive u=%e dW=%e", u[i], dW[i]);
}
/* Test some additional special cases */
float Wtest, dWtest;
kernel_deval(1.930290, &Wtest, &dWtest);
if (Wtest < 0.f) error("Kernel is negative u=%e W=%e", 1.930290, Wtest);
if (dWtest > 0.f)
error("Kernel derivative is positive u=%e dW=%e", 1.930290, dWtest);
#ifdef WITH_VECTORIZATION
message("Vector Output for VEC_SIZE=%d", VEC_SIZE);
message("-------------");
message("Vector Output for kernel_deval_1_vec");
message("-------------");
/* Test vectorised kernel that uses one vector. */
for (int i = 0; i < numPoints; i += VEC_SIZE) {
vector vx, vx_h;
vector W_vec, dW_vec;
for (int j = 0; j < VEC_SIZE; j++)
vx.f[j] = (i + j) * 1.2f * kernel_gamma / numPoints;
vx_h.v = vec_mul(vx.v, vec_set1(1.f / h));
kernel_deval_1_vec(&vx_h, &W_vec, &dW_vec);
for (int j = 0; j < VEC_SIZE; j++) {
/* message("%2d: h= %f H= %f x=%f W(x,h)=%f dW(x,h)=%f\n", i + j, h, */
/* h * kernel_gamma, vx.f[j], W_vec.f[j], dW_vec.f[j]); */
if (W_vec.f[j] < 0.f)
error("Kernel is negative u=%e W=%e", u[i + j], W_vec.f[j]);
if (dW_vec.f[j] > 0.f)
error("Kernel derivative is positive u=%e dW=%e", u[i + j],
dW_vec.f[j]);
if (fabsf(W_vec.f[j] - W[i + j]) > 2e-6)
error("Invalid Wvalue ! scalar= %e, vector= %e\n", W[i + j],
W_vec.f[j]);
if (fabsf(dW_vec.f[j] - dW[i + j]) > 2e-6)
error("Invalid dW value ! scalar= %e, vector= %e %e %e\n", dW[i + j],
dW_vec.f[j], fabsf(dW_vec.f[j] - dW[i + j]), fabsf(dW[i + j]));
}
}
message("Vector Output for kernel_deval_2_vec");
message("-------------");
/* Test vectorised kernel that uses two vectors. */
for (int i = 0; i < numPoints; i += VEC_SIZE) {
vector vx, vx_h;
vector W_vec, dW_vec;
vector vx_2, vx_h_2;
vector W_vec_2, dW_vec_2;
for (int j = 0; j < VEC_SIZE; j++) {
vx.f[j] = (i + j) * 1.2f * kernel_gamma / numPoints;
vx_2.f[j] = (i + j) * 1.2f * kernel_gamma / numPoints;
}
vx_h.v = vec_mul(vx.v, vec_set1(1.f / h));
vx_h_2.v = vec_mul(vx_2.v, vec_set1(1.f / h));
kernel_deval_2_vec(&vx_h, &W_vec, &dW_vec, &vx_h_2, &W_vec_2, &dW_vec_2);
/* Check first vector results. */
for (int j = 0; j < VEC_SIZE; j++) {
/* message("%2d: h= %f H= %f x=%f W(x,h)=%f dW(x,h)=%f\n", i + j, h, */
/* h * kernel_gamma, vx.f[j], W_vec.f[j], dW_vec.f[j]); */
if (W_vec.f[j] < 0.f)
error("Kernel is negative u=%e W=%e", u[i + j], W_vec.f[j]);
if (dW_vec.f[j] > 0.f)
error("Kernel derivative is positive u=%e dW=%e", u[i + j],
dW_vec.f[j]);
if (fabsf(W_vec.f[j] - W[i + j]) > 2e-6)
error("Invalid value ! scalar= %e, vector= %e\n", W[i + j], W_vec.f[j]);
if (fabsf(dW_vec.f[j] - dW[i + j]) > 2e-6)
error("Invalid value ! scalar= %e, vector= %e\n", dW[i + j],
dW_vec.f[j]);
}
/* Check second vector results. */
for (int j = 0; j < VEC_SIZE; j++) {
/* message("%2d: h= %f H= %f x=%f W(x,h)=%f dW(x,h)=%f\n", i + j, h, */
/* h * kernel_gamma, vx_2.f[j], W_vec_2.f[j], dW_vec_2.f[j]); */
if (W_vec_2.f[j] < 0.f)
error("Kernel is negative u=%e W=%e", u[i + j], W_vec_2.f[j]);
if (dW_vec_2.f[j] > 0.f)
error("Kernel derivative is positive u=%e dW=%e", u[i + j],
dW_vec_2.f[j]);
if (fabsf(W_vec_2.f[j] - W[i + j]) > 2e-6)
error("Invalid value ! scalar= %e, vector= %e\n", W[i + j],
W_vec_2.f[j]);
if (fabsf(dW_vec_2.f[j] - dW[i + j]) > 2e-6)
error("Invalid value ! scalar= %e, vector= %e\n", dW[i + j],
dW_vec_2.f[j]);
}
}
message("All values are consistent");
#endif
free(u);
free(W);
free(dW);
return 0;
}