-
Notifications
You must be signed in to change notification settings - Fork 29
/
Mosek.jl
247 lines (195 loc) · 5.67 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
#from: https://groups.google.com/forum/#!topic/julia-users/RLlYPlsT-dU
#VERSION >= v"0.4.0-dev+6521" && __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
using Compat
import MathProgBase
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
# -----
type MosekError <: Exception
rcode :: Int32
msg :: ASCIIString
end
# Environment: typedef void * MSKenv_t;
type MSKenv
env::Ptr{Void}
streamcallbackfunc::Any
end
# Task: typedef void * MSKtask_t;
type MSKtask
env::MSKenv
task::Ptr{Void}
borrowed::Bool
# need to keep a reference to callback funcs for GC
streamcallbackfunc:: Any
userstreamcallbackfunc:: Any
callbackfunc:: Any
usercallbackfunc:: Any
nlinfo:: Any
function MSKtask(env::MSKenv)
temp = Array(Ptr{Void}, 1)
res = @msk_ccall(maketask, Int32, (Ptr{Void}, Int32, Int32, Ptr{Void}), env.env, 0, 0, temp)
if res != MSK_RES_OK
throw(MosekError(res,""))
end
task = new(env,temp[1],false,nothing,nothing,nothing,nothing,nothing)
finalizer(task,deletetask)
task
end
function MSKtask(t::MSKtask)
temp = Array(Ptr{Void}, 1)
res = @msk_ccall(clonetask, Int32, (Ptr{Void}, Ptr{Void}), t.task, temp)
if res != MSK_RES_OK
throw(MosekError(res,""))
end
task = new(env,temp[1],false,nothing,nothing,nothing,nothing,nothing)
finalizer(task,deletetask)
task
end
function MSKtask(t::Ptr{Void},borrowed::Bool)
task = new(msk_global_env,t,borrowed,nothing,nothing,nothing,nothing,nothing)
finalizer(task,deletetask)
task
end
end
# ------------
# API wrappers
# ------------
# TODO: Support other argument
function makeenv()
temp = Array(Ptr{Void}, 1)
res = @msk_ccall(makeenv, Int32, (Ptr{Ptr{Void}}, Ptr{UInt8}), temp, C_NULL)
if res != 0
# TODO: Actually use result code
error("MOSEK: Error creating environment")
end
MSKenv(temp[1],nothing)
end
function makeenv(func::Function)
temp = Array(Ptr{Void}, 1)
res = @msk_ccall(makeenv, Int32, (Ptr{Ptr{Void}}, Ptr{UInt8}), temp, C_NULL)
if res != 0
# TODO: Actually use result code
error("MOSEK: Error creating environment")
end
env = MSKenv(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() :: MSKenv
__init__() = (global msk_global_env = makeenv())
function maketask(env::MSKenv)
MSKtask(env)
end
function maketask(task::MSKtask)
MSKtask(task)
end
function maketask()
MSKtask(msk_global_env)
end
function maketask_ptr(t::Ptr{Void},borrowed::Bool)
MSKtask(t,borrowed)
end
function maketask(func::Function, env::MSKenv)
t = MSKtask(env)
try
func(t)
finally
deletetask(t)
end
end
function maketask(task::MSKtask)
t = MSKtask(task)
try
func(t)
finally
deletetask(t)
end
end
function maketask(func::Function)
t = MSKtask(msk_global_env)
try
func(t)
finally
deletetask(t)
end
end
function deletetask(t::MSKtask)
if t.task != C_NULL
if ! t.borrowed
temp = Array(Ptr{Void},1)
temp[1] = t.task
@msk_ccall(deletetask,Int32,(Ptr{Ptr{Void}},), temp)
end
t.task = C_NULL
end
end
function deleteenv(e::MSKenv)
if e.env != C_NULL
temp = Array(Ptr{Void},1)
temp[1] = t.env
@msk_ccall(deleteenv,Int32,(Ptr{Ptr{Void}},), temp)
e.env = C_NULL
end
end
function getlasterror(t::MSKtask)
lasterrcode = Array(Cint,1)
lastmsglen = Array(Cint,1)
@msk_ccall(getlasterror,Cint,(Ptr{Void},Ptr{Cint},Cint,Ptr{Cint},Ptr{UInt8}),
t.task, lasterrcode, 0, lastmsglen, C_NULL)
lastmsg = Array(UInt8,lastmsglen[1])
@msk_ccall(getlasterror,Cint,(Ptr{Void},Ptr{Cint},Cint,Ptr{Cint},Ptr{UInt8}),
t.task, lasterrcode, lastmsglen[1], lastmsglen, lastmsg)
convert(ASCIIString,lastmsg[1:lastmsglen[1]-1])
end
function _getmosekmajorversion()
build_ = Array(Int32,(1,))
major_ = Array(Int32,(1,))
minor_ = Array(Int32,(1,))
revision_ = Array(Int32,(1,))
@msk_ccall( "getversion",Int32,(Ptr{Int32},Ptr{Int32},Ptr{Int32},Ptr{Int32},),major_,minor_,build_,revision_)
convert(Int32,major_[1])
end
#include("msk_callback.jl")
# Generated content
if _getmosekmajorversion() == 7
include("msk7_enums.jl")
include("msk7_functions.jl")
elseif _getmosekmajorversion() == 8
include("msk8_enums.jl")
include("msk8_functions.jl")
else
error("Unsupported MOSEK version loaded")
end
include("msk_callback.jl")
include("msk_geco.jl")
include("MosekSolverInterface.jl")
using Mosek.MosekMathProgSolverInterface
export MosekSolver
end