You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

device.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import os
  10. import megengine._internal as mgb
  11. _default_device = os.getenv("MGE_DEFAULT_DEVICE", "xpux")
  12. def get_device_count(device_type: str) -> int:
  13. """Gets number of devices installed on this system.
  14. :param device_type: device type, one of 'gpu' or 'cpu'
  15. """
  16. device_type_set = ("cpu", "gpu")
  17. assert device_type in device_type_set, "device must be one of {}".format(
  18. device_type_set
  19. )
  20. return mgb.config.get_device_count(device_type)
  21. def is_cuda_available() -> bool:
  22. """ Returns whether cuda is avaiable.
  23. """
  24. return mgb.config.get_device_count("gpu", warn=False) > 0
  25. def set_default_device(device: str = "xpux"):
  26. r"""Sets default computing node.
  27. :param device: default device type. The type can be 'cpu0', 'cpu1', etc.,
  28. or 'gpu0', 'gpu1', etc., to specify the particular cpu or gpu to use.
  29. To specify multiple devices, use cpu0:1 or gpu0:2.
  30. 'cpux' and 'gupx' can also be used to specify any number of cpu or gpu devices.
  31. The default value is 'xpux' to specify any device available.
  32. It can also be set by environmental variable `MGE_DEFAULT_DEVICE`.
  33. """
  34. global _default_device # pylint: disable=global-statement
  35. _default_device = device
  36. def get_default_device() -> str:
  37. r"""Gets default computing node.
  38. It returns the value set by :func:`~.set_default_device`.
  39. """
  40. return _default_device

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台