distributed_spliter.py 1.5 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 20 21
    hash variable names to several endpoints.

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

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

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


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

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

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