-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvector.c
289 lines (242 loc) · 6.15 KB
/
nvector.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* nvector.c
*
* Copyright (C) 2024 L. Bertini
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "spinner.h"
#include "error.h"
#define SPNR_NVECTOR_D_MAX 8
#define SPNR_PI 3.1415926536
typedef float spin_t;
typedef struct
{
size_t n;
spin_t *spins;
}
nvector_priv_t;
static float
rand_gauss ()
{
static int has_prev = 0;
static float prev_rand;
float u, v, x, y;
if (has_prev)
{
has_prev = 0;
return prev_rand;
}
else
{
u = (float) rand () / RAND_MAX;
v = (float) rand () / RAND_MAX;
x = sqrt (-2. * log (u)) * sin (2 * SPNR_PI * v);
y = sqrt (-2. * log (u)) * cos (2 * SPNR_PI * v);
has_prev = 1;
prev_rand = x;
return y;
}
}
static void
spin_rand (spin_t * const u, const size_t n)
{
size_t i;
float rand_g, norm = 0;
for (i = 0; i < n; ++i)
{
rand_g = rand_gauss ();
u[i] = rand_g;
norm += rand_g * rand_g;
}
norm = sqrt(norm);
for (i = 0; i < n; ++i)
u[i] /= norm;
}
static float
spin_sprod (spin_t * const u, spin_t * const v, size_t const n_comps)
{
size_t i;
float prod;
prod = 0;
for (i = 0; i < n_comps; ++i)
prod += u[i] * v[i];
return prod;
}
static void
spin_sub (spin_t *s, spin_t * const u, spin_t * const v, size_t const n)
{
size_t i;
for (i = 0; i < n; ++i)
s[i] = u[i] - v[i];
}
static float
spin_mod (spin_t * const u, size_t const n)
{
size_t i;
float mag = 0;
for (i = 0; i < n; ++i)
mag += u[i] * u[i];
return sqrt(mag);
}
static void
spin_print (spin_t * const u, size_t const n)
{
size_t i;
for (i = 0; i < n; ++i)
printf ("%f ", u[i]);
}
static void
set_up (void * const priv, size_t const N)
{
nvector_priv_t * const priv_ = (nvector_priv_t *)priv;
spin_t *spins = priv_->spins;
size_t i, j, n = priv_->n;
for (i = 0; i < N; ++i)
{
spins[i * n] = +1.0;
for (j = 1; j < n; ++j)
spins[i * n + j] = 0.0;
}
}
static void
set_rand (void * const priv, size_t const N)
{
nvector_priv_t * const priv_ = (nvector_priv_t *)priv;
spin_t *spins = priv_->spins;
size_t i, j, n = priv_->n;
for (i = 0; i < N; ++i)
spin_rand (spins + i * n, n);
}
static void *
priv_alloc (size_t const N, size_t const n)
{
if (n <= 0)
spnr_err (SPNR_ERROR_PARAM_OOB, "number of components must be positive");
nvector_priv_t * const priv = malloc (sizeof (nvector_priv_t));
priv->n = n;
priv->spins = malloc (N * n * sizeof (spin_t));
set_up (priv, N);
return priv;
}
static void
priv_free (void *priv)
{
nvector_priv_t * const priv_ = (nvector_priv_t *)priv;
free (priv_->spins);
free (priv);
}
static size_t
spin_size (void *priv)
{
nvector_priv_t *priv_ = (nvector_priv_t *)priv;
return sizeof (priv_->n * sizeof(spin_t));
}
static void
fill_prop (void const * const priv, void * const prop, size_t const k)
{
nvector_priv_t const * const priv_ = (nvector_priv_t*) priv;
spin_t * const prop_ = (spin_t*) prop;
spin_rand (prop, priv_->n);
}
static void
accept_prop (void * const priv, void const * const prop, size_t k)
{
nvector_priv_t * const priv_ = (nvector_priv_t*) priv;
spin_t const * const prop_ = (spin_t*) prop;
size_t const n = priv_->n;
spin_t * const spin_k = priv_->spins + k * n;
memcpy (spin_k, prop_, n * sizeof (spin_t));
}
static float
calc_delta_h_binary (void const * const priv,
size_t const n_sites,
float const * const J,
size_t const * const sites,
void const * const prop,
size_t const k)
{
nvector_priv_t const * const priv_ = (nvector_priv_t*) priv;
spin_t const * const spins = priv_->spins;
size_t const n = priv_->n;
spin_t const * const prop_ = (spin_t*) prop;
spin_t const * const spin_k = spins + k * n;
size_t i, j;
float h;
spin_t sum[SPNR_NVECTOR_D_MAX];
memset (sum, 0, sizeof (sum));
for (i = 0; i < n_sites; ++i)
for (j = 0; j < n; ++j)
sum[j] += J[i] * spins[sites[i] * n + j];
h = 0;
for (j = 0; j < n; ++j)
h += (spin_k[j] - prop_[j]) * sum[j];
return h;
}
static float
calc_part_h_binary (void const * const priv, size_t const n_sites,
float const * const J, size_t const * const sites,
size_t const k)
{
nvector_priv_t const * const priv_ = (nvector_priv_t*) priv;
spin_t *spins = priv_->spins;
size_t i, j, n = priv_->n;
spin_t *spin_k = spins + k * n;
float h;
spin_t sum[SPNR_NVECTOR_D_MAX];
memset (sum, 0, sizeof (sum));
for (i = 0; i < n_sites; ++i)
for (j = 0; j < n; ++j)
sum[j] += J[i] * spins[sites[i] * n + j];
h = 0;
for (j = 0; j < n; ++j)
h += spin_k[j] * sum[j];
return -h;
}
static float
calc_phi (void const * const priv, size_t const N)
{
nvector_priv_t const * const priv_ = (nvector_priv_t*) priv;
spin_t *spins = priv_->spins;
size_t i, j, n = priv_->n;
float m;
spin_t sum[SPNR_NVECTOR_D_MAX];
memset (sum, 0, sizeof (sum));
for (i = 0; i < N; ++i)
for (j = 0; j < n; ++j)
sum[j] += spins[i * n + j];
m = 0;
for (j = 0; j < n; ++j)
m += sum[j] * sum[j];
return sqrt(m) / (float) N;
}
static const spnr_sys_kind_t nvector_kind =
{
"nvector",
&priv_alloc,
&priv_free,
&spin_size,
&set_up,
&set_rand,
&fill_prop,
&accept_prop,
&calc_delta_h_binary,
&calc_part_h_binary,
&calc_phi
};
const spnr_sys_kind_t *spnr_nvector = &nvector_kind;