PyQrackIsing 8.14.3__tar.gz → 8.14.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {pyqrackising-8.14.3/pyqrackising.egg-info → pyqrackising-8.14.5}/PKG-INFO +1 -1
  2. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/README.md +1 -3
  3. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyproject.toml +1 -1
  4. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/kernels.cl +105 -278
  5. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim.py +26 -21
  6. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim_sparse.py +33 -25
  7. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim_streaming.py +20 -9
  8. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim_util.py +16 -24
  9. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver.py +25 -48
  10. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver_sparse.py +27 -70
  11. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver_streaming.py +2 -1
  12. {pyqrackising-8.14.3 → pyqrackising-8.14.5/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/setup.py +1 -1
  14. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/LICENSE.md +0 -0
  15. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/MANIFEST.in +0 -0
  16. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/__init__.py +0 -0
  17. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  18. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/generate_tfim_samples.py +0 -0
  19. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver_util.py +0 -0
  20. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-8.14.3 → pyqrackising-8.14.5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.14.3
3
+ Version: 8.14.5
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -153,9 +153,7 @@ When `is_optimized=True`, the `spin_glass_solver(G)` is used as a final optimiza
153
153
 
154
154
  ## Environment Variables
155
155
 
156
- We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for when `is_alt_gpu_sampling=True`. The default value (when the variable is not set) is queried from the OpenCL device properties. You might see performance benefit from tuning this manually to several times your device's number of "compute units" (or tune it down to reduce private memory usage).
157
-
158
- Similarly, for `is_alt_gpu_sampling=True`, define the "top-`n`" count of highest-weight direct neighbors to retain during sampling with environment variable "`PYQRACKISING_GPU_TOP_N`". The default is `32`. Increasing this might increase solution quality, but it will also increase time-to-solution and private memory usage.
156
+ We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for OpenCL-based solvers. The default value (when the variable is not set) is queried from the OpenCL device properties. You might see performance benefit from tuning this manually to several times your device's number of "compute units" (or tune it down to reduce private memory usage).
159
157
 
160
158
  By default, PyQrackIsing expects all `numpy` floating-point array inputs to be 32-bit. If you'd like to use 64-bit, you can set environment variable `PYQRACKISING_FPPOW=6` (meaning, 2^6=64, for the "floating-point (precision) power"). The default is `5`, for 32-bit. 16-bit is stubbed out and compiles for OpenCL, but the bigger hurdle is that `numpy` on `x86_64` doesn't provide a 16-bit floating point implementation. (As author of Qrack, I could suggest to the `numpy` maintainers that open-source, IEEE-compliant software-based implementations exist for `x86_64` and other architectures, but I'm sure they're aware and likely waiting for in-compiler support.) If you're on an ARM-based architecture, there's a good chance 16-bit floating-point will work, if `numpy` uses the native hardware support.
161
159
 
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "8.14.3"
13
+ version = "8.14.5"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -1,21 +1,34 @@
1
- #if FP16
2
- #define fwrapper(f, a) (half)f((float)(a))
3
- #define fwrapper2(f, a, b) (half)f((float)(a), (float)(b))
4
- #else
5
- #define fwrapper(f, a) f(a)
6
- #define fwrapper2(f, a, b) f(a, b)
7
- #endif
8
-
9
- inline uint xorshift32(uint *state) {
10
- uint x = *state;
11
- x ^= x << 13;
12
- x ^= x >> 17;
13
- x ^= x << 5;
14
- *state = x;
15
- return x;
1
+ void reduce_energy_index(real1 energy, int i, __local real1* loc_energy, __local int* loc_index, __global real1* max_energy_ptr, __global int* max_index_ptr) {
2
+ const int lt_id = get_local_id(0);
3
+ const int lt_size = get_local_size(0);
4
+
5
+ loc_energy[lt_id] = energy;
6
+ loc_index[lt_id] = i;
7
+
8
+ // Reduce within workgroup
9
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
10
+ barrier(CLK_LOCAL_MEM_FENCE);
11
+ real1 hid_energy, lid_energy;
12
+ if (lt_id < offset) {
13
+ hid_energy = loc_energy[lt_id + offset];
14
+ lid_energy = loc_energy[lt_id];
15
+ if (hid_energy > lid_energy) {
16
+ loc_energy[lt_id] = hid_energy;
17
+ loc_index[lt_id] = loc_index[lt_id + offset];
18
+ }
19
+ }
20
+ }
21
+
22
+ barrier(CLK_LOCAL_MEM_FENCE);
23
+
24
+ // Write out per-group result
25
+ if (lt_id == 0) {
26
+ max_energy_ptr[get_group_id(0)] = loc_energy[0];
27
+ max_index_ptr[get_group_id(0)] = loc_index[0];
28
+ }
16
29
  }
17
30
 
18
- real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __constant int* indices, const int k, const int n, const bool is_spin_glass) {
31
+ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __global int* indices, const int k, const int n, const bool is_spin_glass) {
19
32
  real1 energy = ZERO_R1;
20
33
  const size_t n_st = (size_t)n;
21
34
  for (int u = 0; u < n; ++u) {
@@ -37,9 +50,9 @@ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __cons
37
50
  }
38
51
  }
39
52
  if (u_bit != v_bit) {
40
- energy -= val;
41
- } else if (is_spin_glass) {
42
53
  energy += val;
54
+ } else if (is_spin_glass) {
55
+ energy -= val;
43
56
  }
44
57
  }
45
58
  }
@@ -50,10 +63,10 @@ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __cons
50
63
  __kernel void bootstrap(
51
64
  __global const real1* G_m,
52
65
  __constant char* best_theta,
53
- __constant int* indices_array,
66
+ __global int* indices,
54
67
  __constant int* args, // args[0] = n, args[1] = k
55
- __global real1* min_energy_ptr, // output: per-group min energy
56
- __global int* min_index_ptr, // output: per-group best index (i)
68
+ __global real1* max_energy_ptr, // output: per-group min energy
69
+ __global int* max_index_ptr, // output: per-group best index (i)
57
70
  __local real1* loc_energy, // local memory buffer
58
71
  __local int* loc_index // local memory buffer
59
72
  ) {
@@ -61,55 +74,26 @@ __kernel void bootstrap(
61
74
  const int k = args[1];
62
75
  const int combo_count = args[2];
63
76
  const bool is_spin_glass = args[3];
64
- uint prng_seed = (uint)args[4];
65
- int i = get_global_id(0);
66
77
 
67
- // The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
68
- prng_seed ^= (uint)i;
78
+ int i = get_global_id(0);
79
+ const int max_i = get_global_size(0);
69
80
 
70
- real1 best_energy = INFINITY;
81
+ real1 best_energy = -INFINITY;
71
82
  int best_i = i;
72
83
 
73
- for (; i < combo_count; i += MAX_PROC_ELEM) {
84
+ for (; i < combo_count; i += max_i) {
74
85
  const int j = i * k;
75
- const real1 energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n, is_spin_glass);
76
- if (energy < best_energy) {
86
+ const real1 energy = bootstrap_worker(best_theta, G_m, indices + j, k, n, is_spin_glass);
87
+ if (energy > best_energy) {
77
88
  best_energy = energy;
78
89
  best_i = i;
79
- } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
80
- best_i = i;
81
- }
82
- }
83
-
84
- const int lt_id = get_local_id(0);
85
- const int lt_size = get_local_size(0);
86
-
87
- loc_energy[lt_id] = best_energy;
88
- loc_index[lt_id] = best_i;
89
-
90
- // Reduce within workgroup
91
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
92
- barrier(CLK_LOCAL_MEM_FENCE);
93
- if (lt_id < offset) {
94
- real1 hid_energy = loc_energy[lt_id + offset];
95
- real1 lid_energy = loc_energy[lt_id];
96
- if (hid_energy < lid_energy) {
97
- loc_energy[lt_id] = hid_energy;
98
- loc_index[lt_id] = loc_index[lt_id + offset];
99
- } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
100
- loc_index[lt_id] = loc_index[lt_id + offset];
101
- }
102
90
  }
103
91
  }
104
92
 
105
- // Write out per-group result
106
- if (lt_id == 0) {
107
- min_energy_ptr[get_group_id(0)] = loc_energy[0];
108
- min_index_ptr[get_group_id(0)] = loc_index[0];
109
- }
93
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
110
94
  }
111
95
 
112
- real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __constant int* indices, const int k, const int n, const bool is_spin_glass) {
96
+ real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __global int* indices, const int k, const int n, const bool is_spin_glass) {
113
97
  real1 energy = ZERO_R1;
114
98
  for (int u = 0; u < n; ++u) {
115
99
  bool u_bit = theta[u];
@@ -131,9 +115,9 @@ real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_da
131
115
  }
132
116
  }
133
117
  if (u_bit != v_bit) {
134
- energy -= val;
135
- } else if (is_spin_glass) {
136
118
  energy += val;
119
+ } else if (is_spin_glass) {
120
+ energy -= val;
137
121
  }
138
122
  }
139
123
  }
@@ -146,10 +130,10 @@ __kernel void bootstrap_sparse(
146
130
  __global const uint* G_rows,
147
131
  __global const uint* G_cols,
148
132
  __constant char* best_theta,
149
- __constant int* indices_array,
133
+ __global int* indices,
150
134
  __constant int* args, // args[0] = n, args[1] = k
151
- __global real1* min_energy_ptr, // output: per-group min energy
152
- __global int* min_index_ptr, // output: per-group best index (i)
135
+ __global real1* max_energy_ptr, // output: per-group min energy
136
+ __global int* max_index_ptr, // output: per-group best index (i)
153
137
  __local real1* loc_energy, // local memory buffer
154
138
  __local int* loc_index // local memory buffer
155
139
  ) {
@@ -157,51 +141,23 @@ __kernel void bootstrap_sparse(
157
141
  const int k = args[1];
158
142
  const int combo_count = args[2];
159
143
  const bool is_spin_glass = args[3];
160
- uint prng_seed = (uint)args[4];
161
- int i = get_global_id(0);
162
144
 
163
- prng_seed ^= (uint)i;
145
+ int i = get_global_id(0);
146
+ const int max_i = get_global_size(0);
164
147
 
165
- real1 best_energy = INFINITY;
148
+ real1 best_energy = -INFINITY;
166
149
  int best_i = i;
167
150
 
168
- for (; i < combo_count; i += MAX_PROC_ELEM) {
151
+ for (; i < combo_count; i += max_i) {
169
152
  const int j = i * k;
170
- const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, is_spin_glass);
171
- if (energy < best_energy) {
153
+ const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices + j, k, n, is_spin_glass);
154
+ if (energy > best_energy) {
172
155
  best_energy = energy;
173
156
  best_i = i;
174
- } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
175
- best_i = i;
176
- }
177
- }
178
-
179
- const int lt_id = get_local_id(0);
180
- const int lt_size = get_local_size(0);
181
-
182
- loc_energy[lt_id] = best_energy;
183
- loc_index[lt_id] = best_i;
184
-
185
- // Reduce within workgroup
186
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
187
- barrier(CLK_LOCAL_MEM_FENCE);
188
- if (lt_id < offset) {
189
- real1 hid_energy = loc_energy[lt_id + offset];
190
- real1 lid_energy = loc_energy[lt_id];
191
- if (hid_energy < lid_energy) {
192
- loc_energy[lt_id] = hid_energy;
193
- loc_index[lt_id] = loc_index[lt_id + offset];
194
- } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
195
- loc_index[lt_id] = loc_index[lt_id + offset];
196
- }
197
157
  }
198
158
  }
199
159
 
200
- // Write out per-group result
201
- if (lt_id == 0) {
202
- min_energy_ptr[get_group_id(0)] = loc_energy[0];
203
- min_index_ptr[get_group_id(0)] = loc_index[0];
204
- }
160
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
205
161
  }
206
162
 
207
163
  // Helper to read from segmented G_m
@@ -216,7 +172,7 @@ inline real1 get_G_m(
216
172
  real1 bootstrap_worker_segmented(
217
173
  __constant char* theta,
218
174
  __global const real1** G_m,
219
- __constant int* indices,
175
+ __global int* indices,
220
176
  const int k,
221
177
  const int n,
222
178
  const int segment_size,
@@ -247,9 +203,9 @@ real1 bootstrap_worker_segmented(
247
203
  }
248
204
 
249
205
  if (u_bit != v_bit) {
250
- energy -= val;
251
- } else if (is_spin_glass) {
252
206
  energy += val;
207
+ } else if (is_spin_glass) {
208
+ energy -= val;
253
209
  }
254
210
  }
255
211
  }
@@ -264,10 +220,10 @@ __kernel void bootstrap_segmented(
264
220
  __global const real1* G_m2,
265
221
  __global const real1* G_m3,
266
222
  __constant char* best_theta,
267
- __constant int* indices_array,
223
+ __global int* indices,
268
224
  __constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
269
- __global real1* min_energy_ptr,
270
- __global int* min_index_ptr,
225
+ __global real1* max_energy_ptr,
226
+ __global int* max_index_ptr,
271
227
  __local real1* loc_energy,
272
228
  __local int* loc_index
273
229
  ) {
@@ -277,50 +233,24 @@ __kernel void bootstrap_segmented(
277
233
  const int k = args[1];
278
234
  const int combo_count = args[2];
279
235
  const bool is_spin_glass = args[3];
280
- uint prng_seed = (uint)args[4];
281
- const int segment_size = args[5];
282
- int i = get_global_id(0);
236
+ const int segment_size = args[4];
283
237
 
284
- prng_seed ^= (uint)i;
238
+ int i = get_global_id(0);
239
+ const int max_i = get_global_size(0);
285
240
 
286
- real1 best_energy = INFINITY;
241
+ real1 best_energy = -INFINITY;
287
242
  int best_i = i;
288
243
 
289
- for (; i < combo_count; i += MAX_PROC_ELEM) {
244
+ for (; i < combo_count; i += max_i) {
290
245
  const int j = i * k;
291
- const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices_array + j, k, n, segment_size, is_spin_glass);
292
- if (energy < best_energy) {
246
+ const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices + j, k, n, segment_size, is_spin_glass);
247
+ if (energy > best_energy) {
293
248
  best_energy = energy;
294
249
  best_i = i;
295
- } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
296
- best_i = i;
297
250
  }
298
251
  }
299
252
 
300
- const int lt_id = get_local_id(0);
301
- const int lt_size = get_local_size(0);
302
-
303
- loc_energy[lt_id] = best_energy;
304
- loc_index[lt_id] = best_i;
305
-
306
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
307
- barrier(CLK_LOCAL_MEM_FENCE);
308
- if (lt_id < offset) {
309
- real1 hid_energy = loc_energy[lt_id + offset];
310
- real1 lid_energy = loc_energy[lt_id];
311
- if (hid_energy < lid_energy) {
312
- loc_energy[lt_id] = hid_energy;
313
- loc_index[lt_id] = loc_index[lt_id + offset];
314
- } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
315
- loc_index[lt_id] = loc_index[lt_id + offset];
316
- }
317
- }
318
- }
319
-
320
- if (lt_id == 0) {
321
- min_energy_ptr[get_group_id(0)] = loc_energy[0];
322
- min_index_ptr[get_group_id(0)] = loc_index[0];
323
- }
253
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
324
254
  }
325
255
 
326
256
  real1 bootstrap_worker_sparse_segmented(
@@ -328,7 +258,7 @@ real1 bootstrap_worker_sparse_segmented(
328
258
  __global const real1** G_data,
329
259
  __global const uint* G_rows,
330
260
  __global const uint* G_cols,
331
- __constant int* indices,
261
+ __global int* indices,
332
262
  const int k,
333
263
  const int n,
334
264
  const int segment_size,
@@ -361,9 +291,9 @@ real1 bootstrap_worker_sparse_segmented(
361
291
  }
362
292
 
363
293
  if (u_bit != v_bit) {
364
- energy -= val;
365
- } else if (is_spin_glass) {
366
294
  energy += val;
295
+ } else if (is_spin_glass) {
296
+ energy -= val;
367
297
  }
368
298
  }
369
299
  }
@@ -379,10 +309,10 @@ __kernel void bootstrap_sparse_segmented(
379
309
  __global const uint* G_rows,
380
310
  __global const uint* G_cols,
381
311
  __constant char* best_theta,
382
- __constant int* indices_array,
312
+ __global int* indices,
383
313
  __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
384
- __global real1* min_energy_ptr,
385
- __global int* min_index_ptr,
314
+ __global real1* max_energy_ptr,
315
+ __global int* max_index_ptr,
386
316
  __local real1* loc_energy,
387
317
  __local int* loc_index
388
318
  ) {
@@ -392,50 +322,24 @@ __kernel void bootstrap_sparse_segmented(
392
322
  const int k = args[1];
393
323
  const int combo_count = args[2];
394
324
  const bool is_spin_glass = args[3];
395
- uint prng_seed = (uint)args[4];
396
- const int segment_size = args[5];
397
- int i = get_global_id(0);
325
+ const int segment_size = args[4];
398
326
 
399
- prng_seed ^= (uint)i;
327
+ int i = get_global_id(0);
328
+ const int max_i = get_global_size(0);
400
329
 
401
- real1 best_energy = INFINITY;
330
+ real1 best_energy = -INFINITY;
402
331
  int best_i = i;
403
332
 
404
- for (; i < combo_count; i += MAX_PROC_ELEM) {
333
+ for (; i < combo_count; i += max_i) {
405
334
  const int j = i * k;
406
- const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, segment_size, is_spin_glass);
407
- if (energy < best_energy) {
335
+ const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices + j, k, n, segment_size, is_spin_glass);
336
+ if (energy > best_energy) {
408
337
  best_energy = energy;
409
338
  best_i = i;
410
- } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
411
- best_i = i;
412
- }
413
- }
414
-
415
- const int lt_id = get_local_id(0);
416
- const int lt_size = get_local_size(0);
417
-
418
- loc_energy[lt_id] = best_energy;
419
- loc_index[lt_id] = best_i;
420
-
421
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
422
- barrier(CLK_LOCAL_MEM_FENCE);
423
- if (lt_id < offset) {
424
- real1 hid_energy = loc_energy[lt_id + offset];
425
- real1 lid_energy = loc_energy[lt_id];
426
- if (hid_energy < lid_energy) {
427
- loc_energy[lt_id] = hid_energy;
428
- loc_index[lt_id] = loc_index[lt_id + offset];
429
- } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
430
- loc_index[lt_id] = loc_index[lt_id + offset];
431
- }
432
339
  }
433
340
  }
434
341
 
435
- if (lt_id == 0) {
436
- min_energy_ptr[get_group_id(0)] = loc_energy[0];
437
- min_index_ptr[get_group_id(0)] = loc_index[0];
438
- }
342
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
439
343
  }
440
344
 
441
345
  inline bool get_bit(__global const uint* theta, const size_t u) {
@@ -472,15 +376,17 @@ __kernel void calculate_cut(
472
376
  __local int* loc_index // local memory buffer
473
377
  ) {
474
378
  const int n = args[0];
475
- const int n32 = (n + 31) >> 5U;
476
379
  const int shots = args[1];
477
380
  const bool is_spin_glass = args[2];
381
+ const int n32 = (n + 31) >> 5U;
382
+
478
383
  int i = get_global_id(0);
384
+ const int max_i = get_global_size(0);
479
385
 
480
386
  real1 best_energy = -INFINITY;
481
387
  int best_i = i;
482
388
 
483
- for (; i < shots; i += MAX_PROC_ELEM) {
389
+ for (; i < shots; i += max_i) {
484
390
  const int j = i * n32;
485
391
  const real1 energy = cut_worker(theta + j, G_m, n, is_spin_glass);
486
392
  if (energy > best_energy) {
@@ -489,30 +395,7 @@ __kernel void calculate_cut(
489
395
  }
490
396
  }
491
397
 
492
- const int lt_id = get_local_id(0);
493
- const int lt_size = get_local_size(0);
494
-
495
- loc_energy[lt_id] = best_energy;
496
- loc_index[lt_id] = best_i;
497
-
498
- // Reduce within workgroup
499
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
500
- barrier(CLK_LOCAL_MEM_FENCE);
501
- if (lt_id < offset) {
502
- real1 hid_energy = loc_energy[lt_id + offset];
503
- real1 lid_energy = loc_energy[lt_id];
504
- if (hid_energy > lid_energy) {
505
- loc_energy[lt_id] = hid_energy;
506
- loc_index[lt_id] = loc_index[lt_id + offset];
507
- }
508
- }
509
- }
510
-
511
- // Write out per-group result
512
- if (lt_id == 0) {
513
- max_energy_ptr[get_group_id(0)] = loc_energy[0];
514
- max_index_ptr[get_group_id(0)] = loc_index[0];
515
- }
398
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
516
399
  }
517
400
 
518
401
  real1 cut_worker_sparse(__global const uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass) {
@@ -549,13 +432,16 @@ __kernel void calculate_cut_sparse(
549
432
  const int n = args[0];
550
433
  const int shots = args[1];
551
434
  const bool is_spin_glass = args[2];
435
+ const int n32 = (n + 31) >> 5U;
436
+
552
437
  int i = get_global_id(0);
438
+ const int max_i = get_global_size(0);
553
439
 
554
440
  real1 best_energy = -INFINITY;
555
441
  int best_i = i;
556
442
 
557
- for (; i < shots; i += MAX_PROC_ELEM) {
558
- const int j = (i * n + 31U) >> 5U;
443
+ for (; i < shots; i += max_i) {
444
+ const int j = i * n32;
559
445
  const real1 energy = cut_worker_sparse(theta + j, G_data, G_rows, G_cols, n, is_spin_glass);
560
446
  if (energy > best_energy) {
561
447
  best_energy = energy;
@@ -563,30 +449,7 @@ __kernel void calculate_cut_sparse(
563
449
  }
564
450
  }
565
451
 
566
- const int lt_id = get_local_id(0);
567
- const int lt_size = get_local_size(0);
568
-
569
- loc_energy[lt_id] = best_energy;
570
- loc_index[lt_id] = best_i;
571
-
572
- // Reduce within workgroup
573
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
574
- barrier(CLK_LOCAL_MEM_FENCE);
575
- if (lt_id < offset) {
576
- real1 hid_energy = loc_energy[lt_id + offset];
577
- real1 lid_energy = loc_energy[lt_id];
578
- if (hid_energy > lid_energy) {
579
- loc_energy[lt_id] = hid_energy;
580
- loc_index[lt_id] = loc_index[lt_id + offset];
581
- }
582
- }
583
- }
584
-
585
- // Write out per-group result
586
- if (lt_id == 0) {
587
- max_energy_ptr[get_group_id(0)] = loc_energy[0];
588
- max_index_ptr[get_group_id(0)] = loc_index[0];
589
- }
452
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
590
453
  }
591
454
 
592
455
  real1 cut_worker_segmented(
@@ -641,13 +504,16 @@ __kernel void calculate_cut_segmented(
641
504
  const bool is_spin_glass = args[2];
642
505
  const int segment_size = args[3];
643
506
  const int theta_segment_size = args[4];
507
+ const int n32 = (n + 31) >> 5U;
508
+
644
509
  int i = get_global_id(0);
510
+ const int max_i = get_global_size(0);
645
511
 
646
512
  real1 best_energy = -INFINITY;
647
513
  int best_i = i;
648
514
 
649
- for (; i < shots; i += MAX_PROC_ELEM) {
650
- const int j = (i * n + 31U) >> 5U;
515
+ for (; i < shots; i += max_i) {
516
+ const int j = i * n32;
651
517
  const real1 energy = cut_worker_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_m, n, segment_size, is_spin_glass);
652
518
  if (energy > best_energy) {
653
519
  best_energy = energy;
@@ -655,28 +521,7 @@ __kernel void calculate_cut_segmented(
655
521
  }
656
522
  }
657
523
 
658
- const int lt_id = get_local_id(0);
659
- const int lt_size = get_local_size(0);
660
-
661
- loc_energy[lt_id] = best_energy;
662
- loc_index[lt_id] = best_i;
663
-
664
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
665
- barrier(CLK_LOCAL_MEM_FENCE);
666
- if (lt_id < offset) {
667
- real1 hid_energy = loc_energy[lt_id + offset];
668
- real1 lid_energy = loc_energy[lt_id];
669
- if (hid_energy > lid_energy) {
670
- loc_energy[lt_id] = hid_energy;
671
- loc_index[lt_id] = loc_index[lt_id + offset];
672
- }
673
- }
674
- }
675
-
676
- if (lt_id == 0) {
677
- max_energy_ptr[get_group_id(0)] = loc_energy[0];
678
- max_index_ptr[get_group_id(0)] = loc_index[0];
679
- }
524
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
680
525
  }
681
526
 
682
527
  real1 cut_worker_sparse_segmented(
@@ -735,13 +580,16 @@ __kernel void calculate_cut_sparse_segmented(
735
580
  const bool is_spin_glass = args[2];
736
581
  const int segment_size = args[3];
737
582
  const int theta_segment_size = args[4];
583
+ const int n32 = (n + 31) >> 5U;
584
+
738
585
  int i = get_global_id(0);
586
+ const int max_i = get_global_size(0);
739
587
 
740
588
  real1 best_energy = -INFINITY;
741
589
  int best_i = i;
742
590
 
743
- for (; i < shots; i += MAX_PROC_ELEM) {
744
- const int j = (i * n + 31U) >> 5U;
591
+ for (; i < shots; i += max_i) {
592
+ const int j = i * n32;
745
593
  const real1 energy = cut_worker_sparse_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_data, G_rows, G_cols, n, segment_size, is_spin_glass);
746
594
  if (energy > best_energy) {
747
595
  best_energy = energy;
@@ -749,26 +597,5 @@ __kernel void calculate_cut_sparse_segmented(
749
597
  }
750
598
  }
751
599
 
752
- const int lt_id = get_local_id(0);
753
- const int lt_size = get_local_size(0);
754
-
755
- loc_energy[lt_id] = best_energy;
756
- loc_index[lt_id] = best_i;
757
-
758
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
759
- barrier(CLK_LOCAL_MEM_FENCE);
760
- if (lt_id < offset) {
761
- real1 hid_energy = loc_energy[lt_id + offset];
762
- real1 lid_energy = loc_energy[lt_id];
763
- if (hid_energy > lid_energy) {
764
- loc_energy[lt_id] = hid_energy;
765
- loc_index[lt_id] = loc_index[lt_id + offset];
766
- }
767
- }
768
- }
769
-
770
- if (lt_id == 0) {
771
- max_energy_ptr[get_group_id(0)] = loc_energy[0];
772
- max_index_ptr[get_group_id(0)] = loc_index[0];
773
- }
600
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
774
601
  }