Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _final_receive_and_kill(self, persis_info):
"""
Tries to receive from any active workers.
If time expires before all active workers have been received from, a
nonblocking receive is posted (though the manager will not receive this
data) and a kill signal is sent.
"""
# Send a handshake signal to each persistent worker.
if any(self.W['persis_state']):
for w in self.W['worker_id'][self.W['persis_state'] > 0]:
logger.debug("Manager sending PERSIS_STOP to worker {}".format(w))
self.wcomms[w-1].send(PERSIS_STOP, MAN_SIGNAL_KILL)
if not self.W[w-1]['active']:
# Re-activate if necessary
self.W[w-1]['active'] = self.W[w-1]['persis_state']
self.persis_pending.append(w)
exit_flag = 0
while (any(self.W['active']) or any(self.W['persis_state'])) and exit_flag == 0:
persis_info = self._receive_from_workers(persis_info)
if self.term_test(logged=False) == 2:
# Elapsed Wallclock has expired
if not any(self.W['persis_state']):
if any(self.W['active']):
logger.manager_warning(_WALLCLOCK_MSG_ACTIVE)
else:
logger.manager_warning(_WALLCLOCK_MSG_ALL_RETURNED)
exit_flag = 2
"""
This generation function always enters into persistent mode and returns
``gen_specs['gen_batch_size']`` uniformly sampled points.
.. seealso::
`test_6-hump_camel_persistent_uniform_sampling.py `_
"""
ub = gen_specs['user']['ub']
lb = gen_specs['user']['lb']
n = len(lb)
b = gen_specs['user']['gen_batch_size']
comm = libE_info['comm']
# Send batches until manager sends stop tag
tag = None
while tag not in [STOP_TAG, PERSIS_STOP]:
O = np.zeros(b, dtype=gen_specs['out'])
O['x'] = persis_info['rand_stream'].uniform(lb, ub, (b, n))
tag, Work, calc_in = sendrecv_mgr_worker_msg(comm, O)
return O, persis_info, tag
for j in range(p):
# Fhist0[i,j,nf//2] = calc_in['f_val'][calc_in['f_ind']==j]
Fhist0[i, j, nf//2] = U['f0'][j]
x_f_pairs = np.array(np.meshgrid(range(n), range(p))).T.reshape(-1, n)
H0 = build_H0(x_f_pairs, gen_specs, noise_h_mat)
iters = np.ones_like(noise_h_mat)
tag, Work, calc_in = sendrecv_mgr_worker_msg(comm, H0)
# import matlab.engine
# eng = matlab.engine.start_matlab()
# Send nf points for each (x_ind, f_ind) pair
while tag not in [STOP_TAG, PERSIS_STOP]:
x_f_pairs = np.unique(calc_in[['x_ind', 'f_ind']])
x_f_pairs_new = []
# Update Fhist0
for i, j in x_f_pairs:
for k in range(nf+1):
if k != nf/2:
logical_conds = (calc_in['x_ind'] == i, calc_in['f_ind'] == j, calc_in['n_ind'] == k)
Fhist0[i, j, k] = calc_in['f_val'][np.logical_and.reduce(logical_conds)]
# Compute noise for (i,j):
# [Fnoise(i,j),~,inform(i,j)] = ECnoise(nf-1,Fhist0(i,j,2:nf));
# t = eng.ECnoise(nf+1,matlab.double(Fhist0[i,j,:nf+1]),nargout=3)
# # Optional: check to see what would get with 2 fewer evals (requires nf>=4):
# [Fnoise2(i,j),~,inform2(i,j)] = ECnoise(nf-1,Fhist0(i,j,2:nf));
else:
something_sent = False
tag = None
first_pass = True
while 1:
new_opt_inds_to_send_mgr = []
new_inds_to_send_mgr = []
if something_sent:
if user_specs.get('standalone'):
tag, Work, calc_in = simulate_recv_from_manager(local_H, gen_specs)
else:
tag, Work, calc_in = get_mgr_worker_msg(comm)
if tag in [STOP_TAG, PERSIS_STOP]:
clean_up_and_stop(local_H, local_opters, run_order)
persis_info['run_order'] = run_order
break
n_s, n_r = update_local_H_after_receiving(local_H, n, n_s, user_specs, Work, calc_in, fields_to_pass)
for row in calc_in:
if sim_id_to_child_inds.get(row['sim_id']):
# Point came from a child local opt run
for child_idx in sim_id_to_child_inds[row['sim_id']]:
x_new = local_opters[child_idx].iterate(row[fields_to_pass])
if isinstance(x_new, ConvergedMsg):
x_opt = x_new.x
opt_flag = x_new.opt_flag
opt_ind = update_history_optimal(x_opt, opt_flag, local_H, run_order[child_idx])
new_opt_inds_to_send_mgr.append(opt_ind)
def get_mgr_worker_msg(comm, status=None):
"""Get message to worker from manager.
"""
tag, Work = comm.recv()
if tag in [STOP_TAG, PERSIS_STOP]:
comm.push_to_buffer(tag, Work)
return tag, Work, None
_, calc_in = comm.recv()
return tag, Work, calc_in
def nlopt_obj_fun(x, grad):
# Check if we can do an early return
if np.array_equiv(x, H['x']):
if gen_specs['localopt_method'] in ['LD_MMA']:
grad[:] = H['grad']
return np.float(H['f'])
# Send back x to the manager, then receive info or stop tag
O = add_to_O(np.zeros(1, dtype=gen_specs['out']), x, 0,
gen_specs['ub'], gen_specs['lb'], local=True, active=True)
tag, Work, calc_in = sendrecv_mgr_worker_msg(comm, O)
if tag in [STOP_TAG, PERSIS_STOP]:
nlopt.forced_stop.message = 'tag=' + str(tag)
raise nlopt.forced_stop
# Return function value (and maybe gradient)
if gen_specs['localopt_method'] in ['LD_MMA']:
grad[:] = calc_in['grad']
return float(calc_in['f'])
def persistent_updater_after_likelihood(H, persis_info, gen_specs, libE_info):
"""
"""
ub = gen_specs['user']['ub']
lb = gen_specs['user']['lb']
n = len(lb)
comm = libE_info['comm']
subbatch_size = gen_specs['user']['subbatch_size']
num_subbatches = gen_specs['user']['num_subbatches']
# Receive information from the manager (or a STOP_TAG)
batch = -1
tag = None
w = np.nan
while tag not in [STOP_TAG, PERSIS_STOP]:
batch += 1
O = np.zeros(subbatch_size*num_subbatches, dtype=gen_specs['out'])
if np.all(~np.isnan(w)):
O['weight'] = w
for j in range(num_subbatches):
for i in range(subbatch_size):
row = subbatch_size*j + i
O['x'][row] = persis_info['rand_stream'].uniform(lb, ub, (1, n))
O['subbatch'][row] = j
O['batch'][row] = batch
O['prior'][row] = np.random.randn()
O['prop'][row] = np.random.randn()
# Send data and get next assignment
tag, Work, calc_in = sendrecv_mgr_worker_msg(comm, O)
if calc_in is not None:
def nlopt_obj_fun(x, grad):
# Check if we can do an early return
if np.array_equiv(x, H['x']):
if gen_specs['user']['localopt_method'] in ['LD_MMA']:
grad[:] = H['grad']
return np.float(H['f'])
# Send back x to the manager, then receive info or stop tag
O = add_to_O(np.zeros(1, dtype=gen_specs['out']), x, 0,
gen_specs['user']['ub'], gen_specs['user']['lb'], local=True, active=True)
tag, Work, calc_in = sendrecv_mgr_worker_msg(comm, O)
if tag in [STOP_TAG, PERSIS_STOP]:
nlopt.forced_stop.message = 'tag=' + str(tag)
raise nlopt.forced_stop
# Return function value (and maybe gradient)
if gen_specs['user']['localopt_method'] in ['LD_MMA']:
grad[:] = calc_in['grad']
return float(calc_in['f'])
persis_info['aResult'] = {}
for prec in precisions:
# Generate Tasmanian grid
grid = Tasmanian.makeGlobalGrid(iNumInputs, iNumOutputs, prec,
"iptotal", "clenshaw-curtis")
aPoints = grid.getNeededPoints()
# Return the points of that need to be evaluated to the manager
H0 = np.zeros(len(aPoints), dtype=gen_specs['out'])
H0['x'] = aPoints
# Receive values from manager
tag, Work, calc_in = sendrecv_mgr_worker_msg(libE_info['comm'], H0)
if tag in [STOP_TAG, PERSIS_STOP]:
break
aModelValues = calc_in['f']
# Update surrogate on grid
t = aModelValues.reshape((aModelValues.shape[0], iNumOutputs))
t = t.flatten()
t = np.atleast_2d(t).T
grid.loadNeededPoints(t)
# Evaluate grid
aResult = grid.evaluate(aPointOfInterest)
persis_info['aResult'][prec] = aResult
return H0, persis_info, FINISHED_PERSISTENT_GEN_TAG
def _check_received_calc(D_recv):
"Checks the type and status fields on a receive calculation"
calc_type = D_recv['calc_type']
calc_status = D_recv['calc_status']
assert calc_type in [EVAL_SIM_TAG, EVAL_GEN_TAG], \
"Aborting, Unknown calculation type received. " \
"Received type: {}".format(calc_type)
assert calc_status in [FINISHED_PERSISTENT_SIM_TAG,
FINISHED_PERSISTENT_GEN_TAG,
UNSET_TAG,
PERSIS_STOP,
MAN_SIGNAL_FINISH,
MAN_SIGNAL_KILL,
WORKER_KILL_ON_ERR,
WORKER_KILL_ON_TIMEOUT,
WORKER_KILL,
TASK_FAILED,
WORKER_DONE], \
"Aborting: Unknown calculation status received. " \
"Received status: {}".format(calc_status)