PyQrackIsing 9.17.0__py3-none-any.whl

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.
@@ -0,0 +1,1238 @@
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
+ }
29
+ }
30
+
31
+ inline bool get_bit(__global const uint* theta, const size_t u) {
32
+ return (theta[u >> 5U] >> (u & 31U)) & 1U;
33
+ }
34
+
35
+ // Helper to read from segmented G_m
36
+ inline real1 get_G_m(
37
+ __global const real1** G_m,
38
+ size_t flat_idx,
39
+ int segment_size
40
+ ) {
41
+ return G_m[flat_idx / segment_size][flat_idx % segment_size];
42
+ }
43
+
44
+ real1 cut_worker(__global const uint* theta, __global const real1* G_m, const int n, const bool is_spin_glass) {
45
+ real1 energy = ZERO_R1;
46
+ const size_t n_st = (size_t)n;
47
+ for (int u = 0; u < n; ++u) {
48
+ const size_t u_offset = u * n_st;
49
+ const bool u_bit = get_bit(theta, u);
50
+ for (int v = u + 1; v < n; ++v) {
51
+ const real1 val = G_m[u_offset + v];
52
+ const bool v_bit = get_bit(theta, v);
53
+ if (u_bit != v_bit) {
54
+ energy += val;
55
+ } else if (is_spin_glass) {
56
+ energy -= val;
57
+ }
58
+ }
59
+ }
60
+
61
+ return energy;
62
+ }
63
+
64
+ __kernel void calculate_cut(
65
+ __global const real1* G_m,
66
+ __global uint* theta,
67
+ __constant int* args, // args[0] = n, args[1] = k
68
+ __global real1* max_energy_ptr, // output: per-group min energy
69
+ __global int* max_index_ptr, // output: per-group best index (i)
70
+ __local real1* loc_energy, // local memory buffer
71
+ __local int* loc_index // local memory buffer
72
+ ) {
73
+ const int n = args[0];
74
+ const int shots = args[1];
75
+ const bool is_spin_glass = args[2];
76
+ const int n32 = (n + 31) >> 5U;
77
+
78
+ int i = get_global_id(0);
79
+ const int max_i = get_global_size(0);
80
+
81
+ real1 best_energy = -INFINITY;
82
+ int best_i = i;
83
+
84
+ for (; i < shots; i += max_i) {
85
+ const int j = i * n32;
86
+ const real1 energy = cut_worker(theta + j, G_m, n, is_spin_glass);
87
+ if (energy > best_energy) {
88
+ best_energy = energy;
89
+ best_i = i;
90
+ }
91
+ }
92
+
93
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
94
+ }
95
+
96
+ inline bool get_const_bit(__constant uint* theta, const size_t u) {
97
+ return (theta[u >> 5U] >> (u & 31U)) & 1U;
98
+ }
99
+
100
+ real1 single_bit_flip_worker(__constant uint* theta, __global const real1* G_m, const int n, const int k) {
101
+ const size_t k_offset = k * (size_t)n;
102
+ const bool k_bit = !get_const_bit(theta, k);
103
+ real1 energy = ZERO_R1;
104
+ for (int v = 0; v < k; ++v) {
105
+ const bool v_bit = get_const_bit(theta, v);
106
+ real1 val = G_m[k_offset + v];
107
+ energy += (k_bit == v_bit) ? -val : val;
108
+ }
109
+ for (int v = k + 1; v < n; ++v) {
110
+ const bool v_bit = get_const_bit(theta, v);
111
+ real1 val = G_m[k_offset + v];
112
+ energy += (k_bit == v_bit) ? -val : val;
113
+ }
114
+
115
+ return energy;
116
+ }
117
+
118
+ __kernel void single_bit_flips(
119
+ __global const real1* G_m,
120
+ __constant uint* best_theta,
121
+ __constant int* args, // args[0] = n, args[1] = k
122
+ __global real1* max_energy_ptr, // output: per-group min energy
123
+ __global int* max_index_ptr, // output: per-group best index (i)
124
+ __local real1* loc_energy, // local memory buffer
125
+ __local int* loc_index // local memory buffer
126
+ ) {
127
+ const int n = args[0];
128
+ const bool is_spin_glass = args[1];
129
+
130
+ int i = get_global_id(0);
131
+ const int max_i = get_global_size(0);
132
+
133
+ real1 best_energy = -INFINITY;
134
+ int best_i = i;
135
+
136
+ for (; i < n; i += max_i) {
137
+ real1 energy = single_bit_flip_worker(best_theta, G_m, n, i);
138
+ if (is_spin_glass) {
139
+ energy *= 2.0;
140
+ }
141
+ if (energy > best_energy) {
142
+ best_energy = energy;
143
+ best_i = i;
144
+ }
145
+ }
146
+
147
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
148
+ }
149
+
150
+ real1 double_bit_flip_worker(__constant uint* theta, __global const real1* G_m, const int n, int k, int l) {
151
+ if (l < k) {
152
+ int t = k;
153
+ k = l;
154
+ l = t;
155
+ }
156
+ const size_t k_offset = k * (size_t)n;
157
+ const bool k_bit = !get_const_bit(theta, k);
158
+ const size_t l_offset = l * (size_t)n;
159
+ const bool l_bit = !get_const_bit(theta, l);
160
+ real1 energy = ZERO_R1;
161
+ for (int v = 0; v < k; ++v) {
162
+ const bool v_bit = get_const_bit(theta, v);
163
+ real1 val = G_m[k_offset + v];
164
+ energy += (k_bit == v_bit) ? -val : val;
165
+ val = G_m[l_offset + v];
166
+ energy += (l_bit == v_bit) ? -val : val;
167
+ }
168
+ for (int v = k + 1; v < l; ++v) {
169
+ const bool v_bit = get_const_bit(theta, v);
170
+ real1 val = G_m[k_offset + v];
171
+ energy += (k_bit == v_bit) ? -val : val;
172
+ val = G_m[l_offset + v];
173
+ energy += (l_bit == v_bit) ? -val : val;
174
+ }
175
+ for (int v = l + 1; v < n; ++v) {
176
+ const bool v_bit = get_const_bit(theta, v);
177
+ real1 val = G_m[k_offset + v];
178
+ energy += (k_bit == v_bit) ? -val : val;
179
+ val = G_m[l_offset + v];
180
+ energy += (l_bit == v_bit) ? -val : val;
181
+ }
182
+
183
+ return energy;
184
+ }
185
+
186
+ __kernel void double_bit_flips(
187
+ __global const real1* G_m,
188
+ __constant uint* best_theta,
189
+ __constant int* args, // args[0] = n, args[1] = k
190
+ __global real1* max_energy_ptr, // output: per-group min energy
191
+ __global int* max_index_ptr, // output: per-group best index (i)
192
+ __local real1* loc_energy, // local memory buffer
193
+ __local int* loc_index // local memory buffer
194
+ ) {
195
+ const int n = args[0];
196
+ const bool is_spin_glass = args[1];
197
+ const int combo_count = (n * (n - 1)) >> 1;
198
+
199
+ int i = get_global_id(0);
200
+ const int max_i = get_global_size(0);
201
+
202
+ real1 best_energy = -INFINITY;
203
+ int best_i = i;
204
+
205
+ for (; i < combo_count; i += max_i) {
206
+ int c = i;
207
+ int k = 0;
208
+ int lcv = n - 1;
209
+ while (c >= lcv) {
210
+ c -= lcv;
211
+ ++k;
212
+ --lcv;
213
+
214
+ if (!lcv) {
215
+ break;
216
+ }
217
+ }
218
+ const int l = c + k + 1;
219
+
220
+ real1 energy = double_bit_flip_worker(best_theta, G_m, n, k, l);
221
+ if (is_spin_glass) {
222
+ energy *= 2.0;
223
+ }
224
+ if (energy > best_energy) {
225
+ best_energy = energy;
226
+ best_i = i;
227
+ }
228
+ }
229
+
230
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
231
+ }
232
+
233
+ 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) {
234
+ real1 energy = ZERO_R1;
235
+ for (int u = 0; u < n; ++u) {
236
+ const bool u_bit = get_bit(theta, u);
237
+ const size_t mCol = G_rows[u + 1];
238
+ for (int col = G_rows[u]; col < mCol; ++col) {
239
+ const int v = G_cols[col];
240
+ const real1 val = G_data[col];
241
+ const bool v_bit = get_bit(theta, v);
242
+ if (u_bit != v_bit) {
243
+ energy += val;
244
+ } else if (is_spin_glass) {
245
+ energy -= val;
246
+ }
247
+ }
248
+ }
249
+
250
+ return energy;
251
+ }
252
+
253
+ __kernel void calculate_cut_sparse(
254
+ __global const real1* G_data,
255
+ __global const uint* G_rows,
256
+ __global const uint* G_cols,
257
+ __global const uint* theta,
258
+ __constant int* args, // args[0] = n, args[1] = k
259
+ __global real1* max_energy_ptr, // output: per-group min energy
260
+ __global int* max_index_ptr, // output: per-group best index (i)
261
+ __local real1* loc_energy, // local memory buffer
262
+ __local int* loc_index // local memory buffer
263
+ ) {
264
+ const int n = args[0];
265
+ const int shots = args[1];
266
+ const bool is_spin_glass = args[2];
267
+ const int n32 = (n + 31) >> 5U;
268
+
269
+ int i = get_global_id(0);
270
+ const int max_i = get_global_size(0);
271
+
272
+ real1 best_energy = -INFINITY;
273
+ int best_i = i;
274
+
275
+ for (; i < shots; i += max_i) {
276
+ const int j = i * n32;
277
+ const real1 energy = cut_worker_sparse(theta + j, G_data, G_rows, G_cols, n, is_spin_glass);
278
+ if (energy > best_energy) {
279
+ best_energy = energy;
280
+ best_i = i;
281
+ }
282
+ }
283
+
284
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
285
+ }
286
+
287
+ real1 single_bit_flip_worker_sparse(__constant 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, const int k) {
288
+ real1 energy = ZERO_R1;
289
+ for (int u = 0; u < n; ++u) {
290
+ bool u_bit = get_const_bit(theta, u);
291
+ if (u == k) {
292
+ u_bit = !u_bit;
293
+ }
294
+ const size_t mCol = G_rows[u + 1];
295
+ for (int col = G_rows[u]; col < mCol; ++col) {
296
+ const int v = G_cols[col];
297
+ const real1 val = G_data[col];
298
+ bool v_bit = get_const_bit(theta, v);
299
+ if (v == k) {
300
+ v_bit = !v_bit;
301
+ }
302
+ if (u_bit != v_bit) {
303
+ energy += val;
304
+ } else if (is_spin_glass) {
305
+ energy -= val;
306
+ }
307
+ }
308
+ }
309
+
310
+ return energy;
311
+ }
312
+
313
+ __kernel void single_bit_flips_sparse(
314
+ __global const real1* G_data,
315
+ __global const uint* G_rows,
316
+ __global const uint* G_cols,
317
+ __constant uint* best_theta,
318
+ __constant int* args, // args[0] = n, args[1] = k
319
+ __global real1* max_energy_ptr, // output: per-group min energy
320
+ __global int* max_index_ptr, // output: per-group best index (i)
321
+ __local real1* loc_energy, // local memory buffer
322
+ __local int* loc_index // local memory buffer
323
+ ) {
324
+ const int n = args[0];
325
+ const bool is_spin_glass = args[1];
326
+
327
+ int i = get_global_id(0);
328
+ const int max_i = get_global_size(0);
329
+
330
+ real1 best_energy = -INFINITY;
331
+ int best_i = i;
332
+
333
+ for (; i < n; i += max_i) {
334
+ const real1 energy = single_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, i);
335
+ if (energy > best_energy) {
336
+ best_energy = energy;
337
+ best_i = i;
338
+ }
339
+ }
340
+
341
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
342
+ }
343
+
344
+ real1 double_bit_flip_worker_sparse(__constant 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, const int k, const int l) {
345
+ real1 energy = ZERO_R1;
346
+ for (int u = 0; u < n; ++u) {
347
+ bool u_bit = get_const_bit(theta, u);
348
+ if ((u == k) || (u == l)) {
349
+ u_bit = !u_bit;
350
+ }
351
+ const size_t mCol = G_rows[u + 1];
352
+ for (int col = G_rows[u]; col < mCol; ++col) {
353
+ const int v = G_cols[col];
354
+ const real1 val = G_data[col];
355
+ bool v_bit = get_const_bit(theta, v);
356
+ if ((v == k) || (v == l)) {
357
+ v_bit = !v_bit;
358
+ }
359
+ if (u_bit != v_bit) {
360
+ energy += val;
361
+ } else if (is_spin_glass) {
362
+ energy -= val;
363
+ }
364
+ }
365
+ }
366
+
367
+ return energy;
368
+ }
369
+
370
+ __kernel void double_bit_flips_sparse(
371
+ __global const real1* G_data,
372
+ __global const uint* G_rows,
373
+ __global const uint* G_cols,
374
+ __constant uint* best_theta,
375
+ __constant int* args, // args[0] = n, args[1] = k
376
+ __global real1* max_energy_ptr, // output: per-group min energy
377
+ __global int* max_index_ptr, // output: per-group best index (i)
378
+ __local real1* loc_energy, // local memory buffer
379
+ __local int* loc_index // local memory buffer
380
+ ) {
381
+ const int n = args[0];
382
+ const bool is_spin_glass = args[1];
383
+ const int combo_count = (n * (n - 1)) >> 1;
384
+
385
+ int i = get_global_id(0);
386
+ const int max_i = get_global_size(0);
387
+
388
+ real1 best_energy = -INFINITY;
389
+ int best_i = i;
390
+
391
+ for (; i < combo_count; i += max_i) {
392
+ int c = i;
393
+ int k = 0;
394
+ int lcv = n - 1;
395
+ while (c >= lcv) {
396
+ c -= lcv;
397
+ ++k;
398
+ --lcv;
399
+
400
+ if (!lcv) {
401
+ break;
402
+ }
403
+ }
404
+ const int l = c + k + 1;
405
+
406
+ const real1 energy = double_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, k, l);
407
+ if (energy > best_energy) {
408
+ best_energy = energy;
409
+ best_i = i;
410
+ }
411
+ }
412
+
413
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
414
+ }
415
+
416
+ real1 cut_worker_segmented(
417
+ __global const uint* theta,
418
+ __global const real1** G_m,
419
+ const int n,
420
+ const int segment_size,
421
+ const bool is_spin_glass
422
+ ) {
423
+ real1 energy = ZERO_R1;
424
+ const size_t n_st = (size_t)n;
425
+
426
+ for (int u = 0; u < n; ++u) {
427
+ const size_t u_offset = u * n_st;
428
+ const bool u_bit = get_bit(theta, u);
429
+ for (int v = u + 1; v < n; ++v) {
430
+ const size_t flat_idx = u_offset + v;
431
+ const real1 val = get_G_m(G_m, flat_idx, segment_size);
432
+ const bool v_bit = get_bit(theta, v);
433
+ if (u_bit != v_bit) {
434
+ energy += val;
435
+ } else if (is_spin_glass) {
436
+ energy -= val;
437
+ }
438
+ }
439
+ }
440
+
441
+ return energy;
442
+ }
443
+
444
+
445
+ __kernel void calculate_cut_segmented(
446
+ __global const real1* G_m0,
447
+ __global const real1* G_m1,
448
+ __global const real1* G_m2,
449
+ __global const real1* G_m3,
450
+ __global const uint* theta0,
451
+ __global const uint* theta1,
452
+ __global const uint* theta2,
453
+ __global const uint* theta3,
454
+ __constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
455
+ __global real1* max_energy_ptr,
456
+ __global int* max_index_ptr,
457
+ __local real1* loc_energy,
458
+ __local int* loc_index
459
+ ) {
460
+ __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
461
+ __global const uint* theta[4] = { theta0, theta1, theta2, theta3 };
462
+
463
+ const int n = args[0];
464
+ const int shots = args[1];
465
+ const bool is_spin_glass = args[2];
466
+ const int segment_size = args[3];
467
+ const int theta_segment_size = args[4];
468
+ const int n32 = (n + 31) >> 5U;
469
+
470
+ int i = get_global_id(0);
471
+ const int max_i = get_global_size(0);
472
+
473
+ real1 best_energy = -INFINITY;
474
+ int best_i = i;
475
+
476
+ for (; i < shots; i += max_i) {
477
+ const int j = i * n32;
478
+ const real1 energy = cut_worker_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_m, n, segment_size, is_spin_glass);
479
+ if (energy > best_energy) {
480
+ best_energy = energy;
481
+ best_i = i;
482
+ }
483
+ }
484
+
485
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
486
+ }
487
+
488
+ real1 single_bit_flip_worker_segmented(__constant uint* theta, __global const real1** G_m, const int n, const int segment_size, const int k) {
489
+ const size_t k_offset = k * (size_t)n;
490
+ const bool k_bit = !get_const_bit(theta, k);
491
+ real1 energy = ZERO_R1;
492
+ for (int v = 0; v < k; ++v) {
493
+ const bool v_bit = get_const_bit(theta, v);
494
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
495
+ energy += (k_bit == v_bit) ? -val : val;
496
+ }
497
+ for (int v = k + 1; v < n; ++v) {
498
+ const bool v_bit = get_const_bit(theta, v);
499
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
500
+ energy += (k_bit == v_bit) ? -val : val;
501
+ }
502
+
503
+ return energy;
504
+ }
505
+
506
+ __kernel void single_bit_flips_segmented(
507
+ __global const real1* G_m0,
508
+ __global const real1* G_m1,
509
+ __global const real1* G_m2,
510
+ __global const real1* G_m3,
511
+ __constant uint* best_theta,
512
+ __constant int* args, // args[0] = n, args[1] = k
513
+ __global real1* max_energy_ptr, // output: per-group min energy
514
+ __global int* max_index_ptr, // output: per-group best index (i)
515
+ __local real1* loc_energy, // local memory buffer
516
+ __local int* loc_index // local memory buffer
517
+ ) {
518
+ __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
519
+
520
+ const int n = args[0];
521
+ const bool is_spin_glass = args[1];
522
+ const int segment_size = args[2];
523
+
524
+ int i = get_global_id(0);
525
+ const int max_i = get_global_size(0);
526
+
527
+ real1 best_energy = -INFINITY;
528
+ int best_i = i;
529
+
530
+ for (; i < n; i += max_i) {
531
+ real1 energy = single_bit_flip_worker_segmented(best_theta, G_m, n, segment_size, i);
532
+ if (is_spin_glass) {
533
+ energy *= 2.0;
534
+ }
535
+ if (energy > best_energy) {
536
+ best_energy = energy;
537
+ best_i = i;
538
+ }
539
+ }
540
+
541
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
542
+ }
543
+
544
+ real1 double_bit_flip_worker_segmented(__constant uint* theta, __global const real1** G_m, const int n, const int segment_size, int k, int l) {
545
+ if (l < k) {
546
+ int t = k;
547
+ k = l;
548
+ l = t;
549
+ }
550
+ const size_t k_offset = k * (size_t)n;
551
+ const bool k_bit = !get_const_bit(theta, k);
552
+ const size_t l_offset = l * (size_t)n;
553
+ const bool l_bit = !get_const_bit(theta, l);
554
+ real1 energy = ZERO_R1;
555
+ for (int v = 0; v < k; ++v) {
556
+ const bool v_bit = get_const_bit(theta, v);
557
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
558
+ energy += (k_bit == v_bit) ? -val : val;
559
+ val = get_G_m(G_m, l_offset + v, segment_size);
560
+ energy += (l_bit == v_bit) ? -val : val;
561
+ }
562
+ for (int v = k + 1; v < l; ++v) {
563
+ const bool v_bit = get_const_bit(theta, v);
564
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
565
+ energy += (k_bit == v_bit) ? -val : val;
566
+ val = get_G_m(G_m, l_offset + v, segment_size);
567
+ energy += (l_bit == v_bit) ? -val : val;
568
+ }
569
+ for (int v = l + 1; v < n; ++v) {
570
+ const bool v_bit = get_const_bit(theta, v);
571
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
572
+ energy += (k_bit == v_bit) ? -val : val;
573
+ val = get_G_m(G_m, l_offset + v, segment_size);
574
+ energy += (l_bit == v_bit) ? -val : val;
575
+ }
576
+
577
+ return energy;
578
+ }
579
+
580
+ __kernel void double_bit_flips_segmented(
581
+ __global const real1* G_m0,
582
+ __global const real1* G_m1,
583
+ __global const real1* G_m2,
584
+ __global const real1* G_m3,
585
+ __constant uint* best_theta,
586
+ __constant int* args, // args[0] = n, args[1] = k
587
+ __global real1* max_energy_ptr, // output: per-group min energy
588
+ __global int* max_index_ptr, // output: per-group best index (i)
589
+ __local real1* loc_energy, // local memory buffer
590
+ __local int* loc_index // local memory buffer
591
+ ) {
592
+ __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
593
+
594
+ const int n = args[0];
595
+ const bool is_spin_glass = args[1];
596
+ const int segment_size = args[2];
597
+ const int combo_count = (n * (n - 1)) >> 1;
598
+
599
+ int i = get_global_id(0);
600
+ const int max_i = get_global_size(0);
601
+
602
+ real1 best_energy = -INFINITY;
603
+ int best_i = i;
604
+
605
+ for (; i < combo_count; i += max_i) {
606
+ int c = i;
607
+ int k = 0;
608
+ int lcv = n - 1;
609
+ while (c >= lcv) {
610
+ c -= lcv;
611
+ ++k;
612
+ --lcv;
613
+
614
+ if (!lcv) {
615
+ break;
616
+ }
617
+ }
618
+ const int l = c + k + 1;
619
+
620
+ real1 energy = double_bit_flip_worker_segmented(best_theta, G_m, n, segment_size, k, l);
621
+ if (is_spin_glass) {
622
+ energy *= 2.0;
623
+ }
624
+ if (energy > best_energy) {
625
+ best_energy = energy;
626
+ best_i = i;
627
+ }
628
+ }
629
+
630
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
631
+ }
632
+
633
+ real1 cut_worker_sparse_segmented(
634
+ __global const uint* theta,
635
+ __global const real1** G_data,
636
+ __global const uint* G_rows,
637
+ __global const uint* G_cols,
638
+ const int n,
639
+ const int segment_size,
640
+ const bool is_spin_glass
641
+ ) {
642
+ real1 energy = ZERO_R1;
643
+
644
+ for (int u = 0; u < n; ++u) {
645
+ const bool u_bit = get_bit(theta, u);
646
+ const uint row_end = G_rows[u + 1];
647
+ for (uint col = G_rows[u]; col < row_end; ++col) {
648
+ const int v = G_cols[col];
649
+ const real1 val = get_G_m(G_data, col, segment_size);
650
+ const bool v_bit = get_bit(theta, v);
651
+ if (u_bit != v_bit) {
652
+ energy += val;
653
+ } else if (is_spin_glass) {
654
+ energy -= val;
655
+ }
656
+ }
657
+ }
658
+
659
+ return energy;
660
+ }
661
+
662
+ __kernel void calculate_cut_sparse_segmented(
663
+ __global const real1* G_data0,
664
+ __global const real1* G_data1,
665
+ __global const real1* G_data2,
666
+ __global const real1* G_data3,
667
+ __global const uint* G_rows,
668
+ __global const uint* G_cols,
669
+ __global const uint* theta0,
670
+ __global const uint* theta1,
671
+ __global const uint* theta2,
672
+ __global const uint* theta3,
673
+ __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
674
+ __global real1* max_energy_ptr,
675
+ __global int* max_index_ptr,
676
+ __local real1* loc_energy,
677
+ __local int* loc_index
678
+ ) {
679
+ __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
680
+ __global const uint* theta[4] = { theta0, theta1, theta2, theta3 };
681
+
682
+ const int n = args[0];
683
+ const int shots = args[1];
684
+ const bool is_spin_glass = args[2];
685
+ const int segment_size = args[3];
686
+ const int theta_segment_size = args[4];
687
+ const int n32 = (n + 31) >> 5U;
688
+
689
+ int i = get_global_id(0);
690
+ const int max_i = get_global_size(0);
691
+
692
+ real1 best_energy = -INFINITY;
693
+ int best_i = i;
694
+
695
+ for (; i < shots; i += max_i) {
696
+ const int j = i * n32;
697
+ 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);
698
+ if (energy > best_energy) {
699
+ best_energy = energy;
700
+ best_i = i;
701
+ }
702
+ }
703
+
704
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
705
+ }
706
+
707
+ real1 single_bit_flip_worker_sparse_segmented(
708
+ __constant uint* theta,
709
+ __global const real1** G_data,
710
+ __global const uint* G_rows,
711
+ __global const uint* G_cols,
712
+ const int n,
713
+ const int segment_size,
714
+ const bool is_spin_glass,
715
+ const int k
716
+ ) {
717
+ real1 energy = ZERO_R1;
718
+
719
+ for (int u = 0; u < n; ++u) {
720
+ bool u_bit = get_const_bit(theta, u);
721
+ if (u == k) {
722
+ u_bit = !u_bit;
723
+ }
724
+ const uint row_end = G_rows[u + 1];
725
+ for (uint col = G_rows[u]; col < row_end; ++col) {
726
+ const int v = G_cols[col];
727
+ const real1 val = get_G_m(G_data, col, segment_size);
728
+ bool v_bit = get_const_bit(theta, v);
729
+ if (v == k) {
730
+ v_bit = !v_bit;
731
+ }
732
+ if (u_bit != v_bit) {
733
+ energy += val;
734
+ } else if (is_spin_glass) {
735
+ energy -= val;
736
+ }
737
+ }
738
+ }
739
+
740
+ return energy;
741
+ }
742
+
743
+ __kernel void single_bit_flips_sparse_segmented(
744
+ __global const real1* G_data0,
745
+ __global const real1* G_data1,
746
+ __global const real1* G_data2,
747
+ __global const real1* G_data3,
748
+ __global const uint* G_rows,
749
+ __global const uint* G_cols,
750
+ __constant uint* best_theta,
751
+ __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
752
+ __global real1* max_energy_ptr,
753
+ __global int* max_index_ptr,
754
+ __local real1* loc_energy,
755
+ __local int* loc_index
756
+ ) {
757
+ __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
758
+
759
+ const int n = args[0];
760
+ const bool is_spin_glass = args[1];
761
+ const int segment_size = args[2];
762
+
763
+ int i = get_global_id(0);
764
+ const int max_i = get_global_size(0);
765
+
766
+ real1 best_energy = -INFINITY;
767
+ int best_i = i;
768
+
769
+ for (; i < n; i += max_i) {
770
+ const real1 energy = single_bit_flip_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, n, segment_size, is_spin_glass, i);
771
+ if (energy > best_energy) {
772
+ best_energy = energy;
773
+ best_i = i;
774
+ }
775
+ }
776
+
777
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
778
+ }
779
+
780
+ real1 double_bit_flip_worker_sparse_segmented(
781
+ __constant uint* theta,
782
+ __global const real1** G_data,
783
+ __global const uint* G_rows,
784
+ __global const uint* G_cols,
785
+ const int n,
786
+ const int segment_size,
787
+ const bool is_spin_glass,
788
+ const int k,
789
+ const int l
790
+ ) {
791
+ real1 energy = ZERO_R1;
792
+
793
+ for (int u = 0; u < n; ++u) {
794
+ bool u_bit = get_const_bit(theta, u);
795
+ if ((u == k) || (u == l)) {
796
+ u_bit = !u_bit;
797
+ }
798
+ const uint row_end = G_rows[u + 1];
799
+ for (uint col = G_rows[u]; col < row_end; ++col) {
800
+ const int v = G_cols[col];
801
+ const real1 val = get_G_m(G_data, col, segment_size);
802
+ bool v_bit = get_const_bit(theta, v);
803
+ if ((v == k) || (v == l)) {
804
+ v_bit = !v_bit;
805
+ }
806
+ if (u_bit != v_bit) {
807
+ energy += val;
808
+ } else if (is_spin_glass) {
809
+ energy -= val;
810
+ }
811
+ }
812
+ }
813
+
814
+ return energy;
815
+ }
816
+
817
+ __kernel void double_bit_flips_sparse_segmented(
818
+ __global const real1* G_data0,
819
+ __global const real1* G_data1,
820
+ __global const real1* G_data2,
821
+ __global const real1* G_data3,
822
+ __global const uint* G_rows,
823
+ __global const uint* G_cols,
824
+ __constant uint* best_theta,
825
+ __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
826
+ __global real1* max_energy_ptr,
827
+ __global int* max_index_ptr,
828
+ __local real1* loc_energy,
829
+ __local int* loc_index
830
+ ) {
831
+ __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
832
+
833
+ const int n = args[0];
834
+ const bool is_spin_glass = args[1];
835
+ const int segment_size = args[2];
836
+ const int combo_count = (n * (n - 1)) >> 1;
837
+
838
+ int i = get_global_id(0);
839
+ const int max_i = get_global_size(0);
840
+
841
+ real1 best_energy = -INFINITY;
842
+ int best_i = i;
843
+
844
+ for (; i < combo_count; i += max_i) {
845
+ int c = i;
846
+ int k = 0;
847
+ int lcv = n - 1;
848
+ while (c >= lcv) {
849
+ c -= lcv;
850
+ ++k;
851
+ --lcv;
852
+
853
+ if (!lcv) {
854
+ break;
855
+ }
856
+ }
857
+ const int l = c + k + 1;
858
+
859
+ const real1 energy = double_bit_flip_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, n, segment_size, is_spin_glass, k, l);
860
+ if (energy > best_energy) {
861
+ best_energy = energy;
862
+ best_i = i;
863
+ }
864
+ }
865
+
866
+ reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
867
+ }
868
+
869
+ inline bool get_local_bit(ulong* theta, const size_t u) {
870
+ return (theta[u >> 6U] >> (u & 63U)) & 1U;
871
+ }
872
+
873
+ inline bool get_const_long_bit(__constant ulong* theta, const size_t u) {
874
+ return (theta[u >> 6U] >> (u & 63U)) & 1U;
875
+ }
876
+
877
+ inline size_t gray_code_next(ulong* theta, const size_t curr_idx, const size_t offset) {
878
+ size_t prev = curr_idx;
879
+ size_t curr = curr_idx + 1U;
880
+ prev = prev ^ (prev >> 1U);
881
+ curr = curr ^ (curr >> 1U);
882
+ size_t diff = prev ^ curr;
883
+ size_t flip_bit = 0;
884
+ while (!((diff >> flip_bit) & 1U)) {
885
+ ++flip_bit;
886
+ }
887
+ flip_bit += offset;
888
+ theta[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
889
+
890
+ return flip_bit;
891
+ }
892
+
893
+
894
+ __kernel void gray(
895
+ __global const real1* G_m,
896
+ __constant ulong* theta,
897
+ __constant int* args,
898
+ __global ulong* theta_out,
899
+ __global real1* energy_out
900
+ ) {
901
+ const int n = args[0];
902
+ const bool is_spin_glass = args[1];
903
+ const int gray_iterations = args[2];
904
+ const int blocks = (n + 63) >> 6U;
905
+ const int last_block = blocks - 1;
906
+ const int rem = n - (blocks << 6U);
907
+
908
+ int i = get_global_id(0);
909
+ const int max_i = get_global_size(0);
910
+
911
+ ulong theta_local[2048];
912
+ for (int b = 0; b < blocks; ++b) {
913
+ theta_local[b] = theta[b];
914
+ }
915
+
916
+ // Initialize different seed per thread
917
+ const ulong seed = i ^ (i >> 1);
918
+ const int max_lcv = (n < 64) ? n : 64;
919
+ for (int b = 0; b < max_lcv; ++b) {
920
+ const bool bit = (seed >> (63U - b)) & 1U;
921
+ if (!bit) {
922
+ continue;
923
+ }
924
+ const int offset = n - (b + 1);
925
+ theta_local[offset >> 6U] ^= 1 << (offset & 63U);
926
+ }
927
+
928
+ real1 best_energy = ZERO_R1;
929
+ for (int u = 0; u < n; ++u) {
930
+ const size_t u_offset = u * n;
931
+ const bool o_u_bit = get_const_long_bit(theta, u);
932
+ const bool n_u_bit = get_local_bit(theta_local, u);
933
+ for (uint v = u + 1; v < n; ++v) {
934
+ const bool o_diff = o_u_bit != get_const_long_bit(theta, v);
935
+ const bool n_diff = n_u_bit != get_local_bit(theta_local, v);
936
+ if (o_diff != n_diff) {
937
+ const real1 val = G_m[u_offset + v];
938
+ best_energy += n_diff ? val : -val;
939
+ }
940
+ }
941
+ }
942
+
943
+ for (; i < gray_iterations; i += max_i) {
944
+ for (int block = 0; block < blocks; ++block) {
945
+ const size_t k = gray_code_next(theta_local, i, block << 6U);
946
+ const size_t k_offset = k * n;
947
+ const bool k_bit = get_local_bit(theta_local, k);
948
+
949
+ real1 energy = ZERO_R1;
950
+ for (uint v = 0; v < k; v++) {
951
+ const bool v_bit = get_local_bit(theta_local, v);
952
+ real1 val = G_m[k_offset + v];
953
+ energy += (k_bit == v_bit) ? -val : val;
954
+ }
955
+ for (uint v = k + 1; v < n; v++) {
956
+ const bool v_bit = get_local_bit(theta_local, v);
957
+ real1 val = G_m[k_offset + v];
958
+ energy += (k_bit == v_bit) ? -val : val;
959
+ }
960
+
961
+ if (energy > ZERO_R1) {
962
+ best_energy += energy;
963
+ } else {
964
+ theta_local[k >> 6U] ^= 1UL << (k & 63U);
965
+ }
966
+ }
967
+ }
968
+
969
+ i = get_global_id(0);
970
+ const size_t offset = i * blocks;
971
+ for (int b = 0; b < blocks; ++b) {
972
+ theta_out[offset + b] = theta_local[b];
973
+ }
974
+ if (!is_spin_glass) {
975
+ best_energy /= 2;
976
+ }
977
+ energy_out[i] = best_energy;
978
+ }
979
+
980
+ __kernel void gray_segmented(
981
+ __global const real1* G_m0,
982
+ __global const real1* G_m1,
983
+ __global const real1* G_m2,
984
+ __global const real1* G_m3,
985
+ __constant ulong* theta,
986
+ __constant int* args,
987
+ __global ulong* theta_out,
988
+ __global real1* energy_out
989
+ ) {
990
+ __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
991
+
992
+ const int n = args[0];
993
+ const bool is_spin_glass = args[1];
994
+ const int gray_iterations = args[2];
995
+ const int segment_size = args[3];
996
+ const int blocks = (n + 63) / 64;
997
+ const int last_block = blocks - 1;
998
+ const int rem = n - (blocks << 6U);
999
+
1000
+ int i = get_global_id(0);
1001
+ const int max_i = get_global_size(0);
1002
+
1003
+ ulong theta_local[2048];
1004
+ for (int b = 0; b < blocks; ++b) {
1005
+ theta_local[b] = theta[b];
1006
+ }
1007
+
1008
+ // Initialize different seed per thread
1009
+ const ulong seed = i ^ (i >> 1);
1010
+ const int max_lcv = (n < 64) ? n : 64;
1011
+ for (int b = 0; b < max_lcv; ++b) {
1012
+ const bool bit = (seed >> (63U - b)) & 1U;
1013
+ if (!bit) {
1014
+ continue;
1015
+ }
1016
+ const int offset = n - (b + 1);
1017
+ theta_local[offset >> 6U] ^= 1 << (offset & 63U);
1018
+ }
1019
+
1020
+ real1 best_energy = ZERO_R1;
1021
+ for (int u = 0; u < n; ++u) {
1022
+ const size_t u_offset = u * n;
1023
+ const bool o_u_bit = get_const_long_bit(theta, u);
1024
+ const bool n_u_bit = get_local_bit(theta_local, u);
1025
+ for (uint v = u + 1; v < n; ++v) {
1026
+ const bool o_diff = o_u_bit != get_const_long_bit(theta, v);
1027
+ const bool n_diff = n_u_bit != get_local_bit(theta_local, v);
1028
+ if (o_diff != n_diff) {
1029
+ const real1 val = get_G_m(G_m, u_offset + v, segment_size);
1030
+ best_energy += n_diff ? val : -val;
1031
+ }
1032
+ }
1033
+ }
1034
+
1035
+ for (; i < gray_iterations; i += max_i) {
1036
+ for (int block = 0; block < blocks; ++block) {
1037
+ const size_t k = gray_code_next(theta_local, i, block << 6U);
1038
+ const size_t k_offset = k * n;
1039
+ const bool k_bit = get_local_bit(theta_local, k);
1040
+
1041
+ real1 energy = ZERO_R1;
1042
+ for (uint v = 0; v < k; v++) {
1043
+ const bool v_bit = get_local_bit(theta_local, v);
1044
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
1045
+ energy += (k_bit == v_bit) ? -val : val;
1046
+ }
1047
+ for (uint v = k + 1; v < n; v++) {
1048
+ const bool v_bit = get_local_bit(theta_local, v);
1049
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
1050
+ energy += (k_bit == v_bit) ? -val : val;
1051
+ }
1052
+
1053
+ if (energy > ZERO_R1) {
1054
+ best_energy += energy;
1055
+ } else {
1056
+ theta_local[k >> 6U] ^= 1UL << (k & 63U);
1057
+ }
1058
+ }
1059
+ }
1060
+
1061
+ i = get_global_id(0);
1062
+ const size_t offset = i * blocks;
1063
+ for (int b = 0; b < blocks; ++b) {
1064
+ theta_out[offset + b] = theta_local[b];
1065
+ }
1066
+ if (!is_spin_glass) {
1067
+ best_energy /= 2;
1068
+ }
1069
+ energy_out[i] = best_energy;
1070
+ }
1071
+
1072
+ __kernel void gray_sparse(
1073
+ __global const real1* G_data,
1074
+ __global const uint* G_rows,
1075
+ __global const uint* G_cols,
1076
+ __constant ulong* theta,
1077
+ __constant int* args,
1078
+ __global ulong* theta_out,
1079
+ __global real1* energy_out
1080
+ ) {
1081
+ const int n = args[0];
1082
+ const bool is_spin_glass = args[1];
1083
+ const int gray_iterations = args[2];
1084
+ const int blocks = (n + 63) / 64;
1085
+ const int last_block = blocks - 1;
1086
+
1087
+ int i = get_global_id(0);
1088
+ const int max_i = get_global_size(0);
1089
+
1090
+ ulong theta_local[2048];
1091
+ for (int b = 0; b < blocks; ++b) {
1092
+ theta_local[b] = theta[b];
1093
+ }
1094
+
1095
+ // Initialize different seed per thread
1096
+ const ulong seed = i ^ (i >> 1);
1097
+ for (int b = 0; b < 64; ++b) {
1098
+ theta_local[last_block] ^= (seed >> (63U - b)) << b;
1099
+ }
1100
+
1101
+ real1 best_energy = ZERO_R1;
1102
+ for (uint u = 0; u < n; u++) {
1103
+ const bool u_bit = get_local_bit(theta_local, u);
1104
+ const size_t mCol = G_rows[u + 1];
1105
+ for (int col = G_rows[u]; col < mCol; ++col) {
1106
+ const int v = G_cols[col];
1107
+ const real1 val = G_data[col];
1108
+ const bool v_bit = get_local_bit(theta_local, v);
1109
+ if (u_bit != v_bit) {
1110
+ best_energy += val;
1111
+ } else if (is_spin_glass) {
1112
+ best_energy -= val;
1113
+ }
1114
+ }
1115
+ }
1116
+
1117
+ for (; i < gray_iterations; i += max_i) {
1118
+ for (int block = 0; block < blocks; ++block) {
1119
+ const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
1120
+ real1 energy = ZERO_R1;
1121
+ for (uint u = 0; u < n; u++) {
1122
+ const bool u_bit = get_local_bit(theta_local, u);
1123
+ const size_t mCol = G_rows[u + 1];
1124
+ for (int col = G_rows[u]; col < mCol; ++col) {
1125
+ const int v = G_cols[col];
1126
+ const real1 val = G_data[col];
1127
+ const bool v_bit = get_local_bit(theta_local, v);
1128
+ if (u_bit != v_bit) {
1129
+ energy += val;
1130
+ } else if (is_spin_glass) {
1131
+ energy -= val;
1132
+ }
1133
+ }
1134
+ }
1135
+
1136
+ if (energy > best_energy) {
1137
+ best_energy = energy;
1138
+ } else {
1139
+ theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
1140
+ }
1141
+ }
1142
+ }
1143
+
1144
+ i = get_global_id(0);
1145
+ const size_t offset = i * blocks;
1146
+ for (int b = 0; b < blocks; ++b) {
1147
+ theta_out[offset + b] = theta_local[b];
1148
+ }
1149
+ energy_out[i] = best_energy;
1150
+ }
1151
+
1152
+ __kernel void gray_sparse_segmented(
1153
+ __global const real1* G_data0,
1154
+ __global const real1* G_data1,
1155
+ __global const real1* G_data2,
1156
+ __global const real1* G_data3,
1157
+ __global const uint* G_rows,
1158
+ __global const uint* G_cols,
1159
+ __constant ulong* theta,
1160
+ __constant int* args,
1161
+ __global ulong* theta_out,
1162
+ __global real1* energy_out
1163
+ ) {
1164
+ __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
1165
+
1166
+ const int n = args[0];
1167
+ const bool is_spin_glass = args[1];
1168
+ const int gray_iterations = args[2];
1169
+ const int segment_size = args[3];
1170
+ const int blocks = (n + 63) / 64;
1171
+ const int last_block = blocks - 1;
1172
+
1173
+ int i = get_global_id(0);
1174
+ const int max_i = get_global_size(0);
1175
+
1176
+ ulong theta_local[2048];
1177
+ for (int b = 0; b < blocks; ++b) {
1178
+ theta_local[b] = theta[b];
1179
+ }
1180
+
1181
+ // Initialize different seed per thread
1182
+ const ulong seed = i ^ (i >> 1);
1183
+ for (int b = 0; b < 64; ++b) {
1184
+ theta_local[last_block] ^= (seed >> (63U - b)) << b;
1185
+ }
1186
+
1187
+ real1 best_energy = ZERO_R1;
1188
+ for (uint u = 0; u < n; u++) {
1189
+ const size_t u_offset = u * n;
1190
+ const bool u_bit = get_local_bit(theta_local, u);
1191
+ const uint row_end = G_rows[u + 1];
1192
+ for (uint col = G_rows[u]; col < row_end; ++col) {
1193
+ const int v = G_cols[col];
1194
+ const real1 val = get_G_m(G_data, col, segment_size);
1195
+ const bool v_bit = get_local_bit(theta_local, v);
1196
+ if (u_bit != v_bit) {
1197
+ best_energy += val;
1198
+ } else if (is_spin_glass) {
1199
+ best_energy -= val;
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+ for (; i < gray_iterations; i += max_i) {
1205
+ for (int block = 0; block < blocks; ++block) {
1206
+ const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
1207
+ real1 energy = ZERO_R1;
1208
+ for (uint u = 0; u < n; u++) {
1209
+ const size_t u_offset = u * n;
1210
+ const bool u_bit = get_local_bit(theta_local, u);
1211
+ const uint row_end = G_rows[u + 1];
1212
+ for (uint col = G_rows[u]; col < row_end; ++col) {
1213
+ const int v = G_cols[col];
1214
+ const real1 val = get_G_m(G_data, col, segment_size);
1215
+ const bool v_bit = get_local_bit(theta_local, v);
1216
+ if (u_bit != v_bit) {
1217
+ energy += val;
1218
+ } else if (is_spin_glass) {
1219
+ energy -= val;
1220
+ }
1221
+ }
1222
+ }
1223
+
1224
+ if (energy > best_energy) {
1225
+ best_energy = energy;
1226
+ } else {
1227
+ theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
1228
+ }
1229
+ }
1230
+ }
1231
+
1232
+ i = get_global_id(0);
1233
+ const size_t offset = i * blocks;
1234
+ for (int b = 0; b < blocks; ++b) {
1235
+ theta_out[offset + b] = theta_local[b];
1236
+ }
1237
+ energy_out[i] = best_energy;
1238
+ }