forked from NVIDIA/cuopt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier.cu
More file actions
3592 lines (3190 loc) · 143 KB
/
barrier.cu
File metadata and controls
3592 lines (3190 loc) · 143 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dual_simplex/barrier.hpp>
#include <dual_simplex/conjugate_gradient.hpp>
#include <dual_simplex/cusparse_info.hpp>
#include <dual_simplex/dense_matrix.hpp>
#include <dual_simplex/dense_vector.hpp>
#include <dual_simplex/device_sparse_matrix.cuh>
#include <dual_simplex/iterative_refinement.hpp>
#include <dual_simplex/presolve.hpp>
#include <dual_simplex/solve.hpp>
#include <dual_simplex/sparse_cholesky.cuh>
#include <dual_simplex/sparse_matrix.hpp>
#include <dual_simplex/sparse_matrix_kernels.cuh>
#include <dual_simplex/tic_toc.hpp>
#include <dual_simplex/types.hpp>
#include <dual_simplex/vector_math.cuh>
#include "dual_simplex/cusparse_view.hpp"
#include <rmm/device_scalar.hpp>
#include <rmm/device_uvector.hpp>
#include <utilities/copy_helpers.hpp>
#include <utilities/cuda_helpers.cuh>
#include <utilities/macros.cuh>
#include <numeric>
#include <raft/sparse/detail/cusparse_wrappers.h>
#include <raft/common/nvtx.hpp>
#include <raft/linalg/dot.cuh>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_output_iterator.h>
namespace cuopt::linear_programming::dual_simplex {
auto constexpr use_gpu = true;
template <typename i_t, typename f_t>
class iteration_data_t {
public:
iteration_data_t(const lp_problem_t<i_t, f_t>& lp,
i_t num_upper_bounds,
const simplex_solver_settings_t<i_t, f_t>& settings)
: upper_bounds(num_upper_bounds),
c(lp.objective),
b(lp.rhs),
w(num_upper_bounds),
x(lp.num_cols),
y(lp.num_rows),
v(num_upper_bounds),
z(lp.num_cols),
w_save(num_upper_bounds),
x_save(lp.num_cols),
y_save(lp.num_rows),
v_save(num_upper_bounds),
z_save(lp.num_cols),
relative_primal_residual_save(inf),
relative_dual_residual_save(inf),
relative_complementarity_residual_save(inf),
primal_residual_norm_save(inf),
dual_residual_norm_save(inf),
complementarity_residual_norm_save(inf),
diag(lp.num_cols),
inv_diag(lp.num_cols),
inv_sqrt_diag(lp.num_cols),
AD(lp.num_cols, lp.num_rows, 0),
AT(lp.num_rows, lp.num_cols, 0),
ADAT(lp.num_rows, lp.num_rows, 0),
augmented(lp.num_cols + lp.num_rows, lp.num_cols + lp.num_rows, 0),
A_dense(lp.num_rows, 0),
AD_dense(0, 0),
H(0, 0),
Hchol(0, 0),
A(lp.A),
primal_residual(lp.num_rows),
bound_residual(num_upper_bounds),
dual_residual(lp.num_cols),
complementarity_xz_residual(lp.num_cols),
complementarity_wv_residual(num_upper_bounds),
cusparse_view_(lp.handle_ptr, lp.A),
primal_rhs(lp.num_rows),
bound_rhs(num_upper_bounds),
dual_rhs(lp.num_cols),
complementarity_xz_rhs(lp.num_cols),
complementarity_wv_rhs(num_upper_bounds),
dw_aff(num_upper_bounds),
dx_aff(lp.num_cols),
dy_aff(lp.num_rows),
dv_aff(num_upper_bounds),
dz_aff(lp.num_cols),
dw(num_upper_bounds),
dx(lp.num_cols),
dy(lp.num_rows),
dv(num_upper_bounds),
dz(lp.num_cols),
cusparse_info(lp.handle_ptr),
device_AD(lp.num_cols, lp.num_rows, 0, lp.handle_ptr->get_stream()),
device_A(lp.num_cols, lp.num_rows, 0, lp.handle_ptr->get_stream()),
device_ADAT(lp.num_rows, lp.num_rows, 0, lp.handle_ptr->get_stream()),
d_original_A_values(0, lp.handle_ptr->get_stream()),
device_A_x_values(0, lp.handle_ptr->get_stream()),
d_inv_diag_prime(0, lp.handle_ptr->get_stream()),
d_flag_buffer(0, lp.handle_ptr->get_stream()),
d_num_flag(lp.handle_ptr->get_stream()),
d_inv_diag(lp.num_cols, lp.handle_ptr->get_stream()),
d_cols_to_remove(0, lp.handle_ptr->get_stream()),
use_augmented(false),
has_factorization(false),
num_factorizations(0),
has_solve_info(false),
settings_(settings),
handle_ptr(lp.handle_ptr),
stream_view_(lp.handle_ptr->get_stream()),
d_diag_(lp.num_cols, lp.handle_ptr->get_stream()),
d_x_(0, lp.handle_ptr->get_stream()),
d_z_(0, lp.handle_ptr->get_stream()),
d_w_(0, lp.handle_ptr->get_stream()),
d_v_(0, lp.handle_ptr->get_stream()),
d_h_(lp.num_rows, lp.handle_ptr->get_stream()),
d_y_(0, lp.handle_ptr->get_stream()),
d_tmp3_(lp.num_cols, lp.handle_ptr->get_stream()),
d_tmp4_(lp.num_cols, lp.handle_ptr->get_stream()),
d_r1_(lp.num_cols, lp.handle_ptr->get_stream()),
d_r1_prime_(lp.num_cols, lp.handle_ptr->get_stream()),
d_c_(lp.num_cols, lp.handle_ptr->get_stream()),
d_upper_(0, lp.handle_ptr->get_stream()),
d_u_(lp.A.n, lp.handle_ptr->get_stream()),
d_upper_bounds_(0, lp.handle_ptr->get_stream()),
d_dx_(0, lp.handle_ptr->get_stream()),
d_dy_(0, lp.handle_ptr->get_stream()),
d_dz_(0, lp.handle_ptr->get_stream()),
d_dv_(0, lp.handle_ptr->get_stream()),
d_dw_(0, lp.handle_ptr->get_stream()),
d_dw_aff_(0, lp.handle_ptr->get_stream()),
d_dx_aff_(0, lp.handle_ptr->get_stream()),
d_dv_aff_(0, lp.handle_ptr->get_stream()),
d_dz_aff_(0, lp.handle_ptr->get_stream()),
d_dy_aff_(0, lp.handle_ptr->get_stream()),
d_primal_residual_(0, lp.handle_ptr->get_stream()),
d_dual_residual_(lp.num_cols, lp.handle_ptr->get_stream()),
d_bound_residual_(0, lp.handle_ptr->get_stream()),
d_complementarity_xz_residual_(lp.num_cols, lp.handle_ptr->get_stream()),
d_complementarity_wv_residual_(0, lp.handle_ptr->get_stream()),
d_y_residual_(lp.num_rows, lp.handle_ptr->get_stream()),
d_dx_residual_(lp.num_cols, lp.handle_ptr->get_stream()),
d_xz_residual_(0, lp.handle_ptr->get_stream()),
d_dw_residual_(0, lp.handle_ptr->get_stream()),
d_wv_residual_(0, lp.handle_ptr->get_stream()),
d_bound_rhs_(0, lp.handle_ptr->get_stream()),
d_complementarity_xz_rhs_(lp.num_cols, lp.handle_ptr->get_stream()),
d_complementarity_wv_rhs_(0, lp.handle_ptr->get_stream()),
d_dual_rhs_(lp.num_cols, lp.handle_ptr->get_stream()),
restrict_u_(0),
transform_reduce_helper_(lp.handle_ptr->get_stream()),
sum_reduce_helper_(lp.handle_ptr->get_stream())
{
raft::common::nvtx::range fun_scope("Barrier: LP Data Creation");
// Allocating GPU flag data for Form ADAT
if (use_gpu) {
raft::common::nvtx::range fun_scope("Barrier: GPU Flag memory allocation");
cub::DeviceSelect::Flagged(
nullptr,
flag_buffer_size,
d_inv_diag_prime.data(), // Not the actual input but just to allcoate the memory
thrust::make_transform_iterator(d_cols_to_remove.data(), cuda::std::logical_not<i_t>{}),
d_inv_diag_prime.data(),
d_num_flag.data(),
inv_diag.size(),
stream_view_);
d_flag_buffer.resize(flag_buffer_size, stream_view_);
}
// Create the upper bounds vector
n_upper_bounds = 0;
for (i_t j = 0; j < lp.num_cols; j++) {
if (lp.upper[j] < inf) { upper_bounds[n_upper_bounds++] = j; }
}
if (n_upper_bounds > 0) {
settings.log.printf("Upper bounds : %d\n", n_upper_bounds);
}
// Decide if we are going to use the augmented system or not
n_dense_columns = 0;
i_t n_dense_rows = 0;
i_t max_row_nz = 0;
f_t estimated_nz_AAT = 0.0;
std::vector<i_t> dense_columns_unordered;
{
f_t start_column_density = tic();
find_dense_columns(
lp.A, settings, dense_columns_unordered, n_dense_rows, max_row_nz, estimated_nz_AAT);
if (settings.concurrent_halt != nullptr && *settings.concurrent_halt == 1) { return; }
#ifdef PRINT_INFO
for (i_t j : dense_columns_unordered) {
settings.log.printf("Dense column %6d\n", j);
}
#endif
float64_t column_density_time = toc(start_column_density);
if (!settings.eliminate_dense_columns) { dense_columns_unordered.clear(); }
n_dense_columns = static_cast<i_t>(dense_columns_unordered.size());
if (n_dense_columns > 0) {
settings.log.printf("Dense columns : %d\n", n_dense_columns);
}
if (n_dense_rows > 0) {
settings.log.printf("Dense rows : %d\n", n_dense_rows);
}
settings.log.printf("Density estimator time : %.2fs\n", column_density_time);
}
if ((settings.augmented != 0) &&
(n_dense_columns > 50 || n_dense_rows > 10 ||
(max_row_nz > 5000 && estimated_nz_AAT > 1e10) || settings.augmented == 1)) {
use_augmented = true;
n_dense_columns = 0;
}
if (use_augmented) {
settings.log.printf("Linear system : augmented\n");
} else {
settings.log.printf("Linear system : ADAT\n");
}
diag.set_scalar(1.0);
if (n_upper_bounds > 0) {
for (i_t k = 0; k < n_upper_bounds; k++) {
i_t j = upper_bounds[k];
diag[j] = 2.0;
}
}
inv_diag.set_scalar(1.0);
if (use_augmented) { diag.multiply_scalar(-1.0); }
if (n_upper_bounds > 0) { diag.inverse(inv_diag); }
if (use_gpu) {
// TMP diag and inv_diag should directly created and filled on the GPU
raft::copy(d_inv_diag.data(), inv_diag.data(), inv_diag.size(), stream_view_);
}
inv_sqrt_diag.set_scalar(1.0);
if (n_upper_bounds > 0) { inv_diag.sqrt(inv_sqrt_diag); }
if (settings.concurrent_halt != nullptr && *settings.concurrent_halt == 1) { return; }
// Copy A into AD
AD = lp.A;
if (!use_augmented && n_dense_columns > 0) {
cols_to_remove.resize(lp.num_cols, 0);
for (i_t k : dense_columns_unordered) {
cols_to_remove[k] = 1;
}
d_cols_to_remove.resize(cols_to_remove.size(), stream_view_);
raft::copy(
d_cols_to_remove.data(), cols_to_remove.data(), cols_to_remove.size(), stream_view_);
dense_columns.clear();
dense_columns.reserve(n_dense_columns);
for (i_t j = 0; j < lp.num_cols; j++) {
if (cols_to_remove[j]) { dense_columns.push_back(j); }
}
AD.remove_columns(cols_to_remove);
sparse_mark.resize(lp.num_cols, 1);
for (i_t k : dense_columns) {
sparse_mark[k] = 0;
}
A_dense.resize(AD.m, n_dense_columns);
i_t k = 0;
for (i_t j : dense_columns) {
A_dense.from_sparse(lp.A, j, k++);
}
}
original_A_values = AD.x;
if (use_gpu) {
d_original_A_values.resize(original_A_values.size(), handle_ptr->get_stream());
raft::copy(d_original_A_values.data(), AD.x.data(), AD.x.size(), handle_ptr->get_stream());
}
AD.transpose(AT);
if (use_gpu) {
device_AD.copy(AD, handle_ptr->get_stream());
// For efficient scaling of AD col we form the col index array
device_AD.form_col_index(handle_ptr->get_stream());
device_A_x_values.resize(original_A_values.size(), handle_ptr->get_stream());
raft::copy(
device_A_x_values.data(), device_AD.x.data(), device_AD.x.size(), handle_ptr->get_stream());
csr_matrix_t<i_t, f_t> host_A_CSR(1, 1, 1); // Sizes will be set by to_compressed_row()
AD.to_compressed_row(host_A_CSR);
device_A.copy(host_A_CSR, lp.handle_ptr->get_stream());
RAFT_CHECK_CUDA(handle_ptr->get_stream());
}
if (settings.concurrent_halt != nullptr && *settings.concurrent_halt == 1) { return; }
i_t factorization_size = use_augmented ? lp.num_rows + lp.num_cols : lp.num_rows;
chol =
std::make_unique<sparse_cholesky_cudss_t<i_t, f_t>>(handle_ptr, settings, factorization_size);
chol->set_positive_definite(false);
if (settings.concurrent_halt != nullptr && *settings.concurrent_halt == 1) { return; }
// Perform symbolic analysis
symbolic_status = 0;
if (use_augmented) {
// Build the sparsity pattern of the augmented system
form_augmented(true);
if (settings.concurrent_halt != nullptr && *settings.concurrent_halt == 1) { return; }
symbolic_status = chol->analyze(augmented);
} else {
form_adat(true);
if (settings.concurrent_halt != nullptr && *settings.concurrent_halt == 1) { return; }
if (use_gpu) {
symbolic_status = chol->analyze(device_ADAT);
} else {
symbolic_status = chol->analyze(ADAT);
}
}
}
void form_augmented(bool first_call = false)
{
i_t n = A.n;
i_t m = A.m;
i_t nnzA = A.col_start[n];
i_t factorization_size = n + m;
const f_t dual_perturb = 0.0;
const f_t primal_perturb = 1e-6;
if (first_call) {
augmented.reallocate(2 * nnzA + n + m);
i_t q = 0;
for (i_t j = 0; j < n; j++) {
augmented.col_start[j] = q;
augmented.i[q] = j;
augmented.x[q++] = -diag[j] - dual_perturb;
const i_t col_beg = A.col_start[j];
const i_t col_end = A.col_start[j + 1];
for (i_t p = col_beg; p < col_end; ++p) {
augmented.i[q] = n + A.i[p];
augmented.x[q++] = A.x[p];
}
}
settings_.log.debug("augmented nz %d predicted %d\n", q, nnzA + n);
for (i_t k = n; k < n + m; ++k) {
augmented.col_start[k] = q;
const i_t l = k - n;
const i_t col_beg = AT.col_start[l];
const i_t col_end = AT.col_start[l + 1];
for (i_t p = col_beg; p < col_end; ++p) {
augmented.i[q] = AT.i[p];
augmented.x[q++] = AT.x[p];
}
augmented.i[q] = k;
augmented.x[q++] = primal_perturb;
}
augmented.col_start[n + m] = q;
cuopt_assert(q == 2 * nnzA + n + m, "augmented nnz != predicted");
cuopt_assert(A.col_start[n] == AT.col_start[m], "A nz != AT nz");
#ifdef CHECK_SYMMETRY
csc_matrix_t<i_t, f_t> augmented_transpose(1, 1, 1);
augmented.transpose(augmented_transpose);
settings_.log.printf("Aug nnz %d Aug^T nnz %d\n",
augmented.col_start[m + n],
augmented_transpose.col_start[m + n]);
augmented.check_matrix();
augmented_transpose.check_matrix();
csc_matrix_t<i_t, f_t> error(m + n, m + n, 1);
add(augmented, augmented_transpose, 1.0, -1.0, error);
settings_.log.printf("|| Aug - Aug^T ||_1 %e\n", error.norm1());
cuopt_assert(error.norm1() <= 1e-2, "|| Aug - Aug^T ||_1 > 1e-2");
#endif
} else {
for (i_t j = 0; j < n; ++j) {
const i_t q = augmented.col_start[j];
augmented.x[q] = -diag[j] - dual_perturb;
}
}
}
void form_adat(bool first_call = false)
{
handle_ptr->sync_stream();
raft::common::nvtx::range fun_scope("Barrier: Form ADAT");
float64_t start_form_adat = tic();
const i_t m = AD.m;
if (use_gpu) {
// TODO do we really need this copy? (it's ok since gpu to gpu)
raft::copy(device_AD.x.data(),
d_original_A_values.data(),
d_original_A_values.size(),
handle_ptr->get_stream());
if (n_dense_columns > 0) {
// Adjust inv_diag
d_inv_diag_prime.resize(AD.n, stream_view_);
// Copy If
cub::DeviceSelect::Flagged(
d_flag_buffer.data(),
flag_buffer_size,
d_inv_diag.data(),
thrust::make_transform_iterator(d_cols_to_remove.data(), cuda::std::logical_not<i_t>{}),
d_inv_diag_prime.data(),
d_num_flag.data(),
d_inv_diag.size(),
stream_view_);
} else {
d_inv_diag_prime.resize(inv_diag.size(), stream_view_);
raft::copy(d_inv_diag_prime.data(), d_inv_diag.data(), inv_diag.size(), stream_view_);
}
cuopt_assert(static_cast<i_t>(d_inv_diag_prime.size()) == AD.n,
"inv_diag_prime.size() != AD.n");
thrust::for_each_n(rmm::exec_policy(stream_view_),
thrust::make_counting_iterator<i_t>(0),
i_t(device_AD.x.size()),
[span_x = cuopt::make_span(device_AD.x),
span_scale = cuopt::make_span(d_inv_diag_prime),
span_col_ind = cuopt::make_span(device_AD.col_index)] __device__(i_t i) {
span_x[i] *= span_scale[span_col_ind[i]];
});
if (settings_.concurrent_halt != nullptr && *settings_.concurrent_halt == 1) { return; }
if (first_call) {
initialize_cusparse_data<i_t, f_t>(
handle_ptr, device_A, device_AD, device_ADAT, cusparse_info);
}
if (settings_.concurrent_halt != nullptr && *settings_.concurrent_halt == 1) { return; }
multiply_kernels<i_t, f_t>(handle_ptr, device_A, device_AD, device_ADAT, cusparse_info);
handle_ptr->sync_stream();
auto adat_nnz = device_ADAT.row_start.element(device_ADAT.m, handle_ptr->get_stream());
float64_t adat_time = toc(start_form_adat);
if (num_factorizations == 0) {
settings_.log.printf("ADAT time : %.2fs\n", adat_time);
settings_.log.printf("ADAT nonzeros : %.2e\n",
static_cast<float64_t>(adat_nnz));
settings_.log.printf(
"ADAT density : %.2f\n",
static_cast<float64_t>(adat_nnz) /
(static_cast<float64_t>(device_ADAT.m) * static_cast<float64_t>(device_ADAT.m)));
}
} else {
// Restore the columns of AD to A
AD.x = original_A_values;
std::vector<f_t> inv_diag_prime;
if (n_dense_columns > 0) {
// Adjust inv_diag
inv_diag_prime.resize(AD.n);
const i_t n = A.n;
i_t new_j = 0;
for (i_t j = 0; j < n; j++) {
if (cols_to_remove[j]) { continue; }
inv_diag_prime[new_j++] = inv_diag[j];
}
} else {
inv_diag_prime = copy(inv_diag);
}
cuopt_assert(static_cast<i_t>(inv_diag_prime.size()) == AD.n,
"inv_diag_prime.size() != AD.n");
AD.scale_columns(inv_diag_prime);
multiply(AD, AT, ADAT);
float64_t adat_time = toc(start_form_adat);
if (num_factorizations == 0) {
settings_.log.printf("ADAT time %.2fs\n", adat_time);
settings_.log.printf("ADAT nonzeros %e density %.2f\n",
static_cast<float64_t>(ADAT.col_start[m]),
static_cast<float64_t>(ADAT.col_start[m]) /
(static_cast<float64_t>(m) * static_cast<float64_t>(m)));
}
}
}
i_t solve_adat(const dense_vector_t<i_t, f_t>& b, dense_vector_t<i_t, f_t>& x, bool debug = false)
{
if (n_dense_columns == 0) {
// Solve ADAT * x = b
if (debug) { settings_.log.printf("||b|| = %.16e\n", vector_norm2<i_t, f_t>(b)); }
i_t solve_status = chol->solve(b, x);
if (debug) { settings_.log.printf("||x|| = %.16e\n", vector_norm2<i_t, f_t>(x)); }
return solve_status;
} else {
// Use Sherman Morrison followed by PCG
// ADA^T = A_sparse * D_sparse * A_sparse^T + A_dense * D_dense * A_dense^T
// Let p be the number of dense columns
// U = A_dense * D_dense^0.5 is m x p
// U^T = D_dense^0.5 * A_dense^T is p x m
// We have that A D A^T *x = b is
// (A_sparse * D_sparse * A_sparse^T + A_dense * D_dense * A_dense^T) * x = b
// (A_sparse * D_sparse * A_sparse^T + U * U^T ) * x = b
// We can write this as the 2x2 system
//
// [ A_sparse * D_sparse * A_sparse^T U ][ x ] = [ b ]
// [ U^T -I][ y ] [ 0 ]
//
// We can write x = (A_sparse * D_sparse * A_sparse^T)^{-1} * (b - U * y)
// So U^T * x - y = 0 or
// U^T * (A_sparse * D_sparse * A_sparse^T)^{-1} * (b - U * y) - y = 0
// (U^T * (A_sparse * D_sparse * A_sparse^T)^{-1} U + I) * y = U^T * (A_sparse * D_sparse *
// A_sparse^T)^{-1} * b
// H * y = g
// where H = U^T * (A_sparse * D_sparse * A_sparse^T)^{-1} U + I
// and g = U^T * (A_sparse * D_sparse * A_sparse^T)^{-1} * b
// Let (A_sparse * D_sparse * A_sparse^T)* w = b
// then g = U^T * w
// Let (A_sparse * D_sparse * A_sparse^T) * M = U
// then H = U^T * M + I
//
// We can use a dense cholesky factorization of H to solve for y
dense_vector_t<i_t, f_t> w(AD.m);
const bool debug = false;
const bool full_debug = false;
if (debug) { settings_.log.printf("||b|| = %.16e\n", vector_norm2<i_t, f_t>(b)); }
i_t solve_status = chol->solve(b, w);
if (debug) { settings_.log.printf("||w|| = %.16e\n", vector_norm2<i_t, f_t>(w)); }
if (solve_status != 0) {
settings_.log.printf("Linear solve failed in Sherman Morrison after ADAT solve\n");
return solve_status;
}
if (!has_solve_info) {
AD_dense = A_dense;
// AD_dense = A_dense * D_dense
dense_vector_t<i_t, f_t> dense_diag(n_dense_columns);
i_t k = 0;
for (i_t j : dense_columns) {
dense_diag[k++] = std::sqrt(inv_diag[j]);
}
AD_dense.scale_columns(dense_diag);
dense_matrix_t<i_t, f_t> M(AD.m, n_dense_columns);
H.resize(n_dense_columns, n_dense_columns);
for (i_t k = 0; k < n_dense_columns; k++) {
dense_vector_t<i_t, f_t> U_col(AD.m);
// U_col = AD_dense(:, k)
for (i_t i = 0; i < AD.m; i++) {
U_col[i] = AD_dense(i, k);
}
dense_vector_t<i_t, f_t> M_col(AD.m);
solve_status = chol->solve(U_col, M_col);
if (solve_status != 0) { return solve_status; }
if (settings_.concurrent_halt != nullptr && *settings_.concurrent_halt == 1) {
return -2;
}
M.set_column(k, M_col);
if (debug) {
dense_vector_t<i_t, f_t> M_residual = U_col;
matrix_vector_multiply(ADAT, 1.0, M_col, -1.0, M_residual);
settings_.log.printf(
"|| A_sparse * D_sparse * A_sparse^T * M(:, k) - AD_dense(:, k) ||_2 = %e\n",
vector_norm2<i_t, f_t>(M_residual));
}
}
// A_sparse * D_sparse * A_sparse^T * M = U = AD_dense
// H = AD_dense^T * M
// AD_dense.transpose_matrix_multiply(1.0, M, 0.0, H);
for (i_t k = 0; k < n_dense_columns; k++) {
AD_dense.transpose_multiply(
1.0, M.values.data() + k * M.m, 0.0, H.values.data() + k * H.m);
if (settings_.concurrent_halt != nullptr && *settings_.concurrent_halt == 1) {
return -2;
}
}
dense_vector_t<i_t, f_t> e(n_dense_columns);
e.set_scalar(1.0);
// H = AD_dense^T * M + I
H.add_diagonal(e);
// H = L*L^T
Hchol.resize(n_dense_columns, n_dense_columns); // Hcol = L
H.chol(Hchol);
has_solve_info = true;
}
dense_vector_t<i_t, f_t> g(n_dense_columns);
// g = D_dense * A_dense^T * w
AD_dense.transpose_multiply(1.0, w, 0.0, g);
if (debug) {
for (i_t k = 0; k < n_dense_columns; k++) {
for (i_t h = 0; h < n_dense_columns; h++) {
if (std::abs(H(k, h) - H(h, k)) > 1e-10) {
settings_.log.printf(
"H(%d, %d) = %e, H(%d, %d) = %e\n", k, h, H(k, h), h, k, H(h, k));
}
}
}
}
dense_vector_t<i_t, f_t> y(n_dense_columns);
// H *y = g
// L*L^T * y = g
// L*u = g
dense_vector_t<i_t, f_t> u(n_dense_columns);
Hchol.triangular_solve(g, u);
// L^T y = u
Hchol.triangular_solve_transpose(u, y);
if (debug) {
dense_vector_t<i_t, f_t> H_residual = g;
H.matrix_vector_multiply(1.0, y, -1.0, H_residual);
settings_.log.printf("|| H * y - g ||_2 = %e\n", vector_norm2<i_t, f_t>(H_residual));
}
// x = (A_sparse * D_sparse * A_sparse^T)^{-1} * (b - U * y)
// v = U *y = AD_dense * y
dense_vector_t<i_t, f_t> v(AD.m);
AD_dense.matrix_vector_multiply(1.0, y, 0.0, v);
// v = b - U*y
v.axpy(1.0, b, -1.0);
// A_sparse * D_sparse * A_sparse^T * x = v
solve_status = chol->solve(v, x);
if (solve_status != 0) { return solve_status; }
if (debug) {
dense_vector_t<i_t, f_t> solve_residual = v;
matrix_vector_multiply(ADAT, 1.0, x, -1.0, solve_residual);
settings_.log.printf("|| A_sparse * D * A_sparse^T * x - v ||_2 = %e\n",
vector_norm2<i_t, f_t>(solve_residual));
}
if (debug) {
// Check U^T * x - y = 0;
dense_vector_t<i_t, f_t> residual_2 = y;
AD_dense.transpose_multiply(1.0, x, -1.0, residual_2);
settings_.log.printf("|| U^T * x - y ||_2 = %e\n", vector_norm2<i_t, f_t>(residual_2));
}
if (debug) {
// Check A_sparse * D_sparse * A_sparse^T * x + U * y = b
dense_vector_t<i_t, f_t> residual_1 = b;
AD_dense.matrix_vector_multiply(1.0, y, -1.0, residual_1);
matrix_vector_multiply(ADAT, 1.0, x, 1.0, residual_1);
settings_.log.printf("|| A_sparse * D_sparse * A_sparse^T * x + U * y - b ||_2 = %e\n",
vector_norm2<i_t, f_t>(residual_1));
}
if (full_debug && debug) {
csc_matrix_t<i_t, f_t> A_full_D = A;
A_full_D.scale_columns(inv_diag);
csc_matrix_t<i_t, f_t> A_full_D_T(A_full_D.n, A_full_D.m, 1);
A_full_D.transpose(A_full_D_T);
csc_matrix_t<i_t, f_t> ADAT_full(AD.m, AD.m, 1);
multiply(A, A_full_D_T, ADAT_full);
f_t max_error = 0.0;
for (i_t i = 0; i < AD.m; i++) {
dense_vector_t<i_t, f_t> ei(AD.m);
ei.set_scalar(0.0);
ei[i] = 1.0;
dense_vector_t<i_t, f_t> u(AD.m);
matrix_vector_multiply(ADAT_full, 1.0, ei, 0.0, u);
adat_multiply(-1.0, ei, 1.0, u);
max_error = std::max(max_error, vector_norm2<i_t, f_t>(u));
}
settings_.log.printf("|| ADAT(e_i) - ADA^T * e_i ||_2 = %e\n", max_error);
}
if (debug) {
dense_matrix_t<i_t, f_t> UUT(AD.m, AD.m);
for (i_t i = 0; i < AD.m; i++) {
dense_vector_t<i_t, f_t> ei(AD.m);
ei.set_scalar(0.0);
ei[i] = 1.0;
dense_vector_t<i_t, f_t> UTei(n_dense_columns);
AD_dense.transpose_multiply(1.0, ei, 0.0, UTei);
dense_vector_t<i_t, f_t> U_col(AD.m);
AD_dense.matrix_vector_multiply(1.0, UTei, 0.0, U_col);
UUT.set_column(i, U_col);
}
csc_matrix_t<i_t, f_t> A_dense_csc = A;
A_dense_csc.remove_columns(sparse_mark);
std::vector<f_t> inv_diag_prime(n_dense_columns);
i_t k = 0;
for (i_t j : dense_columns) {
inv_diag_prime[k++] = std::sqrt(inv_diag[j]);
}
A_dense_csc.scale_columns(inv_diag_prime);
csc_matrix_t<i_t, f_t> AT_dense_transpose(1, 1, 1);
A_dense_csc.transpose(AT_dense_transpose);
csc_matrix_t<i_t, f_t> ADAT_dense_csc(AD.m, AD.m, 1);
multiply(A_dense_csc, AT_dense_transpose, ADAT_dense_csc);
dense_matrix_t<i_t, f_t> ADAT_dense(AD.m, AD.m);
for (i_t k = 0; k < AD.m; k++) {
ADAT_dense.from_sparse(ADAT_dense_csc, k, k);
}
f_t max_error = 0.0;
for (i_t i = 0; i < AD.m; i++) {
for (i_t j = 0; j < AD.m; j++) {
f_t ij_error = std::abs(ADAT_dense(i, j) - UUT(i, j));
max_error = std::max(max_error, ij_error);
}
}
settings_.log.printf("|| ADAT_dense - UUT ||_2 = %e\n", max_error);
csc_matrix_t<i_t, f_t> A_sparse = A;
std::vector<i_t> remove_dense(A.n, 0);
for (i_t k : dense_columns) {
remove_dense[k] = 1;
}
A_sparse.remove_columns(remove_dense);
std::vector<f_t> inv_diag_sparse(A.n - n_dense_columns);
i_t new_j = 0;
for (i_t j = 0; j < A.n; j++) {
if (cols_to_remove[j]) { continue; }
inv_diag_sparse[new_j++] = std::sqrt(inv_diag[j]);
}
A_sparse.scale_columns(inv_diag_sparse);
csc_matrix_t<i_t, f_t> AT_sparse_transpose(1, 1, 1);
A_sparse.transpose(AT_sparse_transpose);
csc_matrix_t<i_t, f_t> ADAT_sparse(AD.m, AD.m, 1);
multiply(A_sparse, AT_sparse_transpose, ADAT_sparse);
csc_matrix_t<i_t, f_t> error(AD.m, AD.m, 1);
add(ADAT_sparse, ADAT, 1.0, -1.0, error);
settings_.log.printf("|| ADAT_sparse - ADAT ||_1 = %e\n", error.norm1());
csc_matrix_t<i_t, f_t> ADAT_test(AD.m, AD.m, 1);
add(ADAT_sparse, ADAT_dense_csc, 1.0, 1.0, ADAT_test);
csc_matrix_t<i_t, f_t> ADAT_all_columns(AD.m, AD.m, 1);
csc_matrix_t<i_t, f_t> AT_all_columns(AD.n, AD.m, 1);
A.transpose(AT_all_columns);
csc_matrix_t<i_t, f_t> A_scaled = A;
A_scaled.scale_columns(inv_diag);
multiply(A_scaled, AT_all_columns, ADAT_all_columns);
csc_matrix_t<i_t, f_t> error2(AD.m, AD.m, 1);
add(ADAT_test, ADAT_all_columns, 1.0, -1.0, error2);
int64_t large_nz = 0;
for (i_t j = 0; j < AD.m; j++) {
i_t col_start = error2.col_start[j];
i_t col_end = error2.col_start[j + 1];
for (i_t p = col_start; p < col_end; p++) {
if (std::abs(error2.x[p]) > 1e-6) {
large_nz++;
settings_.log.printf(
"large_nz (%d,%d) %e. m %d\n", error2.i[p], j, error2.x[p], AD.m);
}
}
}
settings_.log.printf(
"|| A_sparse * D_sparse * A_sparse^T + A_dense * D_dense * A_dense^T - ADAT ||_1 = %e "
"nz "
"%e large_nz %ld\n",
error2.norm1(),
static_cast<f_t>(error2.col_start[AD.m]),
large_nz);
}
if (full_debug && debug) {
f_t max_error = 0.0;
f_t max_row_error = 0.0;
for (i_t i = 0; i < AD.m; i++) {
dense_vector_t<i_t, f_t> ei(AD.m);
ei.set_scalar(0.0);
ei[i] = 1.0;
dense_vector_t<i_t, f_t> VTei(n_dense_columns);
AD_dense.transpose_multiply(1.0, ei, 0.0, VTei);
f_t row_error = 0.0;
for (i_t k = 0; k < n_dense_columns; k++) {
i_t j = dense_columns[k];
row_error += std::abs(VTei[k] - AD_dense(i, k));
}
if (row_error > 1e-10) { settings_.log.printf("row_error %d = %e\n", i, row_error); }
max_row_error = std::max(max_row_error, row_error);
dense_vector_t<i_t, f_t> u(AD.m);
A_dense.matrix_vector_multiply(1.0, VTei, 0.0, u);
matrix_vector_multiply(ADAT, 1.0, ei, 1.0, u);
adat_multiply(-1.0, ei, 1.0, u);
max_error = std::max(max_error, vector_norm2<i_t, f_t>(u));
}
settings_.log.printf(
"|| (A_sparse * D_sparse * A_sparse^T + U * V^T) * e_i - ADA^T * e_i ||_2 = %e\n",
max_error);
}
if (debug) {
dense_vector_t<i_t, f_t> total_residual = b;
adat_multiply(1.0, x, -1.0, total_residual);
settings_.log.printf("|| A * D * A^T * x - b ||_2 = %e\n",
vector_norm2<i_t, f_t>(total_residual));
}
// Now do some rounds of PCG
const bool do_pcg = true;
if (do_pcg) {
struct op_t {
const iteration_data_t* self;
op_t(const iteration_data_t* s) : self(s) {}
void a_multiply(f_t alpha,
const dense_vector_t<i_t, f_t>& x,
f_t beta,
dense_vector_t<i_t, f_t>& y) const
{
self->adat_multiply(alpha, x, beta, y);
}
void m_solve(const dense_vector_t<i_t, f_t>& b, dense_vector_t<i_t, f_t>& x) const
{
self->chol->solve(b, x);
}
} op(this);
preconditioned_conjugate_gradient(op, settings_, b, 1e-9, x);
}
return solve_status;
}
}
i_t gpu_solve_adat(rmm::device_uvector<f_t>& d_b, rmm::device_uvector<f_t>& d_x)
{
if (n_dense_columns == 0) {
// Solve ADAT * x = b
return chol->solve(d_b, d_x);
} else {
// TMP until this is ported to the GPU
dense_vector_t<i_t, f_t> b = host_copy(d_b, stream_view_);
dense_vector_t<i_t, f_t> x = host_copy(d_x, stream_view_);
i_t out = solve_adat(b, x);
d_b.resize(b.size(), stream_view_);
raft::copy(d_b.data(), b.data(), b.size(), stream_view_);
d_x.resize(x.size(), stream_view_);
raft::copy(d_x.data(), x.data(), x.size(), stream_view_);
stream_view_.synchronize(); // host x can go out of scope before copy finishes
return out;
}
}
void to_solution(const lp_problem_t<i_t, f_t>& lp,
i_t iterations,
f_t objective,
f_t user_objective,
f_t primal_residual,
f_t dual_residual,
cusparse_view_t<i_t, f_t>& cusparse_view,
lp_solution_t<i_t, f_t>& solution)
{
solution.x = copy(x);
solution.y = y;
dense_vector_t<i_t, f_t> z_tilde(z.size());
scatter_upper_bounds(v, z_tilde);
z_tilde.axpy(1.0, z, -1.0);
solution.z = z_tilde;
dense_vector_t<i_t, f_t> dual_res = z_tilde;
dual_res.axpy(-1.0, lp.objective, 1.0);
if (use_gpu) {
cusparse_view.transpose_spmv(1.0, solution.y, 1.0, dual_res);
} else {
matrix_transpose_vector_multiply(lp.A, 1.0, solution.y, 1.0, dual_res);
}
f_t dual_residual_norm = vector_norm_inf<i_t, f_t>(dual_res, stream_view_);
#ifdef PRINT_INFO
settings_.log.printf("Solution Dual residual: %e\n", dual_residual_norm);
#endif
solution.iterations = iterations;
solution.objective = objective;
solution.user_objective = user_objective;
solution.l2_primal_residual = primal_residual;
solution.l2_dual_residual = dual_residual_norm;
}
void find_dense_columns(const csc_matrix_t<i_t, f_t>& A,
const simplex_solver_settings_t<i_t, f_t>& settings,
std::vector<i_t>& columns_to_remove,
i_t& n_dense_rows,
i_t& max_row_nz,
f_t& estimated_nz_AAT)
{
f_t start_column_density = tic();
const i_t m = A.m;
const i_t n = A.n;
// Quick return if the problem is small
if (m < 500) { return; }
// The goal of this function is to find a set of dense columns in A
// If a column of A is (partially) dense, it will cause A*A^T to be completely full.
//
// We can write A*A^T = sum_j A(:, j) * A(:, j)^T
// We can split A*A^T into two parts
// A*A^T = sum_{j such that A(:, j) is sparse} A(:, j) * A(:, j)^T
// + sum_{j such that A(:, j) is dense} A(:, j) * A(:, j)^T
// We call the first term A_sparse * A_sparse^T and the second term A_dense * A_dense^T
//
// We can then perform a sparse factorization of A_sparse * A_sparse^T
// And use Schur complement techniques to extend this to allow us to solve with all of A*A^T
// Thus, our goal is to find the columns that add the largest number of nonzeros to A*A^T
// It is too expensive for us to compute the exact sparsity pattern that each column of A
// contributes to A*A^T. Instead, we will use a heuristic method to estimate this.
// This function roughly follows the approach taken in the paper:
//
//
// Meszaros, C. Detecting "dense" columns in interior point methods for linear programs.
// Comput Optim Appl 36, 309-320 (2007). https://doi.org/10.1007/s10589-006-9008-6
//
// But the reason for this detailed comment is to explain what the algorithm
// given in the paper is doing.
//
// A loose upper bound is that column j contributes |A(:, j) |^2 nonzeros to A*A^T
// However, this upper bound assumes that each column of A is independent, when in
// fact there is overlap in the sparsity pattern of A(:, j_1) and A(:, j_2)
//
//
// Sort the columns of A according to their number of nonzeros
std::vector<i_t> column_nz(n);
i_t max_col_nz = 0;
for (i_t j = 0; j < n; j++) {
column_nz[j] = A.col_start[j + 1] - A.col_start[j];
max_col_nz = std::max(column_nz[j], max_col_nz);
}
if (max_col_nz < 100) { return; } // Quick return if all columns of A have few nonzeros
std::vector<i_t> column_nz_permutation(n);
std::iota(column_nz_permutation.begin(), column_nz_permutation.end(), 0);
std::sort(column_nz_permutation.begin(),
column_nz_permutation.end(),
[&column_nz](i_t i, i_t j) { return column_nz[i] < column_nz[j]; });
// We then compute the exact sparsity pattern for columns of A whose where
// the number of nonzeros is less than a threshold. This part can be done
// quickly given that each column has only a few nonzeros. We will approximate
// the effect of the dense columns a little later.
const i_t threshold = 300;
// Let C = A * A^T, the kth column of C is given by
//
// C(:, k) = A * A^T(:, k)
// = A * A(k, :)^T
// = sum_{j=1}^n A(:, j) * A(k, j)
// = sum_{j : A(k, j) != 0} A(:, j) * A(k, j)
//
// Thus we can compute the sparsity pattern associated with
// the kth column of C by maintaining a single array of size m
// and adding entries into that array as we traverse different
// columns A(:, j)
std::vector<i_t> mark(m, 0);
// We will compute two arrays