helper.py 2.3 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

H
heqiaozhi 已提交
15

16
class FileSystem:
H
heqiaozhi 已提交
17
    """
18
    A file system that support hadoop client desc.
H
heqiaozhi 已提交
19 20 21 22 23 24 25 26 27 28

    Args:
        fs_type (string): fs_type, for example is "afs"
        user (string): hadoop param
        passwd (string): hadoop param
        hadoop bin (string): hadoop param
    Examples:
        fs = FileSystm()
    """

29 30 31 32 33 34 35 36
    def __init__(
        self,
        fs_type="afs",
        uri="afs://xx",
        user=None,
        passwd=None,
        hadoop_bin="",
    ):
37 38 39
        assert user is not None
        assert passwd is not None
        assert hadoop_bin is not None
40
        import ps_pb2 as pslib
41

H
heqiaozhi 已提交
42 43 44 45
        self.fs_client = pslib.FsClientParameter()
        self.fs_client.uri = uri
        self.fs_client.user = user
        self.fs_client.passwd = passwd
46
        # self.fs_client.buffer_size = 0
H
heqiaozhi 已提交
47
        self.fs_client.hadoop_bin = hadoop_bin
48
        # self.fs_client.afs_conf = afs_conf if not afs_conf else ""
49

H
heqiaozhi 已提交
50
    def get_desc(self):
H
heqiaozhi 已提交
51 52 53
        """
        get hadoop desc.
        """
H
heqiaozhi 已提交
54
        return self.fs_client
55

H
heqiaozhi 已提交
56

57
class MPIHelper:
H
heqiaozhi 已提交
58
    """
59
    MPIHelper is a wrapper of mpi4py, support get_rank get_size etc.
H
heqiaozhi 已提交
60 61 62 63 64 65 66
    Args:
        No params
    Examples:
        mh = MPIHelper()
        mh.get_ip()
    """

D
dongdaxiang 已提交
67
    def __init__(self):
68
        from mpi4py import MPI
69

D
dongdaxiang 已提交
70
        self.comm = MPI.COMM_WORLD
71
        self.MPI = MPI
D
dongdaxiang 已提交
72 73 74 75 76 77 78 79 80

    def get_rank(self):
        return self.comm.Get_rank()

    def get_size(self):
        return self.comm.Get_size()

    def get_ip(self):
        import socket
81

D
dongdaxiang 已提交
82 83 84 85 86
        local_ip = socket.gethostbyname(socket.gethostname())
        return local_ip

    def get_hostname(self):
        import socket
87

D
dongdaxiang 已提交
88
        return socket.gethostname()
H
heqiaozhi 已提交
89 90

    def finalize(self):
91
        self.MPI.Finalize()