forked from sdemyanov/ConvNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.m
134 lines (119 loc) · 3.93 KB
/
compile.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
function [] = compile(regime, varargin)
clear mex;
kMainFolder = fileparts(mfilename('fullpath'));
cd(kMainFolder);
kMainFolder = pwd;
kCPUFolder = fullfile(kMainFolder, 'c++');
kCppSource = fullfile(kCPUFolder, 'sources');
kCppInclude = fullfile(kCPUFolder, 'include');
kCudaFolder = fullfile(kCPUFolder, 'cuda');
kCudaInclude = fullfile(kCudaFolder, 'include');
kCudaSource = fullfile(kCudaFolder, 'sources');
targets = {'cnntrain_mex.cpp', ...
'classify_mex.cpp', ...
'genweights_mex.cpp'};
if (nargin >= 2)
indices = varargin{1};
else
indices = 1:numel(targets);
end;
cppfiles = fullfile(kCppSource, '*.cpp');
kObjFolder = fullfile(kCPUFolder, 'obj');
if (~exist(kObjFolder, 'dir'))
mkdir(kObjFolder);
end;
% generating c++ object files
if (regime == 0) % single thread CPU
mex(['"' cppfiles '"'], '-c', ...
['-I' kCppInclude], ...
'-outdir', kObjFolder, ...
'-largeArrayDims');
elseif (regime == 1) % multithread CPU
mex(['"' cppfiles '"'], '-c', ...
['-I' kCppInclude], ...
'-outdir', kObjFolder, ...
'-largeArrayDims', ...
'COMPFLAGS="/openmp $COMPFLAGS"', ...
'CXXFLAGS=""\$CXXFLAGS -fopenmp""', ...
'LDFLAGS=""\$LDFLAGS -fopenmp""');
elseif (regime == 2) % GPU
kCudaPath = getenv('CUDA_PATH');
if (isempty(kCudaPath))
assert(0, 'Install CUDA and/or setup its path in the "CUDA_PATH" variable using "setenv"');
%setenv('CUDA_PATH', 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5');
end;
kCudaHeaders = fullfile(kCudaPath, 'include');
cudaInclude = ['-I' kCudaInclude];
cudaHeaders = ['-I' kCudaHeaders];
mex(['"' cppfiles '"'], '-c', ...
['-I' kCppInclude], ...
cudaInclude, cudaHeaders, ...
'-outdir', kObjFolder, ...
'-largeArrayDims');
end;
disp('C++ object files created');
if (ispc)
cppfiles = fullfile(kObjFolder, '*.obj');
elseif (isunix)
cppfiles = fullfile(kObjFolder, '*.o');
end;
% generating mex files
kBuildFolder = fullfile(kCPUFolder, 'build');
if (~exist(kBuildFolder, 'dir'))
mkdir(kBuildFolder);
end;
if (regime ~= 2) % CPU
kBuildFolder = fullfile(kBuildFolder, 'cpu');
if (~exist(kBuildFolder, 'dir'))
mkdir(kBuildFolder);
end;
if (~exist('indices', 'var'))
indices = 1:numel(targets);
end;
for i = 1 : numel(indices)
mexfile = fullfile(kCPUFolder, targets{indices(i)});
mex(mexfile, ['"' cppfiles '"'], ...
['-I' kCppInclude], ...
'-lut', ...
'-largeArrayDims', ...
'-outdir', kBuildFolder);
disp([targets{i} ' compiled']);
end;
elseif (regime == 2) % GPU
% setup cuda settings
kCudaPath = getenv('CUDA_PATH');
kCudaHeaders = fullfile(kCudaPath, 'include');
if (ispc)
kVSFolder = getenv('VS100COMNTOOLS');
if (isempty(kVSFolder))
assert(0 == 1, 'Install Visual Studio and/or setup the path to its "Tools" in the "VS100COMNTOOLS" variable using "setenv"');
%setenv('VS100COMNTOOLS', 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\');
end;
end;
arch = computer;
if (strcmp(arch, 'PCWIN32'))
kCudaLib = fullfile(kCudaPath, 'lib', 'Win32');
elseif (strcmp(arch, 'PCWIN64'))
kCudaLib = fullfile(kCudaPath, 'lib', 'x64');
end;
copyfile(fullfile(kCudaFolder, '*.xml'), kMainFolder, 'f');
cudafiles = fullfile(kCudaSource, '*.cu');
kBuildFolder = fullfile(kBuildFolder, 'gpu');
if (~exist(kBuildFolder, 'dir'))
mkdir(kBuildFolder);
end;
for i = 1 : numel(indices)
mexfile = fullfile(kCPUFolder, targets{indices(i)});
mex(mexfile, ['"' cudafiles '"'], ['"' cppfiles '"'], ...
['-I' kCppInclude], ...
['-I' kCudaInclude], ...
['-I' kCudaHeaders], ...
['-L' kCudaLib], ...
'-lut', '-lcurand', '-lcublas', ...
'-largeArrayDims', ...
'-outdir', kBuildFolder);
disp([targets{indices(i)} ' compiled']);
end;
delete(fullfile(kMainFolder, '*.xml'));
end;
end