Skip to content

Commit 5244488

Browse files
committed
Removed need for eval() in xgboost_ccall
1 parent 10c2e43 commit 5244488

File tree

1 file changed

+134
-154
lines changed

1 file changed

+134
-154
lines changed

src/xgboost_wrapper_h.jl

Lines changed: 134 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -3,150 +3,140 @@ include("../deps/deps.jl")
33
# xgboost_wrapper.h
44

55
"Calls an xgboost API function and correctly reports errors."
6-
macro xgboost_ccall(f, argTypes, args...)
7-
argTypes = eval(argTypes)
6+
macro xgboost(f, params...)
7+
args = [param.args[1] for param in params]
8+
types = [param.args[2] for param in params]
9+
810
return quote
9-
err = ccall(($f, _xgboost), Int64, ($(argTypes...),), $(args...))
11+
err = ccall(($f, _xgboost), Int64, ($(types...),), $(args...))
1012
if err != 0
11-
errMsg = unsafe_string(ccall((:XGBGetLastError, _xgboost), Ptr{UInt8}, ()))
12-
error("Call to XGBoost C function "*string($f)*" failed: $errMsg")
13+
err_msg = unsafe_string(ccall((:XGBGetLastError, _xgboost), Cstring, ()))
14+
error("Call to XGBoost C function ", string($f), " failed: ", err_msg)
1315
end
1416
end
1517
end
1618

17-
function XGDMatrixCreateFromFile(fname::String, slient::Int32)
19+
function XGDMatrixCreateFromFile(fname::String, silent::Int32)
1820
handle = Ref{Ptr{Void}}()
19-
@xgboost_ccall(
20-
:XGDMatrixCreateFromFile,
21-
(Ptr{UInt8}, Int32, Ref{Ptr{Void}}),
22-
fname, slient, handle
23-
)
21+
@xgboost(:XGDMatrixCreateFromFile,
22+
fname => Ptr{UInt8},
23+
silent => Int32,
24+
handle => Ref{Ptr{Void}})
2425
return handle[]
2526
end
2627

2728
function XGDMatrixCreateFromCSC(data::SparseMatrixCSC)
2829
handle = Ref{Ptr{Void}}()
29-
@xgboost_ccall(
30-
:XGDMatrixCreateFromCSC,
31-
(Ptr{UInt64}, Ptr{UInt32}, Ptr{Float32}, UInt64, UInt64, Ref{Ptr{Void}}),
32-
convert(Array{UInt64, 1}, data.colptr - 1),
33-
convert(Array{UInt32, 1}, data.rowval - 1), convert(Array{Float32, 1}, data.nzval),
34-
convert(UInt64, size(data.colptr)[1]),
35-
convert(UInt64, nnz(data)),
36-
handle
37-
)
30+
@xgboost(:XGDMatrixCreateFromCSC,
31+
convert(Array{UInt64, 1}, data.colptr - 1) => Ptr{UInt64},
32+
convert(Array{UInt32, 1}, data.rowval - 1) => Ptr{UInt32},
33+
convert(Array{Float32, 1}, data.nzval) => Ptr{Float32},
34+
convert(UInt64, size(data.colptr)[1]) => UInt64,
35+
convert(UInt64, nnz(data)) => UInt64,
36+
handle => Ref{Ptr{Void}})
3837
return handle[]
3938
end
4039

4140
function XGDMatrixCreateFromCSCT(data::SparseMatrixCSC)
4241
handle = Ref{Ptr{Void}}()
43-
@xgboost_ccall(
44-
:XGDMatrixCreateFromCSR,
45-
(Ptr{UInt64}, Ptr{UInt32}, Ptr{Float32}, UInt64, UInt64, Ref{Ptr{Void}}),
46-
convert(Array{UInt64, 1}, data.colptr - 1),
47-
convert(Array{UInt32, 1}, data.rowval - 1), convert(Array{Float32, 1}, data.nzval),
48-
convert(UInt64, size(data.colptr)[1]),
49-
convert(UInt64, nnz(data)),
50-
handle
51-
)
42+
@xgboost(:XGDMatrixCreateFromCSR,
43+
convert(Array{UInt64, 1}, data.colptr - 1) => Ptr{UInt64},
44+
convert(Array{UInt32, 1}, data.rowval - 1) => Ptr{UInt32},
45+
convert(Array{Float32, 1}, data.nzval) => Ptr{Float32},
46+
convert(UInt64, size(data.colptr)[1]) => UInt64,
47+
convert(UInt64, nnz(data)) => UInt64,
48+
handle => Ref{Ptr{Void}})
5249
return handle[]
5350
end
5451

55-
function XGDMatrixCreateFromMat(data::Array{Float32, 2}, missing::Float32)
52+
function XGDMatrixCreateFromMat(data::Array{Float32,2}, missing::Float32)
5653
XGDMatrixCreateFromMatT(transpose(data), missing)
5754
end
5855

59-
function XGDMatrixCreateFromMatT(data::Array{Float32, 2}, missing::Float32)
60-
nrow = size(data)[2]
61-
ncol = size(data)[1]
56+
function XGDMatrixCreateFromMatT(data::Array{Float32,2}, missing::Float32)
57+
ncol, nrow = size(data)
6258
handle = Ref{Ptr{Void}}()
63-
@xgboost_ccall(
64-
:XGDMatrixCreateFromMat,
65-
(Ptr{Float32}, UInt64, UInt64, Float32, Ref{Ptr{Void}}),
66-
data, nrow, ncol, missing, handle
67-
)
59+
@xgboost(:XGDMatrixCreateFromMat,
60+
data => Ptr{Float32},
61+
nrow => UInt64,
62+
ncol => UInt64,
63+
missing => Float32,
64+
handle => Ref{Ptr{Void}})
6865
return handle[]
6966
end
7067

71-
function XGDMatrixSliceDMatrix(handle::Ptr{Void}, idxset::Array{Int32, 1}, len::UInt64)
68+
function XGDMatrixSliceDMatrix(handle::Ptr{Void}, idxset::Array{Int32,1}, len::UInt64)
7269
ret = Ref{Ptr{Void}}()
73-
@xgboost_ccall(
74-
:XGDMatrixSliceDMatrix,
75-
(Ptr{Void}, Ptr{Int32}, UInt64, Ref{Ptr{Void}}),
76-
handle, idxset, len, ret
77-
)
70+
@xgboost(:XGDMatrixSliceDMatrix,
71+
handle => Ptr{Void},
72+
idxset => Ptr{Int32},
73+
len => UInt64,
74+
ret => Ref{Ptr{Void}})
7875
return ret[]
7976
end
8077

8178
function XGDMatrixFree(handle::Ptr{Void})
82-
@xgboost_ccall(
83-
:XGDMatrixFree,
84-
(Ptr{Void},),
85-
handle
86-
)
79+
@xgboost(:XGDMatrixFree,
80+
handle => Ptr{Void})
8781
end
8882

89-
function XGDMatrixSaveBinary(handle::Ptr{Void}, fname::String, slient::Int32)
90-
@xgboost_ccall(
91-
:XGDMatrixSaveBinary,
92-
(Ptr{Void}, Ptr{UInt8}, Int32),
93-
handle, fname, slient
94-
)
83+
function XGDMatrixSaveBinary(handle::Ptr{Void}, fname::String, silent::Int32)
84+
@xgboost(:XGDMatrixSaveBinary,
85+
handle => Ptr{Void},
86+
fname => Ptr{UInt8},
87+
silent => Int32)
9588
end
9689

97-
function XGDMatrixSetFloatInfo(handle::Ptr{Void}, field::String,
98-
array::Array{Float32, 1}, len::UInt64)
99-
@xgboost_ccall(
100-
:XGDMatrixSetFloatInfo,
101-
(Ptr{Void}, Ptr{UInt8}, Ptr{Float32}, UInt64),
102-
handle, field, array, len
103-
)
90+
function XGDMatrixSetFloatInfo(handle::Ptr{Void}, field::String, array::Array{Float32,1},
91+
len::UInt64)
92+
@xgboost(:XGDMatrixSetFloatInfo,
93+
handle => Ptr{Void},
94+
field => Ptr{UInt8},
95+
array => Ptr{Float32},
96+
len => UInt64)
10497
end
10598

106-
function XGDMatrixSetUIntInfo(handle::Ptr{Void}, field::String,
107-
array::Array{UInt32, 1}, len::UInt64)
108-
@xgboost_ccall(
109-
:XGDMatrixSetUIntInfo,
110-
(Ptr{Void}, Ptr{UInt8}, Ptr{UInt32}, UInt64),
111-
handle, field, array, len
112-
)
99+
function XGDMatrixSetUIntInfo(handle::Ptr{Void}, field::String, array::Array{UInt32,1},
100+
len::UInt64)
101+
@xgboost(:XGDMatrixSetUIntInfo,
102+
handle => Ptr{Void},
103+
field => Ptr{UInt8},
104+
array => Ptr{UInt32},
105+
len => UInt64)
113106
end
114107

115-
function XGDMatrixSetGroup(handle::Ptr{Void}, array::Array{UInt32, 1}, len::UInt64)
116-
@xgboost_ccall(
117-
:XGDMatrixSetGroup,
118-
(Ptr{Void}, Ptr{UInt32}, UInt64),
119-
handle, array, len
120-
)
108+
function XGDMatrixSetGroup(handle::Ptr{Void}, array::Array{UInt32,1}, len::UInt64)
109+
@xgboost(:XGDMatrixSetGroup,
110+
handle => Ptr{Void},
111+
array => Ptr{UInt32},
112+
len => UInt64)
121113
end
122114

123-
function XGDMatrixGetFloatInfo(handle::Ptr{Void}, field::String, outlen::Array{UInt64, 1})
115+
function XGDMatrixGetFloatInfo(handle::Ptr{Void}, field::String, outlen::Array{UInt64,1})
124116
ret = Ref{Ptr{Float32}}()
125-
@xgboost_ccall(
126-
:XGDMatrixGetFloatInfo,
127-
(Ptr{Void}, Ptr{UInt8}, Ptr{UInt64}, Ref{Ptr{Float32}}),
128-
handle, field, outlen, ret
129-
)
117+
@xgboost(:XGDMatrixGetFloatInfo,
118+
handle => Ptr{Void},
119+
field => Ptr{UInt8},
120+
outlen => Ptr{UInt64},
121+
ret => Ref{Ptr{Float32}})
130122
return ret[]
131123
end
132124

133-
function XGDMatrixGetUIntInfo(handle::Ptr{Void}, field::String, outlen::Array{UInt64, 1})
125+
function XGDMatrixGetUIntInfo(handle::Ptr{Void}, field::String, outlen::Array{UInt64,1})
134126
ret = Ref{Ptr{UInt32}}()
135-
@xgboost_ccall(
136-
:XGDMatrixGetUIntInfo,
137-
(Ptr{Void}, Ptr{UInt8}, Ptr{UInt64}, Ref{Ptr{UInt32}}),
138-
handle, field, outlen, ret
139-
)
127+
@xgboost(:XGDMatrixGetUIntInfo,
128+
handle => Ptr{Void},
129+
field => Ptr{UInt8},
130+
outlen => Ptr{UInt64},
131+
ret => Ref{Ptr{UInt32}})
140132
return ret[]
141133
end
142134

143135
function XGDMatrixNumRow(handle::Ptr{Void})
144136
ret = Ref{UInt64}()
145-
@xgboost_ccall(
146-
:XGDMatrixNumRow,
147-
(Ptr{Void}, Ref{UInt64}),
148-
handle, ret
149-
)
137+
@xgboost(:XGDMatrixNumRow,
138+
handle => Ptr{Void},
139+
ret => Ref{UInt64})
150140
return ret[]
151141
end
152142

@@ -162,100 +152,90 @@ function JLGetUintInfo(handle::Ptr{Void}, field::String)
162152
return unsafe_wrap(Array, ptr, len[1])
163153
end
164154

165-
function XGBoosterCreate(cachelist::Array{Ptr{Void}, 1}, len::Int64)
155+
function XGBoosterCreate(cachelist::Array{Ptr{Void},1}, len::Int64)
166156
handle = Ref{Ptr{Void}}()
167-
@xgboost_ccall(
168-
:XGBoosterCreate,
169-
(Ptr{Ptr{Void}}, UInt64, Ref{Ptr{Void}}),
170-
cachelist, len, handle
171-
)
157+
@xgboost(:XGBoosterCreate,
158+
cachelist => Ptr{Ptr{Void}},
159+
len => UInt64,
160+
handle => Ref{Ptr{Void}})
172161
return handle[]
173162
end
174163

175164
function XGBoosterFree(handle::Ptr{Void})
176-
@xgboost_ccall(
177-
:XGBoosterFree,
178-
(Ptr{Void}, ),
179-
handle
180-
)
165+
@xgboost(:XGBoosterFree,
166+
handle => Ptr{Void})
181167
end
182168

183169
function XGBoosterSetParam(handle::Ptr{Void}, key::String, value::String)
184-
@xgboost_ccall(
185-
:XGBoosterSetParam,
186-
(Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}),
187-
handle, key, value
188-
)
170+
@xgboost(:XGBoosterSetParam,
171+
handle => Ptr{Void},
172+
key => Ptr{UInt8},
173+
value => Ptr{UInt8})
189174
end
190175

191176
function XGBoosterUpdateOneIter(handle::Ptr{Void}, iter::Int32, dtrain::Ptr{Void})
192-
@xgboost_ccall(
193-
:XGBoosterUpdateOneIter,
194-
(Ptr{Void}, Int32, Ptr{Void}),
195-
handle, iter, dtrain
196-
)
177+
@xgboost(:XGBoosterUpdateOneIter,
178+
handle => Ptr{Void},
179+
iter => Int32,
180+
dtrain => Ptr{Void})
197181
end
198182

199-
function XGBoosterBoostOneIter(handle::Ptr{Void}, dtrain::Ptr{Void},
200-
grad::Array{Float32, 1},
201-
hess::Array{Float32, 1},
202-
len::UInt64)
203-
@xgboost_ccall(
204-
:XGBoosterBoostOneIter,
205-
(Ptr{Void}, Ptr{Void}, Ptr{Float32}, Ptr{Float32}, UInt64),
206-
handle, dtrain, grad, hess, len
207-
)
183+
function XGBoosterBoostOneIter(handle::Ptr{Void}, dtrain::Ptr{Void}, grad::Array{Float32,1},
184+
hess::Array{Float32,1}, len::UInt64)
185+
@xgboost(:XGBoosterBoostOneIter,
186+
handle => Ptr{Void},
187+
dtrain => Ptr{Void},
188+
grad => Ptr{Float32},
189+
hess => Ptr{Float32},
190+
len => UInt64)
208191
end
209192

210-
function XGBoosterEvalOneIter(handle::Ptr{Void}, iter::Int32,
211-
dmats::Array{Ptr{Void}, 1},
212-
evnames::Array{String, 1}, len::UInt64)
193+
function XGBoosterEvalOneIter(handle::Ptr{Void}, iter::Int32, dmats::Array{Ptr{Void},1},
194+
evnames::Array{String,1}, len::UInt64)
213195
msg = Ref{Ptr{UInt8}}()
214-
@xgboost_ccall(
215-
:XGBoosterEvalOneIter,
216-
(Ptr{Void}, Int32, Ptr{Ptr{Void}}, Ptr{Ptr{UInt8}}, UInt64, Ref{Ptr{UInt8}}),
217-
handle, iter, dmats, evnames, len, msg
218-
)
196+
@xgboost(:XGBoosterEvalOneIter,
197+
handle => Ptr{Void},
198+
iter => Int32,
199+
dmats => Ptr{Ptr{Void}},
200+
evnames => Ptr{Ptr{UInt8}},
201+
len => UInt64,
202+
msg => Ref{Ptr{UInt8}})
219203
return unsafe_string(msg[])
220204
end
221205

222-
223206
function XGBoosterPredict(handle::Ptr{Void}, dmat::Ptr{Void}, output_margin::Int32,
224-
ntree_limit::UInt32, len::Array{UInt64, 1})
207+
ntree_limit::UInt32, len::Array{UInt64,1})
225208
ret = Ref{Ptr{Float32}}()
226-
@xgboost_ccall(
227-
:XGBoosterPredict,
228-
(Ptr{Void}, Ptr{Void}, Int32, UInt32, Ptr{UInt64}, Ref{Ptr{Float32}}),
229-
handle, dmat, output_margin, ntree_limit, len, ret
230-
)
209+
@xgboost(:XGBoosterPredict,
210+
handle => Ptr{Void},
211+
dmat => Ptr{Void},
212+
output_margin => Int32,
213+
ntree_limit => UInt32,
214+
len => Ptr{UInt64},
215+
ret => Ref{Ptr{Float32}})
231216
return ret[]
232217
end
233218

234-
235219
function XGBoosterLoadModel(handle::Ptr{Void}, fname::String)
236-
@xgboost_ccall(
237-
:XGBoosterLoadModel,
238-
(Ptr{Void}, Ptr{UInt8}),
239-
handle, fname
240-
)
220+
@xgboost(:XGBoosterLoadModel,
221+
handle => Ptr{Void},
222+
fname => Ptr{UInt8})
241223
end
242224

243225
function XGBoosterSaveModel(handle::Ptr{Void}, fname::String)
244-
@xgboost_ccall(
245-
:XGBoosterSaveModel,
246-
(Ptr{Void}, Ptr{UInt8}),
247-
handle, fname
248-
)
226+
@xgboost(:XGBoosterSaveModel,
227+
handle => Ptr{Void},
228+
fname => Ptr{UInt8})
249229
end
250230

251-
252231
function XGBoosterDumpModel(handle::Ptr{Void}, fmap::String, with_stats::Int64)
253232
data = Ref{Ptr{Ptr{UInt8}}}()
254233
out_len = Ref{UInt64}(0)
255-
@xgboost_ccall(
256-
:XGBoosterDumpModel,
257-
(Ptr{Void}, Ptr{UInt8}, Int64, Ref{UInt64}, Ref{Ptr{Ptr{UInt8}}}),
258-
handle, fmap, with_stats, out_len, data
259-
)
234+
@xgboost(:XGBoosterDumpModel,
235+
handle => Ptr{Void},
236+
fmap => Ptr{UInt8},
237+
with_stats => Int64,
238+
out_len => Ref{UInt64},
239+
data => Ref{Ptr{Ptr{UInt8}}})
260240
return unsafe_wrap(Array, data[], out_len[])
261241
end

0 commit comments

Comments
 (0)