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
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author OpportunityLiu
-- @file find_cudadevices.lua
--
-- imports
import("core.base.option")
import("core.platform.platform")
import("core.project.config")
import("core.cache.detectcache")
import("lib.detect.find_tool")
-- a magic string to filter output
local _PRINT_SUFFIX = "<find_cudadevices>"
-- filter stdout and stderr with _PRINT_SUFFIX
function _get_lines(str)
local result = {}
for _, l in ipairs(str:split("\n")) do
if l:startswith(_PRINT_SUFFIX) then
table.insert(result, l:sub(#_PRINT_SUFFIX + 1))
end
end
return result
end
-- parse a single value
--
-- format:
-- 1. a number: `2048`
-- 2. an array: `(65536, 2048, 2048)`
-- 3. bool value: `true` or `false`
-- 4. string: `"string"`
--
function _parse_value(value)
local num = tonumber(value)
if num then return num end
if value:lower() == "true" then return true end
if value:lower() == "false" then return false end
if value:startswith('"') and value:endswith('"') then
return value:sub(2, -2)
end
if value:startswith("(") and value:endswith(")") then
local values = value:sub(2, -2):split(",")
local result = {}
for _, v in ipairs(values) do
table.insert(result, _parse_value(v:trim()))
end
return result
end
raise("don't know how to parse value: %s", value)
end
-- parse single line
--
-- format:
-- key = value
--
function _parse_line(line, device)
local key = line:match("%s+(%g+) = .+")
local value = line:match("%s+%g+ = (.+)")
if key and value then
key = key:trim()
value = value:trim()
assert(not device[key], "duplicate key: " .. key)
device[key] = _parse_value(value)
end
end
-- parse filtered lines
function _parse_result(lines, verbose)
if #lines == 0 then
-- not a failure, returns {} rather than nil
utils.warning("no cuda devices was found")
return {}
end
local devices = {}
local currentDevice = nil
for _, l in ipairs(lines) do
if verbose then
cprint("${dim}> %s", l)
end
local devId = tonumber(l:match("%s*DEVICE #(%d+)"))
if devId then
currentDevice = { ["$id"] = devId }
table.insert(devices, currentDevice)
elseif currentDevice then
_parse_line(l, currentDevice)
end
end
return devices
end
-- find devices
function _find_devices(verbose, opt)
-- find nvcc
local nvcc = assert(find_tool("nvcc"), "nvcc not found")
-- trace
if verbose then
cprint("${dim}checking for cuda devices")
end
-- get cuda devices
local sourcefile = path.join(os.programdir(), "scripts", "find_cudadevices.cpp")
local outfile = os.tmpfile({ramdisk = false}) -- no execution permision in docker's /shm
local compile_errors = nil
local results, errors = try
{
function ()
local args = { sourcefile, "-run", "-lcuda", "-o", outfile , '-DPRINT_SUFFIX="' .. _PRINT_SUFFIX .. '"' }
if opt.arch == "x86" then
table.insert(args, "-m32")
elseif opt.arch == "x64" or opt.arch == "x86_64" then
table.insert(args, "-m64")
end
return os.iorunv(nvcc.program, args, {envs = opt.envs})
end,
catch
{
function (errs)
compile_errors = tostring(errs)
end
}
}
if compile_errors then
if not option.get("diagnosis") then
compile_errors = compile_errors:split('\n')[1]
end
utils.warning("failed to find cuda devices: " .. compile_errors)
return nil
end
-- clean up
os.tryrm(outfile)
os.tryrm(outfile .. ".*")
-- get results
local results_lines = _get_lines(results)
local errors_lines = _get_lines(errors)
if #errors_lines ~= 0 then
utils.warning("failed to find cuda devices: " .. table.concat(errors_lines, "\n"))
return nil
end
-- print raw result only with -D flags
local devices = _parse_result(results_lines, option.get("diagnosis"))
if verbose then
for _, v in ipairs(devices) do
cprint("${dim}> found device #%d: ${green bright}%s${reset dim} with compute ${bright}%d.%d${reset dim} capability", v["$id"], v.name, v.major, v.minor)
end
end
return devices
end
-- get devices array form cache or via _find_devices
function _get_devices(opt)
-- init cachekey
local cachekey = "find_cudadevices"
if opt.cachekey then
cachekey = cachekey .. "_" .. opt.cachekey
end
-- check cache
local cachedata = detectcache:get(cachekey) or {}
if cachedata.succeed and not opt.force then
return cachedata.data
end
local verbose = opt.verbose or option.get("verbose") or option.get("diagnosis")
local devices = _find_devices(verbose, opt)
if devices then
cachedata = { succeed = true, data = devices }
else
cachedata = { succeed = false }
devices = {}
end
-- fill cache
detectcache:set(cachekey, cachedata)
return devices
end
function _skip_compute_mode_prohibited(devices)
local results = {}
local cuda_compute_mode_prohibited = 2
for _, dev in ipairs(devices) do
if dev.computeMode ~= cuda_compute_mode_prohibited then
table.insert(results, dev)
end
end
return results
end
function _min_sm_arch(devices, min_sm_arch)
local results = {}
for _, dev in ipairs(devices) do
if dev.major * 10 + dev.minor >= min_sm_arch then
table.insert(results, dev)
end
end
return results
end
function _order_by_flops(devices)
local ngpu_arch_cores_per_sm =
{
[30] = 192
, [32] = 192
, [35] = 192
, [37] = 192
, [50] = 128
, [52] = 128
, [53] = 128
, [60] = 64
, [61] = 128
, [62] = 128
, [70] = 64
, [72] = 64
, [75] = 64
, [80] = 64
, [86] = 128
, [87] = 128
, [89] = 128
, [90] = 128
}
for _, dev in ipairs(devices) do
local sm_per_multiproc = 0
if dev.major == 9999 and dev.minor == 9999 then
sm_per_multiproc = 1
else
sm_per_multiproc = ngpu_arch_cores_per_sm[dev.major * 10 + dev.minor] or 128;
end
dev["$flops"] = dev.multiProcessorCount * sm_per_multiproc * dev.clockRate
end
table.sort(devices, function (a,b) return a["$flops"] > b["$flops"] end)
return devices
end
-- find cuda devices of the host
--
-- @param opt the options
-- e.g. { verbose = false, force = false, cachekey = "xxxx", min_sm_arch = 35, skip_compute_mode_prohibited = false, order_by_flops = true }
--
-- @return { { ["$id"] = 0, name = "GeForce GTX 960M", major = 5, minor = 0, ... }, ... }
-- for all keys, see https://docs.nvidia.com/cuda/cuda-runtime-api/structcudaDeviceProp.html#structcudaDeviceProp
-- keys might be differ as your cuda version varies
--
function main(opt)
-- init options
opt = opt or {}
-- get devices
local devices = _get_devices(opt)
-- apply filters
if opt.min_sm_arch then
devices = _min_sm_arch(devices, opt.min_sm_arch)
end
if opt.skip_compute_mode_prohibited then
devices = _skip_compute_mode_prohibited(devices)
end
if opt.order_by_flops then
devices = _order_by_flops(devices)
end
return devices
end
|