machine_info.py 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import os
H
Hongsheng Zeng 已提交
16
import platform
17 18
import subprocess
from parl.utils import logger
B
Bo Zhou 已提交
19
from parl.utils import utils
20

B
Bo Zhou 已提交
21
__all__ = ['get_gpu_count', 'get_ip_address', 'is_gpu_available']
22 23 24 25 26 27


def get_ip_address():
    """
    get the IP address of the host.
    """
H
Hongsheng Zeng 已提交
28 29 30 31 32 33 34 35 36 37
    platform_sys = platform.system()

    # Only support Linux and MacOS
    if platform_sys != 'Linux' and platform_sys != 'Darwin':
        logger.warning(
            'get_ip_address only support Linux and MacOS, please set ip address manually.'
        )
        return None

    local_ip = None
38
    import socket
H
Hongsheng Zeng 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    try:
        # First way, tested in Ubuntu and MacOS
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        local_ip = s.getsockname()[0]
        s.close()
    except:
        # Second way, tested in CentOS
        try:
            local_ip = socket.gethostbyname(socket.gethostname())
        except:
            pass

    if local_ip == None or local_ip == '127.0.0.1' or local_ip == '127.0.1.1':
        logger.warning(
            'get_ip_address failed, please set ip address manually.')
        return None

57
    return local_ip
58 59 60


def get_gpu_count():
B
Bo Zhou 已提交
61
    """get avaliable gpu count
62 63 64 65 66 67 68 69 70 71

    Returns:
        gpu_count: int    
    """

    gpu_count = 0

    env_cuda_devices = os.environ.get('CUDA_VISIBLE_DEVICES', None)
    if env_cuda_devices is not None:
        assert isinstance(env_cuda_devices, str)
H
Hongsheng Zeng 已提交
72
        try:
H
Hongsheng Zeng 已提交
73 74
            if not env_cuda_devices:
                return 0
H
Hongsheng Zeng 已提交
75 76 77 78
            gpu_count = len(
                [x for x in env_cuda_devices.split(',') if int(x) >= 0])
            logger.info(
                'CUDA_VISIBLE_DEVICES found gpu count: {}'.format(gpu_count))
H
Hongsheng Zeng 已提交
79 80
        except:
            logger.warn('Cannot find available GPU devices, using CPU now.')
H
Hongsheng Zeng 已提交
81
            gpu_count = 0
82 83 84 85 86
    else:
        try:
            gpu_count = str(subprocess.check_output(["nvidia-smi",
                                                     "-L"])).count('UUID')
            logger.info('nvidia-smi -L found gpu count: {}'.format(gpu_count))
H
Hongsheng Zeng 已提交
87 88
        except:
            logger.warn('Cannot find available GPU devices, using CPU now.')
89 90
            gpu_count = 0
    return gpu_count
B
Bo Zhou 已提交
91 92 93 94 95 96 97 98


def is_gpu_available():
    """ check whether parl can access a GPU

    Returns:
      True if a gpu device can be found.
    """
B
Bo Zhou 已提交
99 100 101 102 103 104 105 106
    ret = get_gpu_count() > 0
    if utils._HAS_FLUID:
        from paddle import fluid
        if ret is True and not fluid.is_compiled_with_cuda():
            logger.warn("Found non-empty CUDA_VISIBLE_DEVICES. \
                But PARL found that Paddle was not complied with CUDA, which may cause issues."
                        )
    return ret