PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/spams/compile.m

https://github.com/duythanhphan/ihog
Objective C | 343 lines | 329 code | 14 blank | 0 comment | 60 complexity | b2433701ac2121e91e80d25ce4da1094 MD5 | raw file
Possible License(s): GPL-3.0
  1. get_architecture;
  2. %%%%%%%%%%%%% COMPILER CONFIGURATION %%%%%%%%%%%%%%%%
  3. % set up the compiler you want to use. Possible choices are
  4. % - 'mex' (default matlab compiler), this is the easy choice if your matlab
  5. % is correctly configured. Note that this choice might not compatible
  6. % with the option 'use_multithread=true'.
  7. % - 'icc' (intel compiler), usually produces the fastest code, but the
  8. % compiler is not free and not installed by default.
  9. % - 'gcc' (gnu compiler), good choice (for Mac, use gcc >= 4.6 for
  10. % the multi-threaded version, otherwise set use_multithread=false).
  11. % For windows, you need to have cygwin installed.
  12. % - 'open64' (amd compiler), optimized for opteron cpus.
  13. % - 'vs' (visual studio compiler) for windows computers (10.0 or more is recommended)
  14. % for some unknown reason, the performance obtained with vs is poor compared to icc/gcc
  15. compiler='gcc';
  16. %%%%%%%%%%%% BLAS/LAPACK CONFIGURATION %%%%%%%%%%%%%%
  17. % set up the blas/lapack library you want to use. Possible choices are
  18. % - builtin: blas/lapack shipped with Matlab,
  19. % same as mex: good choice if matlab is correctly configured.
  20. % - mkl: (intel math kernel library), usually the fastest, but not free.
  21. % - acml: (AMD Core math library), optimized for opteron cpus
  22. % - blas: (netlib at atlas version of blas/lapack), free
  23. % ==> you can also tweak this script to include your favorite blas/lapack library
  24. blas='builtin';
  25. %%%%%%%%%%%% MULTITHREADING CONFIGURATION %%%%%%%%%%%%%%
  26. % set true if you want to use multi-threaded capabilities of the toolbox. You
  27. % need an appropriate compiler for that (intel compiler, most recent gcc, or visual studio pro)
  28. use_multithread=true; % (might not compatible with compiler=mex)
  29. % if the compilation fails on Mac, try the single-threaded version.
  30. % to run the toolbox on a cluster, it can be a good idea to deactivate this
  31. % if you use the options 'mex' and 'builtin', you can proceed with the compilation by
  32. % typing 'compile' in the matlab shell. Otherwise, you need to set up a few path below.
  33. %%%%%%%%%%%% PATH CONFIGURATION %%%%%%%%%%%%%%%%%%%%
  34. % only if you do not use the options 'mex' and 'builtin'
  35. % set up the path to the compiler libraries that you intend to use below
  36. if strcmp(compiler,'gcc')
  37. if linux || mac
  38. % example when compiler='gcc' for Linux/Mac: (path containing the files libgcc_s.*)
  39. % NOTE: you might try using one of these libraries
  40. %path_to_compiler_libraries='/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5/';
  41. %path_to_compiler_libraries='/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/x86_64/';
  42. path_to_compiler_libraries='/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.7.3/';
  43. path_to_compiler='/usr/bin/';
  44. else
  45. % example when compiler='gcc' for Windows+cygwin: (the script does not
  46. % work at the moment in this configuration
  47. path_to_compiler='C:\cygwin\bin\';
  48. path_to_compiler_libraries='C:\cygwin\lib\gcc\i686-pc-cygwin\4.5.3\';
  49. end
  50. elseif strcmp(compiler,'open64')
  51. % example when compiler='gcc' for Linux/Mac: (path containing libgcc_s.*)
  52. path_to_compiler_libraries='/opt/amdsdk/v1.0/x86_open64-4.2.4/lib/gcc-lib/x86_64-open64-linux/4.2.4/';
  53. path_to_compiler='/opt/amdsdk/v1.0/x86_open64-4.2.4/bin/';
  54. elseif strcmp(compiler,'icc')
  55. if linux || mac
  56. % example when compiler='icc' for Linux/Mac
  57. path_to_compiler_libraries='/opt/intel/composerxe/lib/intel64/';
  58. path_to_compiler='/opt/intel/composerxe/bin/';
  59. else
  60. % example when compiler='icc' for Windows
  61. path_to_compiler_libraries='C:\Program Files (x86)\Intel\Composer XE\compiler\lib\intel64\';
  62. path_to_compiler='C:\Program Files (x86)\Intel\Composer XE\bin\intel64\';
  63. path_to_compiler_include='C:\Program Files (x86)\Intel\Composer XE\compiler\include\';
  64. path_to_vs='C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\';
  65. end
  66. elseif strcmp(compiler,'vs')
  67. % example when compiler='vs' for Windows
  68. path_to_compiler_libraries='C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\';
  69. path_to_compiler=path_to_compiler_libraries;
  70. elseif strcmp(compiler,'mex')
  71. % leave it blank when compiler='mex'
  72. path_to_compiler_libraries='';
  73. path_to_compiler='';
  74. % use_multithread=false;
  75. end
  76. % set up the path to the blas/lapack libraries.
  77. if strcmp(blas,'mkl')
  78. if linux || mac
  79. path_to_blas='/opt/intel/composerxe/mkl/lib/intel64/';
  80. else
  81. path_to_blas='C:\Program Files (x86)\Intel\Composer XE\mkl\lib\intel64\';
  82. end
  83. elseif strcmp(blas,'blas')
  84. if linux || mac
  85. path_to_blas='/usr/lib/';
  86. else
  87. path_to_blas='?';
  88. end
  89. elseif strcmp(blas,'acml')
  90. if linux || mac
  91. %path_to_blas=' /opt/amdsdk/v1.0/acml/open64_64/lib/';
  92. path_to_blas='/opt/acml5.0.0/ifort64/lib/';
  93. else
  94. path_to_blas='?';
  95. end
  96. elseif strcmp(blas,'builtin')
  97. % leave it to /usr/lib/ for built-in:
  98. path_to_blas='/';
  99. end
  100. debug=false;
  101. if debug
  102. use_multithread=false;
  103. end
  104. %%%%%%%%%%%% END OF THE CONFIGURATION %%%%%%%%%%%%%%
  105. % Do not touch what is below this line, unless you know what you are doing
  106. out_dir='./build/';
  107. COMPILE = {
  108. % compile dictLearn toolbox
  109. '-I./linalg/ -I./decomp/ -I./dictLearn/ dictLearn/mex/mexTrainDL.cpp',
  110. %'-I./linalg/ -I./decomp/ -I./dictLearn/ dictLearn/mex/mexTrainDL_Memory.cpp',
  111. % compile dag toolbox
  112. %'-I./dags/ -I./linalg/ dags/mex/mexRemoveCyclesGraph.cpp',
  113. %'-I./dags/ -I./linalg/ dags/mex/mexCountPathsDAG.cpp',
  114. %'-I./dags/ -I./linalg/ dags/mex/mexCountConnexComponents.cpp',
  115. %% compile proximal toolbox
  116. %'-I./linalg/ -I./prox/ prox/mex/mexEvalPathCoding.cpp',
  117. %'-I./linalg/ -I./prox/ prox/mex/mexFistaFlat.cpp',
  118. %'-I./linalg/ -I./prox/ prox/mex/mexFistaTree.cpp',
  119. %'-I./linalg/ -I./prox/ prox/mex/mexFistaGraph.cpp',
  120. %'-I./linalg/ -I./prox/ prox/mex/mexFistaPathCoding.cpp',
  121. %'-I./linalg/ -I./prox/ prox/mex/mexProximalFlat.cpp',
  122. %'-I./linalg/ -I./prox/ prox/mex/mexProximalTree.cpp',
  123. %'-I./linalg/ -I./prox/ prox/mex/mexProximalGraph.cpp',
  124. %'-I./linalg/ -I./prox/ prox/mex/mexProximalPathCoding.cpp',
  125. %% compile linalg toolbox
  126. %'-I./linalg/ linalg/mex/mexCalcAAt.cpp',
  127. %'-I./linalg/ linalg/mex/mexCalcXAt.cpp',
  128. %'-I./linalg/ linalg/mex/mexCalcXY.cpp',
  129. %'-I./linalg/ linalg/mex/mexCalcXYt.cpp',
  130. %'-I./linalg/ linalg/mex/mexCalcXtY.cpp',
  131. %'-I./linalg/ linalg/mex/mexConjGrad.cpp',
  132. %'-I./linalg/ linalg/mex/mexInvSym.cpp',
  133. %'-I./linalg/ linalg/mex/mexSort.cpp',
  134. %'-I./linalg/ linalg/mex/mexNormalize.cpp',
  135. % compile decomp toolbox
  136. '-I./linalg/ -I./decomp/ decomp/mex/mexLasso.cpp',
  137. %'-I./linalg/ -I./decomp/ decomp/mex/mexOMP.cpp',
  138. %'-I./linalg/ -I./decomp/ decomp/mex/mexCD.cpp'
  139. %'-I./linalg/ -I./decomp/ decomp/mex/mexL1L2BCD.cpp',
  140. %'-I./linalg/ -I./decomp/ decomp/mex/mexLassoMask.cpp',
  141. %'-I./linalg/ -I./decomp/ decomp/mex/mexLassoWeighted.cpp',
  142. %'-I./linalg/ -I./decomp/ decomp/mex/mexOMPMask.cpp',
  143. %'-I./linalg/ -I./decomp/ decomp/mex/mexSOMP.cpp',
  144. %'-I./linalg/ -I./decomp/ decomp/mex/mexSparseProject.cpp'
  145. };
  146. if linux || mac
  147. fid=fopen('run_matlab.sh','w+');
  148. fprintf(fid,'#!/bin/sh\n');
  149. end
  150. if sixtyfourbits
  151. if debug
  152. DEFCOMMON='-largeArrayDims -DDEBUG';
  153. else
  154. DEFCOMMON='-largeArrayDims -DNDEBUG';
  155. end
  156. else
  157. if debug
  158. DEFCOMMON='-DDEBUG';
  159. else
  160. DEFCOMMON='-DNDEBUG';
  161. end
  162. end
  163. if windows
  164. DEFCOMMON=[DEFCOMMON ' -DWINDOWS -DREMOVE_'];
  165. end
  166. DEFBLAS='';
  167. if strcmp(blas,'mkl')
  168. DEFBLAS='-DUSE_BLAS_LIB';
  169. if strcmp(arch,'GLNXA64')
  170. blas_link = sprintf('-Wl,--start-group %slibmkl_intel_lp64.a %slibmkl_sequential.a %slibmkl_core.a -Wl,--end-group',path_to_blas,path_to_blas,path_to_blas);
  171. elseif strcmp(arch,'GLNX86')
  172. blas_link = sprintf('-Wl,--start-group %slibmkl_intel.a %slibmkl_sequential.a %slibmkl_core.a -Wl,--end-group',path_to_blas,path_to_blas,path_to_blas);
  173. elseif strcmp(arch,'MACI64')
  174. blas_link = sprintf('%slibmkl_intel_lp64.a %slibmkl_sequential.a %slibmkl_core.a',path_to_blas,path_to_blas,path_to_blas);
  175. elseif strcmp(arch,'MACI') || strcmp(arch,'MAC')
  176. blas_link = sprintf('%slibmkl_intel.a %slibmkl_sequential.a %slibmkl_core.a',path_to_blas,path_to_blas,path_to_blas);
  177. elseif strcmp(arch,'PCWIN64')
  178. blas_link = sprintf(' -L"%s" -lmkl_intel_lp64 -lmkl_sequential -lmkl_core',path_to_blas);
  179. elseif strcmp(arch,'PCWIN')
  180. blas_link = sprintf(' -L"%s" -lmkl_intel -lmkl_sequential -lmkl_core',path_to_blas);
  181. else
  182. 'unsupported achitecture'
  183. return;
  184. end
  185. elseif strcmp(blas,'blas')
  186. DEFBLAS='-DUSE_BLAS_LIB';
  187. blas_link='-lblas -llapack';
  188. elseif strcmp(blas,'builtin')
  189. blas_link='-lmwblas -lmwlapack';
  190. DEFBLAS='-DUSE_BLAS_LIB';
  191. elseif strcmp(blas,'acml')
  192. DEFBLAS='-DUSE_BLAS_LIB -DNEW_MATLAB';
  193. blas_link=sprintf(' -L%s -L/usr/lib/ -lacml ',path_to_blas);
  194. %blas_link=sprintf(' -L%s -L/usr/lib/ -lacml -lg2c -L%s -lmv',path_to_blas,'/opt/amdsdk/v1.0/x86_open64-4.2.4/lib/gcc-lib/x86_64-open64-linux/4.2.4/');
  195. else
  196. 'please provide a correct blas library';
  197. return;
  198. end
  199. if ~verLessThan('matlab','7.9.0')
  200. DEFBLAS=[DEFBLAS ' -DNEW_MATLAB'];
  201. end
  202. links_lib=[blas_link];
  203. link_flags=' -O ';
  204. if strcmp(compiler,'icc')
  205. if windows
  206. DEFCOMP=['"PATH=%PATH%;' path_to_compiler '";"' path_to_vs '" COMPILER=icl' ' -I"C:\Program Files (x86)\Intel\Composer XE\compiler\include"'];
  207. %compile_flags='/Qvc10 /fast /QaxSSE2,SSE3,SSE4.1,SSE4.2,AVX /Qansi-alias /Qopenmp /fp:fast=2 /MD /Oy- /GR /EHs /Zp8';
  208. compile_flags='/Qvc10 /Qopenmp /MD /QaxSSE2,SSE3,SSE4.1,SSE4.2,AVX,CORE-AVX2,CORE-AVX-I /O2';
  209. else
  210. DEFCOMP=sprintf('CXX=%s/icpc LDCXX=%s/icpc',path_to_compiler,path_to_compiler);
  211. compile_flags='-fPIC -axSSE3,SSE4.1,SSE4.2,AVX,CORE-AVX2,CORE-AVX-I -pipe -w -w0 -O3 -fomit-frame-pointer -no-prec-div -fno-alias -align -falign-functions -fp-model fast -funroll-loops ';
  212. %link_flags=[link_flags sprintf(' -cxxlib=%s',path_to_gcc_libraries)];
  213. %link_flags=[link_flags ' -gcc-version=430'];
  214. end
  215. links_lib=[links_lib ' -L"' path_to_compiler_libraries '" '];
  216. %links_lib=[links_lib ' -L"' path_to_compiler_libraries '" -L"' path_to_gcc_libraries '" -lstdc++'];
  217. if mac
  218. fprintf(fid,'export LIB_INTEL=%s\n',path_to_compiler_libraries);
  219. fprintf(fid,sprintf('export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:%s:%s\n',path_to_compiler_libraries,path_to_blas));
  220. fprintf(fid,'export DYLD_INSERT_LIBRARIES=$LIB_INTEL/libimf.dylib:$LIB_INTEL/libintlc.dylib:$LIB_INTEL/libiomp5.dylib:$LIB_INTEL/libsvml.dylib\n');
  221. elseif linux
  222. fprintf(fid,'export LIB_INTEL=%s\n',path_to_compiler_libraries);
  223. fprintf(fid,sprintf('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s\n',path_to_compiler_libraries,path_to_blas));
  224. fprintf(fid,'export LD_PRELOAD=$LIB_INTEL/libimf.so:$LIB_INTEL/libintlc.so.5:$LIB_INTEL/libiomp5.so:$LIB_INTEL/libsvml.so\n');
  225. end
  226. if use_multithread
  227. if windows
  228. compile_flags=[compile_flags ' /Qopenmp'];
  229. %links_lib=[links_lib ' /nodefaultlib:vcomp libiomp5md.lib'];
  230. links_lib=[links_lib sprintf('-L"%s" -liomp5md -lmmd -lirc -lsvml_dispmd -ldecimal',path_to_compiler_libraries)];
  231. else
  232. compile_flags=[compile_flags ' -openmp'];
  233. links_lib=[links_lib ' -liomp5'];
  234. if mac || linux
  235. fprintf(fid,'export KMP_DUPLICATE_LIB_OK=true\n');
  236. end
  237. end
  238. end
  239. elseif strcmp(compiler,'open64')
  240. DEFCOMP=sprintf('CXX=%s/opencc',path_to_compiler);
  241. % fprintf(fid,'export LIB_INTEL=%s\n',path_to_compiler_libraries);
  242. fprintf(fid,sprintf('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s\n',path_to_compiler_libraries,path_to_blas));
  243. %fprintf(fid,'export LD_PRELOAD=$LIB_INTEL/libimf.so:$LIB_INTEL/libintlc.so.5:$LIB_INTEL/libiomp5.so:$LIB_INTEL/libsvml.so\n');
  244. compile_flags='-O2 -fomit-frame-pointer -march=opteron';
  245. links_lib=[links_lib ' -L"' path_to_compiler_libraries '" -L' path_to_blas ' -lstdc++'];
  246. if use_multithread
  247. compile_flags=[compile_flags ' -fopenmp'];
  248. links_lib=[links_lib ' -lopenmp'];
  249. end
  250. elseif strcmp(compiler,'gcc')
  251. if windows
  252. DEFCOMP=['PATH=%PATH\%;' path_to_compiler ' COMPILER=g++-4' ' COMPFLAGS="-c -fexceptions" NAME_OBJECT=-o'];
  253. else
  254. DEFCOMP=sprintf('CXX=%s/g++',path_to_compiler);
  255. end
  256. if debug
  257. compile_flags='-O2 -g';
  258. else
  259. compile_flags='-O3 -mtune=core2 -fomit-frame-pointer -funsafe-loop-optimizations';
  260. end
  261. links_lib=[links_lib ' -L"' path_to_compiler_libraries '" -L' path_to_blas];
  262. if mac
  263. fprintf(fid,'export LIB_GCC=%s\n',path_to_compiler_libraries);
  264. fprintf(fid,sprintf('export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:%s:%s\n',path_to_compiler_libraries,path_to_blas));
  265. fprintf(fid,'export DYLD_INSERT_LIBRARIES=$LIB_GCC/libgfortran.so:$LIB_GCC/libgcc_s.so:$LIB_GCC/libstdc++.so:$LIB_GCC/libgomp.so\n');
  266. elseif linux
  267. fprintf(fid,'export LIB_GCC=%s\n',path_to_compiler_libraries);
  268. fprintf(fid,sprintf('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s\n',path_to_compiler_libraries,path_to_blas));
  269. fprintf(fid,'export LD_PRELOAD=$LIB_GCC/libgfortran.so:$LIB_GCC/libgcc_s.so:$LIB_GCC/libstdc++.so:$LIB_GCC/libgomp.so\n');
  270. end
  271. if use_multithread
  272. compile_flags=[compile_flags ' -fopenmp'];
  273. links_lib=[links_lib ' -lgomp'];
  274. end
  275. elseif strcmp(compiler,'vs')
  276. DEFCOMP='COMPILER=cl';
  277. compile_flags='/c /02';
  278. if use_multithread
  279. compile_flags=[compile_flags ' /openmp'];
  280. links_lib=[links_lib ' -lvcomp'];
  281. end
  282. elseif strcmp(compiler,'mex')
  283. DEFCOMP='';
  284. compile_flags=' -O';
  285. if mac || linux
  286. fprintf(fid,sprintf('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s\n',path_to_blas));
  287. end
  288. if use_multithread
  289. if (linux || mac)
  290. compile_flags=[compile_flags ' -fopenmp']; % we assume gcc
  291. links_lib=[links_lib ' -lgomp'];
  292. end
  293. end
  294. else
  295. 'unknown compiler'
  296. return;
  297. end
  298. if ~windows
  299. fprintf(fid,'matlab $* -r \"addpath(''./build/''); addpath(''./test_release''); setenv(''MKL_NUM_THREADS'',''1''); setenv(''MKL_SERIAL'',''YES'');"\n');
  300. fclose(fid);
  301. !chmod +x run_matlab.sh
  302. end
  303. DEFS=[DEFBLAS ' ' DEFCOMMON ' ' DEFCOMP];
  304. for k = 1:length(COMPILE),
  305. str = COMPILE{k};
  306. fprintf('compilation of: %s\n',str);
  307. try,
  308. if windows
  309. str = [str ' -outdir ' out_dir, ' ' DEFS ' ' links_lib ' OPTIMFLAGS="' compile_flags '" '];
  310. else
  311. str = [str ' -outdir ' out_dir, ' ' DEFS ' CXXOPTIMFLAGS="' compile_flags '" LDOPTIMFLAGS="' link_flags '" ' links_lib];
  312. end
  313. args = regexp(str, '\s+', 'split');
  314. args = args(find(~cellfun(@isempty, args)));
  315. mex(args{:});
  316. catch e,
  317. fprintf('error: failed to compile: %s\n', str);
  318. fprintf('error: reason: %s\n', e.message);
  319. fprintf('error: you might consider adjusting spams/compile.m settings\n');
  320. fprintf('error: in particular, adjust path_to_compiler_libraries\n');
  321. fprintf('error: we will continue to attempt to compile the rest of the system\n');
  322. fprintf('error: since it might be ok that this failed to compile, but if the\n');
  323. fprintf('error: full system does not work, please fix this error\n');
  324. end
  325. end
  326. copyfile src_release/*.m build/