Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgillis committed Jul 15, 2024
1 parent 176029b commit 222267a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 30 deletions.
5 changes: 0 additions & 5 deletions compiler/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ for (lin_solver_id,print_level, max_iters) in cases

s = madnlp_c_create(unsafe_convert(Ptr{MadnlpCInterface}, pointer_from_objref(nlp_interface)))

@info "lbx" inp.lbx
out = MadnlpCNumericOut()

in_c_ptr = madnlp_c_input(s)
Expand Down Expand Up @@ -176,10 +175,6 @@ for (lin_solver_id,print_level, max_iters) in cases
global mul = unsafe_wrap(Array, out_c.mul, ncon)
global mul_L = unsafe_wrap(Array, out_c.mul_L, nvar)
global mul_U = unsafe_wrap(Array, out_c.mul_U, nvar)
global primal_feas = unsafe_wrap(Array, out_c.primal_feas, 1)
global dual_feas = unsafe_wrap(Array, out_c.dual_feas, 1)

global iter = unsafe_wrap(Array, stats_c.iter, 1)

println("ret_code: ", Cret)
println("linear_solver: ", lin_solver_names[lin_solver_id])
Expand Down
49 changes: 41 additions & 8 deletions src/MadNLP_C.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ mutable struct MadnlpCNumericOut
end

mutable struct MadnlpCStats
iter::Ptr{Int64}
iter::Int64
status::Int64
dual_feas::Float64
primal_feas::Float64
MadnlpCStats() = new()
end

Expand Down Expand Up @@ -194,12 +197,10 @@ end

function NLPModels.jac_coord!(nlp::GenericModel, x::CuArray, J::CuArray)
copyto!(nlp.bf.x, x)
# copyto!(nlp.bf.jac_g, J)
Cx::Ptr{Cdouble} = Base.unsafe_convert(Ptr{Cdouble}, nlp.bf.x)
CJ::Ptr{Cdouble} = Base.unsafe_convert(Ptr{Cdouble}, nlp.bf.jac_g)
ret::Cint = ccall(nlp.eval_jac_g, Cint, (Ptr{Cdouble},Ptr{Cdouble},Ptr{Cvoid}), Cx, CJ, nlp.user_data)
if Bool(ret) @error "GPU jac failed" end
# J = unsafe_wrap(Array, CJ, nlp.meta.nnzj)
copyto!(J, nlp.bf.jac_g)
return J
end
Expand Down Expand Up @@ -307,6 +308,21 @@ Base.@ccallable function madnlp_c_create(nlp_interface::Ptr{MadnlpCInterface})::
unsafe_copyto!(Base.unsafe_convert(Ptr{Int64}, solver.nzh_i), interf.nzh_i, interf.nnzh)
unsafe_copyto!(Base.unsafe_convert(Ptr{Int64}, solver.nzh_j), interf.nzh_j, interf.nnzh)

solver.res = MadNLP.MadNLPExecutionStats{Float64, Vector{Float64}}(
MadNLP.MadNLPOptions(tol=0, callback=MadNLP.SparseCallback, kkt_system=MadNLP.SparseKKTSystem, linear_solver=MadNLPMumps.MumpsSolver),
MadNLP.INTERNAL_ERROR,
Vector{Float64}(undef, interf.nw),
0.0,
Vector{Float64}(undef, interf.nc),
0.0,
0.0,
Vector{Float64}(undef, interf.nc),
Vector{Float64}(undef, interf.nw),
Vector{Float64}(undef, interf.nw),
0,
MadNLP.MadNLPCounters(start_time=0.0)
)

# Copy the solver object to the allocated memory
unsafe_store!(solver_ptr, solver)

Expand All @@ -329,6 +345,11 @@ end
Base.@ccallable function madnlp_c_get_stats(s::Ptr{MadnlpCSolver})::Ptr{MadnlpCStats}
solver = unsafe_load(s)

solver.stats_c.iter = solver.res.iter
solver.stats_c.status = Integer(solver.res.status)
solver.stats_c.dual_feas = solver.res.dual_feas
solver.stats_c.primal_feas = solver.res.primal_feas

return Base.unsafe_convert(Ptr{MadnlpCStats},Ref(solver.stats_c))
end

Expand Down Expand Up @@ -533,7 +554,23 @@ Base.@ccallable function madnlp_c_solve(s::Ptr{MadnlpCSolver})::Cint
)

madnlp_solver = MadNLPSolver(nlp; print_level = madnlp_log, linear_solver = linear_solver)
solver.res = MadNLP.solve!(madnlp_solver, max_iter = Int(solver.max_iters))
res = MadNLP.solve!(madnlp_solver, max_iter = Int(solver.max_iters))
if GPU_DEVICE
copyto!(solver.res.solution, res.solution)
solver.res.objective = res.objective
copyto!(solver.res.constraints, res.constraints)
copyto!(solver.res.multipliers, res.multipliers)
copyto!(solver.res.multipliers_L, res.multipliers_L)
copyto!(solver.res.multipliers_U, res.multipliers_U)
solver.res.dual_feas = res.dual_feas
solver.res.primal_feas = res.primal_feas
solver.res.counters = res.counters
solver.res.iter = res.iter
solver.res.options = res.options
solver.res.status = res.status
else
solver.res = res
end

# Make results available to C
solver.out_c.sol = Base.unsafe_convert(Ptr{Cdouble},solver.res.solution)
Expand All @@ -542,10 +579,6 @@ Base.@ccallable function madnlp_c_solve(s::Ptr{MadnlpCSolver})::Cint
solver.out_c.mul = Base.unsafe_convert(Ptr{Cdouble},solver.res.multipliers)
solver.out_c.mul_L = Base.unsafe_convert(Ptr{Cdouble},solver.res.multipliers_L)
solver.out_c.mul_U = Base.unsafe_convert(Ptr{Cdouble},solver.res.multipliers_U)
solver.out_c.primal_feas = Base.unsafe_convert(Ptr{Cdouble},[solver.res.primal_feas])
solver.out_c.dual_feas = Base.unsafe_convert(Ptr{Cdouble},[solver.res.dual_feas])

solver.stats_c.iter = Base.unsafe_convert(Ptr{Int64},[Int64(solver.res.iter)])

return 0

Expand Down
34 changes: 30 additions & 4 deletions src/madnlp_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ typedef int (*MadnlpCEvalObjGrad)(const double*, double*, void*);
typedef int (*MadnlpCEvalConstrJac)(const double*, double*, void*);
typedef int (*MadnlpCEvalLagHess)(double, const double*, const double*, double*, void*);

enum MadnlpCStatus {
MADNLP_SOLVE_SUCCEEDED = 1,
MADNLP_SOLVED_TO_ACCEPTABLE_LEVEL = 2,
MADNLP_SEARCH_DIRECTION_BECOMES_TOO_SMALL = 3,
MADNLP_DIVERGING_ITERATES = 4,
MADNLP_INFEASIBLE_PROBLEM_DETECTED = 5,
MADNLP_MAXIMUM_ITERATIONS_EXCEEDED = 6,
MADNLP_MAXIMUM_WALLTIME_EXCEEDED = 7,
MADNLP_INITIAL = 11,
MADNLP_REGULAR = 12,
MADNLP_RESTORE = 13,
MADNLP_ROBUST = 14,
MADNLP_RESTORATION_FAILED = -1,
MADNLP_INVALID_NUMBER_DETECTED = -2,
MADNLP_ERROR_IN_STEP_COMPUTATION = -3,
MADNLP_NOT_ENOUGH_DEGREES_OF_FREEDOM = -4,
MADNLP_USER_REQUESTED_STOP = -5,
MADNLP_INTERNAL_ERROR = -6,
MADNLP_INVALID_NUMBER_OBJECTIVE = -7,
MADNLP_INVALID_NUMBER_GRADIENT = -8,
MADNLP_INVALID_NUMBER_CONSTRAINTS = -9,
MADNLP_INVALID_NUMBER_JACOBIAN = -10,
MADNLP_INVALID_NUMBER_HESSIAN_LAGRANGIAN = -11
};

struct MadnlpCInterface {
MadnlpCEvalObj eval_obj;
MadnlpCEvalConstr eval_constr;
Expand Down Expand Up @@ -81,12 +106,13 @@ struct MadnlpCNumericOut {
const double* mul;
const double* mul_L;
const double* mul_U;
const double* primal_feas;
const double* dual_feas;
};

struct MadnlpCStats {
const madnlp_int* iter;
madnlp_int iter;
madnlp_int status;
double dual_feas;
double primal_feas;
};

MADNLP_SYMBOL_EXPORT struct MadnlpCSolver* madnlp_c_create(struct MadnlpCInterface* nlp_interface);
Expand All @@ -111,4 +137,4 @@ MADNLP_SYMBOL_EXPORT void madnlp_c_destroy(struct MadnlpCSolver*);
}
#endif

#endif // _MADNLP_C_H
#endif // _MADNLP_C_H
22 changes: 9 additions & 13 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ int main(int argc, char** argv) {
interf.nzh_i = nzh_i;
interf.nzh_j = nzh_j;

interf.nnzo = nw;
interf.nnzj = 2;
interf.nnzh = 3;

Expand All @@ -81,15 +82,11 @@ int main(int argc, char** argv) {
double lbg[1] = {0};
double ubg[1] = {0};

printf("lbx %p, ubx %p\n", lbx, ubx);

printf("interf %p\n", &interf);

struct MadnlpCSolver* solver = madnlp_c_create(&interf);

madnlp_c_set_option_int(solver, "max_iters", 10);
madnlp_c_set_option_int(solver, "print_level", 2);
madnlp_c_set_option_int(solver, "lin_solver_id", 1);
madnlp_c_set_option_int(solver, "print_level", 0);
madnlp_c_set_option_int(solver, "lin_solver_id", 3);

const MadnlpCNumericIn* in = madnlp_c_input(solver);
std::copy(x0,x0+2,in->x0);
Expand Down Expand Up @@ -118,11 +115,7 @@ int main(int argc, char** argv) {
std::copy(out->mul_L,out->mul_L+nw,mul_L.begin());
std::copy(out->mul_U,out->mul_U+nw,mul_U.begin());
obj = *(out->obj);
primal_feas = *(out->primal_feas);
dual_feas = *(out->dual_feas);

const MadnlpCStats* stats = madnlp_c_get_stats(solver);
madnlp_int iter = *(stats->iter);

cout << "sol: ";
for (auto el: sol) cout << el << " "; cout << endl;
Expand All @@ -136,9 +129,12 @@ int main(int argc, char** argv) {
for (auto el: mul_U) cout << el << " "; cout << endl;

cout << "obj: " << obj << endl;
cout << "primal_feas: " << primal_feas << endl;
cout << "dual_feas: " << dual_feas << endl;
cout << "iter: " << iter << endl;
cout << "iter: " << stats->iter << endl;
cout << "status: " << stats->status << endl;
cout << "dual_feas: " << stats->dual_feas << endl;
cout << "primal_feas: " << stats->primal_feas << endl;

madnlp_c_solve(solver);

shutdown_julia(0);

Expand Down

0 comments on commit 222267a

Please sign in to comment.