distributed_splitter.py 1.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14 15
# 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.


T
typhoonzero 已提交
16
def hash_name(varlist, pserver_endpoints):
T
typhoonzero 已提交
17
    """
T
typhoonzero 已提交
18 19
    hash variable names to several endpoints.

20 21 22 23
    Args:
        varlist(list): a list of Variables

    Returns(dict): a map of pserver endpoint -> varname
T
typhoonzero 已提交
24 25 26 27 28
    """

    def _hash_block(block_str, total):
        return hash(block_str) % total

T
typhoonzero 已提交
29 30 31 32 33 34
    eplist = []
    for var in varlist:
        server_id = _hash_block(var.name(), len(pserver_endpoints))
        server_for_param = pserver_endpoints[server_id]
        eplist.append(server_for_param)
    return eplist
T
typhoonzero 已提交
35 36


T
typhoonzero 已提交
37 38
def round_robin(varlist, pserver_endpoints):
    """
39 40 41 42 43 44
    Distribute variables to several endpoints.
    Args:
        varlist(list): a list of variables
        pserver_endpoints(list): a list of pserver endpoints

    Returns(list[int]): the endpoint for each variable
T
typhoonzero 已提交
45
    """
46
    assert (len(varlist) >= len(pserver_endpoints))
T
typhoonzero 已提交
47

T
typhoonzero 已提交
48
    eplist = []
T
typhoonzero 已提交
49
    pserver_idx = 0
T
typhoonzero 已提交
50 51 52
    for var in varlist:
        server_for_param = pserver_endpoints[pserver_idx]
        eplist.append(server_for_param)
T
typhoonzero 已提交
53

T
typhoonzero 已提交
54 55 56 57
        pserver_idx += 1
        if pserver_idx >= len(pserver_endpoints):
            pserver_idx = 0
    return eplist