distributed_spliter.py 940 字节
Newer Older
T
typhoonzero 已提交
1
def hash_name(varlist, pserver_endpoints):
T
typhoonzero 已提交
2
    """
T
typhoonzero 已提交
3 4 5 6
    hash variable names to several endpoints.

    :param varlist: a list of Variables
    :return: a map of pserver endpoint -> varname
T
typhoonzero 已提交
7 8 9 10 11
    """

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

T
typhoonzero 已提交
12 13 14 15 16 17
    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 已提交
18 19


T
typhoonzero 已提交
20 21 22 23 24
def round_robin(varlist, pserver_endpoints):
    """
    distribute variables to several endpoints.
    """
    assert (len(varlist) > len(pserver_endpoints))
T
typhoonzero 已提交
25

T
typhoonzero 已提交
26
    eplist = []
T
typhoonzero 已提交
27
    pserver_idx = 0
T
typhoonzero 已提交
28 29 30
    for var in varlist:
        server_for_param = pserver_endpoints[pserver_idx]
        eplist.append(server_for_param)
T
typhoonzero 已提交
31

T
typhoonzero 已提交
32 33 34 35
        pserver_idx += 1
        if pserver_idx >= len(pserver_endpoints):
            pserver_idx = 0
    return eplist