-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvcc.m
261 lines (245 loc) · 8.63 KB
/
nvcc.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
function nvcc(varargin)
% This function NVCC is a wraper for the NVIDIA Cuda compiler NVCC.exe
% in combination with a Visual Studio compiler. After this Cuda
% files can be compiled into kernels
%
% It is forked from Matlab File Exchange:
% http://www.mathworks.com/matlabcentral/fileexchange/29611-nvcc-cuda-compiler-wraper
% and modified to support for Mac/Linux and more than 8 input options.
%
% If you call the code the first time, or with "nvcc -config":
% 1) It will try to locate the "The NVIDIA GPU Computing Toolkit", which
% can be downloaded from www.nvidia.com/object/cuda_get.html
% Typical Location :
% C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin
% 2) It will try to locate the visual studio compiler
% Typical Location :
% C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\
% 3) It creates a file nvccbat.bat with the compiler information.
%
% After this configuration procedure, you can compile files with:
%
%
% nvcc(filename);
%
% or
%
% nvcc(options,filename)
%
% filename : A string with the filename, for example 'example.cu'
% options : NVCC Compiler options for example,
% nvcc(option1, option2, option3,filename)
%
% For help on NVCC config options type, "nvcc --help"
%
%
% Note!
% If the NVCC fails to locate the compiler you can try to
% write your own "nvccbat.bat" file in a text-editor, for example:
% echo off
% set PATH=C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\;%PATH%
% set PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin;%PATH%
% call vcvars32.bat
% nvcc %1 %2 %3 %4 %5 %6 %7 %8 %9
%
%
%
% 1 Example, Configuration
% % Locate Cuda and VS compiler
% nvcc -config
%
% % Show the NVCC compiler options
% nvcc --help
%
% % Test some input options
% nvcc --dryrun -ptx example.cu
%
% 2 Example,
%
% % Locate Cuda and VS compiler
% nvcc -config
% % Compile the code
% nvcc('example.cu');
% % It the same as :
% % nvcc -ptx example.cu
%
% % Make the kernel
% Kernel = parallel.gpu.CUDAKernel('example.ptx', 'example.cu');
%
% % We want to execute this kernel 100 times
% Kernel.ThreadBlockSize=100;
%
% % We make an array with 100 random files
% Data=rand(100, 1, 'single');
% DataCuda= gpuArray(Data);
%
% % We will add the value 1
% OneCuda = parallel.gpu.GPUArray.ones(1,1);
%
% % Execute the kernel
% DataOneCuda = feval(Kernel, DataCuda, OneCuda);
%
% % Get the data back
% DataOne=gather(DataOneCuda);
%
% % Show the result
% figure, hold on;
% plot(Data,'b');
% plot(DataOne,'r');
%
% Copyright (c) 2010, Dirk-Jan Kroon
% All rights reserved.
%
% 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.
%
% Function is written by D.Kroon University of Twente (December 2010)
if(nargin<1)
error('nvcc:inputs','Need at least one input');
end
% If no input options compile as .ptx kernel file
if(nargin<2),
str=['-ptx ' varargin{1}];
else
% Add all input options
str=''; for i=1:nargin, str=[str varargin{i} ' ']; end
end
disp(['Arguments: ' str]);
if isunix
[status,result] = system(['nvcc ' str]);
disp(result);
else
[script_dir, script_name, ext] = fileparts(mfilename('fullpath'));
bat_file = fullfile(script_dir, [script_name '.bat']);
% If explicitly asked, go to config mode
if strcmp(varargin{1},'-config')
do_config(bat_file);
else
% If bat file doesn't exist, go to config mode
if ~exist(bat_file,'file')
do_config(bat_file);
end
% Excecute the bat file to compile a .cu file
[status,result] = system([bat_file ' ' str]);
disp(result);
end
end
function do_config(bat_file)
% Locate the Cuda Toolkit
filenametoolkit=toolkit_selection;
disp('.'); disp(['Toolkit Path: ' filenametoolkit]);
% Locate the microsoft compiler
[filenamecompiler,filenamecompilerbat]=compiler_selection();
disp('.'); disp(['VS Compiler: ' filenamecompiler]);
% Create a bat file which will excecute the nvcc compiler
createbatfile(filenamecompiler, filenametoolkit, filenamecompilerbat, bat_file);
function createbatfile(filenamecompiler,filenametoolkit, filenamecompilerbat,cfilename)
fid = fopen(cfilename,'w');
fprintf(fid,'echo off\r\n');
fprintf(fid,'%s\r\n',['set PATH=' filenamecompiler ';%PATH%']);
fprintf(fid,'%s\r\n',['set PATH=' filenametoolkit ';%PATH%']);
fprintf(fid,['call ' filenamecompilerbat '\r\n']);
fprintf(fid,'nvcc %%* \r\n');
fclose(fid);
% This function will try to locate the installed NVIDIA toolkits
function filenametoolkit=toolkit_selection()
str=getenv('ProgramFiles');
str=[str '\NVIDIA GPU Computing Toolkit\CUDA'];
if(~isdir(str))
str=getenv('ProgramW6432');
str=[str '\NVIDIA GPU Computing Toolkit\CUDA'];
if(~isdir(str))
str=getenv('ProgramFiles(x86)');
str=[str '\NVIDIA GPU Computing Toolkit\CUDA'];
else
error('nvcc:notfound','The NVIDIA GPU Computing Toolkit is not found, please make sure you have downloaded and installed the toolkit');
end
end
files=dir([str '\*']);
filenametoolkitlist=cell(1,10);
n=0;
for i=1:length(files)
if((files(i).name(1)~='.')&&files(i).isdir)
filenametoolkit=[str '\' files(i).name '\bin'];
if(exist(filenametoolkit,'dir')),
n=n+1;
filenametoolkitlist{n}=filenametoolkit;
end
end
end
if(n==0)
error('nvcc:notfound','The NVIDIA GPU Computing Toolkit is not found, please make sure you have downloaded and installed the toolkit');
end
disp('Cuda Toolkits Found : ')
disp('------------------------------------------------------------------')
for i=1:n
disp(['[' num2str(i) '] ' filenametoolkitlist{i}])
end
disp('------------------------------------------------------------------')
p=input('Select the Cuda Toolkit you want to use : ');
filenametoolkit=filenametoolkitlist{p};
% This function will try to locate all installed Microsoft Visual Compilers
function [filenamecompiler, filenamecompilerbat]=compiler_selection()
compilerfilenames=cell(1,10);
compilerfilenamesbat=cell(1,10);
n=0;
for j=1:3
switch(j)
case 1
str=getenv('ProgramFiles');
case 2
str=getenv('ProgramW6432');
case 3
str=getenv('ProgramFiles(x86)');
end
if(~isempty(str))
a=dir([str '\Microsoft Visual*']);
for i=1:length(a)
filename=[str '\' a(i).name '\VC\bin\amd64\'];
filenamebat=[filename 'vcvarsamd64.bat'];
if(exist(filenamebat,'file'))
n=n+1;
compilerfilenames{n}=filename;
compilerfilenamesbat{n}='vcvarsamd64.bat';
end
filename=[str '\' a(i).name '\VC\bin\'];
filenamebat=[filename 'vcvars32.bat'];
if(exist(filenamebat,'file'))
n=n+1;
compilerfilenames{n}=filename;
compilerfilenamesbat{n}='vcvars32.bat';
end
end
end
end
if(n==0)
error('nvcc:notfound','No visual studio compilers found, please make sure the compilers are installed');
end
disp('Visual studio compilers Found : ')
disp('------------------------------------------------------------------')
for i=1:n
disp(['[' num2str(i) '] ' compilerfilenames{i}])
end
disp('------------------------------------------------------------------')
p=input('Select the visual studio compiler you want to use : ');
filenamecompiler=compilerfilenames{p};
filenamecompilerbat=compilerfilenamesbat{p};