PyQrackIsing 6.2.4__tar.gz → 6.3.1__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.
- {pyqrackising-6.2.4/pyqrackising.egg-info → pyqrackising-6.3.1}/PKG-INFO +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyproject.toml +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/kernels.cl +264 -16
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/maxcut_tfim.py +8 -4
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/maxcut_tfim_sparse.py +8 -4
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/maxcut_tfim_streaming.py +6 -6
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/maxcut_tfim_util.py +12 -2
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/spin_glass_solver.py +67 -27
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/spin_glass_solver_sparse.py +71 -29
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/spin_glass_solver_streaming.py +4 -2
- {pyqrackising-6.2.4 → pyqrackising-6.3.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/setup.py +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/CMakeLists.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/LICENSE.md +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/MANIFEST.in +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/README.md +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.1}/setup.cfg +0 -0
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "6.
|
|
15
|
+
version = "6.3.1"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -179,25 +179,30 @@ __kernel void bootstrap(
|
|
|
179
179
|
const int n = args[0];
|
|
180
180
|
const int k = args[1];
|
|
181
181
|
const int combo_count = args[2];
|
|
182
|
-
|
|
182
|
+
int i = get_global_id(0);
|
|
183
183
|
|
|
184
184
|
// The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
|
|
185
185
|
prng_seed ^= (uint)i;
|
|
186
186
|
|
|
187
|
-
real1
|
|
187
|
+
real1 best_energy = INFINITY;
|
|
188
|
+
int best_i = i;
|
|
188
189
|
|
|
189
|
-
|
|
190
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
190
191
|
const int j = i * k;
|
|
191
|
-
|
|
192
|
-
|
|
192
|
+
const real1 energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
|
|
193
|
+
if (energy < best_energy) {
|
|
194
|
+
best_energy = energy;
|
|
195
|
+
best_i = i;
|
|
196
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
197
|
+
best_i = i;
|
|
198
|
+
}
|
|
193
199
|
}
|
|
194
200
|
|
|
195
201
|
const int lt_id = get_local_id(0);
|
|
196
202
|
const int lt_size = get_local_size(0);
|
|
197
203
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
loc_index[lt_id] = i;
|
|
204
|
+
loc_energy[lt_id] = best_energy;
|
|
205
|
+
loc_index[lt_id] = best_i;
|
|
201
206
|
|
|
202
207
|
// Reduce within workgroup
|
|
203
208
|
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
@@ -265,24 +270,29 @@ __kernel void bootstrap_sparse(
|
|
|
265
270
|
const int n = args[0];
|
|
266
271
|
const int k = args[1];
|
|
267
272
|
const int combo_count = args[2];
|
|
268
|
-
|
|
273
|
+
int i = get_global_id(0);
|
|
269
274
|
|
|
270
275
|
prng_seed ^= (uint)i;
|
|
271
276
|
|
|
272
|
-
real1
|
|
277
|
+
real1 best_energy = INFINITY;
|
|
278
|
+
int best_i = i;
|
|
273
279
|
|
|
274
|
-
|
|
280
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
275
281
|
const int j = i * k;
|
|
276
|
-
|
|
277
|
-
|
|
282
|
+
const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
|
|
283
|
+
if (energy < best_energy) {
|
|
284
|
+
best_energy = energy;
|
|
285
|
+
best_i = i;
|
|
286
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
287
|
+
best_i = i;
|
|
288
|
+
}
|
|
278
289
|
}
|
|
279
290
|
|
|
280
291
|
const int lt_id = get_local_id(0);
|
|
281
292
|
const int lt_size = get_local_size(0);
|
|
282
293
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
loc_index[lt_id] = i;
|
|
294
|
+
loc_energy[lt_id] = best_energy;
|
|
295
|
+
loc_index[lt_id] = best_i;
|
|
286
296
|
|
|
287
297
|
// Reduce within workgroup
|
|
288
298
|
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
@@ -306,6 +316,244 @@ __kernel void bootstrap_sparse(
|
|
|
306
316
|
}
|
|
307
317
|
}
|
|
308
318
|
|
|
319
|
+
// Helper to read from segmented G_m
|
|
320
|
+
inline real1 get_G_m(
|
|
321
|
+
__global const real1* G_m0,
|
|
322
|
+
__global const real1* G_m1,
|
|
323
|
+
__global const real1* G_m2,
|
|
324
|
+
__global const real1* G_m3,
|
|
325
|
+
size_t flat_idx,
|
|
326
|
+
int segment_size
|
|
327
|
+
) {
|
|
328
|
+
if (flat_idx < segment_size)
|
|
329
|
+
return G_m0[flat_idx];
|
|
330
|
+
flat_idx -= segment_size;
|
|
331
|
+
|
|
332
|
+
if (flat_idx < segment_size)
|
|
333
|
+
return G_m1[flat_idx];
|
|
334
|
+
flat_idx -= segment_size;
|
|
335
|
+
|
|
336
|
+
if (flat_idx < segment_size)
|
|
337
|
+
return G_m2[flat_idx];
|
|
338
|
+
flat_idx -= segment_size;
|
|
339
|
+
|
|
340
|
+
return G_m3[flat_idx];
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
real1 bootstrap_worker_segmented(
|
|
344
|
+
__constant char* theta,
|
|
345
|
+
__global const real1* G_m0,
|
|
346
|
+
__global const real1* G_m1,
|
|
347
|
+
__global const real1* G_m2,
|
|
348
|
+
__global const real1* G_m3,
|
|
349
|
+
__constant int* indices,
|
|
350
|
+
const int k,
|
|
351
|
+
const int n,
|
|
352
|
+
const int segment_size
|
|
353
|
+
) {
|
|
354
|
+
real1 energy = ZERO_R1;
|
|
355
|
+
const size_t n_st = (size_t)n;
|
|
356
|
+
|
|
357
|
+
for (int u = 0; u < n; ++u) {
|
|
358
|
+
const size_t u_offset = u * n_st;
|
|
359
|
+
bool u_bit = theta[u];
|
|
360
|
+
for (int x = 0; x < k; ++x) {
|
|
361
|
+
if (indices[x] == u) {
|
|
362
|
+
u_bit = !u_bit;
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
for (int v = u + 1; v < n; ++v) {
|
|
367
|
+
size_t flat_idx = u_offset + v;
|
|
368
|
+
real1 val = get_G_m(G_m0, G_m1, G_m2, G_m3, flat_idx, segment_size);
|
|
369
|
+
|
|
370
|
+
bool v_bit = theta[v];
|
|
371
|
+
for (int x = 0; x < k; ++x) {
|
|
372
|
+
if (indices[x] == v) {
|
|
373
|
+
v_bit = !v_bit;
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return energy;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
__kernel void bootstrap_segmented(
|
|
386
|
+
uint prng_seed,
|
|
387
|
+
__global const real1* G_m0,
|
|
388
|
+
__global const real1* G_m1,
|
|
389
|
+
__global const real1* G_m2,
|
|
390
|
+
__global const real1* G_m3,
|
|
391
|
+
__constant char* best_theta,
|
|
392
|
+
__constant int* indices_array,
|
|
393
|
+
__constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
|
|
394
|
+
__global real1* min_energy_ptr,
|
|
395
|
+
__global int* min_index_ptr,
|
|
396
|
+
__local real1* loc_energy,
|
|
397
|
+
__local int* loc_index
|
|
398
|
+
) {
|
|
399
|
+
const int n = args[0];
|
|
400
|
+
const int k = args[1];
|
|
401
|
+
const int combo_count = args[2];
|
|
402
|
+
const int segment_size = args[3];
|
|
403
|
+
int i = get_global_id(0);
|
|
404
|
+
|
|
405
|
+
prng_seed ^= (uint)i;
|
|
406
|
+
|
|
407
|
+
real1 best_energy = INFINITY;
|
|
408
|
+
int best_i = i;
|
|
409
|
+
|
|
410
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
411
|
+
const int j = i * k;
|
|
412
|
+
const real1 energy = bootstrap_worker_segmented(best_theta, G_m0, G_m1, G_m2, G_m3, indices_array + j, k, n, segment_size);
|
|
413
|
+
if (energy < best_energy) {
|
|
414
|
+
best_energy = energy;
|
|
415
|
+
best_i = i;
|
|
416
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
417
|
+
best_i = i;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const int lt_id = get_local_id(0);
|
|
422
|
+
const int lt_size = get_local_size(0);
|
|
423
|
+
|
|
424
|
+
loc_energy[lt_id] = best_energy;
|
|
425
|
+
loc_index[lt_id] = best_i;
|
|
426
|
+
|
|
427
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
428
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
429
|
+
if (lt_id < offset) {
|
|
430
|
+
real1 hid_energy = loc_energy[lt_id + offset];
|
|
431
|
+
real1 lid_energy = loc_energy[lt_id];
|
|
432
|
+
if (hid_energy < lid_energy) {
|
|
433
|
+
loc_energy[lt_id] = hid_energy;
|
|
434
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
435
|
+
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
436
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (lt_id == 0) {
|
|
442
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
443
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
real1 bootstrap_worker_sparse_segmented(
|
|
448
|
+
__constant char* theta,
|
|
449
|
+
__global const real1* G_data0,
|
|
450
|
+
__global const real1* G_data1,
|
|
451
|
+
__global const real1* G_data2,
|
|
452
|
+
__global const real1* G_data3,
|
|
453
|
+
__global const uint* G_rows,
|
|
454
|
+
__global const uint* G_cols,
|
|
455
|
+
__constant int* indices,
|
|
456
|
+
const int k,
|
|
457
|
+
const int n,
|
|
458
|
+
const int segment_size
|
|
459
|
+
) {
|
|
460
|
+
real1 energy = ZERO_R1;
|
|
461
|
+
|
|
462
|
+
for (int u = 0; u < n; ++u) {
|
|
463
|
+
bool u_bit = theta[u];
|
|
464
|
+
for (int x = 0; x < k; ++x) {
|
|
465
|
+
if (indices[x] == u) {
|
|
466
|
+
u_bit = !u_bit;
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const uint row_start = G_rows[u];
|
|
472
|
+
const uint row_end = G_rows[u + 1];
|
|
473
|
+
|
|
474
|
+
for (uint col = row_start; col < row_end; ++col) {
|
|
475
|
+
const int v = G_cols[col];
|
|
476
|
+
real1 val = get_G_m(G_data0, G_data1, G_data2, G_data3, col, segment_size);
|
|
477
|
+
|
|
478
|
+
bool v_bit = theta[v];
|
|
479
|
+
for (int x = 0; x < k; ++x) {
|
|
480
|
+
if (indices[x] == v) {
|
|
481
|
+
v_bit = !v_bit;
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return energy;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
__kernel void bootstrap_sparse_segmented(
|
|
494
|
+
uint prng_seed,
|
|
495
|
+
__global const real1* G_data0,
|
|
496
|
+
__global const real1* G_data1,
|
|
497
|
+
__global const real1* G_data2,
|
|
498
|
+
__global const real1* G_data3,
|
|
499
|
+
__global const uint* G_rows,
|
|
500
|
+
__global const uint* G_cols,
|
|
501
|
+
__constant char* best_theta,
|
|
502
|
+
__constant int* indices_array,
|
|
503
|
+
__constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
|
|
504
|
+
__global real1* min_energy_ptr,
|
|
505
|
+
__global int* min_index_ptr,
|
|
506
|
+
__local real1* loc_energy,
|
|
507
|
+
__local int* loc_index
|
|
508
|
+
) {
|
|
509
|
+
const int n = args[0];
|
|
510
|
+
const int k = args[1];
|
|
511
|
+
const int combo_count = args[2];
|
|
512
|
+
const int segment_size = args[3];
|
|
513
|
+
int i = get_global_id(0);
|
|
514
|
+
|
|
515
|
+
prng_seed ^= (uint)i;
|
|
516
|
+
|
|
517
|
+
real1 best_energy = INFINITY;
|
|
518
|
+
int best_i = i;
|
|
519
|
+
|
|
520
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
521
|
+
const int j = i * k;
|
|
522
|
+
const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data0, G_data1, G_data2, G_data3, G_rows, G_cols, indices_array + j, k, n, segment_size);
|
|
523
|
+
if (energy < best_energy) {
|
|
524
|
+
best_energy = energy;
|
|
525
|
+
best_i = i;
|
|
526
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
527
|
+
best_i = i;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const int lt_id = get_local_id(0);
|
|
532
|
+
const int lt_size = get_local_size(0);
|
|
533
|
+
|
|
534
|
+
loc_energy[lt_id] = best_energy;
|
|
535
|
+
loc_index[lt_id] = best_i;
|
|
536
|
+
|
|
537
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
538
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
539
|
+
if (lt_id < offset) {
|
|
540
|
+
real1 hid_energy = loc_energy[lt_id + offset];
|
|
541
|
+
real1 lid_energy = loc_energy[lt_id];
|
|
542
|
+
if (hid_energy < lid_energy) {
|
|
543
|
+
loc_energy[lt_id] = hid_energy;
|
|
544
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
545
|
+
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
546
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (lt_id == 0) {
|
|
552
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
553
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
309
557
|
/// ------ Alternative random sampling rejection kernel by Elara (the OpenAI custom GPT) is below ------ ///
|
|
310
558
|
|
|
311
559
|
inline real1 rand_uniform(uint *state) {
|
|
@@ -141,10 +141,9 @@ def run_sampling_opencl(G_m_np, thresholds_np, shots, n, is_g_buf_reused):
|
|
|
141
141
|
dtype = opencl_context.dtype
|
|
142
142
|
wgs = opencl_context.work_group_size
|
|
143
143
|
|
|
144
|
-
|
|
145
|
-
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM +
|
|
146
|
-
global_size = min(((shots +
|
|
147
|
-
local_size = max_local_size
|
|
144
|
+
local_size = min(wgs, shots) # tune
|
|
145
|
+
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
146
|
+
global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
|
|
148
147
|
num_groups = global_size // local_size
|
|
149
148
|
|
|
150
149
|
# Bit-packing params
|
|
@@ -276,6 +275,11 @@ def maxcut_tfim(
|
|
|
276
275
|
|
|
277
276
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
278
277
|
|
|
278
|
+
is_segmented = (G_m.nbytes << 2) > (3 * opencl_context.max_alloc)
|
|
279
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
280
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
281
|
+
is_alt_gpu_sampling = False
|
|
282
|
+
|
|
279
283
|
if quality is None:
|
|
280
284
|
quality = 3
|
|
281
285
|
|
|
@@ -188,10 +188,9 @@ def run_sampling_opencl(G_m_csr, thresholds_np, shots, n, is_g_buf_reused):
|
|
|
188
188
|
dtype = opencl_context.dtype
|
|
189
189
|
wgs = opencl_context.work_group_size
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM +
|
|
193
|
-
global_size = min(((shots +
|
|
194
|
-
local_size = max_local_size
|
|
191
|
+
local_size = min(wgs, shots) # tune
|
|
192
|
+
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
193
|
+
global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
|
|
195
194
|
num_groups = global_size // local_size
|
|
196
195
|
|
|
197
196
|
# Bit-packing params
|
|
@@ -341,6 +340,11 @@ def maxcut_tfim_sparse(
|
|
|
341
340
|
|
|
342
341
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
343
342
|
|
|
343
|
+
is_segmented = (G_m.data.nbytes << 2) > (3 * opencl_context.max_alloc)
|
|
344
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
345
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
346
|
+
is_alt_gpu_sampling = False
|
|
347
|
+
|
|
344
348
|
if quality is None:
|
|
345
349
|
quality = 3
|
|
346
350
|
|
|
@@ -81,7 +81,8 @@ def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
|
81
81
|
energy = 0
|
|
82
82
|
for u in range(n_qubits):
|
|
83
83
|
for v in range(u + 1, n_qubits):
|
|
84
|
-
|
|
84
|
+
val = G_func((nodes[u], nodes[v]), G_func_args_tuple)
|
|
85
|
+
energy += val if sample[u] == sample[v] else -val
|
|
85
86
|
|
|
86
87
|
return energy
|
|
87
88
|
|
|
@@ -130,7 +131,7 @@ def init_J_and_z(G_func, G_func_args_tuple, nodes, dtype):
|
|
|
130
131
|
val = G_func((nodes[n], nodes[m]), G_func_args_tuple)
|
|
131
132
|
if val != 0.0:
|
|
132
133
|
degree += 1
|
|
133
|
-
|
|
134
|
+
J += val
|
|
134
135
|
G_max = max(val, G_max)
|
|
135
136
|
J = -J / degree if degree > 0 else 0
|
|
136
137
|
degrees[n] = degree
|
|
@@ -180,6 +181,7 @@ def maxcut_tfim_streaming(
|
|
|
180
181
|
is_base_maxcut_gpu=True
|
|
181
182
|
):
|
|
182
183
|
dtype = opencl_context.dtype
|
|
184
|
+
wgs = opencl_context.work_group_size
|
|
183
185
|
n_qubits = len(nodes)
|
|
184
186
|
|
|
185
187
|
if n_qubits < 3:
|
|
@@ -228,7 +230,7 @@ def maxcut_tfim_streaming(
|
|
|
228
230
|
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 4))
|
|
229
231
|
|
|
230
232
|
# Warp size is 32:
|
|
231
|
-
group_size = min(
|
|
233
|
+
group_size = min(wgs, n_qubits)
|
|
232
234
|
global_size = ((n_qubits + group_size - 1) // group_size) * group_size
|
|
233
235
|
|
|
234
236
|
opencl_context.init_theta_kernel(
|
|
@@ -239,9 +241,7 @@ def maxcut_tfim_streaming(
|
|
|
239
241
|
hamming_prob = init_thresholds(n_qubits, dtype)
|
|
240
242
|
|
|
241
243
|
# Warp size is 32:
|
|
242
|
-
group_size = n_qubits - 1
|
|
243
|
-
if group_size > 256:
|
|
244
|
-
group_size = 256
|
|
244
|
+
group_size = min(wgs, n_qubits - 1)
|
|
245
245
|
grid_dim = n_steps * n_qubits * group_size
|
|
246
246
|
|
|
247
247
|
# Move to GPU
|
|
@@ -146,17 +146,20 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits, dtype):
|
|
|
146
146
|
|
|
147
147
|
|
|
148
148
|
class OpenCLContext:
|
|
149
|
-
def __init__(self, p, a, w, d, c, q, i, m, b, s, k, l):
|
|
149
|
+
def __init__(self, p, a, w, d, r, c, q, i, m, b, s, x, y, k, l):
|
|
150
150
|
self.MAX_GPU_PROC_ELEM = p
|
|
151
151
|
self.IS_OPENCL_AVAILABLE = a
|
|
152
152
|
self.work_group_size = w
|
|
153
153
|
self.dtype = d
|
|
154
|
+
self.max_alloc = r
|
|
154
155
|
self.ctx = c
|
|
155
156
|
self.queue = q
|
|
156
157
|
self.init_theta_kernel = i
|
|
157
158
|
self.maxcut_hamming_cdf_kernel = m
|
|
158
159
|
self.bootstrap_kernel = b
|
|
159
160
|
self.bootstrap_sparse_kernel = s
|
|
161
|
+
self.bootstrap_segmented_kernel = x
|
|
162
|
+
self.bootstrap_sparse_segmented_kernel = y
|
|
160
163
|
self.sample_for_solution_best_bitset_kernel = k
|
|
161
164
|
self.sample_for_solution_best_bitset_sparse_kernel = l
|
|
162
165
|
self.G_m_buf = None
|
|
@@ -170,10 +173,13 @@ queue = None
|
|
|
170
173
|
compute_units = None
|
|
171
174
|
dtype = np.float32
|
|
172
175
|
work_group_size = 32
|
|
176
|
+
max_alloc = 0xFFFFFFFFFFFFFFFF
|
|
173
177
|
init_theta_kernel = None
|
|
174
178
|
maxcut_hamming_cdf_kernel = None
|
|
175
179
|
bootstrap_kernel = None
|
|
176
180
|
bootstrap_sparse_kernel = None
|
|
181
|
+
bootstrap_segmented_kernel = None
|
|
182
|
+
bootstrap_sparse_segmented_kernel = None
|
|
177
183
|
sample_for_solution_best_bitset_kernel = None
|
|
178
184
|
sample_for_solution_best_bitset_sparse_kernel = None
|
|
179
185
|
|
|
@@ -227,6 +233,8 @@ try:
|
|
|
227
233
|
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
228
234
|
bootstrap_kernel = program.bootstrap
|
|
229
235
|
bootstrap_sparse_kernel = program.bootstrap_sparse
|
|
236
|
+
bootstrap_segmented_kernel = program.bootstrap_segmented
|
|
237
|
+
bootstrap_sparse_segmented_kernel = program.bootstrap_sparse_segmented
|
|
230
238
|
sample_for_solution_best_bitset_kernel = program.sample_for_solution_best_bitset
|
|
231
239
|
sample_for_solution_best_bitset_sparse_kernel = program.sample_for_solution_best_bitset_sparse
|
|
232
240
|
|
|
@@ -234,8 +242,10 @@ try:
|
|
|
234
242
|
cl.kernel_work_group_info.PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
|
|
235
243
|
ctx.devices[0]
|
|
236
244
|
)
|
|
245
|
+
|
|
246
|
+
max_alloc = ctx.devices[0].get_info(cl.device_info.MAX_MEM_ALLOC_SIZE)
|
|
237
247
|
except ImportError:
|
|
238
248
|
IS_OPENCL_AVAILABLE = False
|
|
239
249
|
print("PyOpenCL not installed. (If you have any OpenCL accelerator devices with available ICDs, you might want to optionally install pyopencl.)")
|
|
240
250
|
|
|
241
|
-
opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, work_group_size, dtype, ctx, queue, init_theta_kernel, maxcut_hamming_cdf_kernel, bootstrap_kernel, bootstrap_sparse_kernel, sample_for_solution_best_bitset_kernel, sample_for_solution_best_bitset_sparse_kernel)
|
|
251
|
+
opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, work_group_size, dtype, max_alloc, ctx, queue, init_theta_kernel, maxcut_hamming_cdf_kernel, bootstrap_kernel, bootstrap_sparse_kernel, bootstrap_segmented_kernel, bootstrap_sparse_segmented_kernel, sample_for_solution_best_bitset_kernel, sample_for_solution_best_bitset_sparse_kernel)
|
|
@@ -67,10 +67,10 @@ def bootstrap(best_theta, G_m, indices_array, k, min_energy, dtype):
|
|
|
67
67
|
return min_energy
|
|
68
68
|
|
|
69
69
|
|
|
70
|
-
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
70
|
+
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, is_segmented, segment_size):
|
|
71
71
|
ctx = opencl_context.ctx
|
|
72
72
|
queue = opencl_context.queue
|
|
73
|
-
bootstrap_kernel = opencl_context.bootstrap_kernel
|
|
73
|
+
bootstrap_kernel = opencl_context.bootstrap_segmented_kernel if is_segmented else opencl_context.bootstrap_kernel
|
|
74
74
|
dtype = opencl_context.dtype
|
|
75
75
|
wgs = opencl_context.work_group_size
|
|
76
76
|
|
|
@@ -79,8 +79,8 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
79
79
|
|
|
80
80
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
81
81
|
|
|
82
|
-
# Args: [n, k, combo_count]
|
|
83
|
-
args_np = np.array([n, k, combo_count], dtype=np.int32)
|
|
82
|
+
# Args: [n, k, combo_count, segment_size]
|
|
83
|
+
args_np = np.array([n, k, combo_count, segment_size], dtype=np.int32)
|
|
84
84
|
|
|
85
85
|
# Buffers
|
|
86
86
|
mf = cl.mem_flags
|
|
@@ -88,31 +88,48 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
88
88
|
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
89
89
|
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
90
90
|
|
|
91
|
-
# Allocate min_energy and min_index result buffers per workgroup
|
|
92
|
-
min_energy_host = np.empty(combo_count, dtype=dtype)
|
|
93
|
-
min_index_host = np.empty(combo_count, dtype=np.int32)
|
|
94
|
-
|
|
95
|
-
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
96
|
-
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
97
|
-
|
|
98
91
|
# Local memory allocation (1 float per work item)
|
|
99
92
|
local_size = min(wgs, n)
|
|
100
|
-
|
|
93
|
+
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
94
|
+
global_size = min(((combo_count + local_size - 1) // local_size) * local_size, max_global_size)
|
|
101
95
|
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
102
96
|
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
103
97
|
|
|
98
|
+
# Allocate min_energy and min_index result buffers per workgroup
|
|
99
|
+
min_energy_host = np.empty(global_size, dtype=dtype)
|
|
100
|
+
min_index_host = np.empty(global_size, dtype=np.int32)
|
|
101
|
+
|
|
102
|
+
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
103
|
+
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
104
|
+
|
|
104
105
|
# Set kernel args
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
if is_segmented:
|
|
107
|
+
bootstrap_kernel.set_args(
|
|
108
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
109
|
+
G_m_buf[0],
|
|
110
|
+
G_m_buf[1],
|
|
111
|
+
G_m_buf[2],
|
|
112
|
+
G_m_buf[3],
|
|
113
|
+
best_theta_buf,
|
|
114
|
+
indices_buf,
|
|
115
|
+
args_buf,
|
|
116
|
+
min_energy_buf,
|
|
117
|
+
min_index_buf,
|
|
118
|
+
local_energy_buf,
|
|
119
|
+
local_index_buf
|
|
120
|
+
)
|
|
121
|
+
else:
|
|
122
|
+
bootstrap_kernel.set_args(
|
|
123
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
124
|
+
G_m_buf,
|
|
125
|
+
best_theta_buf,
|
|
126
|
+
indices_buf,
|
|
127
|
+
args_buf,
|
|
128
|
+
min_energy_buf,
|
|
129
|
+
min_index_buf,
|
|
130
|
+
local_energy_buf,
|
|
131
|
+
local_index_buf
|
|
132
|
+
)
|
|
116
133
|
|
|
117
134
|
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
118
135
|
|
|
@@ -126,7 +143,9 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
126
143
|
if min_energy < energy:
|
|
127
144
|
return min_energy
|
|
128
145
|
|
|
129
|
-
|
|
146
|
+
atol = dtype(1.1920928955078125e-7)
|
|
147
|
+
rtol = dtype(0)
|
|
148
|
+
choices = np.where(np.isclose(min_energy_host, energy, atol=atol, rtol=rtol))[0]
|
|
130
149
|
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
131
150
|
|
|
132
151
|
flip_index_start = best_i * k
|
|
@@ -135,7 +154,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
135
154
|
for i in indices_to_flip:
|
|
136
155
|
best_theta[i] = not best_theta[i]
|
|
137
156
|
|
|
138
|
-
return
|
|
157
|
+
return min_energy_host[best_i]
|
|
139
158
|
|
|
140
159
|
|
|
141
160
|
def spin_glass_solver(
|
|
@@ -160,6 +179,12 @@ def spin_glass_solver(
|
|
|
160
179
|
nodes = list(range(n_qubits))
|
|
161
180
|
G_m = G
|
|
162
181
|
|
|
182
|
+
segment_size = G_m.shape[0] ** 2
|
|
183
|
+
is_segmented = (G_m.nbytes << 2) > (3 * opencl_context.max_alloc)
|
|
184
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
185
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
186
|
+
is_alt_gpu_sampling = False
|
|
187
|
+
|
|
163
188
|
if n_qubits < 3:
|
|
164
189
|
if n_qubits == 0:
|
|
165
190
|
return "", 0, ([], []), 0
|
|
@@ -194,7 +219,22 @@ def spin_glass_solver(
|
|
|
194
219
|
else:
|
|
195
220
|
mf = cl.mem_flags
|
|
196
221
|
ctx = opencl_context.ctx
|
|
197
|
-
|
|
222
|
+
if is_segmented:
|
|
223
|
+
o_shape = segment_size
|
|
224
|
+
segment_size = (segment_size + 3) >> 2
|
|
225
|
+
n_shape = segment_size << 2
|
|
226
|
+
_G_m = np.reshape(G_m, (o_shape,))
|
|
227
|
+
if n_shape != o_shape:
|
|
228
|
+
np.resize(_G_m, (n_shape,))
|
|
229
|
+
_G_m_segments = np.split(_G_m, 4)
|
|
230
|
+
G_m_buf = [
|
|
231
|
+
cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
|
|
232
|
+
for seg in _G_m_segments
|
|
233
|
+
]
|
|
234
|
+
_G_m = None
|
|
235
|
+
_G_m_segments = None
|
|
236
|
+
else:
|
|
237
|
+
G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m)
|
|
198
238
|
|
|
199
239
|
min_energy = compute_energy(best_theta, G_m)
|
|
200
240
|
improved = True
|
|
@@ -217,7 +257,7 @@ def spin_glass_solver(
|
|
|
217
257
|
combos = combos_list[k - 1]
|
|
218
258
|
|
|
219
259
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
220
|
-
energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy)
|
|
260
|
+
energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy, is_segmented, segment_size)
|
|
221
261
|
else:
|
|
222
262
|
energy = bootstrap(best_theta, G_m, combos, k, min_energy, dtype)
|
|
223
263
|
|
|
@@ -70,10 +70,10 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy,
|
|
|
70
70
|
return min_energy
|
|
71
71
|
|
|
72
72
|
|
|
73
|
-
def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy):
|
|
73
|
+
def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy, is_segmented, segment_size):
|
|
74
74
|
ctx = opencl_context.ctx
|
|
75
75
|
queue = opencl_context.queue
|
|
76
|
-
bootstrap_kernel = opencl_context.bootstrap_sparse_kernel
|
|
76
|
+
bootstrap_kernel = opencl_context.bootstrap_sparse_segmented_kernel if is_segmented else opencl_context.bootstrap_sparse_kernel
|
|
77
77
|
dtype = opencl_context.dtype
|
|
78
78
|
wgs = opencl_context.work_group_size
|
|
79
79
|
|
|
@@ -82,8 +82,8 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
82
82
|
|
|
83
83
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
84
84
|
|
|
85
|
-
# Args: [n, k, combo_count]
|
|
86
|
-
args_np = np.array([n, k, combo_count], dtype=np.int32)
|
|
85
|
+
# Args: [n, k, combo_count, segment_size]
|
|
86
|
+
args_np = np.array([n, k, combo_count, segment_size], dtype=np.int32)
|
|
87
87
|
|
|
88
88
|
# Buffers
|
|
89
89
|
mf = cl.mem_flags
|
|
@@ -91,33 +91,52 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
91
91
|
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
92
92
|
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
93
93
|
|
|
94
|
-
# Allocate min_energy and min_index result buffers per workgroup
|
|
95
|
-
min_energy_host = np.empty(combo_count, dtype=dtype)
|
|
96
|
-
min_index_host = np.empty(combo_count, dtype=np.int32)
|
|
97
|
-
|
|
98
|
-
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
99
|
-
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
100
|
-
|
|
101
94
|
# Local memory allocation (1 float per work item)
|
|
102
95
|
local_size = min(wgs, n)
|
|
103
|
-
|
|
96
|
+
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
97
|
+
global_size = min(((combo_count + local_size - 1) // local_size) * local_size, max_global_size)
|
|
104
98
|
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
105
99
|
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
106
100
|
|
|
101
|
+
# Allocate min_energy and min_index result buffers per workgroup
|
|
102
|
+
min_energy_host = np.empty(global_size, dtype=dtype)
|
|
103
|
+
min_index_host = np.empty(global_size, dtype=np.int32)
|
|
104
|
+
|
|
105
|
+
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
106
|
+
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
107
|
+
|
|
107
108
|
# Set kernel args
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
109
|
+
if is_segmented:
|
|
110
|
+
bootstrap_kernel.set_args(
|
|
111
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
112
|
+
G_data_buf[0],
|
|
113
|
+
G_data_buf[1],
|
|
114
|
+
G_data_buf[2],
|
|
115
|
+
G_data_buf[3],
|
|
116
|
+
G_rows_buf,
|
|
117
|
+
G_cols_buf,
|
|
118
|
+
best_theta_buf,
|
|
119
|
+
indices_buf,
|
|
120
|
+
args_buf,
|
|
121
|
+
min_energy_buf,
|
|
122
|
+
min_index_buf,
|
|
123
|
+
local_energy_buf,
|
|
124
|
+
local_index_buf
|
|
125
|
+
)
|
|
126
|
+
else:
|
|
127
|
+
bootstrap_kernel.set_args(
|
|
128
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
129
|
+
G_data_buf,
|
|
130
|
+
G_rows_buf,
|
|
131
|
+
G_cols_buf,
|
|
132
|
+
best_theta_buf,
|
|
133
|
+
indices_buf,
|
|
134
|
+
args_buf,
|
|
135
|
+
min_energy_buf,
|
|
136
|
+
min_index_buf,
|
|
137
|
+
local_energy_buf,
|
|
138
|
+
local_index_buf
|
|
139
|
+
)
|
|
121
140
|
|
|
122
141
|
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
123
142
|
|
|
@@ -131,7 +150,9 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
131
150
|
if min_energy < energy:
|
|
132
151
|
return min_energy
|
|
133
152
|
|
|
134
|
-
|
|
153
|
+
atol = dtype(1.1920928955078125e-7)
|
|
154
|
+
rtol = dtype(0)
|
|
155
|
+
choices = np.where(np.isclose(min_energy_host, energy, atol=atol, rtol=rtol))[0]
|
|
135
156
|
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
136
157
|
|
|
137
158
|
flip_index_start = best_i * k
|
|
@@ -140,7 +161,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
140
161
|
for i in indices_to_flip:
|
|
141
162
|
best_theta[i] = not best_theta[i]
|
|
142
163
|
|
|
143
|
-
return
|
|
164
|
+
return min_energy_host[best_i]
|
|
144
165
|
|
|
145
166
|
|
|
146
167
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
@@ -191,6 +212,12 @@ def spin_glass_solver_sparse(
|
|
|
191
212
|
|
|
192
213
|
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
193
214
|
|
|
215
|
+
segment_size = G_m.data.shape[0]
|
|
216
|
+
is_segmented = (G_m.data.nbytes << 2) > (3 * opencl_context.max_alloc)
|
|
217
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
218
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
219
|
+
is_alt_gpu_sampling = False
|
|
220
|
+
|
|
194
221
|
if quality is None:
|
|
195
222
|
quuality = 3
|
|
196
223
|
|
|
@@ -213,9 +240,24 @@ def spin_glass_solver_sparse(
|
|
|
213
240
|
else:
|
|
214
241
|
mf = cl.mem_flags
|
|
215
242
|
ctx = opencl_context.ctx
|
|
216
|
-
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
217
243
|
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr)
|
|
218
244
|
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices)
|
|
245
|
+
if is_segmented:
|
|
246
|
+
o_shape = segment_size
|
|
247
|
+
segment_size = (segment_size + 3) >> 2
|
|
248
|
+
n_shape = segment_size << 2
|
|
249
|
+
_G_data = np.reshape(G_m.data, (o_shape,))
|
|
250
|
+
if n_shape != o_shape:
|
|
251
|
+
np.resize(_G_data, (n_shape,))
|
|
252
|
+
_G_data_segments = np.split(_G_data, 4)
|
|
253
|
+
G_data_buf = [
|
|
254
|
+
cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
|
|
255
|
+
for seg in _G_data_segments
|
|
256
|
+
]
|
|
257
|
+
_G_data = None
|
|
258
|
+
_G_data_segments = None
|
|
259
|
+
else:
|
|
260
|
+
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
219
261
|
|
|
220
262
|
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
221
263
|
improved = True
|
|
@@ -238,7 +280,7 @@ def spin_glass_solver_sparse(
|
|
|
238
280
|
combos = combos_list[k - 1]
|
|
239
281
|
|
|
240
282
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
241
|
-
energy = run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, min_energy)
|
|
283
|
+
energy = run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, min_energy, is_segmented, segment_size)
|
|
242
284
|
else:
|
|
243
285
|
energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy, dtype)
|
|
244
286
|
|
|
@@ -52,9 +52,11 @@ def bootstrap(best_theta, G_func, G_func_args_tuple, nodes, indices_array, k, mi
|
|
|
52
52
|
|
|
53
53
|
energy = energies.min()
|
|
54
54
|
if energy < min_energy:
|
|
55
|
-
|
|
55
|
+
atol = dtype(1.1920928955078125e-7)
|
|
56
|
+
rtol = dtype(0)
|
|
57
|
+
index_match = np.random.choice(np.where(np.isclose(energies, energy, atol=atol, rtol=rtol))[0])
|
|
56
58
|
indices = indices_array[(index_match * k) : ((index_match + 1) * k)]
|
|
57
|
-
min_energy =
|
|
59
|
+
min_energy = energies[index_match]
|
|
58
60
|
for i in indices:
|
|
59
61
|
best_theta[i] = not best_theta[i]
|
|
60
62
|
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='6.
|
|
54
|
+
version='6.3.1',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|