'Get the op to copy-initialized all local variables from PS.'

def _get_initial_sync_op(self):

    def strip_port(s):
        if s.endswith(':0'):
            return s[:(-2)]
        return s
    local_vars = tf.local_variables()
    local_var_by_name = dict([(strip_port(v.name), v) for v in local_vars])
    ops = []
    nr_shadow_vars = len(self._shadow_vars)
    for v in self._shadow_vars:
        vname = strip_port(v.name)
        for i in range(self.nr_gpu):
            name = ('tower%s/%s' % (i, vname))
            assert (name in local_var_by_name), "Shadow    variable    {}    doesn't    match    a    corresponding    local    variable!".format(v.name)
            copy_to = local_var_by_name[name]
            ops.append(copy_to.assign(v.read_value()))
    return tf.group(name='sync_{}_variables_from_ps'.format(nr_shadow_vars), *ops)
