diff options
Diffstat (limited to 'cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu')
6 files changed, 223 insertions, 0 deletions
diff --git a/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/__init__.py b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/__init__.py new file mode 100644 index 0000000..a226d3f --- /dev/null +++ b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/__init__.py @@ -0,0 +1,28 @@ +from cyborg.accelerator.drivers.gpu.nvidia.driver import NVIDIAGPUDriver +import os +import glob + +from oslo_log import log as logging + + +__import__('pkg_resources').declare_namespace(__name__) +__import__(".".join([__package__, 'base'])) + + +LOG = logging.getLogger(__name__) + + +def load_gpu_vendor_driver(): + files = glob.glob(os.path.join(os.path.dirname(__file__), "*/driver*")) + modules = set(map(lambda s: ".".join(s.rsplit(".")[0].rsplit("/", 2)[-2:]), + files)) + for m in modules: + try: + __import__(".".join([__package__, m])) + LOG.debug("Successfully loaded GPU vendor driver: %s." % m) + except ImportError as e: + LOG.error("Failed to load GPU vendor driver: %s. Details: %s" + % (m, e)) + + +load_gpu_vendor_driver() diff --git a/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/base.py b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/base.py new file mode 100644 index 0000000..80f31fc --- /dev/null +++ b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/base.py @@ -0,0 +1,56 @@ +# Copyright 2018 Lenovo, Inc. +# +# 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. + + +""" +Cyborg GPU driver implementation. +""" +from oslo_log import log as logging + +from cyborg.accelerator.drivers.gpu import utils + + +LOG = logging.getLogger(__name__) + +VENDOR_MAPS = {"10de": "nvidia", "102b": "matrox"} + + +class GPUDriver(object): + """Base class for GPU drivers. + + This is just a virtual GPU drivers interface. + Vedor should implement their specific drivers. + """ + + @classmethod + def create(cls, vendor, *args, **kwargs): + for sclass in cls.__subclasses__(): + vendor_name = VENDOR_MAPS.get(vendor) + if vendor_name == sclass.VENDOR: + return sclass(*args, **kwargs) + # raise LookupError("Not find the GPU driver for vendor_id %s" % vendor) + LOG.warn("Not find the GPU driver for vendor_id %s" % vendor) + + def __init__(self, *args, **kwargs): + pass + + def discover(self): + raise NotImplementedError() + + def program(self, device_path, image): + raise NotImplementedError() + + @classmethod + def discover_vendors(cls): + return utils.discover_vendors() diff --git a/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/__init__.py b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/__init__.py new file mode 100644 index 0000000..4e5d499 --- /dev/null +++ b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/__init__.py @@ -0,0 +1 @@ +__author__ = 'wangzh21' diff --git a/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/driver.py b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/driver.py new file mode 100644 index 0000000..c225555 --- /dev/null +++ b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/driver.py @@ -0,0 +1,41 @@ +# Copyright 2018 Lenovo, Inc. +# +# 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. + + +""" +Cyborg Intel GPU driver implementation. +""" + +import subprocess + +from cyborg.accelerator.drivers.gpu.base import GPUDriver +from cyborg.accelerator.drivers.gpu.nvidia import sysinfo + + +class NVIDIAGPUDriver(GPUDriver): + """Base class for GPU drivers. + + This is just a virtual GPU drivers interface. + Vedor should implement their specific drivers. + """ + VENDOR = "nvidia" + + def __init__(self, *args, **kwargs): + pass + + def discover(self): + return sysinfo.gpu_tree() + + def program(self, device_path, image): + pass diff --git a/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/sysinfo.py b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/sysinfo.py new file mode 100644 index 0000000..cab4c32 --- /dev/null +++ b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/nvidia/sysinfo.py @@ -0,0 +1,30 @@ +# Copyright 2018 Lenovo, Inc. +# +# 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. + + +""" +Cyborg Intel GPU driver implementation. +""" + +from cyborg.accelerator.drivers.gpu import utils +VENDER_ID = "10de" + + +def all_gpus(): + pass + + +def gpu_tree(): + devs = utils.discover_gpus(VENDER_ID) + return devs diff --git a/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/utils.py b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/utils.py new file mode 100644 index 0000000..97c7909 --- /dev/null +++ b/cyborg_enhancement/mitaka_version/cyborg/cyborg/accelerator/drivers/gpu/utils.py @@ -0,0 +1,67 @@ +# Copyright 2018 Lenovo, Inc. +# +# 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. + + +""" +Utils for GPU driver. +""" + +import re +import subprocess + + +GPU_FLAGS = ["VGA compatible controller", "3D controller"] +GPU_INFO_PATTERN = re.compile("(?P<devices>[0-9]{2}:[0-9]{2}\.[0-9]) (?P" + "<name>.*) \[.* [\[](?P<vendor_id>[0-9a-fA-F]{4})" + ":(?P<product_id>[0-9a-fA-F]{4})] .*") + + +def discover_vendors(): + cmd = "sudo lspci -nnn | grep -E '%s'" + cmd = cmd % "|".join(GPU_FLAGS) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + p.wait() + gpus = p.stdout.readlines() + vendors = set() + for gpu in gpus: + m = GPU_INFO_PATTERN.match(gpu) + if m: + vendor_id = m.groupdict().get("vendor_id") + vendors.add(vendor_id) + return vendors + + +def discover_gpus(vender_id=None): + cmd = "sudo lspci -nnn | grep -E '%s'" + cmd = cmd % "|".join(GPU_FLAGS) + if vender_id: + cmd = cmd + "| grep " + vender_id + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + p.wait() + gpus = p.stdout.readlines() + gpu_list = [] + for gpu in gpus: + m = GPU_INFO_PATTERN.match(gpu) + if m: + gpu_dict = m.groupdict() + gpu_dict["function"] = "GPU" + gpu_dict["devices"] = _match_nova_addr(gpu_dict["devices"]) + gpu_dict["assignable"] = True + gpu_list.append(gpu_dict) + return gpu_list + + +def _match_nova_addr(devices): + addr = '0000:'+devices.replace(".", ":") + return addr |