ipopt-src 0.2.3+3.14.16

Redistribution of Coin-OR Ipopt as a crate
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
// Copyright (C) 2005, 2010 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors:  Andreas Waechter                 IBM    2006-01-04
//

#include "IpoptConfig.h"
#include "IpWsmpSolverInterface.hpp"

#include <cmath>
#include <cstdio>

/** Prototypes for WSMP's subroutines */
extern "C"
{
   void IPOPT_WSMP_FUNC(wsetmaxthrds, WSETMAXTHRDS)(
      const ipindex* NTHREADS
   );

   void IPOPT_WSMP_FUNC(wssmp, WSSMP)(
      const ipindex* N,
      const ipindex* IA,
      const ipindex* JA,
      const double*  AVALS,
      double*        DIAG,
      ipindex*       PERM,
      ipindex*       INVP,
      double*        B,
      const ipindex* LDB,
      const ipindex* NRHS,
      double*        AUX,
      const ipindex* NAUX,
      ipindex*       MRP,
      ipindex*       IPARM,
      double*        DPARM
   );

   void IPOPT_WSMP_FUNC_(wsmp_clear, WSMP_CLEAR)(void);

   void IPOPT_WSMP_FUNC_(wsmp_version, WSMP_VERSION)(
      int*  V,
      int*  R,
      int*  M
   );
}

namespace Ipopt
{
#if IPOPT_VERBOSITY > 0
static const Index dbg_verbosity = 3;
#endif

WsmpSolverInterface::WsmpSolverInterface(
#ifdef PARDISO_MATCHING_PREPROCESS
   SmartPtr<LibraryLoader> pardisoloader_
#endif
)  : a_(NULL),
#ifdef PARDISO_MATCHING_PREPROCESS
   ia2(NULL),
   ja2(NULL),
   a2_(NULL),
   perm2(NULL),
   scale2(NULL),
   pardisoloader(pardisoloader_),
   smat_reordering_pardiso_wsmp(NULL),
#endif
   negevals_(-1),
   initialized_(false),
   PERM_(NULL),
   INVP_(NULL),
   MRP_(NULL)
{
   DBG_START_METH("WsmpSolverInterface::WsmpSolverInterface()", dbg_verbosity);

   IPARM_ = new Index[64];
   DPARM_ = new double[64];
}

WsmpSolverInterface::~WsmpSolverInterface()
{
   DBG_START_METH("WsmpSolverInterface::~WsmpSolverInterface()",
                  dbg_verbosity);

   // Clear WSMP's memory
   IPOPT_WSMP_FUNC_(wsmp_clear, WSMP_CLEAR)();

#ifdef PARDISO_MATCHING_PREPROCESS
   delete[] ia2;
   delete[] ja2;
   delete[] a2_;
   delete[] perm2;
   delete[] scale2;
#endif

   delete[] PERM_;
   delete[] INVP_;
   delete[] MRP_;
   delete[] IPARM_;
   delete[] DPARM_;
   delete[] a_;
}

void WsmpSolverInterface::RegisterOptions(
   SmartPtr<RegisteredOptions> roptions
)
{
   roptions->AddIntegerOption(
      "wsmp_num_threads",
      "Number of threads to be used in WSMP",
      1);
   roptions->AddBoundedIntegerOption(
      "wsmp_ordering_option",
      "Determines how ordering is done in WSMP",
      -2, 3,
      1,
      "This corresponds to the value of WSSMP's IPARM(16).");
   roptions->AddBoundedIntegerOption(
      "wsmp_ordering_option2",
      "Determines how ordering is done in WSMP",
      0, 3,
      1,
      "This corresponds to the value of WSSMP's IPARM(20).",
      true);
   roptions->AddBoundedNumberOption(
      "wsmp_pivtol",
      "Pivot tolerance for the linear solver WSMP.",
      0.0, true,
      1.0, true,
      1e-4,
      "A smaller number pivots for sparsity, a larger number pivots for stability.");
   roptions->AddBoundedNumberOption(
      "wsmp_pivtolmax",
      "Maximum pivot tolerance for the linear solver WSMP.",
      0.0, true,
      1.0, true,
      1e-1,
      "Ipopt may increase pivtol as high as pivtolmax to get a more accurate solution to the linear system.");
   roptions->AddBoundedIntegerOption(
      "wsmp_scaling",
      "Determines how the matrix is scaled by WSMP.",
      0, 3,
      0,
      "This corresponds to the value of WSSMP's IPARM(10).");
   roptions->AddBoundedNumberOption(
      "wsmp_singularity_threshold",
      "WSMP's singularity threshold.",
      0.0, true,
      1.0, true,
      1e-18,
      "WSMP's DPARM(10) parameter. "
      "The smaller this value the less likely a matrix is declared singular.");
   roptions->AddLowerBoundedIntegerOption(
      "wsmp_write_matrix_iteration",
      "Iteration in which the matrices are written to files.",
      -1,
      -1,
      "If non-negative, this option determines the iteration in which all matrices given to WSMP are written to files.",
      true);
   roptions->AddBoolOption(
      "wsmp_skip_inertia_check",
      "Whether to always pretend that inertia is correct.",
      false,
      "Setting this option to \"yes\" essentially disables inertia check. "
      "This option makes the algorithm non-robust and easily fail, but it might give some insight into the necessity of inertia control.",
      true);
   roptions->AddStringOption2(
      "wsmp_no_pivoting",
      "Whether to use the static pivoting option of WSMP.",
      "no",
      "no", "use the regular version",
      "yes", "use static pivoting",
      "Setting this option to \"yes\" means that WSMP is instructed not to do pivoting. "
      "This works only in certain situations (when the Hessian block is known to be positive definite or when we are using L-BFGS). "
      "It can also lead to a lot of fill-in.",
      true);
}

void WsmpSolverInterface::GetVersion(
   int& V,
   int& R,
   int& M
)
{
   IPOPT_WSMP_FUNC_(wsmp_version, WSMP_VERSION)(&V, &R, &M);
}

bool WsmpSolverInterface::InitializeImpl(
   const OptionsList& options,
   const std::string& prefix
)
{
#ifdef PARDISO_MATCHING_PREPROCESS
   DBG_ASSERT(IsValid(pardisoloader));
   smat_reordering_pardiso_wsmp = (IPOPT_DECL_SMAT_REORDERING_PARDISO_WSMP(*))pardisoloader->loadSymbol("smat_reordering_pardiso_wsmp");
   DBG_ASSERT(smat_reordering_pardiso_wsmp);
#endif

   options.GetIntegerValue("wsmp_num_threads", wsmp_num_threads_, prefix);
   Index wsmp_ordering_option;
   options.GetIntegerValue("wsmp_ordering_option", wsmp_ordering_option, prefix);
   Index wsmp_ordering_option2;
   options.GetIntegerValue("wsmp_ordering_option2", wsmp_ordering_option2, prefix);
   options.GetNumericValue("wsmp_pivtol", wsmp_pivtol_, prefix);
   if( options.GetNumericValue("wsmp_pivtolmax", wsmp_pivtolmax_, prefix) )
   {
      ASSERT_EXCEPTION(wsmp_pivtolmax_ >= wsmp_pivtol_, OPTION_INVALID,
                       "Option \"wsmp_pivtolmax\": This value must be between "
                       "wsmp_pivtol and 1.");
   }
   else
   {
      wsmp_pivtolmax_ = Max(wsmp_pivtolmax_, wsmp_pivtol_);
   }
   options.GetNumericValue("wsmp_singularity_threshold", wsmp_singularity_threshold_, prefix);
   options.GetIntegerValue("wsmp_scaling", wsmp_scaling_, prefix);
   options.GetIntegerValue("wsmp_write_matrix_iteration", wsmp_write_matrix_iteration_, prefix);
   options.GetBoolValue("wsmp_skip_inertia_check", skip_inertia_check_, prefix);
   options.GetBoolValue("wsmp_no_pivoting", wsmp_no_pivoting_, prefix);

   // Reset all private data
   dim_ = 0;
   initialized_ = false;
   printed_num_threads_ = false;
   pivtol_changed_ = false;
   have_symbolic_factorization_ = false;
   factorizations_since_recomputed_ordering_ = -1;
   delete[] a_;
   a_ = NULL;
   delete[] PERM_;
   PERM_ = NULL;
   delete[] INVP_;
   INVP_ = NULL;
   delete[] MRP_;
   MRP_ = NULL;

#ifdef PARDISO_MATCHING_PREPROCESS
   delete[] ia2;
   ia2 = NULL;

   delete[] ja2;
   ja2 = NULL;

   delete[] a2_;
   a2_ = NULL;

   delete[] perm2;
   perm2 = NULL;

   delete[] scale2;
   scale2 = NULL;
#endif

   // Set the number of threads
   Index NTHREADS = wsmp_num_threads_;
   IPOPT_WSMP_FUNC(wsetmaxthrds, WSETMAXTHRDS)(&NTHREADS);
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "WSMP will use %" IPOPT_INDEX_FORMAT " threads.\n", wsmp_num_threads_);

   // Get WSMP's default parameters and set the ones we want differently
   IPARM_[0] = 0;
   IPARM_[1] = 0;
   IPARM_[2] = 0;
   Index idmy = 0;
   double ddmy = 0.;
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&idmy, &idmy, &idmy, &ddmy, &ddmy, &idmy, &idmy, &ddmy, &idmy, &idmy, &ddmy, &idmy, &idmy,
                                 IPARM_, DPARM_);

   if( IPARM_[63] < 0 )
   {
      Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA, "Error %" IPOPT_INDEX_FORMAT " from WSMP initialization.\n", IPARM_[63]);
      return false;
   }

   IPARM_[15] = wsmp_ordering_option; // ordering option
   IPARM_[17] = 0; // use local minimum fill-in ordering
   IPARM_[19] = wsmp_ordering_option2; // for ordering in IP methods?
   if( wsmp_no_pivoting_ )
   {
      IPARM_[30] = 1; // want L D L^T factorization with diagonal no pivoting
      IPARM_[26] = 1;
   }
   else
   {
      IPARM_[30] = 2; // want L D L^T factorization with diagonal with pivoting
   }
   // pivoting (Bunch/Kaufman)
   //IPARM_[31] = 1; // need D to see where first negative eigenvalue occurs
   //                   if we change this, we need DIAG arguments below!

   IPARM_[10] = 2; // Mark bad pivots

   // Set WSMP's scaling option
   IPARM_[9] = wsmp_scaling_;

   DPARM_[9] = wsmp_singularity_threshold_;

   matrix_file_number_ = 0;

   // Check for SPINLOOPTIME and YIELDLOOPTIME?

   return true;
}

ESymSolverStatus WsmpSolverInterface::MultiSolve(
   bool         new_matrix,
   const Index* ia,
   const Index* ja,
   Index        nrhs,
   double*      rhs_vals,
   bool         check_NegEVals,
   Index        numberOfNegEVals
)
{
   DBG_START_METH("WsmpSolverInterface::MultiSolve", dbg_verbosity);
   DBG_ASSERT(!check_NegEVals || ProvidesInertia());
   DBG_ASSERT(initialized_);

   if( !printed_num_threads_ )
   {
      Jnlst().Printf(J_ITERSUMMARY, J_LINEAR_ALGEBRA,
                     "  -- WSMP is working with %" IPOPT_INDEX_FORMAT " thread%s.\n", IPARM_[32], IPARM_[32] == 1 ? "" : "s");
      printed_num_threads_ = true;
   }
   // check if a factorization has to be done
   if( new_matrix || pivtol_changed_ )
   {
      pivtol_changed_ = false;
      // perform the factorization
      ESymSolverStatus retval;
      retval = Factorization(ia, ja, check_NegEVals, numberOfNegEVals);
      if( retval != SYMSOLVER_SUCCESS )
      {
         DBG_PRINT((1, "FACTORIZATION FAILED!\n"));
         return retval;  // Matrix singular or error occurred
      }
   }

   // do the solve
   return Solve(ia, ja, nrhs, rhs_vals);
}

double* WsmpSolverInterface::GetValuesArrayPtr()
{
   DBG_ASSERT(initialized_);
   DBG_ASSERT(a_);
   return a_;
}

ESymSolverStatus WsmpSolverInterface::InitializeStructure(
   Index        dim,
   Index        nonzeros,
   const Index* ia,
   const Index* ja
)
{
   DBG_START_METH("WsmpSolverInterface::InitializeStructure", dbg_verbosity);
   dim_ = dim;
   nonzeros_ = nonzeros;

   // Make space for storing the matrix elements
   delete[] a_;
   a_ = NULL;
   a_ = new double[nonzeros];

   // Do the symbolic factorization
   ESymSolverStatus retval = SymbolicFactorization(ia, ja);
   if( retval != SYMSOLVER_SUCCESS )
   {
      return retval;
   }

   initialized_ = true;

   return retval;
}

ESymSolverStatus WsmpSolverInterface::SymbolicFactorization(
   const Index* /*ia*/,
   const Index* /*ja*/
)
{
   DBG_START_METH("WsmpSolverInterface::SymbolicFactorization",
                  dbg_verbosity);

   // This is postponed until the first factorization call, since
   // then the values in the matrix are known
   return SYMSOLVER_SUCCESS;
}

ESymSolverStatus WsmpSolverInterface::InternalSymFact(
   const Index* ia,
   const Index* ja,
   Index        numberOfNegEVals
)
{
   if( HaveIpData() )
   {
      IpData().TimingStats().LinearSystemSymbolicFactorization().Start();
   }

   // Create space for the permutations
   delete[] PERM_;
   PERM_ = NULL;
   delete[] INVP_;
   INVP_ = NULL;
   delete[] MRP_;
   MRP_ = NULL;
   PERM_ = new Index[dim_];
   INVP_ = new Index[dim_];
   MRP_ = new Index[dim_];

   Index N = dim_;

#ifdef PARDISO_MATCHING_PREPROCESS

   delete[] ia2;
   ia2 = NULL;

   delete[] ja2;
   ja2 = NULL;

   delete[] a2_;
   a2_ = NULL;

   delete[] perm2;
   perm2 = NULL;

   delete[] scale2;
   scale2 = NULL;

   ia2 = new Index[N + 1];
   ja2 = new Index[nonzeros_];
   a2_ = new double[nonzeros_];
   perm2 = new Index[N];
   scale2 = new double[N];
   Index* tmp2_ = new Index[N];

   smat_reordering_pardiso_wsmp(&N, ia, ja, a_, ia2, ja2, a2_, perm2, scale2, tmp2_, 0);

   delete[] tmp2_;

#endif

   // Call WSSMP for ordering and symbolic factorization
   Index NAUX = 0;
   IPARM_[1] = 1; // ordering
   IPARM_[2] = 2; // symbolic factorization
#ifdef PARDISO_MATCHING_PREPROCESS
   IPARM_[9] = 2; // switch off WSMP's ordering and scaling
   IPARM_[15] = -1;// switch off WSMP's ordering and scaling
   IPARM_[30] = 6;// next step supernode pivoting , since not implemented
   // =2 regular Bunch/Kaufman
   // =1 no pivots
   // =6 limited pivots
   DPARM_[21] = 2e-8;// set pivot perturbation
#endif
   Index idmy = 0;
   double ddmy = 0.;

   if( wsmp_no_pivoting_ )
   {
      IPARM_[14] = dim_ - numberOfNegEVals; // CHECK
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "Restricting WSMP static pivot sequence with IPARM(15) = %" IPOPT_INDEX_FORMAT "\n", IPARM_[14]);
   }

   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Calling WSSMP-1-2 for ordering and symbolic factorization.\n");
#ifdef PARDISO_MATCHING_PREPROCESS
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia2, ja2, a2_, &ddmy, PERM_, INVP_,
#else
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia, ja, a_, &ddmy, PERM_, INVP_,
#endif
                                 &ddmy, &idmy, &idmy, &ddmy, &NAUX, MRP_, IPARM_, DPARM_);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Done with WSSMP-1-2 for ordering and symbolic factorization.\n");

   Index ierror = IPARM_[63];
   if( ierror != 0 )
   {
      if( ierror == -102 )
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error: WSMP is not able to allocate sufficient amount of memory during ordering/symbolic factorization.\n");
      }
      else if( ierror > 0 )
      {
         Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                        "Matrix appears to be singular (with ierror = %" IPOPT_INDEX_FORMAT ").\n", ierror);
         if( HaveIpData() )
         {
            IpData().TimingStats().LinearSystemSymbolicFactorization().End();
         }
         return SYMSOLVER_SINGULAR;
      }
      else
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error in WSMP during ordering/symbolic factorization phase.\n     Error code is %" IPOPT_INDEX_FORMAT ".\n", ierror);
      }
      if( HaveIpData() )
      {
         IpData().TimingStats().LinearSystemSymbolicFactorization().End();
      }
      return SYMSOLVER_FATAL_ERROR;
   }
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "Predicted memory usage for WSSMP after symbolic factorization IPARM(23)= %" IPOPT_INDEX_FORMAT ".\n", IPARM_[22]);
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "Predicted number of nonzeros in factor for WSSMP after symbolic factorization IPARM(23)= %" IPOPT_INDEX_FORMAT ".\n", IPARM_[23]);

   if( HaveIpData() )
   {
      IpData().TimingStats().LinearSystemSymbolicFactorization().End();
   }

   return SYMSOLVER_SUCCESS;
}

ESymSolverStatus WsmpSolverInterface::Factorization(
   const Index* ia,
   const Index* ja,
   bool         check_NegEVals,
   Index        numberOfNegEVals
)
{
   DBG_START_METH("WsmpSolverInterface::Factorization", dbg_verbosity);

   // If desired, write out the matrix
   Index iter_count = -1;
   if( HaveIpData() )
   {
      iter_count = IpData().iter_count();
   }
   if( iter_count == wsmp_write_matrix_iteration_ )
   {
      matrix_file_number_++;
      char buf[256];
      Snprintf(buf, 255, "wsmp_matrix_%" IPOPT_INDEX_FORMAT "_%" IPOPT_INDEX_FORMAT ".dat", iter_count, matrix_file_number_);
      Jnlst().Printf(J_SUMMARY, J_LINEAR_ALGEBRA,
                     "Writing WSMP matrix into file %s.\n", buf);
      FILE* fp = fopen(buf, "w");
      fprintf(fp, "%" IPOPT_INDEX_FORMAT "\n", dim_); // N
      for( Index icol = 0; icol < dim_; icol++ )
      {
         fprintf(fp, "%" IPOPT_INDEX_FORMAT "", ia[icol + 1] - ia[icol]); // number of elements for this column
         // Now for each colum we write row indices and values
         for( Index irow = ia[icol]; irow < ia[icol + 1]; irow++ )
         {
            fprintf(fp, " %23.16e %" IPOPT_INDEX_FORMAT "", a_[irow - 1], ja[irow - 1]);
         }
         fprintf(fp, "\n");
      }
      fclose(fp);
   }

   // Check if we have to do the symbolic factorization and ordering
   // phase yet
   if( !have_symbolic_factorization_ )
   {
      ESymSolverStatus retval = InternalSymFact(ia, ja, numberOfNegEVals);
      if( retval != SYMSOLVER_SUCCESS )
      {
         return retval;
      }
      have_symbolic_factorization_ = true;
   }

   if( HaveIpData() )
   {
      IpData().TimingStats().LinearSystemFactorization().Start();
   }

   // Call WSSMP for numerical factorization
   Index N = dim_;
   Index NAUX = 0;
   IPARM_[1] = 3; // numerical factorization
   IPARM_[2] = 3; // numerical factorization
   DPARM_[10] = wsmp_pivtol_; // set current pivot tolerance
   Index idmy = 0;
   double ddmy = 0.;

#ifdef PARDISO_MATCHING_PREPROCESS
   {
      Index* tmp2_ = new Index[N];
      smat_reordering_pardiso_wsmp(&N, ia, ja, a_, ia2, ja2, a2_, perm2, scale2, tmp2_, 1);
      delete[] tmp2_;
   }
#endif

   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Calling WSSMP-3-3 for numerical factorization.\n");
#ifdef PARDISO_MATCHING_PREPROCESS
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia2, ja2, a2_, &ddmy, PERM_, INVP_, &ddmy, &idmy,
#else
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia, ja, a_, &ddmy, PERM_, INVP_, &ddmy, &idmy,
#endif
                                 &idmy, &ddmy, &NAUX, MRP_, IPARM_, DPARM_);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Done with WSSMP-3-3 for numerical factorization.\n");

   const Index ierror = IPARM_[63];
   if( ierror > 0 )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "WSMP detected that the matrix is singular and encountered %" IPOPT_INDEX_FORMAT " zero pivots.\n", dim_ + 1 - ierror);
      if( HaveIpData() )
      {
         IpData().TimingStats().LinearSystemFactorization().End();
      }
      return SYMSOLVER_SINGULAR;
   }
   else if( ierror != 0 )
   {
      if( ierror == -102 )
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error: WSMP is not able to allocate sufficient amount of memory during factorization.\n");
      }
      else
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error in WSMP during factorization phase.\n     Error code is %" IPOPT_INDEX_FORMAT ".\n", ierror);
      }
      if( HaveIpData() )
      {
         IpData().TimingStats().LinearSystemFactorization().End();
      }
      return SYMSOLVER_FATAL_ERROR;
   }
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "Memory usage for WSSMP after factorization IPARM(23) = %" IPOPT_INDEX_FORMAT "\n", IPARM_[22]);
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "Number of nonzeros in WSSMP after factorization IPARM(24) = %" IPOPT_INDEX_FORMAT "\n", IPARM_[23]);

   if( factorizations_since_recomputed_ordering_ != -1 )
   {
      factorizations_since_recomputed_ordering_++;
   }

   negevals_ = IPARM_[21]; // Number of negative eigenvalues determined during factorization

   // Check whether the number of negative eigenvalues matches the requested
   // count
   if( check_NegEVals && (numberOfNegEVals != negevals_) )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Wrong inertia: required are %" IPOPT_INDEX_FORMAT ", but we got %" IPOPT_INDEX_FORMAT ".\n", numberOfNegEVals, negevals_);
      if( skip_inertia_check_ )
      {
         Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                        "  But wsmp_skip_inertia_check is set.  Ignore inertia.\n");
         IpData().Append_info_string("IC ");
         negevals_ = numberOfNegEVals;
      }
      else
      {
         if( HaveIpData() )
         {
            IpData().TimingStats().LinearSystemFactorization().End();
         }
         return SYMSOLVER_WRONG_INERTIA;
      }
   }

   if( HaveIpData() )
   {
      IpData().TimingStats().LinearSystemFactorization().End();
   }
   return SYMSOLVER_SUCCESS;
}

ESymSolverStatus WsmpSolverInterface::Solve(
   const Index* ia,
   const Index* ja,
   Index        nrhs,
   double*      rhs_vals
)
{
   DBG_START_METH("WsmpSolverInterface::Solve", dbg_verbosity);

   if( HaveIpData() )
   {
      IpData().TimingStats().LinearSystemBackSolve().Start();
   }

   // Call WSMP to solve for some right hand sides (including
   // iterative refinement)
   // ToDo: Make iterative refinement an option?
   Index N = dim_;
   Index LDB = dim_;
   Index NRHS = nrhs;
   Index NAUX = 0;
   IPARM_[1] = 4; // Forward and Backward Elimination
   IPARM_[2] = 5; // Iterative refinement
   IPARM_[5] = 1;
   DPARM_[5] = 1e-12;

   double ddmy;
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Calling WSSMP-4-5 for backsolve.\n");

#ifdef PARDISO_MATCHING_PREPROCESS
   double* X = new double[nrhs * N];

   // Initialize solution with zero and save right hand side
   for (Index i = 0; i < nrhs * N; i++)
   {
      X[perm2[i]] = scale2[i] * rhs_vals[i];
   }
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia, ja, a_, &ddmy, PERM_, INVP_,
                                 X, &LDB, &NRHS, &ddmy, &NAUX,
                                 MRP_, IPARM_, DPARM_);
   for (Index i = 0; i < N; i++)
   {
      rhs_vals[i] = scale2[i] * X[perm2[i]];
   }
#else
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia, ja, a_, &ddmy, PERM_, INVP_, rhs_vals, &LDB, &NRHS, &ddmy, &NAUX, MRP_, IPARM_,
                                 DPARM_);
#endif

   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Done with WSSMP-4-5 for backsolve.\n");
   if( HaveIpData() )
   {
      IpData().TimingStats().LinearSystemBackSolve().End();
   }

   Index ierror = IPARM_[63];
   if( ierror != 0 )
   {
      if( ierror == -102 )
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error: WSMP is not able to allocate sufficient amount of memory during ordering/symbolic factorization.\n");
      }
      else
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error in WSMP during ordering/symbolic factorization phase.\n     Error code is %" IPOPT_INDEX_FORMAT ".\n", ierror);
      }
      return SYMSOLVER_FATAL_ERROR;
   }
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "Number of iterative refinement steps in WSSMP: %" IPOPT_INDEX_FORMAT "\n", IPARM_[5]);

#ifdef PARDISO_MATCHING_PREPROCESS
   delete [] X;
#endif

   return SYMSOLVER_SUCCESS;
}

Index WsmpSolverInterface::NumberOfNegEVals() const
{
   DBG_START_METH("WsmpSolverInterface::NumberOfNegEVals", dbg_verbosity);
   DBG_ASSERT(negevals_ >= 0);
   return negevals_;
}

bool WsmpSolverInterface::IncreaseQuality()
{
   DBG_START_METH("WsmpSolverInterface::IncreaseQuality", dbg_verbosity);

   if( factorizations_since_recomputed_ordering_ == -1 || factorizations_since_recomputed_ordering_ > 2 )
   {
      DPARM_[14] = 1.0;
      pivtol_changed_ = true;
      IpData().Append_info_string("RO ");
      factorizations_since_recomputed_ordering_ = 0;
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Triggering WSMP's recomputation of the ordering for next factorization.\n");
      return true;
   }
   if( wsmp_pivtol_ == wsmp_pivtolmax_ )
   {
      return false;
   }
   pivtol_changed_ = true;

   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "Increasing pivot tolerance for WSMP from %7.2e ", wsmp_pivtol_);
   wsmp_pivtol_ = Min(wsmp_pivtolmax_, std::pow(wsmp_pivtol_, 0.75));
   Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                  "to %7.2e.\n", wsmp_pivtol_);
   return true;
}

bool WsmpSolverInterface::ProvidesDegeneracyDetection() const
{
   return true;
}

ESymSolverStatus WsmpSolverInterface::DetermineDependentRows(
   const Index*      ia,
   const Index*      ja,
   std::list<Index>& c_deps
)
{
   DBG_START_METH("WsmpSolverInterface::DetermineDependentRows",
                  dbg_verbosity);

   c_deps.clear();

   ASSERT_EXCEPTION(!wsmp_no_pivoting_, OPTION_INVALID, "WSMP dependency detection does not work without pivoting.");

   if( !have_symbolic_factorization_ )
   {
      ESymSolverStatus retval = InternalSymFact(ia, ja, 0);
      if( retval != SYMSOLVER_SUCCESS )
      {
         return retval;
      }
      have_symbolic_factorization_ = true;
   }

   // Call WSSMP for numerical factorization to detect degenerate
   // rows/columns
   Index N = dim_;
   Index NAUX = 0;
   IPARM_[1] = 3; // numerical factorization
   IPARM_[2] = 3; // numerical factorization
   DPARM_[10] = wsmp_pivtol_; // set current pivot tolerance
   Index idmy = 0;
   double ddmy = 0.;

#ifdef PARDISO_MATCHING_PREPROCESS
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia2, ja2, a2_, &ddmy, PERM_, INVP_, &ddmy, &idmy,
                                 &idmy, &ddmy, &NAUX, MRP_, IPARM_, DPARM_);
#else
   IPOPT_WSMP_FUNC(wssmp, WSSMP)(&N, ia, ja, a_, &ddmy, PERM_, INVP_, &ddmy, &idmy, &idmy, &ddmy, &NAUX, MRP_, IPARM_,
                                 DPARM_);
#endif

   const Index ierror = IPARM_[63];
   if( ierror == 0 )
   {
      Index ii = 0;
      for( Index i = 0; i < N; i++ )
      {
         if( MRP_[i] == -1 )
         {
            c_deps.push_back(i);
            ii++;
         }
      }
      DBG_ASSERT(ii == IPARM_[20]);
   }
   if( ierror > 0 )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "WSMP detected that the matrix is singular and encountered %" IPOPT_INDEX_FORMAT " zero pivots.\n", dim_ + 1 - ierror);
      if( HaveIpData() )
      {
         IpData().TimingStats().LinearSystemFactorization().End();
      }
      return SYMSOLVER_SINGULAR;
   }
   else if( ierror != 0 )
   {
      if( ierror == -102 )
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error: WSMP is not able to allocate sufficient amount of memory during factorization.\n");
      }
      else
      {
         Jnlst().Printf(J_ERROR, J_LINEAR_ALGEBRA,
                        "Error in WSMP during factorization phase.\n     Error code is %" IPOPT_INDEX_FORMAT ".\n", ierror);
      }
      if( HaveIpData() )
      {
         IpData().TimingStats().LinearSystemFactorization().End();
      }
      return SYMSOLVER_FATAL_ERROR;
   }

   return SYMSOLVER_SUCCESS;
}

} // namespace Ipopt