distributed_spliter.py 1.5 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#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.
T
typhoonzero 已提交
14
def hash_name(varlist, pserver_endpoints):
T
typhoonzero 已提交
15
    """
T
typhoonzero 已提交
16 17 18 19
    hash variable names to several endpoints.

    :param varlist: a list of Variables
    :return: a map of pserver endpoint -> varname
T
typhoonzero 已提交
20 21 22 23 24
    """

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

T
typhoonzero 已提交
25 26 27 28 29 30
    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 已提交
31 32


T
typhoonzero 已提交
33 34 35 36 37
def round_robin(varlist, pserver_endpoints):
    """
    distribute variables to several endpoints.
    """
    assert (len(varlist) > len(pserver_endpoints))
T
typhoonzero 已提交
38

T
typhoonzero 已提交
39
    eplist = []
T
typhoonzero 已提交
40
    pserver_idx = 0
T
typhoonzero 已提交
41 42 43
    for var in varlist:
        server_for_param = pserver_endpoints[pserver_idx]
        eplist.append(server_for_param)
T
typhoonzero 已提交
44

T
typhoonzero 已提交
45 46 47 48
        pserver_idx += 1
        if pserver_idx >= len(pserver_endpoints):
            pserver_idx = 0
    return eplist