Skip to content

Commit a16d445

Browse files
authored
Add pardiso settings (#61)
Add pardiso settings in support of oxfordcontrol/Clarabel.rs#181
1 parent 8961b97 commit a16d445

File tree

9 files changed

+154
-7
lines changed

9 files changed

+154
-7
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,20 @@ jobs:
4747
run: |
4848
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_expcone
4949
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_lp
50-
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_callback
5150
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_powcone
5251
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_genpowcone
5352
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_qp
53+
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_qp_f32
54+
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_qp_f64
5455
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_socp
56+
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/c/example_callback
57+
5558
5659
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_expcone
5760
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_lp
58-
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_callback
5961
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_powcone
6062
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_genpowcone
6163
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_qp
6264
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_socp
65+
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --error-exitcode=1 ./examples/cpp/cpp_example_callback
66+

examples/c/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(C_EXAMPLES
1212
example_json
1313
example_print_stream
1414
example_callback
15+
example_pardiso_mkl
1516
)
1617

1718
# Compile utils.c as a library

examples/c/example_pardiso_mkl.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// To use float instead of double:
2+
// #define CLARABEL_USE_FLOAT
3+
4+
#include "utils.h"
5+
#include <stdio.h>
6+
#include <Clarabel.h>
7+
8+
int main(void)
9+
{
10+
11+
#ifndef FEATURE_PARDISO_MKL
12+
printf("This example requires MKL Pardiso support.\n");
13+
return 1;
14+
#else
15+
16+
// QP Example
17+
18+
/* From dense matrix:
19+
* [[6., 0.],
20+
* [0., 4.]]
21+
*/
22+
ClarabelCscMatrix P;
23+
clarabel_CscMatrix_init(
24+
&P,
25+
2, // row
26+
2, // col
27+
(uintptr_t[]){ 0, 1, 2 }, // colptr
28+
(uintptr_t[]){ 0, 1 }, // rowval
29+
(ClarabelFloat[]){ 6., 4. } // nzval
30+
);
31+
32+
ClarabelFloat q[2] = { -1., -4. };
33+
34+
/* From dense matrix:
35+
* [[ 1., -2.], // <-- LHS of equality constraint (lower bound)
36+
* [ 1., 0.], // <-- LHS of inequality constraint (upper bound)
37+
* [ 0., 1.], // <-- LHS of inequality constraint (upper bound)
38+
* [-1., 0.], // <-- LHS of inequality constraint (lower bound)
39+
* [ 0., -1.]] // <-- LHS of inequality constraint (lower bound)
40+
*/
41+
ClarabelCscMatrix A;
42+
clarabel_CscMatrix_init(
43+
&A,
44+
5, // row
45+
2, // col
46+
(uintptr_t[]){ 0, 3, 6 }, // colptr
47+
(uintptr_t[]){ 0, 1, 3, 0, 2, 4 }, // rowval
48+
(ClarabelFloat[]){ 1., 1., -1., -2., 1., -1. } // nzval
49+
);
50+
51+
ClarabelFloat b[5] = { 0., 1., 1., 1., 1. };
52+
53+
ClarabelSupportedConeT cones[2] = { ClarabelZeroConeT(1), ClarabelNonnegativeConeT(4) };
54+
55+
// Settings
56+
ClarabelDefaultSettings settings = clarabel_DefaultSettings_default();
57+
//settings.direct_solve_method = PARDISO_PANUA;
58+
settings.direct_solve_method = PARDISO_MKL;
59+
60+
// turn on verbose output and change some settings
61+
settings.pardiso_verbose = true;
62+
settings.pardiso_iparm[1] = 0; // tries minimum degree ordering instead of default
63+
64+
// Build solver
65+
ClarabelDefaultSolver *solver = clarabel_DefaultSolver_new(
66+
&P, // P
67+
q, // q
68+
&A, // A
69+
b, // b
70+
2, // n_cones
71+
cones, &settings
72+
);
73+
74+
// Solve
75+
clarabel_DefaultSolver_solve(solver);
76+
77+
// Get solution
78+
ClarabelDefaultSolution solution = clarabel_DefaultSolver_solution(solver);
79+
print_solution(&solution);
80+
81+
// Get some detailed solve information
82+
ClarabelDefaultInfo_f64 info = clarabel_DefaultSolver_info(solver);
83+
printf("primal residual = %e\n", info.res_primal);
84+
printf("dual residual = %e\n", info.res_dual);
85+
printf("# of threads = %d\n", info.linsolver.threads);
86+
printf("KKT nonzeros = %d\n", info.linsolver.nnzA);
87+
printf("factor nonzeros = %d\n", info.linsolver.nnzL);
88+
89+
// Free the matrices and the solver
90+
clarabel_DefaultSolver_free(solver);
91+
92+
return 0;
93+
94+
#endif
95+
}

examples/cpp/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ set(CPP_EXAMPLES
77
example_qp
88
example_socp
99
example_sdp
10-
example_faer
11-
example_mkl_pardiso
1210
example_json
1311
example_print_stream
1412
example_callback
13+
example_faer
14+
example_pardiso_mkl
1515
)
1616

1717
# Define an executable target for each example
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ int main(void)
5454

5555
// Settings
5656
DefaultSettings<double> settings = DefaultSettings<double>::default_settings();
57-
settings.direct_solve_method = ClarabelDirectSolveMethods::PARDISO_MKL;
57+
58+
//settings.direct_solve_method = ClarabelDirectSolveMethods::PARDISO_PANUA;
59+
settings.direct_solve_method = ClarabelDirectSolveMethods::PARDISO_MKL;
60+
61+
settings.pardiso_verbose = true;
62+
settings.pardiso_iparm[1] = 0; // tries minimum degree ordering instead of default
63+
5864
//set the number of threads
5965
settings.max_threads = 16;
6066

@@ -68,6 +74,14 @@ int main(void)
6874
DefaultSolution<double> solution = solver.solution();
6975
utils::print_solution(solution);
7076

77+
DefaultInfo<double> info = solver.info();
78+
79+
printf("primal residual = %e\n", info.res_primal);
80+
printf("dual residual = %e\n", info.res_dual);
81+
printf("# of threads = %d\n", info.linsolver.threads);
82+
printf("KKT nonzeros = %d\n", info.linsolver.nnzA);
83+
printf("factor nonzeros = %d\n", info.linsolver.nnzL);
84+
7185
return 0;
7286

7387
#endif // FEATURE_FAER_SPARSE

include/c/DefaultSettings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ typedef struct ClarabelDefaultSettings_f64
7878
bool chordal_decomposition_compact;
7979
bool chordal_decomposition_complete_dual;
8080
#endif
81+
#ifdef FEATURE_PARDISO_ANY
82+
int32_t pardiso_iparm[64];
83+
bool pardiso_verbose;
84+
#endif
8185
} ClarabelDefaultSettings_f64;
8286

8387
typedef struct ClarabelDefaultSettings_f32
@@ -126,6 +130,10 @@ typedef struct ClarabelDefaultSettings_f32
126130
bool chordal_decomposition_compact;
127131
bool chordal_decomposition_complete_dual;
128132
#endif
133+
#ifdef FEATURE_PARDISO_ANY
134+
int32_t pardiso_iparm[64];
135+
bool pardiso_verbose;
136+
#endif
129137
} ClarabelDefaultSettings_f32;
130138

131139
#ifdef CLARABEL_USE_FLOAT

include/cpp/DefaultSettings.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ struct DefaultSettings
7979
bool chordal_decomposition_compact;
8080
bool chordal_decomposition_complete_dual;
8181
#endif
82-
82+
#ifdef FEATURE_PARDISO_ANY
83+
int32_t pardiso_iparm[64];
84+
bool pardiso_verbose;
85+
#endif
8386

8487
static DefaultSettings<T> default_settings();
8588
};
@@ -355,6 +358,22 @@ class DefaultSettingsBuilder
355358
return *this;
356359
}
357360
#endif
361+
#ifdef FEATURE_PARDISO_ANY
362+
DefaultSettingsBuilder<T> &pardiso_iparm(int32_t pardiso_iparm[64])
363+
{
364+
for (int i = 0; i < 64; i++)
365+
{
366+
settings.pardiso_iparm[i] = pardiso_iparm[i];
367+
}
368+
return *this;
369+
}
370+
371+
DefaultSettingsBuilder<T> &pardiso_verbose(bool pardiso_verbose)
372+
{
373+
settings.pardiso_verbose = pardiso_verbose;
374+
return *this;
375+
}
376+
#endif
358377

359378
};
360379

rust_wrapper/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ if(CLARABEL_FEATURE_PARDISO_MKL)
5757
# Define the FEATURE_PARDISO_MKL flag for all targets that link against clarabel_c
5858
target_compile_definitions(libclarabel_c_static INTERFACE FEATURE_PARDISO_MKL)
5959
target_compile_definitions(libclarabel_c_shared INTERFACE FEATURE_PARDISO_MKL)
60+
6061
endif()
6162

6263
# PARDISO_PANUA feature flag
@@ -70,6 +71,11 @@ if(CLARABEL_FEATURE_PARDISO_PANUA)
7071
target_compile_definitions(libclarabel_c_shared INTERFACE FEATURE_PARDISO_PANUA)
7172
endif()
7273

74+
if(CLARABEL_FEATURE_PARDISO_MKL OR CLARABEL_FEATURE_PARDISO_PANUA)
75+
target_compile_definitions(libclarabel_c_static INTERFACE FEATURE_PARDISO_ANY)
76+
target_compile_definitions(libclarabel_c_shared INTERFACE FEATURE_PARDISO_ANY)
77+
endif()
78+
7379

7480
# SERDE feature flag
7581
if(CLARABEL_FEATURE_SERDE)

0 commit comments

Comments
 (0)