scs 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/ext/scs/extconf.rb +3 -9
  4. data/lib/scs/version.rb +1 -1
  5. data/vendor/scs/CITATION.cff +2 -2
  6. data/vendor/scs/CMakeLists.txt +138 -10
  7. data/vendor/scs/CONTRIBUTING.md +21 -2
  8. data/vendor/scs/Makefile +4 -3
  9. data/vendor/scs/README.md +25 -14
  10. data/vendor/scs/include/cones.h +27 -9
  11. data/vendor/scs/include/glbopts.h +25 -5
  12. data/vendor/scs/include/scs.h +10 -8
  13. data/vendor/scs/include/scs_work.h +2 -1
  14. data/vendor/scs/linsys/cudss/direct/private.c +50 -2
  15. data/vendor/scs/linsys/gpu/indirect/private.c +17 -4
  16. data/vendor/scs/linsys/mkl/direct/private.c +1 -1
  17. data/vendor/scs/scs.mk +43 -2
  18. data/vendor/scs/src/aa.c +27 -7
  19. data/vendor/scs/src/cones.c +851 -163
  20. data/vendor/scs/src/linalg.c +16 -3
  21. data/vendor/scs/src/scs.c +91 -46
  22. data/vendor/scs/src/spectral_cones/logdeterminant/logdet_cone.c +15 -14
  23. data/vendor/scs/src/spectral_cones/nuclear/ell1_cone.c +1 -4
  24. data/vendor/scs/src/spectral_cones/sum-largest/sum_largest_eval_cone.c +11 -9
  25. data/vendor/scs/test/minunit.h +27 -3
  26. data/vendor/scs/test/mkl_psd_guard.c +282 -0
  27. data/vendor/scs/test/problem_utils.h +123 -22
  28. data/vendor/scs/test/problems/complex_PSD.h +2 -1
  29. data/vendor/scs/test/problems/degenerate.h +10 -3
  30. data/vendor/scs/test/problems/dense_qp.h +2 -1
  31. data/vendor/scs/test/problems/hs21_tiny_qp.h +7 -3
  32. data/vendor/scs/test/problems/hs21_tiny_qp_rw.h +5 -2
  33. data/vendor/scs/test/problems/issue_140 +0 -0
  34. data/vendor/scs/test/problems/issue_140.h +19 -0
  35. data/vendor/scs/test/problems/issue_220 +0 -0
  36. data/vendor/scs/test/problems/issue_220.h +37 -0
  37. data/vendor/scs/test/problems/issue_499.h +79 -0
  38. data/vendor/scs/test/problems/lp_update.h +5 -5
  39. data/vendor/scs/test/problems/qafiro_tiny_qp.h +25 -5
  40. data/vendor/scs/test/problems/rob_gauss_cov_est.h +24 -4
  41. data/vendor/scs/test/problems/sd_and_complex_sd.h +2 -1
  42. data/vendor/scs/test/problems/small_lp.h +2 -1
  43. data/vendor/scs/test/problems/small_qp.h +7 -0
  44. data/vendor/scs/test/problems/test_box_cone.h +2 -1
  45. data/vendor/scs/test/problems/test_dual_exp_cone.h +2 -1
  46. data/vendor/scs/test/problems/test_mixed_cones.h +2 -1
  47. data/vendor/scs/test/problems/test_non_psd_p.h +76 -0
  48. data/vendor/scs/test/problems/test_normalize_roundtrip.h +14 -12
  49. data/vendor/scs/test/problems/test_power_cone.h +139 -5
  50. data/vendor/scs/test/problems/test_prob_from_data_file.h +14 -4
  51. data/vendor/scs/test/problems/test_psd_metric.h +11 -7
  52. data/vendor/scs/test/problems/test_psd_n1.h +2 -1
  53. data/vendor/scs/test/problems/test_psd_parallel.h +383 -0
  54. data/vendor/scs/test/problems/test_psd_scale_updates.h +186 -0
  55. data/vendor/scs/test/problems/test_soc_sizes.h +10 -5
  56. data/vendor/scs/test/problems/test_solver_options.h +10 -3
  57. data/vendor/scs/test/problems/test_zero_cone.h +2 -1
  58. data/vendor/scs/test/run_from_file.c +8 -0
  59. data/vendor/scs/test/run_tests.c +84 -19
  60. data/vendor/scs/test/spectral_cones_problems/exp_design.h +1 -0
  61. data/vendor/scs/test/spectral_cones_problems/several_logdet_cones.h +1 -0
  62. data/vendor/scs/test/spectral_cones_problems/test_spectral_metric_updates.h +163 -0
  63. metadata +12 -3
  64. data/vendor/scs/CLAUDE.md +0 -115
@@ -13,8 +13,20 @@
13
13
  #include "scs_blas.h" /* contains BLAS(X) macros and type info */
14
14
  #include "util.h"
15
15
 
16
+ #include <float.h>
16
17
  #include <stdio.h>
17
18
  #include <string.h>
19
+ /* USE_PARALLEL_PSD is a build option, off by default, that compiles in the
20
+ * parallel projection of the PSD blocks. It needs OpenMP and LAPACK. Every
21
+ * other build, with OpenMP or not, projects the blocks one after another. It
22
+ * is an option because it is only faster when the BLAS runs on one thread
23
+ * inside the threads of SCS: see "PARALLEL PROJECTION OF THE PSD BLOCKS"
24
+ * below for what it does and what it asks of the BLAS. */
25
+ #if defined(USE_PARALLEL_PSD) && defined(_OPENMP) && defined(USE_LAPACK)
26
+ #define PSD_PARALLEL (1)
27
+ #include <omp.h>
28
+ #include <stdlib.h> /* qsort */
29
+ #endif
18
30
 
19
31
  /*
20
32
  * Cross-platform Complex Type Handling
@@ -47,8 +59,22 @@ typedef struct {
47
59
 
48
60
  /* Constants */
49
61
  #define BOX_CONE_MAX_ITERS (25)
50
- #define POW_CONE_TOL (1e-9)
51
- #define POW_CONE_MAX_ITERS (20)
62
+ /* Power cone projection: POW_CONE_TOL is the tolerance relative to |v| for
63
+ * the Newton solve and the membership checks, POW_CONE_START_SLOPE bounds
64
+ * |dx/dr| and |dy/dr| at r = rh for the Newton solve to start there, and
65
+ * POW_CONE_EPS is the machine epsilon. All must suit the working precision;
66
+ * in particular the first Newton step from rh is only representable when
67
+ * POW_CONE_START_SLOPE^2 * POW_CONE_EPS is small, hence the float value. */
68
+ #ifdef SFLOAT
69
+ #define POW_CONE_TOL (1e-6)
70
+ #define POW_CONE_START_SLOPE (10)
71
+ #define POW_CONE_EPS (FLT_EPSILON)
72
+ #else
73
+ #define POW_CONE_TOL (1e-12)
74
+ #define POW_CONE_START_SLOPE (1e3)
75
+ #define POW_CONE_EPS (DBL_EPSILON)
76
+ #endif
77
+ #define POW_CONE_MAX_ITERS (50)
52
78
 
53
79
  /* Box cone limits (+ or -) taken to be INF */
54
80
  #define MAX_BOX_VAL (1e15)
@@ -76,16 +102,16 @@ void BLASC(heevr)(const char *jobz, const char *range, const char *uplo,
76
102
  scs_float *rwork, blas_int *lrwork, blas_int *iwork,
77
103
  blas_int *liwork, blas_int *info);
78
104
 
79
- blas_int BLAS(syrk)(const char *uplo, const char *trans, const blas_int *n,
80
- const blas_int *k, const scs_float *alpha,
81
- const scs_float *a, const blas_int *lda,
82
- const scs_float *beta, scs_float *c, const blas_int *ldc);
105
+ void BLAS(syrk)(const char *uplo, const char *trans, const blas_int *n,
106
+ const blas_int *k, const scs_float *alpha, const scs_float *a,
107
+ const blas_int *lda, const scs_float *beta, scs_float *c,
108
+ const blas_int *ldc);
83
109
 
84
- blas_int BLASC(herk)(const char *uplo, const char *trans, const blas_int *n,
85
- const blas_int *k, const scs_float *alpha,
86
- const SCS_BLAS_COMPLEX_TYPE *a, const blas_int *lda,
87
- const scs_float *beta, SCS_BLAS_COMPLEX_TYPE *c,
88
- const blas_int *ldc);
110
+ void BLASC(herk)(const char *uplo, const char *trans, const blas_int *n,
111
+ const blas_int *k, const scs_float *alpha,
112
+ const SCS_BLAS_COMPLEX_TYPE *a, const blas_int *lda,
113
+ const scs_float *beta, SCS_BLAS_COMPLEX_TYPE *c,
114
+ const blas_int *ldc);
89
115
 
90
116
  void BLAS(scal)(const blas_int *n, const scs_float *sa, scs_float *sx,
91
117
  const blas_int *incx);
@@ -93,6 +119,10 @@ void BLASC(scal)(const blas_int *n, const SCS_BLAS_COMPLEX_TYPE *sa,
93
119
  SCS_BLAS_COMPLEX_TYPE *sx, const blas_int *incx);
94
120
 
95
121
  #ifdef USE_SPECTRAL_CONES
122
+ void BLAS(syev)(const char *jobz, const char *uplo, blas_int *n, scs_float *a,
123
+ blas_int *lda, scs_float *w, scs_float *work, blas_int *lwork,
124
+ blas_int *info);
125
+
96
126
  void BLAS(gesvd)(const char *jobu, const char *jobvt, const blas_int *m,
97
127
  const blas_int *n, scs_float *a, const blas_int *lda,
98
128
  scs_float *s, scs_float *u, const blas_int *ldu, scs_float *vt,
@@ -285,32 +315,30 @@ void SCS(finish_cone)(ScsConeWork *c) {
285
315
  if (!c)
286
316
  return;
287
317
  #ifdef USE_LAPACK
288
- if (c->Xs)
289
- scs_free(c->Xs);
290
- if (c->cXs)
291
- scs_free(c->cXs);
292
- if (c->Z)
293
- scs_free(c->Z);
294
- if (c->cZ)
295
- scs_free(c->cZ);
296
- if (c->e)
297
- scs_free(c->e);
298
- if (c->isuppz)
299
- scs_free(c->isuppz);
300
- if (c->work)
301
- scs_free(c->work);
302
- if (c->iwork)
303
- scs_free(c->iwork);
304
- if (c->cwork)
305
- scs_free(c->cwork);
306
- /* c->rwork is aliased to c->work in setup, no free needed */
318
+ if (c->psd_tw) {
319
+ scs_int t, j;
320
+ for (t = 0; t < MAX(c->psd_nthreads, 1); ++t) {
321
+ ScsPsdWork *w = &(c->psd_tw[t]);
322
+ void *bufs[] = {w->Xs, w->Z, w->e, w->work, w->gamma,
323
+ w->cXs, w->cZ, w->cwork, w->isuppz, w->iwork};
324
+ for (j = 0; j < (scs_int)(sizeof(bufs) / sizeof(bufs[0])); ++j) {
325
+ if (bufs[j])
326
+ scs_free(bufs[j]);
327
+ }
328
+ }
329
+ scs_free(c->psd_tw);
330
+ }
307
331
  #endif
332
+ if (c->psd_blk)
333
+ scs_free(c->psd_blk);
334
+ if (c->psd_tptr)
335
+ scs_free(c->psd_tptr);
336
+ if (c->psd_off)
337
+ scs_free(c->psd_off);
308
338
  if (c->cone_boundaries)
309
339
  scs_free(c->cone_boundaries);
310
340
  if (c->cone_boundaries_psd_n)
311
341
  scs_free(c->cone_boundaries_psd_n);
312
- if (c->psd_gamma)
313
- scs_free(c->psd_gamma);
314
342
  if (c->r_box_inv)
315
343
  scs_free(c->r_box_inv);
316
344
  if (c->s)
@@ -391,7 +419,7 @@ static void fit_psd_block_weights(scs_float *vec, scs_int n) {
391
419
  scs_float lo = SQRTF(DIAG_SCALE_MULT_MIN), hi = SQRTF(DIAG_SCALE_MULT_MAX);
392
420
  scs_float *ell = (scs_float *)scs_calloc(n, sizeof(scs_float));
393
421
  if (!ell) {
394
- return; /* leave profile as-is; caller falls back to scalar */
422
+ return; /* leave the block as it is */
395
423
  }
396
424
  /* Floor entries relative to the block max: (near-)zero rows carry no
397
425
  * information about the block's scale structure (e.g. unconstrained
@@ -855,6 +883,469 @@ scs_int SCS(validate_cones)(const ScsData *d, const ScsCone *k) {
855
883
  return 0;
856
884
  }
857
885
 
886
+ /* ================ PSD Block Workspaces and Threads ==================== */
887
+
888
+ /*
889
+ * PARALLEL PROJECTION OF THE PSD BLOCKS (build option USE_PARALLEL_PSD)
890
+ *
891
+ * What the option does. The real and complex PSD blocks of a cone are
892
+ * projected independently of each other. A build with USE_PARALLEL_PSD, OpenMP
893
+ * and LAPACK shares them out over OpenMP threads, and each thread calls LAPACK
894
+ * for its own blocks. Every other build projects them one after another on
895
+ * the calling thread.
896
+ *
897
+ * Two levels of threads, one at a time. The projection of a block is an
898
+ * eigendecomposition in LAPACK, which runs on BLAS, and a BLAS can have
899
+ * threads of its own. So the cores can be used in two ways:
900
+ * - by the BLAS, inside one block at a time. This is what a build without
901
+ * the option does, and what a build with it does when it keeps a
902
+ * projection serial (psd_num_threads): there is no parallel region then,
903
+ * and the BLAS is as free to use its threads as it is without OpenMP;
904
+ * - by SCS, across the blocks, with the BLAS on one thread inside each.
905
+ * Across the blocks is the faster way when there are several of them: on 4
906
+ * threads, 4 to 16 blocks of n = 256 to 1024 were projected 3 times faster,
907
+ * while the threads of the BLAS inside one block gained nothing below n of
908
+ * about 1000 (OpenBLAS, Accelerate, NVPL). It cannot be faster than its
909
+ * largest block, though, so a cone with one dominant block is left to the
910
+ * BLAS. What must not happen is both at once: t threads of SCS that each
911
+ * start the threads of the BLAS are more threads than cores, and slower than
912
+ * either way alone.
913
+ *
914
+ * What the BLAS does inside the threads of SCS. This decides whether the
915
+ * option pays. SCS cannot ensure it, and cannot even see it for every BLAS,
916
+ * which is why the option is off by default and left to the build, which
917
+ * knows its BLAS:
918
+ * - A BLAS on the OpenMP runtime of SCS (OpenMP builds of OpenBLAS and BLIS,
919
+ * NVPL, MKL on the matching threading layer) runs on one thread there by
920
+ * itself. Its parallel regions are nested in ours, and OpenMP gives a
921
+ * nested region one thread by default. This is the setup the option is
922
+ * for. The OpenMP thread count (OMP_NUM_THREADS) is then one budget for
923
+ * both levels: our team during a parallel projection, the threads of the
924
+ * BLAS everywhere else. It rests on that default: with nested parallelism
925
+ * switched on (OMP_MAX_ACTIVE_LEVELS above 1), such a BLAS may start
926
+ * threads inside ours.
927
+ * - A BLAS on another OpenMP runtime (libiomp5 or libomp under a GNU-compiled
928
+ * SCS) cannot see our region and starts its own threads in each of our
929
+ * threads. Measured with an OpenBLAS on libomp: 16 threads for 4, at half
930
+ * the speed. For MKL, each of our threads therefore sets MKL's thread-local
931
+ * thread count to one for the projection (mkl_set_local_threads). This
932
+ * goes through a weak reference: it needs ELF and MKL's symbols visible to
933
+ * SCS.
934
+ * - A BLAS with a thread pool of its own knows nothing of OpenMP. That is a
935
+ * pthreads OpenBLAS: the default on Debian and Ubuntu, and what Julia and
936
+ * the SciPy wheels ship. With more than one thread, the BLAS calls of our
937
+ * threads go to its pool as well and the cores are oversubscribed (measured
938
+ * up to 3.6x slower than serial), and it has no thread-local setting to
939
+ * stop that. So when the linked BLAS is a pthreads OpenBLAS on more than
940
+ * one thread the blocks stay serial (blas_threads_outside_openmp), and
941
+ * OPENBLAS_NUM_THREADS=1 is what enables the parallel projection. This
942
+ * check goes through weak references too: it is off for a static OpenBLAS,
943
+ * one with suffixed symbols, and one loaded privately by another library
944
+ * (libblastrampoline in Julia). On one thread an OpenBLAS without OpenMP
945
+ * still makes the BLAS calls of our threads wait for one lock, and the
946
+ * estimate of the thread count adds that (PSD_PAR_LOCK_COST): small
947
+ * blocks then stay serial, and the others get fewer threads than there
948
+ * are. That needs the same symbols.
949
+ * - Accelerate keeps threads of its own as well, which stayed mostly idle
950
+ * inside ours: at its default settings the parallel projection was 2.3 to
951
+ * 3.3x faster than the serial one on 4 threads, and at most 17% slower
952
+ * than with Accelerate on one thread.
953
+ * Neither check exists on Mach-O or Windows, and there is none for other
954
+ * libraries, or for a BLAS other than MKL on another OpenMP runtime. There
955
+ * the BLAS has to be put on one thread for the whole process, or the build
956
+ * left without the option. An environment variable cannot do that for an
957
+ * OpenMP BLAS on another runtime: OMP_NUM_THREADS is read by both runtimes,
958
+ * and an OpenMP build of OpenBLAS ignores OPENBLAS_NUM_THREADS.
959
+ *
960
+ * The split over threads is fixed at setup: blocks in order of decreasing
961
+ * estimated time (psd_block_cost) go to the least loaded thread (LPT
962
+ * scheduling). Thread t projects its blocks (list t) with workspace
963
+ * c->psd_tw[t], sized for its own largest block; psd_tw[0] is the workspace
964
+ * of all the matrix cones and no other is larger, so the extra memory is at
965
+ * most (psd_nthreads - 1) times the serial PSD workspace. Every workspace has
966
+ * the same LAPACK work-array sizes, found for the largest blocks: syevr and
967
+ * heevr pass the work array left after their own arrays to sytrd/hetrd, which
968
+ * lower their block size to fit, so the blocking (and the rounding) of a
969
+ * block larger than the crossover depends on the exact size. Each block then
970
+ * runs the same code with the same sizes on any thread, so with
971
+ * single-threaded BLAS the iterates do not depend on the number of threads.
972
+ *
973
+ * The number of threads is the OpenMP thread count at setup, at most the
974
+ * number of blocks with n > 1. A projection uses fewer threads when the
975
+ * OpenMP thread count has been lowered since: a smaller team still projects
976
+ * every list, each with its own workspace. The number of threads is also
977
+ * bounded by the work. Each thread of a parallel region takes time to wake
978
+ * and to join (PSD_PAR_THREAD_COST), so the region is fastest with fewer
979
+ * threads than there are when the blocks are little work: about
980
+ * sqrt(S / PSD_PAR_THREAD_COST) of them for an estimated serial time S over
981
+ * many blocks. The threads are not used, and the BLAS is left to its own,
982
+ * when
983
+ * - the estimated gain is below PSD_PAR_MIN_GAIN: little work, or one block
984
+ * with most of it (the parallel loop is no faster than its largest block),
985
+ * - a block larger than PSD_PAR_BLAS_N holds more than half of the work (the
986
+ * parallel loop then gains less than 2x, while that block alone may run
987
+ * faster with multithreaded BLAS), or
988
+ * - the workspace is set up inside an active parallel region (the caller's
989
+ * threads are the parallelism then).
990
+ */
991
+ #ifdef PSD_PARALLEL
992
+ /* Size of PSD block b: real block b if b < ssize, else complex b - ssize */
993
+ static inline scs_int psd_block_n(const ScsCone *k, scs_int b) {
994
+ return b < k->ssize ? k->s[b] : k->cs[b - k->ssize];
995
+ }
996
+
997
+ /* Estimated serial time, in nanoseconds, below which the threads are not
998
+ * used. Zero, because the rules of psd_num_threads keep little work serial
999
+ * already. A large value keeps every projection serial. Those rules take the
1000
+ * threads to be awake. Where they sleep between projections (the GNU runtime
1001
+ * on Linux, when the rest of an iteration takes long enough), waking them can
1002
+ * cost more than a projection of less than a millisecond gains, and a minimum
1003
+ * of that size here would avoid it. It is zero there too, on purpose: the
1004
+ * same minimum would keep small problems serial, whose threads stay awake and
1005
+ * gain the most (an iteration 2.5 times faster, against one up to 27% slower,
1006
+ * both measured). OMP_WAIT_POLICY=active, or a build without
1007
+ * USE_PARALLEL_PSD, is the switch for the other case. */
1008
+ #ifndef PSD_PAR_MIN_COST
1009
+ #define PSD_PAR_MIN_COST (0.)
1010
+ #endif
1011
+
1012
+ /* Time, in nanoseconds, that each thread adds to a parallel region, measured
1013
+ * with the GNU runtime on projections that follow each other closely: about
1014
+ * 5 us per thread on macOS (Apple M4), where the threads of a team sleep
1015
+ * between regions, and 1.5 to 2 us on Linux (Cortex-X925), where they spin
1016
+ * for a while after a region. */
1017
+ #ifndef PSD_PAR_THREAD_COST
1018
+ #ifdef __APPLE__
1019
+ #define PSD_PAR_THREAD_COST (5e3)
1020
+ #else
1021
+ #define PSD_PAR_THREAD_COST (2e3)
1022
+ #endif
1023
+ #endif
1024
+
1025
+ /* smallest estimated gain for which the threads are used */
1026
+ #ifndef PSD_PAR_MIN_GAIN
1027
+ #define PSD_PAR_MIN_GAIN (1.25)
1028
+ #endif
1029
+
1030
+ /* Largest block for which the threads of the BLAS are not worth more than
1031
+ * the loop. With a block of n = 300 or 500 that holds more than half of the
1032
+ * work, the loop was 1.6x faster than the serial projection, and the serial
1033
+ * projection with a multithreaded BLAS 0.7x to 1.25x (NVPL, OpenBLAS;
1034
+ * Accelerate 1.0x at 300). At n = 1000 the two were about equal, and at 2000
1035
+ * the BLAS threads were ahead (1.7x to 1.9x against 1.2x to 1.4x). Blocks of
1036
+ * n = 1000 to 2000 were 1.3x to 2.1x faster with BLAS threads in #505
1037
+ * (OpenBLAS, MKL). */
1038
+ #ifndef PSD_PAR_BLAS_N
1039
+ #define PSD_PAR_BLAS_N (512)
1040
+ #endif
1041
+
1042
+ /* An OpenBLAS that was not built with OpenMP takes one mutex in every BLAS
1043
+ * call that needs a work buffer (dsymv: one call per column in the reduction
1044
+ * to tridiagonal form), so the calls of the threads of a team wait for each
1045
+ * other. A block of size n then takes longer in a team of t threads than
1046
+ * alone, by n * (PSD_PAR_LOCK_COST * (t - 1) + PSD_PAR_LOCK_COST2 * (t - 1)^2)
1047
+ * nanoseconds. Measured on a Cortex-X925 under Linux (OpenBLAS 0.3.31,
1048
+ * pthreads build on one thread), the time per column depended little on the
1049
+ * size of the block, for n = 3 to 32: about 0.3, 0.5, 1.1, 1.7 and 4.8 us
1050
+ * with 2, 3, 4, 5 and 10 threads. The constants are a third above the fit to
1051
+ * that, because the smallest blocks lost more. An OpenMP build did not show
1052
+ * this (measured on macOS). */
1053
+ #ifndef PSD_PAR_LOCK_COST
1054
+ #define PSD_PAR_LOCK_COST (300.)
1055
+ #endif
1056
+ #ifndef PSD_PAR_LOCK_COST2
1057
+ #define PSD_PAR_LOCK_COST2 (50.)
1058
+ #endif
1059
+
1060
+ /* OpenBLAS: openblas_get_parallel() is 0 sequential, 1 pthreads, 2 OpenMP.
1061
+ * MKL: MKL_Set_Num_Threads_Local(n) sets the number of MKL threads for the
1062
+ * calling thread only, and returns the previous one (0: the global setting).
1063
+ * Weak references, so other BLAS libraries leave them NULL (ELF only: a
1064
+ * weak reference to a missing symbol does not link on Mach-O). C linkage,
1065
+ * so a C++ build looks up the same symbols. */
1066
+ #if defined(__GNUC__) && defined(__ELF__)
1067
+ #ifdef __cplusplus
1068
+ extern "C" {
1069
+ #endif
1070
+ int openblas_get_parallel(void) __attribute__((weak));
1071
+ int openblas_get_num_threads(void) __attribute__((weak));
1072
+ int MKL_Set_Num_Threads_Local(int) __attribute__((weak));
1073
+ #ifdef __cplusplus
1074
+ }
1075
+ #endif
1076
+ #endif
1077
+
1078
+ /* Whether BLAS calls would start threads of their own inside OpenMP */
1079
+ static scs_int blas_threads_outside_openmp(void) {
1080
+ #if defined(__GNUC__) && defined(__ELF__)
1081
+ if (openblas_get_parallel && openblas_get_num_threads) {
1082
+ return openblas_get_parallel() == 1 && openblas_get_num_threads() > 1;
1083
+ }
1084
+ #endif
1085
+ return 0;
1086
+ }
1087
+
1088
+ /* Whether the BLAS is an OpenBLAS without OpenMP, whose calls from several
1089
+ * threads wait for one lock (PSD_PAR_LOCK_COST) */
1090
+ static scs_int blas_calls_share_lock(void) {
1091
+ #if defined(__GNUC__) && defined(__ELF__)
1092
+ if (openblas_get_parallel) {
1093
+ return openblas_get_parallel() != 2;
1094
+ }
1095
+ #endif
1096
+ return 0;
1097
+ }
1098
+
1099
+ /* Set the number of MKL threads for the calling thread and return the previous
1100
+ * setting, to restore it with. Does nothing, and returns 0, without MKL. */
1101
+ static int mkl_set_local_threads(int nt) {
1102
+ #if defined(__GNUC__) && defined(__ELF__)
1103
+ if (MKL_Set_Num_Threads_Local) {
1104
+ return MKL_Set_Num_Threads_Local(nt);
1105
+ }
1106
+ #endif
1107
+ (void)nt;
1108
+ return 0;
1109
+ }
1110
+
1111
+ typedef struct {
1112
+ scs_float cost;
1113
+ scs_int blk;
1114
+ } PsdBlockCost;
1115
+
1116
+ /* Estimated time of the projection of block b, in nanoseconds: a cubic in n
1117
+ * fitted to the measured times of single blocks, with 4x the n^3 term for a
1118
+ * complex block. It is within 25% of them for n from 2 to 300 on an Apple
1119
+ * M4 with Accelerate and on a Cortex-X925 with OpenBLAS, and within 30% of
1120
+ * the times of n = 1000 to 2000 on x86-64 with OpenBLAS and with MKL (#505).
1121
+ * It is up to 1.5x too low at n = 1000 on the Cortex-X925, and up to 1.9x too
1122
+ * low below n = 10 with NVPL, which takes longer for small blocks. Only the
1123
+ * relative sizes and the order of magnitude matter. n^3 alone makes small
1124
+ * blocks far too cheap: a block of n = 10 takes 1/650 of the time of one of
1125
+ * n = 300, not 1/27000. LAPACK takes a short path for n = 2 (0.2 us, but
1126
+ * 1.5 us with NVPL), and a block of n = 1 is a clamp. */
1127
+ static scs_float psd_block_cost(const ScsCone *k, scs_int b) {
1128
+ scs_float n = (scs_float)psd_block_n(k, b);
1129
+ if (b < k->ssize)
1130
+ return n < 3. ? (n < 2. ? 2. : 200.)
1131
+ : 600. + n * (250. + n * (45. + 0.06 * n));
1132
+ return n < 3. ? (n < 2. ? 2. : 300.)
1133
+ : 600. + n * (250. + n * (70. + 0.24 * n));
1134
+ }
1135
+
1136
+ /* decreasing cost, ties by block index so the split is deterministic */
1137
+ static int cmp_psd_block_cost(const void *a, const void *b) {
1138
+ const PsdBlockCost *x = (const PsdBlockCost *)a;
1139
+ const PsdBlockCost *y = (const PsdBlockCost *)b;
1140
+ if (x->cost != y->cost)
1141
+ return x->cost < y->cost ? 1 : -1;
1142
+ return (x->blk > y->blk) - (x->blk < y->blk);
1143
+ }
1144
+
1145
+ /* LPT: each block, largest first, to the least loaded thread; each thread's
1146
+ * list largest first. Fills psd_tptr and psd_blk for psd_nthreads > 1. */
1147
+ static scs_int assign_psd_blocks(ScsConeWork *c, const ScsCone *k) {
1148
+ scs_int nblk = k->ssize + k->cssize, nt = c->psd_nthreads, i, j, t;
1149
+ scs_int status = 0, *pos = (scs_int *)scs_calloc(nblk, sizeof(scs_int));
1150
+ scs_float *load = (scs_float *)scs_calloc(nt, sizeof(scs_float));
1151
+ PsdBlockCost *bc = (PsdBlockCost *)scs_calloc(nblk, sizeof(PsdBlockCost));
1152
+ if (!pos || !load || !bc) {
1153
+ status = -1;
1154
+ goto out;
1155
+ }
1156
+ for (i = 0; i < nblk; ++i) {
1157
+ bc[i].blk = i;
1158
+ bc[i].cost = psd_block_cost(k, i);
1159
+ }
1160
+ qsort(bc, nblk, sizeof(PsdBlockCost), cmp_psd_block_cost);
1161
+ memset(c->psd_tptr, 0, (nt + 1) * sizeof(scs_int));
1162
+ for (j = 0; j < nblk; ++j) {
1163
+ scs_int tmin = 0;
1164
+ for (t = 1; t < nt; ++t) {
1165
+ if (load[t] < load[tmin])
1166
+ tmin = t;
1167
+ }
1168
+ load[tmin] += bc[j].cost;
1169
+ pos[j] = tmin;
1170
+ c->psd_tptr[tmin + 1]++;
1171
+ }
1172
+ for (t = 0; t < nt; ++t)
1173
+ c->psd_tptr[t + 1] += c->psd_tptr[t];
1174
+ /* psd_tptr[t] is the fill cursor of thread t, then shift back to list
1175
+ * starts */
1176
+ for (j = 0; j < nblk; ++j)
1177
+ c->psd_blk[c->psd_tptr[pos[j]]++] = bc[j].blk;
1178
+ for (t = nt; t > 0; --t)
1179
+ c->psd_tptr[t] = c->psd_tptr[t - 1];
1180
+ c->psd_tptr[0] = 0;
1181
+ out:
1182
+ if (pos)
1183
+ scs_free(pos);
1184
+ if (load)
1185
+ scs_free(load);
1186
+ if (bc)
1187
+ scs_free(bc);
1188
+ return status;
1189
+ }
1190
+
1191
+ /* Estimated time of the parallel loop with nt threads over nblk blocks, which
1192
+ * take total in all and cmax for the largest. With a lock in the BLAS
1193
+ * (PSD_PAR_LOCK_COST), ncol is the number of columns of these blocks and nmax
1194
+ * that of the largest; without one, both are zero. */
1195
+ static scs_float psd_par_time(scs_int nt, scs_int nblk, scs_float total,
1196
+ scs_float cmax, scs_float ncol, scs_float nmax) {
1197
+ scs_float lock =
1198
+ (nt - 1) * (PSD_PAR_LOCK_COST + (nt - 1) * PSD_PAR_LOCK_COST2);
1199
+ scs_float list =
1200
+ (scs_float)((nblk + nt - 1) / nt) * ((total + lock * ncol) / nblk);
1201
+ return MAX(cmax + lock * nmax, list) + nt * PSD_PAR_THREAD_COST;
1202
+ }
1203
+ #endif
1204
+
1205
+ /* Number of threads for the PSD blocks (see above) */
1206
+ static scs_int psd_num_threads(const ScsCone *k) {
1207
+ #ifdef PSD_PARALLEL
1208
+ scs_int b, t, ntmax, nbig = 0, nmax = 0, ncol = 0;
1209
+ scs_int nt = omp_get_max_threads(), lock = blas_calls_share_lock();
1210
+ scs_float total = 0., cmax = 0., est, best = 0., lcol, lmax;
1211
+ for (b = 0; b < k->ssize + k->cssize; ++b) {
1212
+ scs_int n = psd_block_n(k, b);
1213
+ scs_float cost = psd_block_cost(k, b);
1214
+ total += cost;
1215
+ cmax = MAX(cmax, cost);
1216
+ nmax = MAX(nmax, n);
1217
+ if (n > 1) {
1218
+ nbig++;
1219
+ ncol += n;
1220
+ }
1221
+ }
1222
+ nt = MIN(nt, nbig);
1223
+ if (nt < 2 || total < PSD_PAR_MIN_COST || omp_in_parallel() ||
1224
+ blas_threads_outside_openmp())
1225
+ return 1;
1226
+ if (2 * cmax > total && nmax > PSD_PAR_BLAS_N)
1227
+ return 1;
1228
+ /* With t threads the parallel loop takes about as long as its longest
1229
+ * list, which is at least the largest block and at least its share of the
1230
+ * blocks at their average time, plus the time of the t threads. Find the
1231
+ * fewest threads with the smallest such estimate. Without a lock in the
1232
+ * BLAS, then take the most threads that come within 5% of it: the estimate
1233
+ * is flat where one block bounds the loop, and more threads are then safer
1234
+ * for the lists of the other blocks. With a lock every thread slows the
1235
+ * others down. */
1236
+ lcol = lock ? (scs_float)ncol : 0.;
1237
+ lmax = lock ? (scs_float)nmax : 0.;
1238
+ ntmax = nt;
1239
+ for (t = 2; t <= ntmax; ++t) {
1240
+ est = psd_par_time(t, nbig, total, cmax, lcol, lmax);
1241
+ if (t == 2 || est < best) {
1242
+ best = est;
1243
+ nt = t;
1244
+ }
1245
+ }
1246
+ for (t = ntmax; !lock && t > nt; --t) {
1247
+ if (psd_par_time(t, nbig, total, cmax, lcol, lmax) <= 1.05 * best)
1248
+ nt = t; /* the last pass: t > nt no longer holds */
1249
+ }
1250
+ return total < PSD_PAR_MIN_GAIN * best ? 1 : nt;
1251
+ #else
1252
+ (void)k;
1253
+ return 1;
1254
+ #endif
1255
+ }
1256
+
1257
+ /* Offsets of the PSD blocks and their split over threads */
1258
+ static scs_int set_up_psd_schedule(ScsConeWork *c, const ScsCone *k) {
1259
+ scs_int nblk = k->ssize + k->cssize, nt = psd_num_threads(k), b;
1260
+ c->psd_tptr = (scs_int *)scs_calloc(nt + 1, sizeof(scs_int));
1261
+ c->psd_blk = (scs_int *)scs_calloc(nblk, sizeof(scs_int));
1262
+ c->psd_off = (scs_int *)scs_calloc(nblk, sizeof(scs_int));
1263
+ if (!c->psd_tptr || !c->psd_blk || !c->psd_off)
1264
+ return -1;
1265
+ c->psd_nthreads = nt;
1266
+ c->psd_len = 0;
1267
+ for (b = 0; b < nblk; ++b) {
1268
+ c->psd_off[b] = c->psd_len;
1269
+ c->psd_len += b < k->ssize ? get_sd_cone_size(k->s[b])
1270
+ : get_csd_cone_size(k->cs[b - k->ssize]);
1271
+ c->psd_blk[b] = b;
1272
+ }
1273
+ c->psd_tptr[1] = nblk;
1274
+ #ifdef PSD_PARALLEL
1275
+ if (nt > 1)
1276
+ return assign_psd_blocks(c, k);
1277
+ #endif
1278
+ return 0;
1279
+ }
1280
+
1281
+ #ifdef USE_LAPACK
1282
+ /* Buffers of a workspace for eigenvalues up to n, real matrices up to nr
1283
+ * and complex ones up to nc (none if 0) */
1284
+ static scs_int alloc_psd_bufs(ScsPsdWork *w, blas_int n, blas_int nr,
1285
+ blas_int nc) {
1286
+ w->e = (scs_float *)scs_calloc(n, sizeof(scs_float));
1287
+ w->gamma = (scs_float *)scs_calloc(n, sizeof(scs_float));
1288
+ w->isuppz = (blas_int *)scs_calloc(MAX(2, 2 * n), sizeof(blas_int));
1289
+ if (!w->e || !w->gamma || !w->isuppz)
1290
+ return -1;
1291
+ if (nr > 0) {
1292
+ w->Xs = (scs_float *)scs_calloc(nr * nr, sizeof(scs_float));
1293
+ w->Z = (scs_float *)scs_calloc(nr * nr, sizeof(scs_float));
1294
+ if (!w->Xs || !w->Z)
1295
+ return -1;
1296
+ }
1297
+ if (nc > 0) {
1298
+ w->cXs = (scs_complex_float *)scs_calloc(nc * nc, sizeof(scs_complex_float));
1299
+ w->cZ = (scs_complex_float *)scs_calloc(nc * nc, sizeof(scs_complex_float));
1300
+ if (!w->cXs || !w->cZ)
1301
+ return -1;
1302
+ }
1303
+ return 0;
1304
+ }
1305
+
1306
+ /* LAPACK work arrays, the same sizes in every workspace (see above) */
1307
+ static scs_int alloc_psd_work_arrays(ScsPsdWork *w, blas_int lwork,
1308
+ blas_int liwork, blas_int lcwork) {
1309
+ w->lwork = lwork;
1310
+ w->liwork = liwork;
1311
+ w->lcwork = lcwork;
1312
+ if (lwork > 0 && !(w->work = (scs_float *)scs_calloc(lwork, sizeof(scs_float))))
1313
+ return -1;
1314
+ if (liwork > 0 &&
1315
+ !(w->iwork = (blas_int *)scs_calloc(liwork, sizeof(blas_int))))
1316
+ return -1;
1317
+ if (lcwork > 0 && !(w->cwork = (scs_complex_float *)scs_calloc(
1318
+ lcwork, sizeof(scs_complex_float))))
1319
+ return -1;
1320
+ return 0;
1321
+ }
1322
+
1323
+ /* Workspaces of PSD threads 1, 2, ..., sized for each thread's largest
1324
+ * blocks, with the work-array sizes of psd_tw[0]; none on the serial path.
1325
+ * Each is at most the size of psd_tw[0], so the extra memory is at most
1326
+ * (psd_nthreads - 1) times the serial PSD workspace. */
1327
+ static scs_int set_up_psd_thread_work(ScsConeWork *c, const ScsCone *k) {
1328
+ const ScsPsdWork *w0 = c->psd_tw;
1329
+ scs_int t, j, b;
1330
+ for (t = 1; t < c->psd_nthreads; ++t) {
1331
+ blas_int nr = 0, nc = 0;
1332
+ for (j = c->psd_tptr[t]; j < c->psd_tptr[t + 1]; ++j) {
1333
+ b = c->psd_blk[j];
1334
+ if (b < k->ssize)
1335
+ nr = MAX(nr, (blas_int)k->s[b]);
1336
+ else
1337
+ nc = MAX(nc, (blas_int)k->cs[b - k->ssize]);
1338
+ }
1339
+ if (alloc_psd_bufs(&(c->psd_tw[t]), MAX(MAX(nr, nc), 1), nr > 1 ? nr : 0,
1340
+ nc > 1 ? nc : 0) < 0 ||
1341
+ alloc_psd_work_arrays(&(c->psd_tw[t]), w0->lwork, w0->liwork,
1342
+ nc > 1 ? w0->lcwork : 0) < 0)
1343
+ return -1;
1344
+ }
1345
+ return 0;
1346
+ }
1347
+ #endif
1348
+
858
1349
  /* ======================== Workspace Setup ============================= */
859
1350
 
860
1351
  /*
@@ -863,6 +1354,8 @@ scs_int SCS(validate_cones)(const ScsData *d, const ScsCone *k) {
863
1354
  static scs_int set_up_cone_work_spaces(ScsConeWork *c, const ScsCone *k) {
864
1355
  scs_int i;
865
1356
  #ifdef USE_LAPACK
1357
+ ScsPsdWork *w0; /* the workspace of the matrix cones, and of PSD thread 0 */
1358
+ scs_int real_mats = k->ssize > 0; /* whether Xs and Z are needed */
866
1359
  /* Max dim for eigenvalues (e) and integer work (isuppz) */
867
1360
  blas_int n_max = 1;
868
1361
  blas_int n_max_real = 1; /* Max dim for Real PSD matrix (Xs) */
@@ -881,6 +1374,7 @@ static scs_int set_up_cone_work_spaces(ScsConeWork *c, const ScsCone *k) {
881
1374
  /* Max workspace sizes across all cone types */
882
1375
  blas_int lwork_max = 0;
883
1376
  blas_int liwork_max = 0;
1377
+ blas_int lcwork = 0;
884
1378
 
885
1379
  /* Calculate Dimensions */
886
1380
  for (i = 0; i < k->ssize; ++i) {
@@ -925,34 +1419,29 @@ static scs_int set_up_cone_work_spaces(ScsConeWork *c, const ScsCone *k) {
925
1419
  }
926
1420
 
927
1421
  /* Update Max Real Dims based on Spectral needs */
1422
+ real_mats = real_mats || k->dsize > 0 || k->sl_size > 0;
928
1423
  n_max = MAX(n_max, n_max_logdet);
929
1424
  n_max = MAX(n_max, n_max_sl);
930
1425
  n_max_real = MAX(n_max_real, n_max_logdet);
931
1426
  n_max_real = MAX(n_max_real, n_max_sl);
932
1427
  #endif
933
1428
 
934
- /* Allocate standard eigenvalue buffers
935
- * 'e' stores eigenvalues, 'isuppz' supports them. Shared by Real/Complex.
936
- */
937
- c->e = (scs_float *)scs_calloc(n_max, sizeof(scs_float));
938
- c->psd_gamma = (scs_float *)scs_calloc(n_max, sizeof(scs_float));
939
- c->isuppz = (blas_int *)scs_calloc(MAX(2, 2 * n_max), sizeof(blas_int));
940
- if (!c->e || !c->isuppz)
1429
+ /* Allocate the buffers: 'e' stores eigenvalues, 'isuppz' supports them
1430
+ * (shared by Real/Complex), Xs and Z for real matrices, cXs and cZ for
1431
+ * complex ones */
1432
+ c->psd_tw = (ScsPsdWork *)scs_calloc(MAX(c->psd_nthreads, 1),
1433
+ sizeof(ScsPsdWork));
1434
+ if (!c->psd_tw)
1435
+ return -1;
1436
+ w0 = c->psd_tw;
1437
+ if (alloc_psd_bufs(w0, n_max, real_mats ? n_max_real : 0,
1438
+ k->cssize > 0 ? n_max_csd : 0) < 0)
941
1439
  return -1;
942
1440
 
943
1441
  /* 1. Real PSD Workspace Query (syevr) */
944
- if (k->ssize > 0
945
- #ifdef USE_SPECTRAL_CONES
946
- || k->dsize > 0 || k->sl_size > 0
947
- #endif
948
- ) {
949
- c->Xs = (scs_float *)scs_calloc(n_max_real * n_max_real, sizeof(scs_float));
950
- c->Z = (scs_float *)scs_calloc(n_max_real * n_max_real, sizeof(scs_float));
951
- if (!c->Xs || !c->Z)
952
- return -1;
953
-
954
- BLAS(syevr)("V", "A", "L", &n_max_real, c->Xs, &n_max_real, &d_f, &d_f,
955
- &d_i, &d_i, &abstol, &m, c->e, c->Z, &n_max_real, c->isuppz,
1442
+ if (real_mats) {
1443
+ BLAS(syevr)("V", "A", "L", &n_max_real, w0->Xs, &n_max_real, &d_f, &d_f,
1444
+ &d_i, &d_i, &abstol, &m, w0->e, w0->Z, &n_max_real, w0->isuppz,
956
1445
  &wkopt, &neg_one, &iwkopt, &neg_one, &info);
957
1446
 
958
1447
  if (info != 0) {
@@ -964,18 +1453,40 @@ static scs_int set_up_cone_work_spaces(ScsConeWork *c, const ScsCone *k) {
964
1453
  liwork_max = MAX(liwork_max, iwkopt);
965
1454
  }
966
1455
 
967
- /* 2. Complex PSD Workspace Query (heevr) */
968
- if (k->cssize > 0) {
969
- c->cXs = (scs_complex_float *)scs_calloc(n_max_csd * n_max_csd,
970
- sizeof(scs_complex_float));
971
- c->cZ = (scs_complex_float *)scs_calloc(n_max_csd * n_max_csd,
972
- sizeof(scs_complex_float));
973
- if (!c->cXs || !c->cZ)
1456
+ #ifdef USE_SPECTRAL_CONES
1457
+ /* 1b. Spectral Workspace Query (syev)
1458
+ *
1459
+ * The logdet and sum-of-largest projections eigendecompose with syev, not
1460
+ * syevr, but share the consolidated 'work' array sized above. Sizing that
1461
+ * array from the syevr query alone shortchanges syev: both LAPACKs we
1462
+ * measured (Apple Accelerate and netlib) report an optimum of 34n for
1463
+ * syev against 33n for syevr, so every spectral syev call was running
1464
+ * below its optimum. That stays above syev's *minimum* of 3n-1, so it
1465
+ * costs block size rather than correctness, but a vendor whose optimum
1466
+ * and minimum are closer together would push it under the minimum and
1467
+ * syev would start failing with info = -8 deep inside a projection.
1468
+ * Query syev too and keep whichever workspace is larger.
1469
+ */
1470
+ if (k->dsize > 0 || k->sl_size > 0) {
1471
+ blas_int n_max_spectral = MAX(n_max_logdet, n_max_sl);
1472
+
1473
+ BLAS(syev)("V", "L", &n_max_spectral, w0->Xs, &n_max_spectral, w0->e, &wkopt,
1474
+ &neg_one, &info);
1475
+
1476
+ if (info != 0) {
1477
+ scs_printf("FATAL: syev workspace query failure, info = %li\n",
1478
+ (long)info);
974
1479
  return -1;
1480
+ }
1481
+ lwork_max = MAX(lwork_max, (blas_int)(wkopt + 1));
1482
+ }
1483
+ #endif
975
1484
 
976
- BLASC(heevr)("V", "A", "L", &n_max_csd, SCS_BLAS_COMPLEX_CAST(c->cXs),
977
- &n_max_csd, &d_f, &d_f, &d_i, &d_i, &abstol, &m, c->e,
978
- SCS_BLAS_COMPLEX_CAST(c->cZ), &n_max_csd, c->isuppz,
1485
+ /* 2. Complex PSD Workspace Query (heevr) */
1486
+ if (k->cssize > 0) {
1487
+ BLASC(heevr)("V", "A", "L", &n_max_csd, SCS_BLAS_COMPLEX_CAST(w0->cXs),
1488
+ &n_max_csd, &d_f, &d_f, &d_i, &d_i, &abstol, &m, w0->e,
1489
+ SCS_BLAS_COMPLEX_CAST(w0->cZ), &n_max_csd, w0->isuppz,
979
1490
  SCS_BLAS_COMPLEX_CAST(&lcwork_opt), &neg_one, &lrwork_opt,
980
1491
  &neg_one, &liwork_opt_c, &neg_one, &info);
981
1492
 
@@ -985,11 +1496,7 @@ static scs_int set_up_cone_work_spaces(ScsConeWork *c, const ScsCone *k) {
985
1496
  return -1;
986
1497
  }
987
1498
 
988
- c->lcwork = (blas_int)(lcwork_opt[0]);
989
- c->cwork =
990
- (scs_complex_float *)scs_calloc(c->lcwork, sizeof(scs_complex_float));
991
- if (!c->cwork)
992
- return -1;
1499
+ lcwork = (blas_int)(lcwork_opt[0]);
993
1500
 
994
1501
  /* heevr uses a real 'rwork' array. We alias this to the shared real 'work'
995
1502
  * array */
@@ -1028,22 +1535,13 @@ static scs_int set_up_cone_work_spaces(ScsConeWork *c, const ScsCone *k) {
1028
1535
  #endif
1029
1536
 
1030
1537
  /* Final Consolidated Allocation
1031
- * c->work aliases 'work' for syevr and 'rwork' for heevr
1538
+ * w0->work aliases 'work' for syevr and 'rwork' for heevr
1032
1539
  */
1033
- if (lwork_max > 0) {
1034
- c->lwork = lwork_max;
1035
- c->work = (scs_float *)scs_calloc(c->lwork, sizeof(scs_float));
1036
- if (!c->work)
1037
- return -1;
1038
- }
1039
- if (liwork_max > 0) {
1040
- c->liwork = liwork_max;
1041
- c->iwork = (blas_int *)scs_calloc(c->liwork, sizeof(blas_int));
1042
- if (!c->iwork)
1043
- return -1;
1044
- }
1540
+ if (alloc_psd_work_arrays(w0, lwork_max, liwork_max, lcwork) < 0)
1541
+ return -1;
1045
1542
 
1046
- return 0;
1543
+ /* the other PSD threads */
1544
+ return set_up_psd_thread_work(c, k);
1047
1545
  #else
1048
1546
  /* Non-LAPACK fallback check */
1049
1547
  if (k->ssize > 0 || k->cssize > 0) {
@@ -1099,7 +1597,7 @@ static scs_int set_up_ell1_cone_work_space(ScsConeWork *c, const ScsCone *k) {
1099
1597
  * of the cone makes any uniform factor in rho drop out, so the
1100
1598
  * historical scalar-per-block case reduces to the unscaled path. */
1101
1599
  static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1102
- ScsConeWork *c,
1600
+ ScsPsdWork *w,
1103
1601
  const scs_float *rho) {
1104
1602
  scs_int scaled = 0;
1105
1603
  if (n == 0)
@@ -1121,7 +1619,7 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1121
1619
 
1122
1620
  /* Diagonal-congruence pre-scale (skip when the metric is uniform on
1123
1621
  * this block so the historical path stays bit-identical). */
1124
- if (rho && c->psd_gamma) {
1622
+ if (rho) {
1125
1623
  scs_float wmin = rho[0], wmax = rho[0];
1126
1624
  for (i = 1; i < n; ++i) {
1127
1625
  scs_float wii = rho[i * n - ((i - 1) * i) / 2];
@@ -1132,13 +1630,13 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1132
1630
  scs_int jj, ii;
1133
1631
  scaled = 1;
1134
1632
  for (i = 0; i < n; ++i) {
1135
- c->psd_gamma[i] = POWF(rho[i * n - ((i - 1) * i) / 2], -0.25);
1633
+ w->gamma[i] = POWF(rho[i * n - ((i - 1) * i) / 2], -0.25);
1136
1634
  }
1137
1635
  for (jj = 0; jj < n; ++jj) {
1138
- scs_float gj = c->psd_gamma[jj];
1636
+ scs_float gj = w->gamma[jj];
1139
1637
  scs_float *col = &(X[jj * n - ((jj - 1) * jj) / 2]);
1140
1638
  for (ii = jj; ii < n; ++ii) {
1141
- col[ii - jj] *= c->psd_gamma[ii] * gj;
1639
+ col[ii - jj] *= w->gamma[ii] * gj;
1142
1640
  }
1143
1641
  }
1144
1642
  }
@@ -1146,29 +1644,32 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1146
1644
 
1147
1645
  /* Copy lower triangular part to full matrix buffer Xs */
1148
1646
  for (i = 0; i < n; ++i) {
1149
- memcpy(&(c->Xs[i * (n + 1)]), &(X[i * n - ((i - 1) * i) / 2]),
1647
+ memcpy(&(w->Xs[i * (n + 1)]), &(X[i * n - ((i - 1) * i) / 2]),
1150
1648
  (n - i) * sizeof(scs_float));
1151
1649
  }
1152
1650
 
1153
1651
  /* Scale diagonals by sqrt(2) */
1154
- BLAS(scal)(&nb, &sqrt2, c->Xs, &nb_p1);
1652
+ BLAS(scal)(&nb, &sqrt2, w->Xs, &nb_p1);
1155
1653
 
1156
1654
  /* Solve Eigenproblem: Xs = Z * diag(e) * Z' */
1157
- BLAS(syevr)("V", "A", "L", &nb, c->Xs, &nb, &d_f, &d_f, &d_i, &d_i, &abstol,
1158
- &m, c->e, c->Z, &nb, c->isuppz, c->work, &c->lwork, c->iwork,
1159
- &c->liwork, &info);
1655
+ BLAS(syevr)("V", "A", "L", &nb, w->Xs, &nb, &d_f, &d_f, &d_i, &d_i, &abstol,
1656
+ &m, w->e, w->Z, &nb, w->isuppz, w->work, &w->lwork, w->iwork,
1657
+ &w->liwork, &info);
1658
+ /* info < 0: bad argument. info > 0: no convergence. Either way there is no
1659
+ * eigendecomposition to project with, so fail (the caller reports it: this
1660
+ * may run in a PSD thread, which must not print). */
1160
1661
  if (info != 0)
1161
- return (int)info;
1662
+ return -1;
1162
1663
 
1163
1664
  /* Filter negative eigenvalues and scale eigenvectors */
1164
1665
  /* Note: e is in ascending order */
1165
1666
  for (i = 0; i < n; ++i) {
1166
- if (c->e[i] > 0) {
1667
+ if (w->e[i] > 0) {
1167
1668
  if (first_idx == -1) {
1168
1669
  first_idx = i;
1169
1670
  }
1170
- sq_eig = SQRTF(c->e[i]);
1171
- BLAS(scal)(&nb, &sq_eig, &c->Z[i * n], &one_int);
1671
+ sq_eig = SQRTF(w->e[i]);
1672
+ BLAS(scal)(&nb, &sq_eig, &w->Z[i * n], &one_int);
1172
1673
  }
1173
1674
  }
1174
1675
  if (first_idx == -1) {
@@ -1178,15 +1679,15 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1178
1679
  }
1179
1680
  /* Reconstruct Xs = Z * Z' */
1180
1681
  ncols_z = (blas_int)(n - first_idx);
1181
- BLAS(syrk)("Lower", "NoTrans", &nb, &ncols_z, &one, &c->Z[first_idx * n], &nb,
1182
- &zero, c->Xs, &nb);
1682
+ BLAS(syrk)("Lower", "NoTrans", &nb, &ncols_z, &one, &w->Z[first_idx * n], &nb,
1683
+ &zero, w->Xs, &nb);
1183
1684
 
1184
1685
  /* Rescale diagonals by 1/sqrt(2) */
1185
- BLAS(scal)(&nb, &sqrt2_inv, c->Xs, &nb_p1);
1686
+ BLAS(scal)(&nb, &sqrt2_inv, w->Xs, &nb_p1);
1186
1687
 
1187
1688
  /* Extract lower triangular matrix back to X */
1188
1689
  for (i = 0; i < n; ++i) {
1189
- memcpy(&(X[i * n - ((i - 1) * i) / 2]), &(c->Xs[i * (n + 1)]),
1690
+ memcpy(&(X[i * n - ((i - 1) * i) / 2]), &(w->Xs[i * (n + 1)]),
1190
1691
  (n - i) * sizeof(scs_float));
1191
1692
  }
1192
1693
 
@@ -1194,10 +1695,10 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1194
1695
  if (scaled) {
1195
1696
  scs_int jj, ii;
1196
1697
  for (jj = 0; jj < n; ++jj) {
1197
- scs_float gj = c->psd_gamma[jj];
1698
+ scs_float gj = w->gamma[jj];
1198
1699
  scs_float *col = &(X[jj * n - ((jj - 1) * jj) / 2]);
1199
1700
  for (ii = jj; ii < n; ++ii) {
1200
- col[ii - jj] /= c->psd_gamma[ii] * gj;
1701
+ col[ii - jj] /= w->gamma[ii] * gj;
1201
1702
  }
1202
1703
  }
1203
1704
  }
@@ -1211,7 +1712,7 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
1211
1712
  * Projection: Complex Semi-Definite Cone
1212
1713
  */
1213
1714
  static scs_int proj_complex_semi_definite_cone(scs_float *X, const scs_int n,
1214
- ScsConeWork *c) {
1715
+ ScsPsdWork *w) {
1215
1716
  if (n == 0)
1216
1717
  return 0;
1217
1718
  if (n == 1) {
@@ -1235,36 +1736,37 @@ static scs_int proj_complex_semi_definite_cone(scs_float *X, const scs_int n,
1235
1736
 
1236
1737
  /* Unpack X (real array) into cXs (complex matrix) */
1237
1738
  for (i = 0; i < n - 1; ++i) {
1238
- c->cXs[i * (n + 1)][0] = X[i * (2 * n - i)]; /* Diagonal (Real) */
1239
- c->cXs[i * (n + 1)][1] = 0.0;
1240
- memcpy(&(c->cXs[i * (n + 1) + 1]), &(X[i * (2 * n - i) + 1]),
1739
+ w->cXs[i * (n + 1)][0] = X[i * (2 * n - i)]; /* Diagonal (Real) */
1740
+ w->cXs[i * (n + 1)][1] = 0.0;
1741
+ memcpy(&(w->cXs[i * (n + 1) + 1]), &(X[i * (2 * n - i) + 1]),
1241
1742
  2 * (n - i - 1) * sizeof(scs_float));
1242
1743
  }
1243
- c->cXs[n * n - 1][0] = X[n * n - 1]; /* Last element */
1244
- c->cXs[n * n - 1][1] = 0.0;
1744
+ w->cXs[n * n - 1][0] = X[n * n - 1]; /* Last element */
1745
+ w->cXs[n * n - 1][1] = 0.0;
1245
1746
 
1246
1747
  /* Scale diagonals by sqrt*/
1247
1748
  BLASC(scal)(&nb, SCS_BLAS_COMPLEX_CAST(&csqrt2),
1248
- SCS_BLAS_COMPLEX_CAST(c->cXs), &nb_p1);
1249
-
1250
- /* Solve Eigenproblem. Note: c->work acts as rwork here. */
1251
- BLASC(heevr)("V", "A", "L", &nb, SCS_BLAS_COMPLEX_CAST(c->cXs), &nb, &d_f,
1252
- &d_f, &d_i, &d_i, &abstol, &m, c->e,
1253
- SCS_BLAS_COMPLEX_CAST(c->cZ), &nb, c->isuppz,
1254
- SCS_BLAS_COMPLEX_CAST(c->cwork), &c->lcwork, c->work, &c->lwork,
1255
- c->iwork, &c->liwork, &info);
1749
+ SCS_BLAS_COMPLEX_CAST(w->cXs), &nb_p1);
1750
+
1751
+ /* Solve Eigenproblem. Note: w->work acts as rwork here. */
1752
+ BLASC(heevr)("V", "A", "L", &nb, SCS_BLAS_COMPLEX_CAST(w->cXs), &nb, &d_f,
1753
+ &d_f, &d_i, &d_i, &abstol, &m, w->e,
1754
+ SCS_BLAS_COMPLEX_CAST(w->cZ), &nb, w->isuppz,
1755
+ SCS_BLAS_COMPLEX_CAST(w->cwork), &w->lcwork, w->work, &w->lwork,
1756
+ w->iwork, &w->liwork, &info);
1757
+ /* info != 0: no eigendecomposition, so fail (see the real case) */
1256
1758
  if (info != 0)
1257
- return (int)info;
1759
+ return -1;
1258
1760
 
1259
1761
  /* Reconstruct */
1260
1762
  for (i = 0; i < n; ++i) {
1261
- if (c->e[i] > 0) {
1763
+ if (w->e[i] > 0) {
1262
1764
  if (first_idx == -1) {
1263
1765
  first_idx = i;
1264
1766
  }
1265
- csq_eig[0] = SQRTF(c->e[i]);
1767
+ csq_eig[0] = SQRTF(w->e[i]);
1266
1768
  BLASC(scal)(&nb, SCS_BLAS_COMPLEX_CAST(&csq_eig),
1267
- SCS_BLAS_COMPLEX_CAST(&c->cZ[i * n]), &one_int);
1769
+ SCS_BLAS_COMPLEX_CAST(&w->cZ[i * n]), &one_int);
1268
1770
  }
1269
1771
  }
1270
1772
  if (first_idx == -1) {
@@ -1276,20 +1778,20 @@ static scs_int proj_complex_semi_definite_cone(scs_float *X, const scs_int n,
1276
1778
  /* cXs = cZ * cZ' */
1277
1779
  ncols_z = (blas_int)(n - first_idx);
1278
1780
  BLASC(herk)("Lower", "NoTrans", &nb, &ncols_z, &one,
1279
- SCS_BLAS_COMPLEX_CAST(&c->cZ[first_idx * n]), &nb, &zero,
1280
- SCS_BLAS_COMPLEX_CAST(c->cXs), &nb);
1781
+ SCS_BLAS_COMPLEX_CAST(&w->cZ[first_idx * n]), &nb, &zero,
1782
+ SCS_BLAS_COMPLEX_CAST(w->cXs), &nb);
1281
1783
 
1282
1784
  /* Rescale diagonals */
1283
1785
  BLASC(scal)(&nb, SCS_BLAS_COMPLEX_CAST(&csqrt2_inv),
1284
- SCS_BLAS_COMPLEX_CAST(c->cXs), &nb_p1);
1786
+ SCS_BLAS_COMPLEX_CAST(w->cXs), &nb_p1);
1285
1787
 
1286
1788
  /* Repack into X */
1287
1789
  for (i = 0; i < n - 1; ++i) {
1288
- X[i * (2 * n - i)] = c->cXs[i * (n + 1)][0];
1289
- memcpy(&(X[i * (2 * n - i) + 1]), &(c->cXs[i * (n + 1) + 1]),
1790
+ X[i * (2 * n - i)] = w->cXs[i * (n + 1)][0];
1791
+ memcpy(&(X[i * (2 * n - i) + 1]), &(w->cXs[i * (n + 1) + 1]),
1290
1792
  2 * (n - i - 1) * sizeof(scs_float));
1291
1793
  }
1292
- X[n * n - 1] = c->cXs[n * n - 1][0];
1794
+ X[n * n - 1] = w->cXs[n * n - 1][0];
1293
1795
  return 0;
1294
1796
  #else
1295
1797
  return -1;
@@ -1369,7 +1871,7 @@ static scs_float proj_box_cone(scs_float *tx, const scs_float *bl,
1369
1871
  * should) if ht is very large, which can happen with some pathological
1370
1872
  * problems.
1371
1873
  */
1372
- if (ABS(gt / MAX(ht, 1e-6)) < 1e-12 * MAX(t, 1.) ||
1874
+ if (ABS(gt / MAX(ht, (scs_float)1e-6)) < 1e-12 * MAX(t, 1.) ||
1373
1875
  ABS(t - t_prev) < 1e-11 * MAX(t, 1.))
1374
1876
  break;
1375
1877
  }
@@ -1424,18 +1926,37 @@ static void proj_soc(scs_float *x, scs_int q) {
1424
1926
  */
1425
1927
  static scs_float pow_calc_x(scs_float r, scs_float xh, scs_float rh,
1426
1928
  scs_float a) {
1427
- scs_float x = 0.5 * (xh + SQRTF(xh * xh + 4 * a * (rh - r) * r));
1428
- return MAX(x, 1e-12);
1929
+ /* Positive root of x^2 - xh * x - a * (rh - r) * r = 0, in a form that
1930
+ * avoids cancellation when xh < 0. */
1931
+ scs_float d = 4 * a * (rh - r) * r, s = SQRTF(xh * xh + d);
1932
+ return xh >= 0 ? 0.5 * (xh + s) : 0.5 * d / (s - xh);
1429
1933
  }
1430
1934
 
1431
1935
  static void proj_power_cone(scs_float *v, scs_float a) {
1432
1936
  scs_float xh = v[0], yh = v[1], rh = ABS(v[2]);
1433
- scs_float x = 0.0, y = 0.0, r;
1434
- scs_int i;
1937
+ scs_float x = 0.0, y = 0.0, r, lo, hi;
1938
+ scs_float f_lo, f_hi; /* f at the bracket ends, for regula falsi */
1939
+ scs_float phi0; /* phi(0) = phi(rh) = max(xh, 0)^a * max(yh, 0)^(1-a) */
1940
+ /* Projection onto a cone is positively homogeneous, so work on v scaled
1941
+ * to unit inf-norm; this makes the tolerances below relative to |v|. */
1942
+ scs_float nrm = MAX(MAX(ABS(xh), ABS(yh)), rh);
1943
+ scs_int i, side = 0;
1944
+
1945
+ if (nrm <= 0)
1946
+ return;
1947
+ xh /= nrm;
1948
+ yh /= nrm;
1949
+ rh /= nrm;
1950
+
1951
+ if (xh >= 0 && yh >= 0)
1952
+ phi0 = POWF(xh, a) * POWF(yh, (1 - a));
1953
+ else if (a > 0 && a < 1)
1954
+ phi0 = 0;
1955
+ else
1956
+ phi0 = POWF(MAX(xh, 0), a) * POWF(MAX(yh, 0), (1 - a));
1435
1957
 
1436
1958
  /* Check v membership in K_a */
1437
- if (xh >= 0 && yh >= 0 &&
1438
- POW_CONE_TOL + POWF(xh, a) * POWF(yh, (1 - a)) >= rh)
1959
+ if (xh >= 0 && yh >= 0 && POW_CONE_TOL + phi0 >= rh)
1439
1960
  return;
1440
1961
 
1441
1962
  /* Check -v membership in Polar K_a^* */
@@ -1446,9 +1967,41 @@ static void proj_power_cone(scs_float *v, scs_float a) {
1446
1967
  return;
1447
1968
  }
1448
1969
 
1970
+ /* Find the root r in (0, rh) of f(r) = x(r)^a * y(r)^(1-a) - r. The first
1971
+ * term is concave in r, so f > 0 left of the root and f < 0 right of it,
1972
+ * which gives a bracket [lo, hi] that safeguards the Newton steps. f_lo
1973
+ * and f_hi are f at the bracket ends, f(0) = phi(0) and f(rh) = phi(rh) - rh
1974
+ * to start, for the regula falsi fallback. */
1975
+ lo = 0;
1976
+ hi = rh;
1977
+ f_lo = phi0;
1978
+ f_hi = phi0 - rh;
1449
1979
  r = rh / 2;
1980
+ /* With exactly one negative component, for small r x(r) ~ a * rh * r / |xh|
1981
+ * (or the same for y), and phi(r) = r then gives the root directly. This is
1982
+ * the common case for ADMM iterates, and its root is often tiny, which
1983
+ * Newton from rh / 2 reaches only slowly. */
1984
+ if (xh < 0 && yh > 0) {
1985
+ scs_float r0 = POWF(a * rh / (-xh), a / (1 - a)) * yh;
1986
+ if (r0 > 0 && r0 < rh / 2)
1987
+ r = r0;
1988
+ } else if (yh < 0 && xh > 0) {
1989
+ scs_float r0 = POWF((1 - a) * rh / (-yh), (1 - a) / a) * xh;
1990
+ if (r0 > 0 && r0 < rh / 2)
1991
+ r = r0;
1992
+ }
1993
+ /* Points just outside the cone (e.g. converging iterates) have their root
1994
+ * near rh; starting there avoids Newton overshooting from the left into
1995
+ * bisection. Only do this when the point is close to the cone,
1996
+ * phi(rh) >= rh / 2, and the slopes dx/dr = -a * rh / xh and
1997
+ * dy/dr = -(1 - a) * rh / yh at rh are moderate: otherwise the first step
1998
+ * is too small to represent and looks converged. */
1999
+ if (xh > 0 && yh > 0 && phi0 >= rh / 2 &&
2000
+ a * rh <= POW_CONE_START_SLOPE * xh &&
2001
+ (1 - a) * rh <= POW_CONE_START_SLOPE * yh)
2002
+ r = rh;
1450
2003
  for (i = 0; i < POW_CONE_MAX_ITERS; ++i) {
1451
- scs_float f, fp, dxdr, dydr, xa, y1a;
2004
+ scs_float f, fp, dxdr, dydr, xa, y1a, r_new, r_g, sens;
1452
2005
  x = pow_calc_x(r, xh, rh, a);
1453
2006
  y = pow_calc_x(r, yh, rh, 1 - a);
1454
2007
 
@@ -1457,19 +2010,130 @@ static void proj_power_cone(scs_float *v, scs_float a) {
1457
2010
  xa = POWF(x, a);
1458
2011
  y1a = POWF(y, (1 - a));
1459
2012
  f = xa * y1a - r;
1460
- if (ABS(f) < POW_CONE_TOL)
1461
- break;
2013
+ /* Illinois: halve the f value kept at an end that was not just moved
2014
+ * twice in a row, so regula falsi does not stagnate. */
2015
+ if (f > 0) {
2016
+ lo = r;
2017
+ f_lo = f;
2018
+ if (side > 0)
2019
+ f_hi *= 0.5;
2020
+ side = 1;
2021
+ } else {
2022
+ hi = r;
2023
+ f_hi = f;
2024
+ if (side < 0)
2025
+ f_lo *= 0.5;
2026
+ side = -1;
2027
+ }
1462
2028
 
1463
2029
  dxdr = a * (rh - 2 * r) / (2 * x - xh);
1464
2030
  dydr = (1 - a) * (rh - 2 * r) / (2 * y - yh);
1465
2031
  fp = xa * y1a * (a * dxdr / x + (1 - a) * dydr / y) - 1;
1466
2032
 
1467
- r = MAX(r - f / fp, 0);
1468
- r = MIN(r, rh);
2033
+ r_new = r - f / fp;
2034
+ if (f < 0) {
2035
+ /* Near the polar boundary f is nearly flat around a double root and
2036
+ * Newton on f converges only linearly; Newton on f(r) / r, which
2037
+ * removes the trivial root at 0, converges much faster there. It is
2038
+ * also taken elsewhere: restricting it saved a few projection
2039
+ * evaluations on some inputs but made whole solves worse. */
2040
+ r_g = r - r * f / (r * fp - f);
2041
+ if (r_g > lo && r_g < hi)
2042
+ r_new = r_g;
2043
+ }
2044
+ /* Stop when the step is small relative to r and moves x, y and r by at
2045
+ * most POW_CONE_TOL (relative to |v| after normalization). Stop on the
2046
+ * step rather than on |f|: a small |f| does not imply r is accurate when
2047
+ * f is flat, and near the apex x can be far more sensitive to r than r
2048
+ * is. r is an end of the bracket here, so a converged step may land
2049
+ * exactly on it. The relative test cannot be met when the root is at or
2050
+ * near 0, so also stop once the bracket is that narrow, or when it cannot
2051
+ * shrink any further in the working precision. */
2052
+ sens = MAX(1, MAX(ABS(dxdr), ABS(dydr)));
2053
+ if (ABS(r_new - r) <= POW_CONE_TOL * r &&
2054
+ ABS(r_new - r) * sens <= POW_CONE_TOL && r_new >= lo && r_new <= hi)
2055
+ break;
2056
+ /* If the Newton step leaves the bracket (from the left of a concave f it
2057
+ * overshoots), fall back to regula falsi, then to bisection, which also
2058
+ * catches NaN. Bisection alone is very slow when the root is within a few
2059
+ * ulps of rh, e.g. a point just outside the cone with a tiny component. */
2060
+ if (!(r_new > lo && r_new < hi))
2061
+ r_new = (lo * f_hi - hi * f_lo) / (f_hi - f_lo);
2062
+ if (!(r_new > lo && r_new < hi))
2063
+ r_new = 0.5 * (lo + hi);
2064
+ if ((hi - lo) * sens <= POW_CONE_TOL || hi - lo <= 4 * POW_CONE_EPS * hi)
2065
+ break;
2066
+ r = r_new;
2067
+ }
2068
+ v[0] = x * nrm;
2069
+ v[1] = y * nrm;
2070
+ v[2] = ((v[2] < 0) ? -r : r) * nrm;
2071
+ }
2072
+
2073
+ /* ===================== PSD Blocks, all Threads ======================== */
2074
+
2075
+ /* Project the blocks of lists t, t + dt, t + 2 dt, ..., each list with its own
2076
+ * workspace: all of them in order for t = 0 and dt = 1, the share of thread t
2077
+ * in a team of dt otherwise. x and rho as in proj_psd_cones. */
2078
+ static scs_int proj_psd_lists(scs_float *x, ScsConeWork *c,
2079
+ const scs_float *rho, scs_int t, scs_int dt) {
2080
+ const ScsCone *k = c->k;
2081
+ scs_int j, b, st, status = 0;
2082
+ for (; t < c->psd_nthreads; t += dt) {
2083
+ ScsPsdWork *w = SCS_NULL; /* no workspace without LAPACK */
2084
+ #ifdef USE_LAPACK
2085
+ w = &(c->psd_tw[t]);
2086
+ #endif
2087
+ for (j = c->psd_tptr[t]; j < c->psd_tptr[t + 1]; ++j) {
2088
+ b = c->psd_blk[j];
2089
+ if (b < k->ssize) {
2090
+ st = proj_semi_definite_cone(&(x[c->psd_off[b]]), k->s[b], w,
2091
+ rho ? &(rho[c->psd_off[b]]) : SCS_NULL);
2092
+ } else {
2093
+ st = proj_complex_semi_definite_cone(&(x[c->psd_off[b]]),
2094
+ k->cs[b - k->ssize], w);
2095
+ }
2096
+ if (st < 0)
2097
+ status = st;
2098
+ }
2099
+ }
2100
+ return status;
2101
+ }
2102
+
2103
+ /* Project the PSD blocks x (from the first PSD entry); rho is the matching
2104
+ * part of r_y or NULL. */
2105
+ static scs_int proj_psd_cones(scs_float *x, ScsConeWork *c,
2106
+ const scs_float *rho) {
2107
+ #ifdef PSD_PARALLEL
2108
+ /* the threads of the split, or fewer if the OpenMP thread count has been
2109
+ * lowered since setup */
2110
+ int nt = omp_get_max_threads();
2111
+ if ((scs_int)nt > c->psd_nthreads)
2112
+ nt = (int)c->psd_nthreads;
2113
+ if (nt > 1) {
2114
+ scs_int status = 0;
2115
+ #pragma omp parallel num_threads(nt)
2116
+ {
2117
+ /* thread t of a team of dt, which may be smaller than asked for (e.g.
2118
+ * nested), projects lists t, t + dt, ...: every list is covered. The
2119
+ * BLAS has to run on one thread in here (see the comment on the option
2120
+ * above). SCS can only see to that for MKL: one MKL thread meanwhile,
2121
+ * then back to the setting before. */
2122
+ int mkl_nt = mkl_set_local_threads(1);
2123
+ scs_int st = proj_psd_lists(x, c, rho, omp_get_thread_num(),
2124
+ omp_get_num_threads());
2125
+ mkl_set_local_threads(mkl_nt);
2126
+ if (st < 0) {
2127
+ #pragma omp critical(scs_psd_status)
2128
+ status = st;
2129
+ }
2130
+ }
2131
+ return status;
1469
2132
  }
1470
- v[0] = x;
1471
- v[1] = y;
1472
- v[2] = (v[2] < 0) ? -r : r;
2133
+ #endif
2134
+ /* no parallel region: the BLAS is free to use its own threads, as in a
2135
+ * build without USE_PARALLEL_PSD */
2136
+ return proj_psd_lists(x, c, rho, 0, 1);
1473
2137
  }
1474
2138
 
1475
2139
  /* ================ Main Cone Projection Dispatch ====================== */
@@ -1517,32 +2181,19 @@ static scs_int proj_cone(scs_float *x, const ScsCone *k, ScsConeWork *c,
1517
2181
  }
1518
2182
  }
1519
2183
 
1520
- /* 5. PSD Cone */
1521
- if (k->ssize) {
1522
- for (i = 0; i < k->ssize; ++i) {
2184
+ /* 5-6. PSD and Complex PSD Cones */
2185
+ if (c->psd_nthreads > 0) {
1523
2186
  #ifdef USE_SPECTRAL_CONES
1524
- SPECTRAL_TIMING(SCS(tic)(&spec_mat_proj_timer);)
2187
+ SPECTRAL_TIMING(SCS(tic)(&spec_mat_proj_timer);)
1525
2188
  #endif
1526
- status = proj_semi_definite_cone(&(x[count]), k->s[i], c,
1527
- r_y ? &(r_y[count]) : SCS_NULL);
2189
+ status = proj_psd_cones(&(x[count]), c, r_y ? &(r_y[count]) : SCS_NULL);
1528
2190
  #ifdef USE_SPECTRAL_CONES
1529
- SPECTRAL_TIMING(c->tot_time_mat_cone_proj +=
1530
- SCS(tocq)(&spec_mat_proj_timer);)
2191
+ SPECTRAL_TIMING(c->tot_time_mat_cone_proj +=
2192
+ SCS(tocq)(&spec_mat_proj_timer);)
1531
2193
  #endif
1532
- if (status < 0)
1533
- return status;
1534
- count += get_sd_cone_size(k->s[i]);
1535
- }
1536
- }
1537
-
1538
- /* 6. Complex PSD Cone */
1539
- if (k->cssize) {
1540
- for (i = 0; i < k->cssize; ++i) {
1541
- status = proj_complex_semi_definite_cone(&(x[count]), k->cs[i], c);
1542
- if (status < 0)
1543
- return status;
1544
- count += get_csd_cone_size(k->cs[i]);
1545
- }
2194
+ if (status < 0)
2195
+ return status;
2196
+ count += c->psd_len;
1546
2197
  }
1547
2198
 
1548
2199
  if (k->ep || k->ed) { /* doesn't use r_y */
@@ -1655,6 +2306,14 @@ ScsConeWork *SCS(init_cone)(ScsCone *k, scs_int m) {
1655
2306
  (scs_float *)scs_calloc(k->bsize - 1, sizeof(scs_float));
1656
2307
  }
1657
2308
 
2309
+ /* PSD blocks: offsets and threads (the workspaces are set up below) */
2310
+ if ((k->ssize && k->s) || (k->cssize && k->cs)) {
2311
+ if (set_up_psd_schedule(c, k) < 0) {
2312
+ SCS(finish_cone)(c);
2313
+ return SCS_NULL;
2314
+ }
2315
+ }
2316
+
1658
2317
  /* Set up workspaces if matrix cones are present */
1659
2318
  if ((k->ssize && k->s) || (k->cssize && k->cs)
1660
2319
  #ifdef USE_SPECTRAL_CONES
@@ -1666,7 +2325,6 @@ ScsConeWork *SCS(init_cone)(ScsCone *k, scs_int m) {
1666
2325
  return SCS_NULL;
1667
2326
  }
1668
2327
  }
1669
-
1670
2328
  #ifdef USE_SPECTRAL_CONES
1671
2329
  if (k->ell1_size > 0 && k->ell1) {
1672
2330
  if (set_up_ell1_cone_work_space(c, k) < 0) {
@@ -1679,6 +2337,36 @@ ScsConeWork *SCS(init_cone)(ScsCone *k, scs_int m) {
1679
2337
  return c;
1680
2338
  }
1681
2339
 
2340
+ /* Discard inner-solver state that was cached under a previous metric.
2341
+ *
2342
+ * The standard cones project in closed form, so their projections are a
2343
+ * function of the point and the current metric alone. The spectral cones
2344
+ * are not: proj_logdet_cone runs a Newton/IPM inner solve and warm-starts
2345
+ * Newton from the projection it produced on the previous outer iteration.
2346
+ * That saved iterate belongs to the metric that produced it -- once diag_r
2347
+ * changes, the point being projected jumps, and the stale iterate is no
2348
+ * longer a good (nor necessarily a domain-interior) starting point. So
2349
+ * callers must invalidate it whenever the metric changes, and whenever a
2350
+ * new solve begins.
2351
+ *
2352
+ * Clearing the flags is a complete invalidation: saved_log_projs is read
2353
+ * only where the matching flag is set, log_cone_Newton writes every
2354
+ * component of its output on the cold path, and the IPM fallback always
2355
+ * starts from the all-ones point, so it carries nothing across iterations.
2356
+ *
2357
+ * Dropping a warm start only costs inner iterations, never correctness,
2358
+ * which is why this is safe to call unconditionally.
2359
+ */
2360
+ void SCS(reset_cone_cache)(ScsConeWork *c) {
2361
+ #ifdef USE_SPECTRAL_CONES
2362
+ if (c && c->k && c->log_cone_warmstarts && c->k->dsize > 0) {
2363
+ memset(c->log_cone_warmstarts, 0, c->k->dsize * sizeof(bool));
2364
+ }
2365
+ #else
2366
+ (void)c;
2367
+ #endif
2368
+ }
2369
+
1682
2370
  /* Outward facing cone projection routine, performs projection in-place.
1683
2371
  If normalize > 0 then will use normalized (equilibrated) cones if applicable.
1684
2372