summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-06-07 00:37:22 +0800
committerruki <[email protected]>2019-06-06 21:44:15 +0800
commitd1d8b6736904134159bc6271ca1cbd26d7796ca7 (patch)
treec7a9dbb2275ee04b4bc731b684f855f0017320bc
parent2a1919432128b99ff7623a55ba9d323cb6e10bab (diff)
move cuda gencodes rule
-rw-r--r--xmake/includes/add_cugencodes.lua101
-rw-r--r--xmake/rules/cuda/gencodes/xmake.lua131
-rw-r--r--xmake/rules/cuda/xmake.lua6
3 files changed, 135 insertions, 103 deletions
diff --git a/xmake/includes/add_cugencodes.lua b/xmake/includes/add_cugencodes.lua
index d68517962..71e5f8b4e 100644
--- a/xmake/includes/add_cugencodes.lua
+++ b/xmake/includes/add_cugencodes.lua
@@ -34,110 +34,11 @@
-- e.g.
-- includes("add_cugencodes.lua")
-- target("test")
--- set_kind("binary")
+-- add_rules("cuda.console")
-- add_files("src/*.cu")
-- add_cugencodes("native", "compute_50,sm_50", "compute_70")
--
-
-
--- define rule
-rule("cuda.add_cugencodes")
- before_load(function (target)
-
- local function set (list)
- local result = {}
- for _, l in ipairs(list) do result[l] = true end
- return result
- end
-
- -- sm_20 and compute_20 is supported until CUDA 8
- local knownVArchs = set { 20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75, }
- local knownRArchs = set { 20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75, }
-
- local function nf_cugencode(archs)
-
- if type(archs) ~= 'string' then
- return nil
- end
- archs = archs:trim():lower()
- if archs == 'native' then
- import("lib.detect.find_cudadevices")
- local device = find_cudadevices({ skip_compute_mode_prohibited = true, order_by_flops = true })[1]
- if device then
- return nf_cugencode('sm_' .. device.major .. device.minor)
- end
- return nil
- end
-
- local vArch = nil
- local rArchs = {}
-
- local function parse_arch(value, prefix, knowList)
- if not value:startswith(prefix) then
- return nil
- end
- local arch = tonumber(value:sub(#prefix + 1)) or tonumber(value:sub(#prefix + 2))
- if arch == nil then
- raise("Unknown architecture: " .. value)
- end
- if not knowList[arch] then
- if arch <= table.maxn(knowList) then
- raise("Unknown architecture: " .. prefix .. "_" .. arch)
- else
- utils.warning("Unknown architecture: " .. prefix .. "_" .. arch)
- end
- end
- return arch
- end
-
- for _, v in ipairs(archs:split(',')) do
- local arch = v:trim()
- local tempRArch = parse_arch(arch, 'sm', knownRArchs)
- if tempRArch then
- table.insert(rArchs, tempRArch)
- end
-
- local tempVArch = parse_arch(arch, 'compute', knownVArchs)
- if tempVArch then
- if vArch ~= nil then
- raise("More than one virtual architecture is defined in one gpu gencode option: compute_" .. vArch .. " and compute_" .. tempVArch)
- end
- vArch = tempVArch
- end
- if not (tempRArch or tempVArch) then
- raise("Unknown architecture: " .. arch)
- end
- end
-
- if vArch == nil and #rArchs == 0 then
- return nil
- end
- if #rArchs == 0 then
- return '-gencode arch=compute_' .. vArch .. ',code=compute_' .. vArch
- end
-
- rArchs = table.unique(rArchs)
- vArch = vArch or math.min(unpack(rArchs))
- if #rArchs == 1 then
- return '-gencode arch=compute_' .. vArch .. ',code=sm_' .. rArchs[1]
- else
- return '-gencode arch=compute_' .. vArch .. ',code=[sm_' .. table.concat(rArchs, ',sm_') .. ']'
- end
- end
-
- for _, v in ipairs(target:values("cuda.gencode")) do
- local flag = nf_cugencode(v)
- if flag then
- target:add('cuflags', flag)
- target:add('ldflags', flag)
- end
- end
- end)
-rule_end()
-
--- add cuda gencode to target
function add_cugencodes(...)
- add_rules("cuda.add_cugencodes")
add_values("cuda.gencode", ...)
end
diff --git a/xmake/rules/cuda/gencodes/xmake.lua b/xmake/rules/cuda/gencodes/xmake.lua
new file mode 100644
index 000000000..c00a79490
--- /dev/null
+++ b/xmake/rules/cuda/gencodes/xmake.lua
@@ -0,0 +1,131 @@
+--!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 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+-- define rule: gencodes
+rule("cuda.gencodes")
+
+ -- add rule: cuda environment
+ add_deps("cuda.env")
+
+ -- add cuda `-gencode` flags to target
+ --
+ -- the gpu arch format syntax
+ -- - compute_xx --> `-gencode arch=compute_xx,code=compute_xx`
+ -- - sm_xx --> `-gencode arch=compute_xx,code=sm_xx`
+ -- - sm_xx,sm_yy --> `-gencode arch=compute_xx,code=[sm_xx,sm_yy]`
+ -- - compute_xx,sm_yy --> `-gencode arch=compute_xx,code=sm_yy`
+ -- - compute_xx,sm_yy,sm_zz --> `-gencode arch=compute_xx,code=[sm_yy,sm_zz]`
+ -- - native --> match the fastest cuda device on current host,
+ -- eg. for a Tesla P100, `-gencode arch=compute_60,code=sm_60` will be added,
+ -- if no available device is found, no `-gencode` flags will be added
+ -- @seealso xmake/modules/lib/detect/find_cudadevices
+ --
+ before_load(function (target)
+
+ local function set (list)
+ local result = {}
+ for _, l in ipairs(list) do result[l] = true end
+ return result
+ end
+
+ -- sm_20 and compute_20 is supported until CUDA 8
+ local knownVArchs = set { 20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75, }
+ local knownRArchs = set { 20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75, }
+
+ local function nf_cugencode(archs)
+
+ if type(archs) ~= 'string' then
+ return nil
+ end
+ archs = archs:trim():lower()
+ if archs == 'native' then
+ import("lib.detect.find_cudadevices")
+ local device = find_cudadevices({ skip_compute_mode_prohibited = true, order_by_flops = true })[1]
+ if device then
+ return nf_cugencode('sm_' .. device.major .. device.minor)
+ end
+ return nil
+ end
+
+ local vArch = nil
+ local rArchs = {}
+
+ local function parse_arch(value, prefix, knowList)
+ if not value:startswith(prefix) then
+ return nil
+ end
+ local arch = tonumber(value:sub(#prefix + 1)) or tonumber(value:sub(#prefix + 2))
+ if arch == nil then
+ raise("Unknown architecture: " .. value)
+ end
+ if not knowList[arch] then
+ if arch <= table.maxn(knowList) then
+ raise("Unknown architecture: " .. prefix .. "_" .. arch)
+ else
+ utils.warning("Unknown architecture: " .. prefix .. "_" .. arch)
+ end
+ end
+ return arch
+ end
+
+ for _, v in ipairs(archs:split(',')) do
+ local arch = v:trim()
+ local tempRArch = parse_arch(arch, 'sm', knownRArchs)
+ if tempRArch then
+ table.insert(rArchs, tempRArch)
+ end
+
+ local tempVArch = parse_arch(arch, 'compute', knownVArchs)
+ if tempVArch then
+ if vArch ~= nil then
+ raise("More than one virtual architecture is defined in one gpu gencode option: compute_" .. vArch .. " and compute_" .. tempVArch)
+ end
+ vArch = tempVArch
+ end
+ if not (tempRArch or tempVArch) then
+ raise("Unknown architecture: " .. arch)
+ end
+ end
+
+ if vArch == nil and #rArchs == 0 then
+ return nil
+ end
+ if #rArchs == 0 then
+ return '-gencode arch=compute_' .. vArch .. ',code=compute_' .. vArch
+ end
+
+ rArchs = table.unique(rArchs)
+ vArch = vArch or math.min(unpack(rArchs))
+ if #rArchs == 1 then
+ return '-gencode arch=compute_' .. vArch .. ',code=sm_' .. rArchs[1]
+ else
+ return '-gencode arch=compute_' .. vArch .. ',code=[sm_' .. table.concat(rArchs, ',sm_') .. ']'
+ end
+ end
+
+ for _, v in ipairs(target:values("cuda.gencode")) do
+ local flag = nf_cugencode(v)
+ if flag then
+ target:add('cuflags', flag)
+ target:add('ldflags', flag)
+ end
+ end
+ end)
+rule_end()
diff --git a/xmake/rules/cuda/xmake.lua b/xmake/rules/cuda/xmake.lua
index 54d3f4215..2f3564f63 100644
--- a/xmake/rules/cuda/xmake.lua
+++ b/xmake/rules/cuda/xmake.lua
@@ -22,7 +22,7 @@
rule("cuda.static")
-- add rules
- add_deps("cuda.device_link")
+ add_deps("cuda.device_link", "cuda.gencodes")
-- we must set kind before target.on_load(), may we will use target in on_load()
before_load(function (target)
@@ -33,7 +33,7 @@ rule("cuda.static")
rule("cuda.shared")
-- add rules
- add_deps("cuda.device_link")
+ add_deps("cuda.device_link", "cuda.gencodes")
-- we must set kind before target.on_load(), may we will use target in on_load()
before_load(function (target)
@@ -44,7 +44,7 @@ rule("cuda.shared")
rule("cuda.console")
-- add rules
- add_deps("cuda.device_link")
+ add_deps("cuda.device_link", "cuda.gencodes")
-- we must set kind before target.on_load(), may we will use target in on_load()
before_load(function (target)