-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpips_parallel_cfunc.jl
More file actions
628 lines (561 loc) · 20.7 KB
/
pips_parallel_cfunc.jl
File metadata and controls
628 lines (561 loc) · 20.7 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
#
# Wrapper for the paralel/structured PIPS-NLP interface
#
module PipsNlpSolver
using StructJuMPSolverInterface
import MPI
try
sharedLib=ENV["PIPS_NLP_PAR_SHARED_LIB"]
# Explicitly check if the file exists. dlopen sometimes does not throw an
# error for invalid filenames, resulting in a seg fault)
if(!isfile(sharedLib))
error(string("The specified shared library ([", sharedLib, "]) does not exist"))
end
global const libparpipsnlp=Libdl.dlopen(get(ENV,"PIPS_NLP_PAR_SHARED_LIB",""))
catch
warn("Could not load PIPS-NLP shared library. Make sure the ENV variable 'PIPS_NLP_PAR_SHARED_LIB' points to its location, usually in the PIPS repo at PIPS/build_pips/PIPS-NLP/libparpipsnlp.so")
rethrow()
end
#######################
type FakeModel <: ModelInterface
sense::Symbol
status::Int
nscen::Int
rowmap::Dict{Int,Int}
colmap::Dict{Int,Int}
eq_rowmap::Dict{Int,Int}
inq_rowmap::Dict{Int,Int}
get_num_scen::Function
get_sense::Function
get_status::Function
get_num_rows::Function
get_num_cols::Function
get_num_eq_cons::Function
get_num_ineq_cons::Function
set_status::Function
set_num_rows::Function
set_num_cols::Function
set_num_eq_cons::Function
set_num_ineq_cons::Function
str_init_x0::Function
str_prob_info::Function
str_eval_f::Function
str_eval_g::Function
str_eval_grad_f::Function
str_eval_jac_g::Function
str_eval_h::Function
str_write_solution::Function
function FakeModel(sense::Symbol,status::Int,nscen::Int, str_init_x0, str_prob_info, str_eval_f, str_eval_g,str_eval_grad_f,str_eval_jac_g, str_eval_h)
instance = new(sense,status,nscen,Dict{Int,Int}(),Dict{Int,Int}(),Dict{Int,Int}(),Dict{Int,Int}())
instance.str_init_x0 = str_init_x0
instance.str_prob_info = str_prob_info
instance.str_eval_f = str_eval_f
instance.str_eval_g = str_eval_g
instance.str_eval_grad_f = str_eval_grad_f
instance.str_eval_jac_g = str_eval_jac_g
instance.str_eval_h = str_eval_h
instance.str_write_solution = function(id::Integer, x::Vector{Float64}, y_eq::Vector{Float64}, y_ieq::Vector{Float64})
@show id
@show x, y_eq, y_ieq
end
instance.get_num_scen = function()
return instance.nscen
end
instance.get_sense = function()
return instance.sense
end
instance.get_status = function()
return instance.status
end
instance.get_num_rows = function(id::Integer)
return instance.rowmap[id]
end
instance.get_num_cols = function(id::Integer)
return instance.colmap[id]
end
instance.get_num_eq_cons = function(id::Integer)
return instance.eq_rowmap[id]
end
instance.get_num_ineq_cons = function(id::Integer)
return instance.inq_rowmap[id]
end
instance.set_status = function(s::Integer)
instance.status = s
end
instance.set_num_rows = function(id::Integer, v::Integer)
return instance.rowmap[id] = v
end
instance.set_num_cols = function(id::Integer, v::Integer)
return instance.colmap[id] = v
end
instance.set_num_eq_cons = function(id::Integer, v::Integer)
return instance.eq_rowmap[id] = v
end
instance.set_num_ineq_cons = function(id::Integer, v::Integer)
return instance.inq_rowmap[id] = v
end
return instance
end
end
#######################
type PipsNlpProblemStruct
ref::Ptr{Void}
model::ModelInterface
comm::MPI.Comm
prof::Bool
n_iter::Int
t_jl_init_x0::Float64
t_jl_str_prob_info::Float64
t_jl_eval_f::Float64
t_jl_eval_g::Float64
t_jl_eval_grad_f::Float64
t_jl_eval_jac_g::Float64
t_jl_str_eval_jac_g::Float64
t_jl_eval_h::Float64
t_jl_str_eval_h::Float64
t_jl_write_solution::Float64
t_jl_str_total::Float64
t_jl_eval_total::Float64
function PipsNlpProblemStruct(comm, model, prof)
prob = new(C_NULL, model, comm, prof,-3
,0.0,0.0,0.0,0.0,0.0
,0.0,0.0,0.0,0.0,0.0
,0.0,0.0
)
finalizer(prob, freeProblemStruct)
return prob
end
end
immutable CallBackData
prob::Ptr{Void}
row_node_id::Cint
col_node_id::Cint
flag::Cint
end
export ModelInterface, FakeModel,
createProblemStruct, solveProblemStruct
###########################################################################
# Callback wrappers
###########################################################################
function str_init_x0_wrapper(x0_ptr::Ptr{Float64}, cbd::Ptr{CallBackData})
# @show " julia - str_init_x0_wrapper "
# @show cbd
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
# @show prob
# data = unsafe_pointer_to_objref(cbd)::CallBackData
# out = Array(Ptr{CallBackData},1)
rowid = Int(Int(data.row_node_id))
colid = Int(Int(data.col_node_id))
assert(rowid == colid)
n0 = prob.model.get_num_cols(colid)
x0 = pointer_to_array(x0_ptr, n0)
@timing prob.prof tic()
prob.model.str_init_x0(colid,x0)
@timing prob.prof prob.t_jl_init_x0 += toq()
return Int32(1)
end
# prob info (prob_info)
function str_prob_info_wrapper(n_ptr::Ptr{Cint}, col_lb_ptr::Ptr{Float64}, col_ub_ptr::Ptr{Float64}, m_ptr::Ptr{Cint}, row_lb_ptr::Ptr{Float64}, row_ub_ptr::Ptr{Float64}, cbd::Ptr{CallBackData})
# @show " julia - str_prob_info_wrapper "
# @show cbd
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
# @show prob
# data = unsafe_pointer_to_objref(cbd)::CallBackData
# out = Array(Ptr{CallBackData},1)
rowid = Int(Int(data.row_node_id))
colid = Int(data.col_node_id)
flag = Int(data.flag)
assert(rowid == colid)
mode = (col_lb_ptr == C_NULL) ? (:Structure) : (:Values)
# @show flag
if flag == 0
# @show mode
if(mode==:Structure)
col_lb = pointer_to_array(col_lb_ptr,0)
col_ub = pointer_to_array(col_ub_ptr,0)
row_lb = pointer_to_array(row_lb_ptr,0)
row_ub = pointer_to_array(row_ub_ptr,0)
@timing prob.prof tic()
(n,m) = prob.model.str_prob_info(colid,flag,mode,col_lb,col_ub,row_lb,row_ub)
@timing prob.prof prob.t_jl_str_prob_info += toq()
unsafe_store!(n_ptr,convert(Cint,n)::Cint)
unsafe_store!(m_ptr,convert(Cint,m)::Cint)
# @show typeof(colid), typeof(m)
prob.model.set_num_rows(colid, m)
prob.model.set_num_cols(colid, n)
else
n = unsafe_load(n_ptr)
m = unsafe_load(m_ptr)
col_lb = pointer_to_array(col_lb_ptr,n)
col_ub = pointer_to_array(col_ub_ptr,n)
row_lb = pointer_to_array(row_lb_ptr,m)
row_ub = pointer_to_array(row_ub_ptr,m)
@timing prob.prof tic()
prob.model.str_prob_info(colid,flag,mode,col_lb,col_ub,row_lb,row_ub)
@timing prob.prof prob.t_jl_str_prob_info += toq()
neq = 0
nineq = 0
for i = 1:length(row_lb)
if row_lb[i] == row_ub[i]
neq += 1
else
nineq += 1
end
end
assert(neq+nineq == length(row_lb) == m)
prob.model.set_num_eq_cons(colid,neq)
prob.model.set_num_ineq_cons(colid,nineq)
end
else
@assert flag ==1
if mode == :Structure
col_lb = pointer_to_array(col_lb_ptr,0)
col_ub = pointer_to_array(col_ub_ptr,0)
row_lb = pointer_to_array(row_lb_ptr,0)
row_ub = pointer_to_array(row_ub_ptr,0)
(n,m) = prob.model.str_prob_info(colid,flag,mode,col_lb,col_ub,row_lb,row_ub)
# @show n,m
unsafe_store!(m_ptr,convert(Cint,m)::Cint)
else
n = unsafe_load(n_ptr)
m = unsafe_load(m_ptr)
@assert m==0
end
end
return Int32(1)
end
# Objective (eval_f)
function str_eval_f_wrapper(x0_ptr::Ptr{Float64}, x1_ptr::Ptr{Float64}, obj_ptr::Ptr{Float64}, cbd::Ptr{CallBackData})
# @show " julia - eval_f_wrapper "
data = unsafe_load(cbd)
# @show data
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
rowid = Int(Int(data.row_node_id))
colid = Int(data.col_node_id)
assert(rowid == colid)
n0 = prob.model.get_num_cols(0)
n1 = prob.model.get_num_cols(colid)
# Calculate the new objective
x0 = pointer_to_array(x0_ptr, n0)
x1 = pointer_to_array(x1_ptr, n1)
@timing prob.prof tic()
new_obj = convert(Float64, prob.model.str_eval_f(colid,x0,x1))::Float64
@timing prob.prof prob.t_jl_eval_f += toq()
# Fill out the pointer
unsafe_store!(obj_ptr, new_obj)
# Done
return Int32(1)
end
# Constraints (eval_g)
function str_eval_g_wrapper(x0_ptr::Ptr{Float64}, x1_ptr::Ptr{Float64}, eq_g_ptr::Ptr{Float64}, inq_g_ptr::Ptr{Float64}, cbd::Ptr{CallBackData})
# @show " julia - eval_g_wrapper "
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
rowid = Int(data.row_node_id)
colid = Int(data.col_node_id)
assert(rowid == colid)
n0 = prob.model.get_num_cols(0)
n1 = prob.model.get_num_cols(colid)
x0 = pointer_to_array(x0_ptr, n0)
x1 = pointer_to_array(x1_ptr, n1)
# Calculate the new constraint values
neq = prob.model.get_num_eq_cons(rowid)
nineq = prob.model.get_num_ineq_cons(rowid)
new_eq_g = pointer_to_array(eq_g_ptr,neq)
new_inq_g = pointer_to_array(inq_g_ptr, nineq)
@timing prob.prof tic()
prob.model.str_eval_g(colid,x0,x1,new_eq_g,new_inq_g)
@timing prob.prof prob.t_jl_eval_g += toq()
# Done
return Int32(1)
end
# Objective gradient (eval_grad_f)
function str_eval_grad_f_wrapper(x0_ptr::Ptr{Float64}, x1_ptr::Ptr{Float64}, grad_f_ptr::Ptr{Float64}, cbd::Ptr{CallBackData})
# @show " julia - eval_grad_f_wrapper "
# Extract Julia the problem from the pointer
# @show cbd
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
rowid = Int(data.row_node_id)
colid = Int(data.col_node_id)
n0 = prob.model.get_num_cols(0)
n1 = prob.model.get_num_cols(rowid)
# @show n0,n1
x0 = pointer_to_array(x0_ptr, n0)
x1 = pointer_to_array(x1_ptr, n1)
# Calculate the gradient
grad_len = prob.model.get_num_cols(colid)
new_grad_f = pointer_to_array(grad_f_ptr, grad_len)
@timing prob.prof tic()
prob.model.str_eval_grad_f(rowid,colid,x0,x1,new_grad_f)
@timing prob.prof prob.t_jl_eval_grad_f += toq()
if prob.model.get_sense() == :Max
new_grad_f *= -1.0
end
# Done
return Int32(1)
end
# Jacobian (eval_jac_g)
function str_eval_jac_g_wrapper(x0_ptr::Ptr{Float64}, x1_ptr::Ptr{Float64},
e_nz_ptr::Ptr{Cint}, e_values_ptr::Ptr{Float64}, e_row_ptr::Ptr{Cint}, e_col_ptr::Ptr{Cint},
i_nz_ptr::Ptr{Cint}, i_values_ptr::Ptr{Float64}, i_row_ptr::Ptr{Cint}, i_col_ptr::Ptr{Cint},
cbd::Ptr{CallBackData}
)
# @show " julia - eval_jac_g_wrapper "
# Extract Julia the problem from the pointer
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
rowid = Int(Int(data.row_node_id))
colid = Int(Int(data.col_node_id))
flag = Int(data.flag)
n0 = prob.model.get_num_cols(0)
n1 = prob.model.get_num_cols(rowid) #we can do this because of 2-level and no linking constraint
# @show n0, n1
x0 = pointer_to_array(x0_ptr, n0)
x1 = pointer_to_array(x1_ptr, n1)
# @show x0
# @show x1
nrow = prob.model.get_num_rows(rowid)
ncol = prob.model.get_num_cols(colid)
#@show prob
# Determine mode
mode = (e_col_ptr == C_NULL && i_col_ptr == C_NULL) ? (:Structure) : (:Values)
if flag != 1
if(mode == :Structure)
e_values = pointer_to_array(e_values_ptr,0)
e_colptr = pointer_to_array(e_col_ptr,0)
e_rowidx = pointer_to_array(e_row_ptr,0)
i_values = pointer_to_array(i_values_ptr,0)
i_colptr = pointer_to_array(i_col_ptr,0)
i_rowidx = pointer_to_array(i_row_ptr,0)
@timing prob.prof tic()
(e_nz,i_nz) = prob.model.str_eval_jac_g(rowid,colid,flag,x0,x1,mode,e_rowidx,e_colptr,e_values,i_rowidx,i_colptr,i_values)
@timing prob.prof prob.t_jl_str_eval_jac_g += toq()
unsafe_store!(e_nz_ptr,convert(Cint,e_nz)::Cint)
unsafe_store!(i_nz_ptr,convert(Cint,i_nz)::Cint)
# @show "structure - ",(e_nz,i_nz)
else
e_nz = unsafe_load(e_nz_ptr)
e_values = pointer_to_array(e_values_ptr,e_nz)
e_rowidx = pointer_to_array(e_row_ptr, e_nz)
e_colptr = pointer_to_array(e_col_ptr, ncol+1)
i_nz = unsafe_load(i_nz_ptr)
# @show "values - ",(e_nz,i_nz), ncol
i_values = pointer_to_array(i_values_ptr,i_nz)
i_rowidx = pointer_to_array(i_row_ptr, i_nz)
i_colptr = pointer_to_array(i_col_ptr, ncol+1)
# @show x0
# @show x1
@timing prob.prof tic()
prob.model.str_eval_jac_g(rowid,colid,flag,x0,x1,mode,e_rowidx,e_colptr,e_values,i_rowidx,i_colptr,i_values)
@timing prob.prof prob.t_jl_eval_jac_g += toq()
end
else
@assert flag == 1
if mode == :Structure
e_nz = 0
i_nz = 0
unsafe_store!(e_nz_ptr,convert(Cint,e_nz)::Cint)
unsafe_store!(i_nz_ptr,convert(Cint,i_nz)::Cint)
else
e_nz = unsafe_load(e_nz_ptr)
i_nz = unsafe_load(i_nz_ptr)
@assert e_nz == i_nz == 0
end
end
# Done
return Int32(1)
end
# Hessian
function str_eval_h_wrapper(x0_ptr::Ptr{Float64}, x1_ptr::Ptr{Float64}, lambda_ptr::Ptr{Float64}, nz_ptr::Ptr{Cint}, values_ptr::Ptr{Float64}, row_ptr::Ptr{Cint}, col_ptr::Ptr{Cint}, cbd::Ptr{CallBackData})
# @show " julia - eval_h_wrapper "
# Extract Julia the problem from the pointer
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
rowid = Int(data.row_node_id)
colid = Int(data.col_node_id)
flag = Int(data.flag)
# @message @sprintf(" julia - eval_h_wrapper - %d %d %d", rowid, colid, flag)
@timing prob.prof begin
if rowid == colid ==0
prob.n_iter += 1
if prob.n_iter == 0
prob.t_jl_str_total = t_reset(prob)
end
end
end
# @show prob.n_iter
high = max(rowid,colid)
low = min(rowid,colid)
n0 = prob.model.get_num_cols(0)
n1 = prob.model.get_num_cols(high)
x0 = pointer_to_array(x0_ptr, n0)
x1 = pointer_to_array(x1_ptr, n1)
# @show x0
# @show x1
ncol = prob.model.get_num_cols(low)
g0 = prob.model.get_num_rows(high)
# @show g0
# @show ncol
lambda = pointer_to_array(lambda_ptr, g0)
obj_factor = 1.0
if prob.model.get_sense() == :Max
obj_factor *= -1.0
end
# Did the user specify a Hessian
mode = (col_ptr == C_NULL) ? (:Structure) : (:Values)
if(mode == :Structure)
values = pointer_to_array(values_ptr,0)
colptr = pointer_to_array(col_ptr,0)
rowidx = pointer_to_array(row_ptr,0)
@timing prob.prof tic()
nz = prob.model.str_eval_h(rowid,colid,flag, x0,x1,obj_factor,lambda,mode,rowidx,colptr,values)
@timing prob.prof prob.t_jl_str_eval_h += toq()
unsafe_store!(nz_ptr,convert(Cint,nz)::Cint)
# @show "structure - ", nz
else
nz = unsafe_load(nz_ptr)
values = pointer_to_array(values_ptr, nz)
rowidx = pointer_to_array(row_ptr, nz)
colptr = pointer_to_array(col_ptr, ncol+1)
# @show "value - ", nz
@timing prob.prof tic()
prob.model.str_eval_h(rowid,colid,flag,x0,x1,obj_factor,lambda,mode,rowidx,colptr,values)
@timing prob.prof prob.t_jl_eval_h += toq()
end
# Done
return Int32(1)
end
#write solution
function str_write_solution_wrapper(x_ptr::Ptr{Float64}, y_eq_ptr::Ptr{Float64}, y_ieq_ptr::Ptr{Float64}, cbd::Ptr{CallBackData})
data = unsafe_load(cbd)
# @show data
userdata = data.prob
prob = unsafe_pointer_to_objref(userdata)::PipsNlpProblemStruct
rowid = Int(data.row_node_id)
colid = Int(data.col_node_id)
@assert rowid == colid
nx = prob.model.get_num_cols(rowid)
neq = prob.model.get_num_eq_cons(rowid)
nieq = prob.model.get_num_ineq_cons(rowid)
x = pointer_to_array(x_ptr, nx)
y_eq = pointer_to_array(y_eq_ptr,neq)
y_ieq = pointer_to_array(y_ieq_ptr,nieq)
@timing prob.prof tic()
prob.model.str_write_solution(rowid,x,y_eq,y_ieq)
@timing prob.prof prob.t_jl_write_solution += toq()
return Int32(1)
end
###########################################################################
# C function wrappers
###########################################################################
function createProblemStruct(comm::MPI.Comm, model::ModelInterface, prof::Bool)
# println(" createProblemStruct -- julia")
str_init_x0_cb = cfunction(str_init_x0_wrapper, Cint, (Ptr{Float64}, Ptr{CallBackData}) )
str_prob_info_cb = cfunction(str_prob_info_wrapper, Cint, (Ptr{Cint}, Ptr{Float64}, Ptr{Float64}, Ptr{Cint}, Ptr{Float64}, Ptr{Float64}, Ptr{CallBackData}) )
str_eval_f_cb = cfunction(str_eval_f_wrapper,Cint, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{CallBackData}) )
str_eval_g_cb = cfunction(str_eval_g_wrapper,Cint, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{CallBackData}) )
str_eval_grad_f_cb = cfunction(str_eval_grad_f_wrapper, Cint, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{CallBackData}) )
str_eval_jac_g_cb = cfunction(str_eval_jac_g_wrapper, Cint, (Ptr{Float64}, Ptr{Float64},
Ptr{Cint}, Ptr{Float64}, Ptr{Cint}, Ptr{Cint},
Ptr{Cint}, Ptr{Float64}, Ptr{Cint}, Ptr{Cint},
Ptr{CallBackData}))
str_eval_h_cb = cfunction(str_eval_h_wrapper, Cint, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{Cint}, Ptr{Float64}, Ptr{Cint}, Ptr{Cint}, Ptr{CallBackData}))
str_write_solution_cb = cfunction(str_write_solution_wrapper, Cint, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{CallBackData}))
# println(" callback created ")
prob = PipsNlpProblemStruct(comm, model, prof)
# @show prob
ret = ccall(Libdl.dlsym(libparpipsnlp,:CreatePipsNlpProblemStruct),Ptr{Void},
(MPI.CComm,
Cint, Ptr{Void}, Ptr{Void},
Ptr{Void}, Ptr{Void}, Ptr{Void},
Ptr{Void}, Ptr{Void}, Ptr{Void},Any
),
MPI.CComm(comm),
model.get_num_scen(),
str_init_x0_cb,
str_prob_info_cb,
str_eval_f_cb,
str_eval_g_cb,
str_eval_grad_f_cb,
str_eval_jac_g_cb,
str_eval_h_cb,
str_write_solution_cb,
prob
)
# println(" ccall CreatePipsNlpProblemStruct done ")
# @show ret
if ret == C_NULL
error("PIPS-NLP: Failed to construct problem.")
else
prob.ref = ret
end
# @show prob
# println("end createProblemStruct - julia")
return prob
end
function solveProblemStruct(prob::PipsNlpProblemStruct)
# println("solveProblemStruct - julia")
# @show prob
ret = ccall(Libdl.dlsym(libparpipsnlp,:PipsNlpSolveStruct), Cint,
(Ptr{Void},),
prob.ref)
# @show ret
prob.model.set_status(Int(ret))
prob.t_jl_eval_total = report_total_now(prob)
# @show prob
return prob.model.get_status()
end
function freeProblemStruct(prob::PipsNlpProblemStruct)
# @show "freeProblemStruct"
ret = ccall(Libdl.dlsym(libparpipsnlp,:FreePipsNlpProblemStruct),
Void, (Ptr{Void},),
prob.ref)
# @show ret
return ret
end
function report_total_now(prob::PipsNlpProblemStruct)
total = 0.0
total += prob.t_jl_init_x0
total += prob.t_jl_str_prob_info
total += prob.t_jl_eval_f
total += prob.t_jl_eval_g
total += prob.t_jl_eval_grad_f
total += prob.t_jl_eval_jac_g
total += prob.t_jl_str_eval_jac_g
total += prob.t_jl_eval_h
total += prob.t_jl_str_eval_h
total += prob.t_jl_write_solution
return total
end
function t_reset(prob::PipsNlpProblemStruct)
total = report_total_now(prob)
prob.t_jl_init_x0 = 0.0
prob.t_jl_str_prob_info = 0.0
prob.t_jl_eval_f = 0.0
prob.t_jl_eval_g = 0.0
prob.t_jl_eval_grad_f = 0.0
prob.t_jl_eval_jac_g = 0.0
prob.t_jl_str_eval_jac_g = 0.0
prob.t_jl_eval_h = 0.0
prob.t_jl_str_eval_h = 0.0
prob.t_jl_write_solution = 0.0
return total
end
end