-
Notifications
You must be signed in to change notification settings - Fork 29
/
Mosek.jl
151 lines (115 loc) · 3.15 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
module Mosek
if isfile(joinpath(Pkg.dir("Mosek"),"deps","deps.jl"))
include("../deps/deps.jl")
else
error("Mosek not properly installed. Please run Pkg.build(\"Mosek\")")
end
export
makeenv, maketask,
MosekError
# A macro to make calling C API a little cleaner
macro msk_ccall(func, args...)
f = "MSK_$(func)"
quote
ccall(($f,libmosek), $(args...))
end
end
# -----
# Types
# -----
type MosekError
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}
streamcallbackfunc:: Any
callbackfunc:: 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],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],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
const msk_global_env = makeenv() :: MSKenv
function maketask(env::MSKenv)
MSKtask(env)
end
function maketask(task::MSKtask)
MSKtask(task)
end
function maketask()
MSKtask(msk_global_env)
end
function deletetask(t::MSKtask)
if t.task != C_NULL
temp = Array(Ptr{Void},1)
temp[1] = t.task
@msk_ccall(deletetask,Int32,(Ptr{Ptr{Void}},), temp)
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
#include("msk_callback.jl")
# Generated content
include("msk_enums.jl")
include("msk_functions.jl")
include("msk_callback.jl")
include("msk_geco.jl")
include("MosekSolverInterface.jl")
using Mosek.MosekMathProgSolverInterface
export MosekSolver
end