-
Notifications
You must be signed in to change notification settings - Fork 36
/
install_lightspeed.m
235 lines (225 loc) · 8.47 KB
/
install_lightspeed.m
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
%Install_lightspeed
% Compiles mex files for the lightspeed library.
% Written by Tom Minka
% (c) Microsoft Corporation. All rights reserved.
% thanks to Kevin Murphy for suggesting this routine.
% thanks to Ruben Martinez-Cantin for UNDERSCORE_LAPACK_CALL
% check that we are in the right directory
d = dir('install_lightspeed.m');
if length(d) == 0
error('You must run install_lightspeed from within the lightspeed directory. Use cd to change the current directory.');
end
fprintf('Compiling lightspeed 2.7 mex files...\n');
% Matlab version
v = sscanf(version,'%d.%d.%*s (R%d) %*s');
% v(3) is the R number
% could also use v(3)>=13
atleast65 = (v(1)>6 || (v(1)==6 && v(2)>=5));
atleast73 = (v(1)>7 || (v(1)==7 && v(2)>=3));
atleast75 = (v(1)>7 || (v(1)==7 && v(2)>=5));
atleast76 = (v(1)>7 || (v(1)==7 && v(2)>=6));
atleast78 = (v(1)>7 || (v(1)==7 && v(2)>=8)); % R2009a
atleast82 = (v(1)>8 || (v(1)==8 && v(2)>=2)); % R2013b
atleast83 = (v(1)>8 || (v(1)==8 && v(2)>=3)); % R2014a
if atleast73
% largeArrayDims and mwSize were added in version 7.3 (R2006b)
% http://www.mathworks.com/help/techdoc/rn/bqt6wtq.html
flags = ' -largeArrayDims ';
else
flags = ' -DmwSize=int -DmwIndex=int ';
end
if atleast83
flags = [flags ' -silent '];
end
% To catch ANSI C violations with gcc:
%flags = [flags ' CFLAGS=''-Wall -std=c99'' '];
replace_repmat = ~atleast82;
% copy matlab's original repmat.m as xrepmat.m
if replace_repmat && exist('xrepmat.m') ~= 2
w = fullfile(matlabroot,'toolbox','matlab','elmat','repmat.m');
cmd = ['"' w '" xrepmat.m'];
if ispc
system(['copy ' cmd]);
else
system(['cp -rp ' cmd]);
end
end
% these are done first to initialize mex
eval(['mex' flags '-c flops.c']);
eval(['mex' flags 'sameobject.c']);
eval(['mex' flags 'int_hist.c']);
eval(['mex' flags '-c mexutil.c']);
eval(['mex' flags '-c util.c']);
libdir = '';
if ispc || atleast83
[compiler,options] = mexcompiler;
libdir = options.LIBLOC;
engmatopts = [compiler 'engmatopts.bat'];
elseif ismac
options = struct;
% this installer is set up for 64-bit MacOSX 10.6 with gcc-4.0
% if you are using something else, run 'mex -v -c flops.c'
% and use the output to change these strings
options.COMPILER = 'gcc-4.0';
options.COMPFLAGS = '-fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 -fexceptions';
options.OPTIMFLAGS = '-O -DNDEBUG';
else
options = struct;
options.COMPILER = 'cc';
options.COMPFLAGS = '-fPIC';
options.OPTIMFLAGS = '-O';
end
% Routines that use LAPACK
lapacklib = '';
blaslib = '';
lapackflags = flags;
if atleast78
lapackflags = [lapackflags ' -DBLAS64'];
end
if ispc
if strncmp(compiler,'MSVC',4)
if atleast65
% version >= 6.5
lapacklib = fullfile(libdir,'libmwlapack.lib');
end
else
lapacklib = fullfile(libdir,'libmwlapack.lib');
end
if atleast75
blaslib = fullfile(libdir,'libmwblas.lib');
end
%%% Paste the location of libmwlapack.lib %%%
%lapacklib = '';
if ~exist(lapacklib,'file')
lapacklib = 'dtrsm.c';
fprintf('libmwlapack.lib was not found. To get additional optimizations, paste its location into install_lightspeed.m\n');
else
fprintf('Using the lapack library at %s\n',lapacklib);
end
else
% in version 7.5, non-PC systems do not need to specify lapacklib,
% but they must use an underscore when calling lapack routines
% http://www.mathworks.com/help/techdoc/matlab_external/br_2m24-1.html
lapackflags = [lapackflags ' -DUNDERSCORE_LAPACK_CALL'];
if atleast76
lapacklib = '-lmwlapack';
blaslib = '-lmwblas';
end
end
disp(['mex' lapackflags ' solve_triu.c "' lapacklib '" "' blaslib '"'])
eval(['mex' lapackflags ' solve_triu.c "' lapacklib '" "' blaslib '"']);
eval(['mex' lapackflags ' solve_tril.c "' lapacklib '" "' blaslib '"']);
if ispc
% Windows
%if exist('util.obj','file')
eval(['mex' flags 'addflops.c flops.obj'])
if atleast78
[success,message] = mkdir('@double');
eval(['mex' flags 'gammaln.c util.obj -outdir @double'])
else
eval(['mex' flags 'gammaln.c util.obj'])
end
eval(['mex' flags 'digamma.c util.obj'])
eval(['mex' flags 'trigamma.c util.obj'])
eval(['mex' flags 'tetragamma.c util.obj'])
eval(['mex' flags 'setnonzeros.c'])
if strncmp(compiler,'MSVC',4)
clear random.dll randomseed randbinom randgamma sample_hist
disp(['install_random.bat "' options.VSINSTALLDIR '" ' options.vcvarsopts]);
system(['install_random.bat "' options.VSINSTALLDIR '" ' options.vcvarsopts]);
eval(['mex' flags 'randomseed.c util.obj random.lib'])
eval(['mex' flags 'randbinom.c mexutil.obj util.obj random.lib'])
eval(['mex' flags 'randgamma.c mexutil.obj util.obj random.lib'])
eval(['mex' flags 'sample_hist.c util.obj random.lib'])
else
fprintf('mexcompiler is not MSVC. The randomseed() function will have no effect.');
eval(['mex' flags 'randomseed.c util.obj random.c'])
eval(['mex' flags 'randbinom.c mexutil.obj util.obj random.c'])
eval(['mex' flags 'randgamma.c mexutil.obj util.obj random.c'])
eval(['mex' flags 'sample_hist.c util.obj random.c'])
end
if replace_repmat
eval(['mex' flags 'repmat.c mexutil.obj'])
else
eval(['mex' flags 'repmat.c mexutil.obj -output xrepmat'])
end
try
% standalone programs
% compilation instructions are described at:
% http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch1_im15.html#27765
if atleast78
%disp('lightspeed''s matfile utility is not supported for this version of Matlab, but everything else should work.');
elseif atleast65
% -V5 is required for Matlab >=6.5
mex('-f',engmatopts,'matfile.c','-V5');
else
mex('-f',engmatopts,'matfile.c');
end
% uncomment the line below if you want to build test_flops.exe
% This program lets you check the flop counts on your processor.
% mex('-f',engmatopts,'tests/test_flops.c');
catch
disp('Could not install the standalone programs.');
disp(lasterr)
end
else
% UNIX
eval(['mex' flags 'addflops.c flops.o'])
if atleast78
[success,message] = mkdir('@double');
eval(['mex' flags 'gammaln.c util.o -lm -outdir @double'])
else
eval(['mex' flags 'gammaln.c util.o -lm'])
end
eval(['mex' flags 'digamma.c util.o -lm'])
eval(['mex' flags 'trigamma.c util.o -lm'])
eval(['mex' flags 'tetragamma.c util.o -lm'])
eval(['mex' flags 'setnonzeros.c'])
if ismac
% thanks to Nicholas Butko for these mac-specific lines
clear librandom.dylib randomseed randbinom randgamma sample_hist
cmd = [options.COMPILER ' ' options.COMPFLAGS ' ' options.OPTIMFLAGS ' -c random.c; ' options.COMPILER ' ' options.COMPFLAGS ' -dynamiclib -Wl,-install_name,"`pwd`/librandom.dylib" -o librandom.dylib random.o'];
disp(cmd);
system(cmd)
eval(['mex' flags 'randomseed.c util.o librandom.dylib -lm'])
eval(['mex' flags 'randbinom.c mexutil.o util.o librandom.dylib -lm'])
eval(['mex' flags 'randgamma.c mexutil.o util.o librandom.dylib -lm'])
eval(['mex' flags 'sample_hist.c util.o librandom.dylib -lm'])
else
% this command only works on linux
clear librandom.so randomseed randbinom randgamma sample_hist
cmd = [options.COMPILER ' ' options.COMPFLAGS ' ' options.OPTIMFLAGS ' -c random.c; ' options.COMPILER ' ' options.COMPFLAGS ' -shared -Wl,-E -Wl,-soname,`pwd`/librandom.so -o librandom.so random.o'];
disp(cmd);
system(cmd)
eval(['mex' flags 'randomseed.c util.o librandom.so -lm'])
eval(['mex' flags 'randbinom.c mexutil.o util.o librandom.so -lm'])
eval(['mex' flags 'randgamma.c mexutil.o util.o librandom.so -lm'])
eval(['mex' flags 'sample_hist.c util.o librandom.so -lm'])
end
if replace_repmat
eval(['mex' flags 'repmat.c mexutil.o'])
else
eval(['mex' flags 'repmat.c mexutil.o -output xrepmat'])
end
try
% standalone programs
if atleast78
disp('lightspeed''s matfile utility is not supported for this version of Matlab');
elseif atleast65
% -V5 is required only for Matlab >=6.5
mex -f matopts.sh matfile.c -V5
else
mex -f matopts.sh matfile.c
end
% uncomment the line below if you want to build test_flops.exe
% This program lets you check the flop counts on your processor.
% mex -f matopts.sh tests/test_flops.c
catch
disp('Could not install the standalone programs.');
disp(lasterr);
fprintf('Note: if matlab cannot find matopts.sh, your installation of matlab is faulty.\nIf you get this error, don''t worry, lightspeed should still work.');
end
end
addpath(genpath(pwd))
fprintf('Done.\n');
fprintf('Type "test_lightspeed" to verify the installation.\n');