You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.7 KiB
58 lines
1.7 KiB
7 years ago
|
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||
7 years ago
|
#
|
||
7 years ago
|
# 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
|
||
7 years ago
|
#
|
||
7 years ago
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
7 years ago
|
#
|
||
7 years ago
|
# 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.
|
||
|
|
||
|
|
||
7 years ago
|
def hash_name(varlist, pserver_endpoints):
|
||
7 years ago
|
"""
|
||
7 years ago
|
hash variable names to several endpoints.
|
||
|
|
||
7 years ago
|
Args:
|
||
|
varlist(list): a list of Variables
|
||
|
|
||
|
Returns(dict): a map of pserver endpoint -> varname
|
||
7 years ago
|
"""
|
||
|
|
||
|
def _hash_block(block_str, total):
|
||
|
return hash(block_str) % total
|
||
|
|
||
7 years ago
|
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
|
||
7 years ago
|
|
||
|
|
||
7 years ago
|
def round_robin(varlist, pserver_endpoints):
|
||
|
"""
|
||
7 years ago
|
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
|
||
7 years ago
|
"""
|
||
7 years ago
|
assert (len(varlist) >= len(pserver_endpoints))
|
||
7 years ago
|
|
||
7 years ago
|
eplist = []
|
||
7 years ago
|
pserver_idx = 0
|
||
7 years ago
|
for var in varlist:
|
||
|
server_for_param = pserver_endpoints[pserver_idx]
|
||
|
eplist.append(server_for_param)
|
||
7 years ago
|
|
||
7 years ago
|
pserver_idx += 1
|
||
|
if pserver_idx >= len(pserver_endpoints):
|
||
|
pserver_idx = 0
|
||
|
return eplist
|