-
Notifications
You must be signed in to change notification settings - Fork 1
/
mtimesx_build.m
executable file
·475 lines (446 loc) · 16.5 KB
/
mtimesx_build.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
% mtimesx_build compiles mtimesx.c with BLAS libraries
%******************************************************************************
%
% MATLAB (R) is a trademark of The Mathworks (R) Corporation
%
% Function: mtimesx_build
% Filename: mtimesx_build.m
% Programmer: James Tursa
% Version: 1.40
% Date: October 4, 2010
% Copyright: (c) 2009, 2010 by James Tursa, All Rights Reserved
%
% This code uses the BSD License:
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
%--
%
% mtimesx_build compiles mtimesx.c and mtimesx_RealTimesReal.c with the BLAS
% libraries libmwblas.lib (if present) or libmwlapack.lib (if libmwblas.lib
% is not present). This function basically works as follows:
%
% - Opens the current mexopts.bat file in the directory [prefdir], and
% checks to make sure that the compiler selected is cl or lcc. If it
% is not, then a warning is issued and the compilation continues with
% the assumption that the microsoft BLAS libraries will work.
%
% - Looks for the file libmwblas.lib or libmwlapack.lib files in the
% appropriate directory: [matlabroot '\extern\lib\win32\microsoft']
% or [matlabroot '\extern\lib\win32\lcc']
% or [matlabroot '\extern\lib\win64\microsoft']
% or [matlabroot '\extern\lib\win64\lcc']
%
% - Changes directory to the directory of the file mtimesx.m.
%
% - Compiles mtimesx.c (which includes mtimesx_RealTimesReal.c) along with
% either libmwblas.lib or libmwlapack.lib depending on the version of
% MATLAB. The resulting exedcutable mex file is placed in the same
% directory as the source code. The files mtimesx.m, mtimesx.c, and
% mtimesx_RealTimesReal.c must all be in the same directory.
%
% - Changes the directory back to the original directory.
%
% Change Log:
% 2009/Sep/27 --> 1.00, Initial Release
% 2010/Feb/15 --> 1.10, Fixed largearrardims typo to largeArrayDims
% 2010/Oct/04 --> 1.40, Updated support for OpenMP compiling
%
%**************************************************************************
function mtimesx_build(x)
disp(' ');
disp('... Build routine for mtimesx');
TRUE = 1;
FALSE = 0;
%\
% Check for number of inputs & outputs
%/
noopenmp = FALSE;
if( nargin == 1 )
if( isequal(upper(x),'NOOPENMP') )
noopenmp = TRUE;
else
error('Invalid input.');
end
elseif( nargin ~= 0 )
error('Too many inputs. Expected none.');
end
if( nargout ~= 0 )
error('Too many outputs. Expected none.');
end
%\
% Check for non-PC
%/
disp('... Checking for PC');
try
% ispc does not appear in MATLAB 5.3
pc = ispc ;
catch
% if ispc fails, assume we are on a Windows PC if it's not unix
pc = ~isunix ;
end
if( ~pc )
disp('Non-PC auto build is not currently supported. You will have to');
disp('manually compile the mex routine. E.g., as follows:');
disp(' ');
disp('>> blas_lib = ''the_actual_path_and_name_of_your_systems_BLAS_library''');
disp('>> mex(''-DDEFINEUNIX'',''mtimesx.c'',blas_lib)');
disp(' ');
disp('or');
disp(' ');
disp('>> mex(''-DDEFINEUNIX'',''-largeArrayDims'',''mtimesx.c'',blas_lib)');
disp(' ');
error('Unable to compile mtimesx.c');
end
%\
% Check to see that mtimesx.c source code is present
%/
disp('... Finding path of mtimesx C source code files');
try
mname = mfilename('fullpath');
catch
mname = mfilename;
end
cname = [mname(1:end-6) '.c'];
if( isempty(dir(cname)) )
disp('Cannot find the file mtimesx.c in the same directory as the');
disp('file mtimesx_build.m. Please ensure that they are in the same');
disp('directory and try again. The following file was not found:');
disp(' ');
disp(cname);
disp(' ');
error('Unable to compile mtimesx.c');
end
disp(['... Found file mtimesx.c in ' cname]);
%\
% Check to see that mtimesx_RealTimesReal.c source code is present
%/
rname = [mname(1:end-13) 'mtimesx_RealTimesReal.c'];
if( isempty(dir(rname)) )
disp('Cannot find the file mtimesx_RealTimesReal.c in the same');
disp('directory as the file mtimesx_build.m. Please ensure that');
disp('they are in the same directory and try again. The');
disp('following file was not found:');
disp(' ');
disp(rname);
disp(' ');
error('Unable to compile mtimesx.c');
end
disp(['... Found file mtimesx_RealTimesReal.c in ' rname]);
%\
% Open the current mexopts.bat file
%/
mexopts = [prefdir '\mexopts.bat'];
fid = fopen(mexopts);
if( fid == -1 )
error('A C/C++ compiler has not been selected with mex -setup');
end
disp(['... Opened the mexopts.bat file in ' mexopts]);
disp('... Reading the mexopts.bat file to find the compiler and options used.');
%\
% Check for the correct compiler selected.
%/
ok_cl = FALSE;
ok_lcc = FALSE;
omp_option = '';
compiler = '(unknown)';
compilername = '';
while( TRUE )
tline = fgets(fid);
if( isequal(tline,-1) )
break;
else
if( isempty(compilername) )
y = findstr(tline,'OPTS.BAT');
if( ~isempty(y) )
x = findstr(tline,'rem ');
if( ~isempty(x) )
compilername = tline(x+4:y-1);
end
end
end
x = findstr(tline,'COMPILER=lcc');
if( ~isempty(x) )
ok_lcc = TRUE;
libdir = 'lcc';
compiler = 'LCC';
disp(['... ' compiler ' is the selected compiler']);
break;
end
x = findstr(tline,'COMPILER=cl');
if( ~isempty(x) )
ok_cl = TRUE;
libdir = 'microsoft';
compiler = ['Microsoft_' compilername '_cl'];
omp_option = ' /openmp';
disp(['... ' compiler ' is the selected compiler']);
break;
end
x = findstr(tline,'COMPILER=bcc32');
if( ~isempty(x) )
ok_cl = TRUE;
libdir = 'microsoft';
compiler = ['Borland_' compilername '_bcc32'];
disp(['... ' compiler ' is the selected compiler']);
disp('... Assuming that Borland will link with Microsoft libraries');
break;
end
x = findstr(tline,'COMPILER=icl');
if( ~isempty(x) )
ok_cl = TRUE;
if( pc )
omp_option = ' -Qopenmp';
else
omp_option = ' -openmp';
end
libdir = 'microsoft';
compiler = ['Intel_' compilername '_icl'];
disp(['... ' compiler ' is the selected compiler']);
disp('... Assuming that Intel will link with Microsoft libraries');
break;
end
x = findstr(tline,'COMPILER=wc1386');
if( ~isempty(x) )
ok_cl = TRUE;
libdir = 'microsoft';
compiler = ['Watcom_' compilername '_wc1386'];
disp(['... ' compiler ' is the selected compiler']);
disp('... Assuming that Watcom will link with Microsoft libraries');
break;
end
x = findstr(tline,'COMPILER=gcc');
if( ~isempty(x) )
ok_cl = TRUE;
libdir = 'microsoft';
omp_option = ' -fopenmp';
compiler = 'GCC';
disp(['... ' compiler ' is the selected compiler']);
disp('... Assuming that GCC will link with Microsoft libraries');
break;
end
end
end
fclose(fid);
%\
% MS Visual C/C++ or lcc compiler has not been selected
%/
if( ~(ok_cl | ok_lcc) )
warning('... Supported C/C++ compiler has not been selected with mex -setup');
warning('... Assuming that Selected Compiler will link with Microsoft libraries');
warning('... Continuing at risk ...');
libdir = 'microsoft';
end
%\
% If an OpenMP supported compiler is potentially present, make sure that the
% necessary compile option is present in the mexopts.bat file on the COMPFLAGS
% line. If necessary, build a new mexopts.bat file with the correct option
% added to the COMPFLAGS line.
%/
while( TRUE )
ok_openmp = FALSE;
ok_compflags = FALSE;
xname = '';
if( isempty(omp_option) )
disp('... OpenMP compiler not detected ... you may want to check this website:');
disp(' http://openmp.org/wp/openmp-compilers/');
elseif( noopenmp )
disp(['... OpenMP compiler potentially detected, but not checking for ''' omp_option ''' compile option']);
else
disp('... OpenMP compiler potentially detected');
disp(['... Checking to see that the ''' omp_option ''' compile option is present']);
fid = fopen(mexopts);
while( TRUE )
tline = fgets(fid);
if( isequal(tline,-1) )
break;
else
x = findstr(tline,'set COMPFLAGS');
if( ~isempty(x) )
ok_compflags = TRUE;
x = findstr(tline,omp_option);
if( ~isempty(x) )
ok_openmp = TRUE;
end
break;
end
end
end
fclose(fid);
if( ~ok_compflags )
warning(['... COMPFLAGS line not found ... ''' omp_option ''' will not be added.']);
elseif( ~ok_openmp )
disp(['... The ''' omp_option ''' compile option is not present ... adding it']);
xname = [mname(1:end-6) '_mexopts.bat'];
disp(['... Creating custom options file ' xname ' with the ''' omp_option ''' option added.']);
fid = fopen(mexopts);
fidx = fopen(xname,'w');
if( fidx == -1 )
xname = '';
warning(['... Unable to create custom mexopts.bat file ... ''' omp_option ''' will not be added']);
else
while( TRUE )
tline = fgets(fid);
if( isequal(tline,-1) )
break;
else
x = findstr(tline,'set COMPFLAGS');
if( ~isempty(x) )
n = numel(tline);
e = n;
while( tline(e) < 32 )
e = e - 1;
end
tline = [tline(1:e) omp_option tline(e+1:n)];
end
fwrite(fidx,tline);
end
end
fclose(fidx);
end
fclose(fid);
end
end
%\
% Construct full file name of libmwblas.lib and libmwlapack.lib. Note that
% not all versions have both files. Earlier versions only had the lapack
% file, which contained both blas and lapack routines.
%/
comp = computer;
mext = mexext;
lc = length(comp);
lm = length(mext);
cbits = comp(max(1:lc-1):lc);
mbits = mext(max(1:lm-1):lm);
if( isequal(cbits,'64') | isequal(mbits,'64') )
compdir = 'win64';
largearraydims = '-largeArrayDims';
else
compdir = 'win32';
largearraydims = '';
end
lib_blas = [matlabroot '\extern\lib\' compdir '\' libdir '\libmwblas.lib'];
d = dir(lib_blas);
if( isempty(d) )
disp('... BLAS library file not found, so linking with the LAPACK library');
lib_blas = [matlabroot '\extern\lib\' compdir '\' libdir '\libmwlapack.lib'];
end
disp(['... Using BLAS library lib_blas = ''' lib_blas '''']);
%\
% Save old directory and change to source code directory
%/
cdold = cd;
if( length(mname) > 13 )
cd(mname(1:end-13));
end
%\
% Do the compile
%/
disp('... Now attempting to compile ...');
disp(' ');
try
if( isunix )
if( isempty(largearraydims) )
if( isempty(xname) )
disp(['mex(''-DDEFINEUNIX'',''' cname ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex('-DDEFINEUNIX',cname,lib_blas,['-DCOMPILER=' compiler]);
else
disp(['mex(''-f'',''' xname ''',''-DDEFINEUNIX'',''' cname ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex('-f',xname,'-DDEFINEUNIX',cname,lib_blas,['-DCOMPILER=' compiler]);
end
else
if( isempty(xname) )
disp(['mex(''-DDEFINEUNIX'',''' cname ''',''' largearraydims ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex('-DDEFINEUNIX',largearraydims,cname,lib_blas,['-DCOMPILER=' compiler]);
else
disp(['mex(''-f'',''' xname ''',''-DDEFINEUNIX'',''' cname ''',''' largearraydims ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex('-f',xname,'-DDEFINEUNIX',largearraydims,cname,lib_blas,['-DCOMPILER=' compiler]);
end
end
else
if( isempty(largearraydims) )
if( isempty(xname) )
disp(['mex(''' cname ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex(cname,lib_blas,['-DCOMPILER=' compiler]);
else
disp(['mex(''-f'',''' xname ''',''' cname ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex('-f',xname,cname,lib_blas,['-DCOMPILER=' compiler]);
end
else
if( isempty(xname) )
disp(['mex(''' cname ''',''' largearraydims ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex(cname,largearraydims,lib_blas,['-DCOMPILER=' compiler]);
else
disp(['mex(''-f'',''' xname ''',''' cname ''',''' largearraydims ''',lib_blas,''-DCOMPILER=' compiler ''')']);
disp(' ');
mex('-f',xname,cname,largearraydims,lib_blas,['-DCOMPILER=' compiler]);
end
end
end
disp('... mex mtimesx.c build completed ... you may now use mtimesx.');
disp(' ');
mtimesx;
break;
catch
if( noopenmp )
cd(cdold);
disp(' ');
disp('... Well, *that* didn''t work either!');
disp(' ');
disp('The mex command failed. This may be because you have already run');
disp('mex -setup and selected a non-C compiler, such as Fortran. If this');
disp('is the case, then rerun mex -setup and select a C/C++ compiler.');
disp(' ');
error('Unable to compile mtimesx.c');
else
disp(' ');
disp('... Well, *that* didn''t work ...');
disp(' ');
if( isequal(omp_option,' /openmp') )
disp('This may be because an OpenMP compile option was added that the');
disp('compiler did not like. For example, the Standard versions of the');
disp('Microsoft C/C++ compilers do not support OpenMP, only the');
disp('Professional versions do. Attempting to compile again but this');
disp(['time will not add the ''' omp_option ''' option.'])
else
disp('This may be because an OpenMP compile option was added that the');
disp('compiler did not like. Attempting to compile again, but this time');
disp(['will not add the ''' omp_option ''' option.'])
end
disp(' ');
noopenmp = TRUE;
end
end
end
%\
% Restore old directory
%/
cd(cdold);
return
end