How to use libensemble - 10 common examples

To help you get started, we’ve selected a few libensemble examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Libensemble / libensemble / code / examples / calling_scripts / call_6-hump_camel_test_elapsed_time.py View on Github external
],
             'lb': np.array([-3,-2]),
             'ub': np.array([ 3, 2]),
             'gen_batch_size': 5,
             'num_inst': 1,
             'batch_mode': False,
             # 'save_every_k': 10
             }

# Tell libEnsemble when to stop
exit_criteria = {'elapsed_wallclock_time': 0.1}

np.random.seed(1)

# Perform the run
H, flag = libE(sim_specs, gen_specs, exit_criteria)

filename = '6-hump_camel_results_History_length=' + str(len(H)) + '_evals=' + str(sum(H['returned'])) + '_ranks=' + str(MPI.COMM_WORLD.Get_size())
print("\n\n\nRun completed.\nSaving results to file: " + filename)
if flag == 2:
    print("\n\n\nKilling COMM_WORLD")
    MPI.COMM_WORLD.Abort()
github Libensemble / libensemble / libensemble / sim_funcs / comms_testing.py View on Github external
def float_x1000(H, persis_info, sim_specs, _):
    """
    Multiplies worker ID by 1000 and sends back values
    Input (X) is ignored in this case
    """

    # All values
    output = np.zeros(1, dtype=sim_specs['out'])

    # First test fill - even though - it will do arr_vals and scal_val
    jobctl = JobController.controller
    x = jobctl.workerID * 1000.0

    output.fill(x)  # All set
    output['scal_val'] = x + x/1e7  # Make scalar value distinct

    return output, persis_info
github Libensemble / libensemble / libensemble / sim_funcs / six_hump_camel.py View on Github external
# Run with Executor --------------------------------------------------
        task = exctr.submit(calc_type='sim', machinefile=machinefilename,
                            stdout=outfile, stderr=errfile, hyperthreads=True)
        while(not task.finished):
            time.sleep(0.2)
            task.poll()
        task_states.append(task.state)

        H_o['f'][i] = six_hump_camel_func(x)

        # v = np.random.uniform(0, 10)
        # print('About to sleep for :' + str(v))
        # time.sleep(v)

    calc_status = UNSET_TAG  # Returns to worker
    if all(t == 'FINISHED' for t in task_states):
        calc_status = WORKER_DONE
    elif any(t == 'FAILED' for t in task_states):
        calc_status = TASK_FAILED

    return H_o, persis_info, calc_status
github Libensemble / libensemble / libensemble / libE_manager.py View on Github external
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
github Libensemble / libensemble / libensemble / resources / env_resources.py View on Github external
if len(part_splitstr) == 1:     # Single Partition
            splitstr = fullstr.split('[', 1)
            if len(splitstr) == 1:      # Single Node
                return splitstr
            prefix = splitstr[0]
            nidstr = splitstr[1].strip("]")
            nidlst = EnvResources._noderange_append(prefix, nidstr)
        else:                           # Multiple Partitions
            splitgroups = [str.split('[', 1) for str in part_splitstr]
            prefixgroups = [group[0] for group in splitgroups]
            nodegroups = [group[1].strip(']') for group in splitgroups]
            nidlst = []
            for i in range(len(prefixgroups)):
                prefix = prefixgroups[i]
                nidstr = nodegroups[i]
                nidlst.extend(EnvResources._noderange_append(prefix, nidstr))

        return sorted(nidlst)
github Libensemble / libensemble / libensemble / worker_class.py View on Github external
if not empty:
            if 'sim_dir' in Worker.sim_specs:
                #worker_dir = Worker.sim_specs['sim_dir'] + '_' + str(comm_color) + "_" + str(rank) 
                self.worker_dir = Worker.sim_specs['sim_dir'] + '_' + str(self.workerID)
    
                if 'sim_dir_prefix' in Worker.sim_specs:
                    self.worker_dir =  os.path.join(os.path.expanduser(Worker.sim_specs['sim_dir_prefix']), os.path.split(os.path.abspath(os.path.expanduser(self.worker_dir)))[1])
    
                assert ~os.path.isdir(self.worker_dir), "Worker directory already exists."
                # if not os.path.exists(worker_dir):
                shutil.copytree(Worker.sim_specs['sim_dir'], self.worker_dir)
                self.locations[EVAL_SIM_TAG] = self.worker_dir
                
                #Optional - set workerID in job_controller - so will be added to jobnames
                try:
                    jobctl = JobController.controller
                    jobctl.set_workerID(workerID)
                except Exception as e:
                    #logger
                    print("Info: No job_controller set on worker", workerID)
                    self.job_controller_set = False
                else:
                    self.job_controller_set = True
                    #jobctl.set_workerID(workerID)
github Libensemble / libensemble / libensemble / tools / gen_support.py View on Github external
def send_mgr_worker_msg(comm, O):
    """Send message from worker to manager.
    """
    D = {'calc_out': O,
         'libE_info': {'persistent': True},
         'calc_status': UNSET_TAG,
         'calc_type': EVAL_GEN_TAG
         }
    comm.send(EVAL_GEN_TAG, D)
github Libensemble / libensemble / libensemble / alloc_funcs / start_only_persistent.py View on Github external
- If no points are left, call the gen_f 
    """

    Work = {}
    gen_count = sum(W['persis_state'] == EVAL_GEN_TAG)
    already_in_Work = np.zeros(len(H),dtype=bool) # To mark points as they are included in Work, but not yet marked as 'given' in H.

    # If i is idle, but in persistent mode, and its calculated values have
    # returned, give them back to i. Otherwise, give nothing to i
    for i in W['worker_id'][np.logical_and(W['active']==0,W['persis_state']!=0)]:
        gen_inds = H['gen_worker']==i 
        if np.all(H['returned'][gen_inds]):
            last_ind = np.nonzero(gen_inds)[0][np.argmax(H['given_time'][gen_inds])]
            Work[i] = {'persis_info': persis_info[i],
                       'H_fields': sim_specs['in'] + [name[0] for name in sim_specs['out']],
                       'tag':EVAL_GEN_TAG, 
                       'libE_info': {'H_rows': np.atleast_1d(last_ind),
                                     'persistent': True
                                }
                       }


    for i in W['worker_id'][np.logical_and(W['active']==0,W['persis_state']==0)]:
        # perform sim evaluations from existing runs (if they exist).
        q_inds_logical = np.logical_and.reduce((~H['given'],~H['paused'],~already_in_Work))

        if np.any(q_inds_logical):
            sim_ids_to_send = np.nonzero(q_inds_logical)[0][0] # oldest point

            Work[i] = {'H_fields': sim_specs['in'],
                       'persis_info': {}, # Our sims don't need information about how points were generatored
                       'tag':EVAL_SIM_TAG, 
github Libensemble / libensemble / libensemble / alloc_funcs / start_persistent_local_opt_gens.py View on Github external
persis_info[i]['run_order'] = [ind]
            gen_count += 1

        elif np.any(task_avail):

            # Perform sim evaluations from existing runs
            q_inds_logical = np.logical_and(task_avail, H['local_pt'])
            if not np.any(q_inds_logical):
                q_inds_logical = task_avail
            sim_ids_to_send = np.nonzero(q_inds_logical)[0][0]  # oldest point
            sim_work(Work, i, sim_specs['in'], np.atleast_1d(sim_ids_to_send), [])
            task_avail[sim_ids_to_send] = False

        elif (gen_count == 0
              and not np.any(np.logical_and(W['active'] == EVAL_GEN_TAG,
                                            W['persis_state'] == 0))):

            # Finally, generate points since there is nothing else to do
            gen_count += 1
            gen_work(Work, i, gen_specs['in'], [], persis_info[i])

    return Work, persis_info
github Libensemble / libensemble / libensemble / tools / alloc_support.py View on Github external
def gen_work(Work, i, H_fields, H_rows, persis_info, **libE_info):
    "Add gen work record to work array."
    libE_info['H_rows'] = H_rows
    Work[i] = {'H_fields': H_fields,
               'persis_info': persis_info,
               'tag': EVAL_GEN_TAG,
               'libE_info': libE_info}