PyQrackIsing 6.2.4__tar.gz → 6.3.0__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.0}/PKG-INFO +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyproject.toml +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/kernels.cl +264 -16
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/maxcut_tfim.py +8 -4
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/maxcut_tfim_sparse.py +8 -4
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/maxcut_tfim_util.py +12 -2
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/spin_glass_solver.py +63 -25
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/spin_glass_solver_sparse.py +67 -27
- {pyqrackising-6.2.4 → pyqrackising-6.3.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/setup.py +1 -1
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/CMakeLists.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/LICENSE.md +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/MANIFEST.in +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/README.md +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/__init__.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising/tsp.py +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-6.2.4 → pyqrackising-6.3.0}/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.0"
|
|
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
|
|
|
@@ -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
|
|
|
@@ -160,6 +177,12 @@ def spin_glass_solver(
|
|
|
160
177
|
nodes = list(range(n_qubits))
|
|
161
178
|
G_m = G
|
|
162
179
|
|
|
180
|
+
segment_size = G_m.shape[0] ** 2
|
|
181
|
+
is_segmented = (G_m.nbytes << 2) > (3 * opencl_context.max_alloc)
|
|
182
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
183
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
184
|
+
is_alt_gpu_sampling = False
|
|
185
|
+
|
|
163
186
|
if n_qubits < 3:
|
|
164
187
|
if n_qubits == 0:
|
|
165
188
|
return "", 0, ([], []), 0
|
|
@@ -194,7 +217,22 @@ def spin_glass_solver(
|
|
|
194
217
|
else:
|
|
195
218
|
mf = cl.mem_flags
|
|
196
219
|
ctx = opencl_context.ctx
|
|
197
|
-
|
|
220
|
+
if is_segmented:
|
|
221
|
+
o_shape = segment_size
|
|
222
|
+
segment_size = (segment_size + 3) >> 2
|
|
223
|
+
n_shape = segment_size << 2
|
|
224
|
+
_G_m = np.reshape(G_m, (o_shape,))
|
|
225
|
+
if n_shape != o_shape:
|
|
226
|
+
np.resize(_G_m, (n_shape,))
|
|
227
|
+
_G_m_segments = np.split(_G_m, 4)
|
|
228
|
+
G_m_buf = [
|
|
229
|
+
cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
|
|
230
|
+
for seg in _G_m_segments
|
|
231
|
+
]
|
|
232
|
+
_G_m = None
|
|
233
|
+
_G_m_segments = None
|
|
234
|
+
else:
|
|
235
|
+
G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m)
|
|
198
236
|
|
|
199
237
|
min_energy = compute_energy(best_theta, G_m)
|
|
200
238
|
improved = True
|
|
@@ -217,7 +255,7 @@ def spin_glass_solver(
|
|
|
217
255
|
combos = combos_list[k - 1]
|
|
218
256
|
|
|
219
257
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
220
|
-
energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy)
|
|
258
|
+
energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy, is_segmented, segment_size)
|
|
221
259
|
else:
|
|
222
260
|
energy = bootstrap(best_theta, G_m, combos, k, min_energy, dtype)
|
|
223
261
|
|
|
@@ -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
|
|
|
@@ -191,6 +210,12 @@ def spin_glass_solver_sparse(
|
|
|
191
210
|
|
|
192
211
|
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
193
212
|
|
|
213
|
+
segment_size = G_m.data.shape[0]
|
|
214
|
+
is_segmented = (G_m.data.nbytes << 2) > (3 * opencl_context.max_alloc)
|
|
215
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
216
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
217
|
+
is_alt_gpu_sampling = False
|
|
218
|
+
|
|
194
219
|
if quality is None:
|
|
195
220
|
quuality = 3
|
|
196
221
|
|
|
@@ -213,9 +238,24 @@ def spin_glass_solver_sparse(
|
|
|
213
238
|
else:
|
|
214
239
|
mf = cl.mem_flags
|
|
215
240
|
ctx = opencl_context.ctx
|
|
216
|
-
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
217
241
|
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr)
|
|
218
242
|
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices)
|
|
243
|
+
if is_segmented:
|
|
244
|
+
o_shape = segment_size
|
|
245
|
+
segment_size = (segment_size + 3) >> 2
|
|
246
|
+
n_shape = segment_size << 2
|
|
247
|
+
_G_data = np.reshape(G_m.data, (o_shape,))
|
|
248
|
+
if n_shape != o_shape:
|
|
249
|
+
np.resize(_G_data, (n_shape,))
|
|
250
|
+
_G_data_segments = np.split(_G_data, 4)
|
|
251
|
+
G_data_buf = [
|
|
252
|
+
cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
|
|
253
|
+
for seg in _G_data_segments
|
|
254
|
+
]
|
|
255
|
+
_G_data = None
|
|
256
|
+
_G_data_segments = None
|
|
257
|
+
else:
|
|
258
|
+
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
219
259
|
|
|
220
260
|
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
221
261
|
improved = True
|
|
@@ -238,7 +278,7 @@ def spin_glass_solver_sparse(
|
|
|
238
278
|
combos = combos_list[k - 1]
|
|
239
279
|
|
|
240
280
|
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)
|
|
281
|
+
energy = run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, min_energy, is_segmented, segment_size)
|
|
242
282
|
else:
|
|
243
283
|
energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy, dtype)
|
|
244
284
|
|
|
@@ -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.0',
|
|
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
|
|
File without changes
|
|
File without changes
|