-
Notifications
You must be signed in to change notification settings - Fork 29
/
Mosek.jl
284 lines (225 loc) · 6.95 KB
/
Mosek.jl
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
__precompile__()
module Mosek
if isfile(joinpath(dirname(@__FILE__),"..","deps","deps.jl"))
include("../deps/deps.jl")
else
error("Mosek not properly installed. Please run Pkg.build(\"Mosek\")")
end
export
makeenv, maketask, maketask_ptr,
MosekError
# A macro to make calling C API a little cleaner
macro msk_ccall(func, args...)
f = Base.Meta.quot(Symbol("MSK_$(func)"))
args = [esc(a) for a in args]
quote
ccall(($f,libmosek), $(args...))
end
end
# -----
# Types
# -----
struct MosekError <: Exception
rcode :: Int32
msg :: String
end
# Environment: typedef void * Env_t;
mutable struct Env
env::Ptr{Nothing}
streamcallbackfunc::Any
end
function msk_info_callback_wrapper end
# Task: typedef void * Task_t;
mutable struct Task
env::Env
task::Ptr{Nothing}
borrowed::Bool
# need to keep a reference to callback funcs for GC
streamcallbackfunc:: Any
userstreamcallbackfunc:: Any
callbackfunc:: Any
usercallbackfunc:: Any
nlinfo:: Any
function Task(env::Env)
temp = Array{Ptr{Nothing}}(undef,1)
res = @msk_ccall(maketask, Int32, (Ptr{Nothing}, Int32, Int32, Ptr{Nothing}), env.env, 0, 0, temp)
if res != MSK_RES_OK.value
throw(MosekError(res,""))
end
task = new(env,temp[1],false,nothing,nothing,nothing,nothing,nothing)
task.callbackfunc = @cfunction(msk_info_callback_wrapper, Cint, (Ptr{Nothing}, Ptr{Nothing}, Int32, Ptr{Float64}, Ptr{Int32}, Ptr{Int64}))
task.usercallbackfunc = nothing
finalizer(deletetask,task)
r = @msk_ccall(putcallbackfunc, Cint, (Ptr{Nothing}, Ptr{Nothing}, Any), task.task, task.callbackfunc, task)
if r != MSK_RES_OK.value
throw(MosekError(r,getlasterror(t)))
end
task
end
function Task(t::Task)
temp = Array{Ptr{Nothing}}(undef,1)
res = @msk_ccall(clonetask, Int32, (Ptr{Nothing}, Ptr{Nothing}), t.task, temp)
if res != MSK_RES_OK.value
throw(MosekError(res,""))
end
task = new(t.env,temp[1],false,nothing,nothing,nothing,nothing,nothing)
task.callbackfunc = @cfunction(msk_info_callback_wrapper, Cint, (Ptr{Nothing}, Ptr{Nothing}, Int32, Ptr{Float64}, Ptr{Int32}, Ptr{Int64}))
task.usercallbackfunc = nothing
finalizer(deletetask,task)
r = @msk_ccall(putcallbackfunc, Cint, (Ptr{Nothing}, Ptr{Nothing}, Any), task.task, task.callbackfunc, task)
if r != MSK_RES_OK.value
throw(MosekError(r,getlasterror(t)))
end
task
end
function Task(t::Ptr{Nothing},borrowed::Bool)
task = new(msk_global_env,t,borrowed,nothing,nothing,nothing,nothing,nothing)
task.callbackfunc = @cfunction(msk_info_callback_wrapper, Cint, (Ptr{Nothing}, Ptr{Nothing}, Int32, Ptr{Float64}, Ptr{Int32}, Ptr{Int64}))
task.usercallbackfunc = nothing
finalizer(deletetask,task)
r = @msk_ccall(putcallbackfunc, Cint, (Ptr{Nothing}, Ptr{Nothing}, Any), task.task, task.callbackfunc, task)
if r != MSK_RES_OK.value
throw(MosekError(r,getlasterror(t)))
end
task
end
end
const MSKtask = Task
const MSKenv = Env
# ------------
# API wrappers
# ------------
# TODO: Support other argument
"""
makeenv()
Create a MOSEK environment.
"""
function makeenv()
temp = Array{Ptr{Nothing}}(undef,1)
res = @msk_ccall(makeenv, Int32, (Ptr{Ptr{Nothing}}, Ptr{UInt8}), temp, C_NULL)
if res != 0
# TODO: Actually use result code
error("MOSEK: Error creating environment")
end
Env(temp[1],nothing)
end
"""
makeenv(func::Function)
Create a MOSEK environment for use with `do`-syntax.
"""
function makeenv(func::Function)
temp = Array{Ptr{Nothing}}(undef,1)
res = @msk_ccall(makeenv, Int32, (Ptr{Ptr{Nothing}}, Ptr{UInt8}), temp, C_NULL)
if res != 0
# TODO: Actually use result code
error("MOSEK: Error creating environment")
end
env = Env(temp[1],nothing)
try
func(env)
finally
deleteenv(env)
end
end
# Note on initialization of msk_global_env:
#
# When loading Mosek from source this works fine, but when loading
# precompiled module, makeenv() is not called (and some garbage
# value is put in msk_global_env). It appears that static
# initializers must be called from __init__(). That is called a bad solution here:
# https://github.com/JuliaLang/julia/issues/12010
msk_global_env = makeenv() :: Env
__init__() = (global msk_global_env = makeenv())
"""
maketask(env:Env)
maketask()
Create a task that belongs to either the given `env` or to the global `env`.
maketask(task::Task)
Create a clone of `task`.
maketask(func::Function, env::Env)
maketask(func::Function)
Create a task that either longs to the given `env` or to the global `env`. The `func` parameter is a function
```julia
func(task :: Task) :: Any
```
This can be used with the `do`-syntax:
```julia
maketask(env) do task
readdata(task,"MyFile.task")
end
```
"""
function maketask end
maketask(env::Env) = Task(env)
maketask(task::Task) = Task(task)
maketask() = Task(msk_global_env)
function maketask(func::Function, env::Env)
t = Task(env)
try
func(t)
finally
deletetask(t)
end
end
function maketask(func::Function)
t = Task(msk_global_env)
try
func(t)
finally
deletetask(t)
end
end
function maketask_ptr(t::Ptr{Nothing},borrowed::Bool)
Task(t,borrowed)
end
"""
deletetask(t::Task)
Destroy the task object.
"""
function deletetask(t::Task)
if t.task != C_NULL
if ! t.borrowed
temp = Array{Ptr{Nothing}}(undef,1)
temp[1] = t.task
@msk_ccall(deletetask,Int32,(Ptr{Ptr{Nothing}},), temp)
end
t.task = C_NULL
end
end
"""
deleteenv(t::Env)
Destroy the task object.
"""
function deleteenv(e::Env)
if e.env != C_NULL
temp = Array{Ptr{Nothing}}(undef,1)
temp[1] = t.env
@msk_ccall(deleteenv,Int32,(Ptr{Ptr{Nothing}},), temp)
e.env = C_NULL
end
end
function getlasterror(t::Task)
lasterrcode = Array{Cint}(undef,1)
lastmsglen = Array{Cint}(undef,1)
@msk_ccall(getlasterror,Cint,(Ptr{Nothing},Ptr{Cint},Cint,Ptr{Cint},Ptr{UInt8}),
t.task, lasterrcode, 0, lastmsglen, C_NULL)
lastmsg = Array{UInt8}(undef,lastmsglen[1])
@msk_ccall(getlasterror,Cint,(Ptr{Nothing},Ptr{Cint},Cint,Ptr{Cint},Ptr{UInt8}),
t.task, lasterrcode, lastmsglen[1], lastmsglen, lastmsg)
String(lastmsg[1:lastmsglen[1]-1])
end
using SparseArrays
include("msk_enums.jl")
include("msk_functions.jl")
include("msk_functions_ext.jl")
include("msk_callback.jl")
include("msk_geco.jl")
using MathProgBase
# Convex.jl only works if MosekSovler is defined in the top-level module
struct MosekSolver <: MathProgBase.AbstractMathProgSolver
options
end
MosekSolver(;kwargs...) = MosekSolver(kwargs)
export MosekSolver
include("MosekSolverInterface.jl")
end