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
@@ -8,7 +8,12 @@
8
8
  #include "scs.h"
9
9
  #include "util.h"
10
10
 
11
- static const char *_test_prob_from_data(const char *file, scs_float OPT) {
11
+ /* Solve `file` at the given tolerance and check the objective against OPT to
12
+ * within `obj_tol`. Most problems want the defaults of
13
+ * `_test_prob_from_data` below; a problem with a slow tail (see issue_220)
14
+ * needs a looser tolerance to converge in a sane number of iterations. */
15
+ static const char *_test_prob_from_data_eps(const char *file, scs_float OPT,
16
+ scs_float eps, scs_float obj_tol) {
12
17
  scs_int read_status;
13
18
  ScsData *d;
14
19
  ScsCone *k;
@@ -28,8 +33,8 @@ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
28
33
  return "Data read failure, exit.\n";
29
34
  }
30
35
 
31
- stgs->eps_abs = 1e-6;
32
- stgs->eps_rel = 1e-6;
36
+ stgs->eps_abs = eps;
37
+ stgs->eps_rel = eps;
33
38
  /* Force verbosity for the test */
34
39
  stgs->verbose = 1;
35
40
 
@@ -51,7 +56,8 @@ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
51
56
  scs_printf("primal obj error %4e\n", perr);
52
57
  scs_printf("dual obj error %4e\n", derr);
53
58
 
54
- success = ABS(perr) < 1e-4 && ABS(derr) < 1e-4 && exitflag == SCS_SOLVED;
59
+ success = ABS(perr) < obj_tol && ABS(derr) < obj_tol &&
60
+ exitflag == SCS_SOLVED;
55
61
  if (!success) {
56
62
  scs_printf("%s: FAILED\n", file);
57
63
  }
@@ -70,4 +76,8 @@ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
70
76
  return fail;
71
77
  }
72
78
 
79
+ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
80
+ return _test_prob_from_data_eps(file, OPT, 1e-6, 1e-4);
81
+ }
82
+
73
83
  #endif
@@ -55,10 +55,11 @@ static const char *test_psd_metric(void) {
55
55
  for (i = j; i < PSD_N; ++i) {
56
56
  scs_float w = prof[psd_svec_idx(i, j, PSD_N)];
57
57
  mu_assert("psd_metric: fit not rank-one",
58
- ABS(w - delta[i] * delta[j]) <= 1e-12 * (1. + ABS(w)));
58
+ ABS(w - delta[i] * delta[j]) <=
59
+ VERIFY_TOL(1e-12, 1e-5) * (1. + ABS(w)));
59
60
  mu_assert("psd_metric: fit outside clamp bounds",
60
- w >= DIAG_SCALE_MULT_MIN * (1. - 1e-12) &&
61
- w <= DIAG_SCALE_MULT_MAX * (1. + 1e-12));
61
+ w >= DIAG_SCALE_MULT_MIN * (1. - VERIFY_TOL(1e-12, 1e-5)) &&
62
+ w <= DIAG_SCALE_MULT_MAX * (1. + VERIFY_TOL(1e-12, 1e-5)));
62
63
  }
63
64
  }
64
65
 
@@ -79,17 +80,20 @@ static const char *test_psd_metric(void) {
79
80
  SCS(proj_dual_cone)(chk, eucl, SCS_NULL, SCS_NULL);
80
81
  dist = SCS(norm_inf_diff)(chk, out, PSD_M);
81
82
  nrm = SCS(norm_inf)(out, PSD_M);
82
- mu_assert("psd_metric: primal part not in cone", dist <= 1e-9 * (1. + nrm));
83
+ mu_assert("psd_metric: primal part not in cone",
84
+ dist <= VERIFY_TOL(1e-9, 1e-4) * (1. + nrm));
83
85
 
84
86
  memcpy(chk, p, PSD_M * sizeof(scs_float));
85
87
  SCS(proj_dual_cone)(chk, eucl, SCS_NULL, SCS_NULL);
86
88
  dist = SCS(norm_inf_diff)(chk, p, PSD_M);
87
89
  nrm = SCS(norm_inf)(p, PSD_M);
88
- mu_assert("psd_metric: dual part not in cone", dist <= 1e-9 * (1. + nrm));
90
+ mu_assert("psd_metric: dual part not in cone",
91
+ dist <= VERIFY_TOL(1e-9, 1e-4) * (1. + nrm));
89
92
 
90
93
  dot = SCS(dot)(out, p, PSD_M);
91
94
  mu_assert("psd_metric: Moreau parts not orthogonal",
92
- ABS(dot) <= 1e-9 * (1. + SCS(norm_inf)(x, PSD_M)));
95
+ ABS(dot) <=
96
+ VERIFY_TOL(1e-9, 1e-4) * (1. + SCS(norm_inf)(x, PSD_M)));
93
97
 
94
98
  /* uniform metric must reproduce the Euclidean projection exactly */
95
99
  for (i = 0; i < PSD_M; ++i) {
@@ -100,7 +104,7 @@ static const char *test_psd_metric(void) {
100
104
  SCS(proj_dual_cone)(out, cw, SCS_NULL, r_y);
101
105
  SCS(proj_dual_cone)(chk, eucl, SCS_NULL, SCS_NULL);
102
106
  mu_assert("psd_metric: uniform metric deviates from Euclidean",
103
- SCS(norm_inf_diff)(chk, out, PSD_M) <= 1e-13);
107
+ SCS(norm_inf_diff)(chk, out, PSD_M) <= VERIFY_TOL(1e-13, 1e-5));
104
108
 
105
109
  SCS(finish_cone)(cw);
106
110
  SCS(finish_cone)(eucl);
@@ -71,7 +71,8 @@ static const char *test_psd_n1(void) {
71
71
  scs_printf("primal obj error %4e\n", perr);
72
72
  scs_printf("dual obj error %4e\n", derr);
73
73
 
74
- success = ABS(perr) < 1e-4 && ABS(derr) < 1e-4 && exitflag == SCS_SOLVED;
74
+ success = ABS(perr) < SOL_TOL && ABS(derr) < SOL_TOL &&
75
+ exitflag == SCS_SOLVED;
75
76
  mu_assert("test_psd_n1: SCS failed to produce SCS_SOLVED", success);
76
77
  fail = verify_solution_correct(d, k, stgs, &info, sol, exitflag);
77
78
 
@@ -0,0 +1,383 @@
1
+ #include "cones.h"
2
+ #include "glbopts.h"
3
+ #include "linalg.h"
4
+ #include "minunit.h"
5
+ #include "problem_utils.h"
6
+ #include "scs.h"
7
+ #include "util.h"
8
+
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #ifdef _OPENMP
12
+ #include <omp.h>
13
+ #endif
14
+
15
+ /* A build with USE_PARALLEL_PSD and OpenMP projects the PSD blocks in parallel
16
+ * (cones.c). Each block goes through the same LAPACK calls with the same
17
+ * workspace sizes as in the serial projection, so with single-threaded BLAS
18
+ * the result is the same bit for bit, whatever the number of threads.
19
+ * test_psd_parallel projects one vector with 1 and with 4 threads, and
20
+ * test_psd_parallel_solve solves one problem both ways.
21
+ *
22
+ * Every other build projects the blocks one after another. With OpenMP and
23
+ * without USE_PARALLEL_PSD, test_psd_parallel checks that the projection stays
24
+ * serial under 4 OpenMP threads.
25
+ *
26
+ * The comparisons are bit for bit when the BLAS is an OpenBLAS that the tests
27
+ * can set to one thread. They are to rounding error for the other libraries,
28
+ * which may round one block differently from call to call (MKL: the result
29
+ * can depend on the alignment of the workspace) or cannot be set to one
30
+ * thread from here (Accelerate). */
31
+
32
+ #if defined(USE_PARALLEL_PSD) && defined(_OPENMP)
33
+ /* this build projects the PSD blocks in parallel */
34
+ #define PSD_PAR_BUILD (1)
35
+ #endif
36
+
37
+ #ifdef PSD_PAR_BUILD
38
+ #if defined(__GNUC__) && defined(__ELF__)
39
+ /* weak references, NULL when the library is not linked (ELF only) */
40
+ #define PSD_PAR_WEAK_BLAS (1)
41
+ #ifdef __cplusplus
42
+ extern "C" {
43
+ #endif
44
+ void openblas_set_num_threads(int) __attribute__((weak));
45
+ int openblas_get_num_threads(void) __attribute__((weak));
46
+ int openblas_get_parallel(void) __attribute__((weak));
47
+ void MKL_Set_Num_Threads(int) __attribute__((weak));
48
+ int MKL_Get_Max_Threads(void) __attribute__((weak));
49
+ #ifdef __cplusplus
50
+ }
51
+ #endif
52
+ #else
53
+ #define PSD_PAR_WEAK_BLAS (0) /* e.g. Accelerate: VECLIB_MAXIMUM_THREADS=1 */
54
+ #endif
55
+
56
+ /* largest difference between two projections that is rounding error */
57
+ #ifdef SFLOAT
58
+ #define PSD_PAR_TOL (1e-3)
59
+ #else
60
+ #define PSD_PAR_TOL (1e-9)
61
+ #endif
62
+
63
+ /* the thread settings before a test, and what the test can rely on */
64
+ typedef struct {
65
+ int omp_nt; /* OpenMP threads */
66
+ int ob_nt; /* OpenBLAS threads, 0: no OpenBLAS found */
67
+ int mkl_nt; /* MKL threads, 0: no MKL found */
68
+ scs_int exact; /* projections are the same bit for bit */
69
+ scs_int exact_solve; /* and so is everything else in a solve */
70
+ } PsdParState;
71
+
72
+ /* Save the thread settings in st. Then BLAS on one thread where the library
73
+ * can be told: a threaded BLAS rounds differently in the serial projection,
74
+ * and a pthreads OpenBLAS with more than one thread keeps the projection
75
+ * serial. */
76
+ static void psd_par_enter(PsdParState *st) {
77
+ memset(st, 0, sizeof(PsdParState));
78
+ st->omp_nt = omp_get_max_threads();
79
+ #if PSD_PAR_WEAK_BLAS
80
+ if (openblas_set_num_threads && openblas_get_num_threads) {
81
+ st->ob_nt = openblas_get_num_threads();
82
+ openblas_set_num_threads(1);
83
+ st->exact = !MKL_Set_Num_Threads;
84
+ /* an OpenMP OpenBLAS (2) follows the OpenMP thread count in the BLAS calls
85
+ * outside of the PSD threads */
86
+ st->exact_solve =
87
+ st->exact && openblas_get_parallel && openblas_get_parallel() != 2;
88
+ }
89
+ if (MKL_Set_Num_Threads && MKL_Get_Max_Threads) {
90
+ st->mkl_nt = MKL_Get_Max_Threads();
91
+ MKL_Set_Num_Threads(1);
92
+ }
93
+ #endif
94
+ }
95
+
96
+ /* back to the settings saved in st */
97
+ static void psd_par_leave(PsdParState *st) {
98
+ omp_set_num_threads(st->omp_nt);
99
+ #if PSD_PAR_WEAK_BLAS
100
+ if (st->ob_nt > 0)
101
+ openblas_set_num_threads(st->ob_nt);
102
+ if (st->mkl_nt > 0)
103
+ MKL_Set_Num_Threads(st->mkl_nt);
104
+ #endif
105
+ }
106
+
107
+ /* Whether the blocks stay serial even when 4 OpenMP threads are asked for, so
108
+ * that there is nothing to test (says why). Leaves the thread count at 4. */
109
+ static scs_int psd_par_unavailable(void) {
110
+ omp_set_num_threads(4);
111
+ #ifdef PSD_PAR_MIN_COST
112
+ scs_printf("skipping: PSD_PAR_MIN_COST is set in this build\n");
113
+ return 1;
114
+ #else
115
+ if (omp_get_max_threads() < 2) {
116
+ scs_printf("skipping: OpenMP is limited to one thread\n");
117
+ return 1;
118
+ }
119
+ return 0;
120
+ #endif
121
+ }
122
+
123
+ /* whether a and b agree: bit for bit if exact, else to rounding error */
124
+ static scs_int psd_par_same(const scs_float *a, const scs_float *b, scs_int m,
125
+ scs_int exact) {
126
+ if (exact)
127
+ return memcmp(a, b, m * sizeof(scs_float)) == 0;
128
+ return SCS(norm_inf_diff)(a, b, m) <= PSD_PAR_TOL;
129
+ }
130
+
131
+ /* out = projection of v onto the dual cone in the metric r_y, with workspace c
132
+ * and nt OpenMP threads. Returns the status of the projection. */
133
+ static scs_int psd_par_proj(ScsConeWork *c, const scs_float *v, scs_float *out,
134
+ scs_float *r_y, scs_int m, int nt) {
135
+ memcpy(out, v, m * sizeof(scs_float));
136
+ omp_set_num_threads(nt);
137
+ return SCS(proj_dual_cone)(out, c, SCS_NULL, r_y);
138
+ }
139
+ #endif /* PSD_PAR_BUILD */
140
+
141
+ #ifdef _OPENMP
142
+ /* 40 real blocks (four above LAPACK's blocking crossover, ~32, of different
143
+ * sizes) and 4 complex ones: with workspaces sized per thread instead of the
144
+ * serial sizes, the large blocks would round differently, and this mix shows
145
+ * it. The largest block holds less than half of the work, so the parallel
146
+ * path is used. Other cones before and after the PSD blocks check the
147
+ * offsets. */
148
+ static scs_int psd_par_s[] = {1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 48, 5, 8, 13,
149
+ 21, 1, 2, 3, 5, 8, 64, 21, 1, 2, 3, 5, 8, 13,
150
+ 21, 1, 40, 3, 5, 8, 13, 36, 1, 2, 3, 5};
151
+ static scs_int psd_par_cs[] = {2, 3, 6, 10};
152
+ static scs_int psd_par_q[] = {4, 6};
153
+
154
+ /* number of rows of the cone k */
155
+ static scs_int psd_par_rows(const ScsCone *k) {
156
+ scs_int i, m = k->z + k->l + 3 * k->ep;
157
+ for (i = 0; i < k->qsize; ++i)
158
+ m += k->q[i];
159
+ for (i = 0; i < k->ssize; ++i)
160
+ m += (k->s[i] * (k->s[i] + 1)) / 2;
161
+ for (i = 0; i < k->cssize; ++i)
162
+ m += k->cs[i] * k->cs[i];
163
+ return m;
164
+ }
165
+
166
+ /* the cone of test_psd_parallel: its rows are returned */
167
+ static scs_int psd_par_cone(ScsCone *k) {
168
+ memset(k, 0, sizeof(ScsCone));
169
+ k->z = 5;
170
+ k->l = 10;
171
+ k->qsize = sizeof(psd_par_q) / sizeof(scs_int);
172
+ k->q = psd_par_q;
173
+ k->ssize = sizeof(psd_par_s) / sizeof(scs_int);
174
+ k->s = psd_par_s;
175
+ k->cssize = sizeof(psd_par_cs) / sizeof(scs_int);
176
+ k->cs = psd_par_cs;
177
+ k->ep = 2;
178
+ return psd_par_rows(k);
179
+ }
180
+ #endif /* _OPENMP */
181
+
182
+ #ifdef PSD_PAR_BUILD
183
+ /* the cone of test_psd_parallel_solve: enough work in the PSD blocks for the
184
+ * parallel path, also with an OpenBLAS whose lock makes blocks of n = 12 not
185
+ * worth the threads, and no block with more than half of it */
186
+ static scs_int psd_par_solve_s[] = {24, 24, 22, 24};
187
+ static scs_int psd_par_solve_cs[] = {12};
188
+ static scs_int psd_par_solve_q[] = {5};
189
+
190
+ /* Solve with nt OpenMP threads; psd_nt is set to the number of PSD threads of
191
+ * the workspace. Returns NULL if the problem is solved and the solution is
192
+ * correct. */
193
+ static const char *psd_par_solve(ScsData *d, ScsCone *k, ScsSettings *stgs,
194
+ ScsSolution *opt_sol, ScsSolution *sol,
195
+ ScsInfo *info, int nt, scs_int *psd_nt) {
196
+ ScsWork *w;
197
+ scs_int status;
198
+ scs_float perr, derr;
199
+ omp_set_num_threads(nt);
200
+ w = scs_init(d, k, stgs);
201
+ mu_assert("test_psd_parallel_solve: scs_init failed", w);
202
+ *psd_nt = w->cone_work->psd_nthreads;
203
+ status = scs_solve(w, sol, info, 0);
204
+ scs_finish(w);
205
+ perr = SCS(dot)(d->c, sol->x, d->n) - SCS(dot)(d->c, opt_sol->x, d->n);
206
+ derr = -SCS(dot)(d->b, sol->y, d->m) + SCS(dot)(d->b, opt_sol->y, d->m);
207
+ scs_printf("psd threads %li, primal error %4e, dual error %4e\n",
208
+ (long)*psd_nt, perr, derr);
209
+ mu_assert("test_psd_parallel_solve: SCS failed to produce SCS_SOLVED",
210
+ status == SCS_SOLVED && ABS(perr) < 1e-4 && ABS(derr) < 1e-4);
211
+ return verify_solution_correct(d, k, stgs, info, sol, status);
212
+ }
213
+ #endif /* PSD_PAR_BUILD */
214
+
215
+ static const char *test_psd_parallel(void) {
216
+ #ifndef _OPENMP
217
+ return mu_skipped; /* the projection is serial without OpenMP */
218
+ #elif !defined(PSD_PAR_BUILD)
219
+ /* OpenMP without USE_PARALLEL_PSD: serial, whatever the OpenMP threads */
220
+ ScsCone k;
221
+ scs_int m = psd_par_cone(&k), nt = 0;
222
+ int omp_nt = omp_get_max_threads();
223
+ ScsConeWork *c;
224
+ omp_set_num_threads(4);
225
+ c = SCS(init_cone)(&k, m);
226
+ if (c)
227
+ nt = c->psd_nthreads;
228
+ SCS(finish_cone)(c);
229
+ omp_set_num_threads(omp_nt);
230
+ mu_assert("test_psd_parallel: cone setup failed", c);
231
+ mu_assert("test_psd_parallel: PSD projection in parallel without "
232
+ "USE_PARALLEL_PSD",
233
+ nt == 1);
234
+ scs_printf("psd blocks projected serially: no USE_PARALLEL_PSD\n");
235
+ return 0;
236
+ #else
237
+ ScsCone k;
238
+ ScsConeWork *cs = SCS_NULL, *cp = SCS_NULL, *cc;
239
+ scs_float *v, *vs, *vp, *r_y;
240
+ scs_int i, m;
241
+ const char *fail = SCS_NULL;
242
+ PsdParState st;
243
+
244
+ psd_par_enter(&st);
245
+ if (psd_par_unavailable()) {
246
+ psd_par_leave(&st);
247
+ return mu_skipped;
248
+ }
249
+
250
+ m = psd_par_cone(&k);
251
+
252
+ v = (scs_float *)scs_calloc(m, sizeof(scs_float));
253
+ vs = (scs_float *)scs_calloc(m, sizeof(scs_float));
254
+ vp = (scs_float *)scs_calloc(m, sizeof(scs_float));
255
+ r_y = (scs_float *)scs_calloc(m, sizeof(scs_float));
256
+ if (v && vs && vp && r_y) {
257
+ ran_start(99);
258
+ for (i = 0; i < m; ++i) {
259
+ v[i] = rand_scs_float();
260
+ r_y[i] = 1.5 + rand_scs_float(); /* non-uniform metric */
261
+ }
262
+ omp_set_num_threads(1); /* an OpenMP BLAS then runs serially too */
263
+ cs = SCS(init_cone)(&k, m);
264
+ omp_set_num_threads(4);
265
+ cp = SCS(init_cone)(&k, m);
266
+ }
267
+
268
+ if (!cs || !cp) {
269
+ fail = "test_psd_parallel: cone setup failed";
270
+ } else if (cs->psd_nthreads != 1 || cp->psd_nthreads < 2) {
271
+ fail = "test_psd_parallel: parallel PSD projection not set up";
272
+ } else if (psd_par_proj(cs, v, vs, r_y, m, 1) ||
273
+ psd_par_proj(cp, v, vp, r_y, m, 4)) {
274
+ fail = "test_psd_parallel: projection failed";
275
+ } else if (!psd_par_same(vs, vp, m, st.exact)) {
276
+ fail = "test_psd_parallel: parallel projection differs from serial";
277
+ } else if (psd_par_proj(cp, v, vp, r_y, m, 2) ||
278
+ !psd_par_same(vs, vp, m, st.exact)) {
279
+ /* a team smaller than the split: each thread projects several lists */
280
+ fail = "test_psd_parallel: 2 threads on a split for more differ";
281
+ } else if (psd_par_proj(cp, v, vp, r_y, m, 1) ||
282
+ !psd_par_same(vs, vp, m, st.exact)) {
283
+ fail = "test_psd_parallel: 1 thread on a split for more differs";
284
+ } else {
285
+ scs_printf("psd blocks projected by %li threads, compared %s\n",
286
+ (long)cp->psd_nthreads,
287
+ st.exact ? "bit for bit" : "to rounding error");
288
+ }
289
+
290
+ if (!fail) {
291
+ /* the OpenMP thread count at setup is the upper bound, also later */
292
+ omp_set_num_threads(2);
293
+ cc = SCS(init_cone)(&k, m);
294
+ if (!cc || cc->psd_nthreads != 2)
295
+ fail = "test_psd_parallel: 2 OpenMP threads at setup are not 2 threads";
296
+ else if (psd_par_proj(cc, v, vp, r_y, m, 4) ||
297
+ !psd_par_same(vs, vp, m, st.exact))
298
+ fail = "test_psd_parallel: a split for 2 threads differs from serial";
299
+ SCS(finish_cone)(cc);
300
+ }
301
+
302
+ SCS(finish_cone)(cs);
303
+ SCS(finish_cone)(cp);
304
+ scs_free(v);
305
+ scs_free(vs);
306
+ scs_free(vp);
307
+ scs_free(r_y);
308
+ psd_par_leave(&st);
309
+ return fail;
310
+ #endif
311
+ }
312
+
313
+ /* No other solve in the tests has enough PSD blocks for the parallel path.
314
+ * A random feasible problem over several cones, solved with 4 threads and
315
+ * with 1: both must solve it, to the same iterates where the BLAS allows the
316
+ * comparison. It has enough variables for every PSD block to matter: with
317
+ * fewer (n = 50) a solver that left one list of blocks unprojected still
318
+ * reached the optimum, and here it reports an unbounded problem. */
319
+ static const char *test_psd_parallel_solve(void) {
320
+ #ifndef PSD_PAR_BUILD
321
+ return mu_skipped; /* the projection is serial in this build */
322
+ #else
323
+ ScsCone k = {0};
324
+ ScsData *d;
325
+ ScsSettings *stgs;
326
+ ScsSolution *sol1, *sol4, *opt_sol;
327
+ ScsInfo info1 = {0}, info4 = {0};
328
+ scs_int n = 200, col_nnz = 8, nt1 = 0, nt4 = 0;
329
+ const char *fail;
330
+ PsdParState st;
331
+
332
+ psd_par_enter(&st);
333
+ if (psd_par_unavailable()) {
334
+ psd_par_leave(&st);
335
+ return mu_skipped;
336
+ }
337
+
338
+ k.z = 4;
339
+ k.l = 6;
340
+ k.qsize = sizeof(psd_par_solve_q) / sizeof(scs_int);
341
+ k.q = psd_par_solve_q;
342
+ k.ssize = sizeof(psd_par_solve_s) / sizeof(scs_int);
343
+ k.s = psd_par_solve_s;
344
+ k.cssize = sizeof(psd_par_solve_cs) / sizeof(scs_int);
345
+ k.cs = psd_par_solve_cs;
346
+ k.ep = 2;
347
+
348
+ d = (ScsData *)scs_calloc(1, sizeof(ScsData));
349
+ stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings));
350
+ sol1 = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
351
+ sol4 = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
352
+ opt_sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
353
+ d->m = psd_par_rows(&k);
354
+ d->n = n;
355
+ omp_set_num_threads(1);
356
+ gen_random_prob_data(n * col_nnz, col_nnz, d, &k, opt_sol, 5678);
357
+ scs_set_default_settings(stgs);
358
+ stgs->eps_abs = 1e-5;
359
+ stgs->eps_rel = 1e-5;
360
+
361
+ fail = psd_par_solve(d, &k, stgs, opt_sol, sol4, &info4, 4, &nt4);
362
+ if (!fail)
363
+ fail = psd_par_solve(d, &k, stgs, opt_sol, sol1, &info1, 1, &nt1);
364
+ if (!fail) {
365
+ if (nt1 != 1 || nt4 < 2)
366
+ fail = "test_psd_parallel_solve: parallel PSD projection not set up";
367
+ else if (st.exact_solve &&
368
+ (info1.iter != info4.iter ||
369
+ !psd_par_same(sol1->x, sol4->x, d->n, 1) ||
370
+ !psd_par_same(sol1->y, sol4->y, d->m, 1) ||
371
+ !psd_par_same(sol1->s, sol4->s, d->m, 1)))
372
+ fail = "test_psd_parallel_solve: iterates depend on the thread count";
373
+ }
374
+
375
+ SCS(free_data)(d);
376
+ SCS(free_sol)(sol1);
377
+ SCS(free_sol)(sol4);
378
+ SCS(free_sol)(opt_sol);
379
+ scs_free(stgs);
380
+ psd_par_leave(&st);
381
+ return fail;
382
+ #endif
383
+ }
@@ -0,0 +1,186 @@
1
+ #include "glbopts.h"
2
+ #include "minunit.h"
3
+ #include "problem_utils.h"
4
+ #include "scs.h"
5
+ #include "util.h"
6
+
7
+ /*
8
+ * The per-row multipliers of adaptive_diag_scale must not refactorize the
9
+ * matrix for a change that the cone blocks then average away.
10
+ *
11
+ * A PSD block does not keep one multiplier per row: update_scale fits the
12
+ * rank-one form the projection can absorb. A single row of the block can
13
+ * ask for a step beyond the update threshold while the fitted block barely
14
+ * moves. If the change is measured before the fit, that row triggers a new
15
+ * factorization, and a reset of the acceleration and of the cone cache, as
16
+ * often as every RESCALING_MIN_ITERS iterations for a metric that has not
17
+ * changed.
18
+ *
19
+ * The problem is a small Lyapunov SDP,
20
+ *
21
+ * minimize trace(P) subject to A'P + PA + I <= 0, P >= I,
22
+ *
23
+ * with A an 8 x 8 matrix that is stable by a small margin (largest real
24
+ * part of an eigenvalue: -0.044), which is what makes it slow. There are 36
25
+ * variables and two PSD blocks of 36 rows each.
26
+ *
27
+ * Measured on macOS arm64 with the direct solver over 300 solves with the
28
+ * data perturbed by 1e-13: with the change measured before the fit, 23 to
29
+ * 67 scale updates and 3,125 to 11,325 iterations, about one update per 150
30
+ * iterations; measured on the multipliers that are installed, 2 or 3 scale
31
+ * updates and 1,100 to 1,600 iterations (4 updates with the indirect
32
+ * solver).
33
+ *
34
+ * The test asks for at most 10 updates, or failing that for at most one per
35
+ * 250 iterations. The second form is for a linear solver whose arithmetic
36
+ * is not reproducible from run to run (Apple Accelerate with more than one
37
+ * thread). In about 5,000 runs of this test there, about one in a thousand
38
+ * took a slow path of 5,000 to 15,000 iterations, on which the updates grow
39
+ * with the iterations: 12 to 24 updates, one per 400 to 600 iterations. A
40
+ * count alone would fail such a run although nothing is wrong with it. The
41
+ * old trigger makes one update per 120 to 170 iterations there.
42
+ *
43
+ * The tolerance is beyond single precision, so the test is skipped there.
44
+ */
45
+ #define LYAP_N (8)
46
+ #define LYAP_P ((LYAP_N * (LYAP_N + 1)) / 2)
47
+
48
+ /* position of entry (i, j), i >= j, in the scaled lower triangle */
49
+ static scs_int lyap_svec_idx(scs_int i, scs_int j) {
50
+ return j * LYAP_N - (j * (j - 1)) / 2 + (i - j);
51
+ }
52
+
53
+ static const char *test_psd_scale_updates(void) {
54
+ static const scs_float Am[LYAP_N][LYAP_N] = {
55
+ {-1.37, 0.22, -0.06, -2.32, 0.43, -2.13, 0.91, 0.61},
56
+ {0.83, -1.09, 0.30, -0.54, -0.31, 1.51, -0.58, -0.23},
57
+ {-0.72, -0.52, -2.23, 0.26, -0.29, -0.35, -0.62, 0.09},
58
+ {-1.34, 0.05, 1.31, -2.69, -0.05, 2.82, -0.99, -1.62},
59
+ {-0.43, 1.81, 2.04, -1.22, -1.35, 0.00, -0.59, -1.95},
60
+ {1.02, -0.86, 0.30, 0.08, 1.87, -0.75, -1.16, -0.26},
61
+ {1.04, -1.33, -0.39, 0.23, -1.62, -1.24, -3.63, 0.29},
62
+ {-0.56, 0.93, 0.11, 0.57, -0.08, 0.75, -0.10, -2.90}};
63
+ /* trace(P) at the solution */
64
+ const scs_float opt = 235.52015;
65
+ const scs_int n = LYAP_P, m = 2 * LYAP_P;
66
+ const scs_float sqrt2 = SQRTF(2.0);
67
+
68
+ ScsCone *k = (ScsCone *)scs_calloc(1, sizeof(ScsCone));
69
+ ScsData *d = (ScsData *)scs_calloc(1, sizeof(ScsData));
70
+ ScsSettings *stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings));
71
+ ScsSolution *sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
72
+ ScsInfo info = {0};
73
+ scs_int exitflag, i, j, r, c, l, nnz = 0;
74
+ scs_int s_dims[2] = {LYAP_N, LYAP_N};
75
+ const char *fail;
76
+
77
+ scs_float *Ax = (scs_float *)scs_calloc(m * n, sizeof(scs_float));
78
+ scs_int *Ai = (scs_int *)scs_calloc(m * n, sizeof(scs_int));
79
+ scs_int *Ap = (scs_int *)scs_calloc(n + 1, sizeof(scs_int));
80
+ scs_float *b = (scs_float *)scs_calloc(m, sizeof(scs_float));
81
+ scs_float *cvec = (scs_float *)scs_calloc(n, sizeof(scs_float));
82
+ scs_float B[LYAP_N][LYAP_N], M[LYAP_N][LYAP_N], col[2 * LYAP_P];
83
+
84
+ #ifdef SFLOAT
85
+ scs_free(Ax);
86
+ scs_free(Ai);
87
+ scs_free(Ap);
88
+ scs_free(b);
89
+ scs_free(cvec);
90
+ scs_free(sol);
91
+ scs_free(stgs);
92
+ scs_free(d);
93
+ scs_free(k);
94
+ return mu_skipped;
95
+ #endif
96
+
97
+ /* One variable per entry (i, j), i >= j, of P, in the order of the
98
+ * scaled lower triangle. Its column is the image of the symmetric unit
99
+ * matrix B with B_ij = B_ji = 1: svec(A'B + BA) in the first block
100
+ * (s_1 = -(A'P + PA) - I) and -svec(B) in the second (s_2 = P - I). */
101
+ for (j = 0; j < LYAP_N; ++j) {
102
+ for (i = j; i < LYAP_N; ++i) {
103
+ for (r = 0; r < LYAP_N; ++r) {
104
+ for (c = 0; c < LYAP_N; ++c) {
105
+ B[r][c] = 0.;
106
+ }
107
+ }
108
+ B[i][j] = 1.;
109
+ B[j][i] = 1.;
110
+ for (r = 0; r < LYAP_N; ++r) {
111
+ for (c = 0; c < LYAP_N; ++c) {
112
+ M[r][c] = 0.;
113
+ for (l = 0; l < LYAP_N; ++l) {
114
+ M[r][c] += Am[l][r] * B[l][c] + B[r][l] * Am[l][c];
115
+ }
116
+ }
117
+ }
118
+ for (c = 0; c < LYAP_N; ++c) {
119
+ for (r = c; r < LYAP_N; ++r) {
120
+ scs_float sc = (r == c) ? 1. : sqrt2;
121
+ col[lyap_svec_idx(r, c)] = sc * M[r][c];
122
+ col[LYAP_P + lyap_svec_idx(r, c)] = -sc * B[r][c];
123
+ }
124
+ }
125
+ for (r = 0; r < m; ++r) {
126
+ if (col[r] != 0.) {
127
+ Ax[nnz] = col[r];
128
+ Ai[nnz] = r;
129
+ nnz++;
130
+ }
131
+ }
132
+ Ap[lyap_svec_idx(i, j) + 1] = nnz;
133
+ cvec[lyap_svec_idx(i, j)] = (i == j) ? 1. : 0.;
134
+ }
135
+ }
136
+ /* b = -(svec(I), svec(I)) */
137
+ for (i = 0; i < LYAP_N; ++i) {
138
+ b[lyap_svec_idx(i, i)] = -1.;
139
+ b[LYAP_P + lyap_svec_idx(i, i)] = -1.;
140
+ }
141
+
142
+ d->m = m;
143
+ d->n = n;
144
+ d->b = b;
145
+ d->c = cvec;
146
+ d->A = (ScsMatrix *)scs_calloc(1, sizeof(ScsMatrix));
147
+ d->A->m = m;
148
+ d->A->n = n;
149
+ d->A->x = Ax;
150
+ d->A->i = Ai;
151
+ d->A->p = Ap;
152
+
153
+ k->s = s_dims;
154
+ k->ssize = 2;
155
+
156
+ scs_set_default_settings(stgs);
157
+ stgs->adaptive_diag_scale = 1; /* the setting under test */
158
+ stgs->eps_abs = 1e-6;
159
+ stgs->eps_rel = 1e-6;
160
+
161
+ exitflag = scs(d, k, stgs, sol, &info);
162
+
163
+ scs_printf("test_psd_scale_updates: %li iterations, %li scale updates\n",
164
+ (long)info.iter, (long)info.scale_updates);
165
+
166
+ mu_assert("test_psd_scale_updates: expected SCS_SOLVED",
167
+ exitflag == SCS_SOLVED);
168
+ mu_assert_less("test_psd_scale_updates: objective ERROR",
169
+ ABS(info.pobj - opt), 1e-2);
170
+ mu_assert("test_psd_scale_updates: too many scale updates",
171
+ info.scale_updates <= 10 ||
172
+ info.scale_updates * 250 <= info.iter);
173
+ fail = verify_solution_correct(d, k, stgs, &info, sol, exitflag);
174
+
175
+ SCS(free_sol)(sol);
176
+ scs_free(d->A);
177
+ scs_free(Ax);
178
+ scs_free(Ai);
179
+ scs_free(Ap);
180
+ scs_free(b);
181
+ scs_free(cvec);
182
+ scs_free(k);
183
+ scs_free(stgs);
184
+ scs_free(d);
185
+ return fail;
186
+ }