def test_worker_init_run():

    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()

    L = exit_criteria['sim_max']
    #H = np.zeros(L + len(H0), dtype=list(set(libE_fields + sim_specs['out'] + gen_specs['out'] + alloc_specs['out'])))
    H = np.zeros(L, dtype=list(set(libE_fields + sim_specs['out'] + gen_specs['out'])))

    #check
    H['sim_id'][-L:] = -1
    H['given_time'][-L:] = np.inf
    H_ind = 0

    #Create work
    array_size=1 #One value at a time
    sim_ids=np.zeros(1,dtype=int)

    #For loop - increment sim_ids here

    Work = {'tag': EVAL_SIM_TAG, 'persis_info': {}, 'libE_info': {'H_rows': sim_ids}, 'H_fields': sim_specs['in']}
    calc_in = H[Work['H_fields']][Work['libE_info']['H_rows']]

    Worker.init_workers(sim_specs, gen_specs)

    workerID = 1

    #Testing two worker routines: init and run
    worker = Worker(workerID)
    worker.run(Work, calc_in)

    print(worker.data)
Example #2
0
def test_checking_inputs():

    # Don't take more points than there is space in history.
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()

    H0 = np.zeros(3,
                  dtype=sim_specs['out'] + gen_specs['out'] +
                  [('returned', bool)])
    # Should fail because H0 has points with 'return'==False
    try:
        check_inputs(libE_specs, al, sim_specs, gen_specs, exit_criteria, H0)
    except AssertionError:
        assert 1
    else:
        assert 0

    # Should not fail
    H0['returned'] = True
    check_inputs(libE_specs, al, sim_specs, gen_specs, exit_criteria, H0)

    # Removing 'returned' and then testing again.
    H0 = rmfield(H0, 'returned')
    check_inputs(libE_specs, al, sim_specs, gen_specs, exit_criteria, H0)

    # Should fail because H0 has fields not in H
    H0 = np.zeros(3,
                  dtype=sim_specs['out'] + gen_specs['out'] +
                  [('bad_name', bool), ('bad_name2', bool)])
    try:
        check_inputs(libE_specs, al, sim_specs, gen_specs, exit_criteria, H0)
    except AssertionError:
        assert 1
    else:
        assert 0
Example #3
0
def test_checking_inputs_noworkers():
    # Don't take more points than there is space in history.
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    H0 = np.empty(0)

    # Should fail because only got a manager
    libE_specs = {'comm': MPI.COMM_WORLD, 'comms': 'mpi'}
    errstr = check_assertion(libE_specs, alloc_specs, sim_specs, gen_specs,
                             exit_criteria, H0)
    assert 'must be at least one worker' in errstr, 'Incorrect assertion error: ' + errstr
Example #4
0
def test_exception_raising_manager_no_abort():
    """Running until fake_MPI tries to send msg to test (mocked) comm.Abort is called

    Manager should raise MPISendException when fakeMPI tries to send message, which
    will be caught by libE and raise MPIAbortException from fakeMPI.Abort"""
    libE_specs['abort_on_exception'] = False
    libE_specs['comm'] = fake_mpi
    with pytest.raises(MPISendException):
        sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
        libE(sim_specs, gen_specs, exit_criteria, libE_specs=libE_specs)
        pytest.fail('Expected MPISendException exception')
Example #5
0
def test_checking_inputs_single():
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    libE_specs = {'comm': fake_mpi, 'comms': 'mpi'}

    check_inputs(libE_specs=libE_specs)
    check_inputs(alloc_specs=alloc_specs)
    check_inputs(sim_specs=sim_specs)
    check_inputs(gen_specs=gen_specs)
    check_inputs(exit_criteria=exit_criteria,
                 sim_specs=sim_specs,
                 gen_specs=gen_specs)
Example #6
0
def test_checking_inputs_H0():
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    libE_specs = {'comm': fake_mpi, 'comms': 'mpi'}

    # Should fail because H0 has points with 'return'==False
    H0 = np.array([
        (False, 0., 0, 0., 1, True, 1, True, [0., 0., 0.], True, 0.1, 1.1),
        (False, 0., 0, 0., 1, True, 2, True, [0., 0., 0.], True, 0.2, 1.2),
        (False, 0., 0, 0., 1, True, 3, True, [0., 0., 0.], True, 0.3, 1.3),
        (False, 0., 0, 0., -1, False, 0, False, [0., 0., 0.], False, 0., inf),
        (False, 0., 0, 0., -1, False, 0, False, [0., 0., 0.], False, 0., inf)
    ],
                  dtype=[('local_pt', '?'), ('priority', '<f8'),
                         ('gen_worker', '<i8'), ('x_on_cube', '<f8'),
                         ('sim_id', '<i8'), ('given', '?'),
                         ('sim_worker', '<i8'), ('returned', '?'),
                         ('fvec', '<f8', (3, )), ('allocated', '?'),
                         ('f', '<f8'), ('given_time', '<f8')])

    # This should work
    check_inputs(libE_specs, alloc_specs, sim_specs, gen_specs, exit_criteria,
                 H0)

    # A value has not been returned
    H0['returned'][2] = False
    errstr = check_assertion(libE_specs, alloc_specs, sim_specs, gen_specs,
                             exit_criteria, H0)
    assert 'H0 contains unreturned or invalid points' in errstr, 'Incorrect assertion error: ' + errstr

    # Ungiven points shown as returned
    H0['returned'] = True
    errstr = check_assertion(libE_specs, alloc_specs, sim_specs, gen_specs,
                             exit_criteria, H0)
    assert 'H0 contains unreturned or invalid points' in errstr, 'Incorrect assertion error: ' + errstr

    # Return to correct state
    H0['returned'][3:len(H0)] = False
    check_inputs(libE_specs, alloc_specs, sim_specs, gen_specs, exit_criteria,
                 H0)

    # Removing 'returned' and then testing again. Should be successful as 'returned' does not exist
    H0 = rmfield(H0, 'returned')
    check_inputs(libE_specs, alloc_specs, sim_specs, gen_specs, exit_criteria,
                 H0)

    # Should fail because H0 has fields not in H
    H0 = np.zeros(3,
                  dtype=sim_specs['out'] + gen_specs['out'] +
                  alloc_specs['out'] + [('bad_name', bool),
                                        ('bad_name2', bool)])
    errstr = check_assertion(libE_specs, alloc_specs, sim_specs, gen_specs,
                             exit_criteria, H0)
    assert 'not in the History' in errstr, 'Incorrect assertion error: ' + errstr
Example #7
0
def test_checking_inputs_exit_crit():
    sim_specs, gen_specs, _ = setup.make_criteria_and_specs_0()
    libE_specs = {'comm': fake_mpi, 'comms': 'mpi'}
    H0 = np.empty(0)

    exit_criteria = {}
    errstr = check_assertion(libE_specs, alloc_specs, sim_specs, gen_specs,
                             exit_criteria, H0)
    assert 'Must have some exit criterion' in errstr, 'Incorrect assertion error: ' + errstr

    exit_criteria = {'swim_max': 10}
    errstr = check_assertion(libE_specs, alloc_specs, sim_specs, gen_specs,
                             exit_criteria, H0)
    assert 'Valid termination options' in errstr, 'Incorrect assertion error: ' + errstr
Example #8
0
def test_checking_inputs_single():
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    libE_specs = {'comm': fake_mpi, 'comms': 'mpi'}

    check_inputs(libE_specs=libE_specs)
    check_inputs(alloc_specs=alloc_specs)
    try:
        check_inputs(sim_specs=sim_specs)
    except AssertionError:
        assert 1, "Fails because sim_specs['in']=['x_on_cube'] and that's not an 'out' of anything"
    else:
        assert 0, "Should have failed"
    check_inputs(gen_specs=gen_specs)
    check_inputs(exit_criteria=exit_criteria,
                 sim_specs=sim_specs,
                 gen_specs=gen_specs)
Example #9
0
def test_manager_exception():
    """Checking dump of history and pickle file on abort"""
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    remove_file_if_exists(hfile_abort)
    remove_file_if_exists(pfile_abort)

    with mock.patch('libensemble.libE.manager_main') as managerMock:
        managerMock.side_effect = Exception
        with mock.patch('libensemble.libE.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            # Need fake MPI to get past the Manager only check and dump history
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs={'comm': fake_mpi})
                pytest.fail('Expected exception')
            assert os.path.isfile(hfile_abort), "History file not dumped"
            assert os.path.isfile(pfile_abort), "Pickle file not dumped"
            os.remove(hfile_abort)
            os.remove(pfile_abort)
Example #10
0
def test_logging_disabling():
    remove_file_if_exists('ensemble.log')
    remove_file_if_exists('libE_stats.txt')
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    libE_specs = {'comm': fake_mpi, 'comms': 'mpi', 'disable_log_files': True}
    logconfig = LogConfig.config
    logconfig.logger_set = False

    with mock.patch('libensemble.libE_manager.manager_main') as managerMock:
        managerMock.side_effect = Exception
        with mock.patch('libensemble.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs=libE_specs)
                pytest.fail('Expected exception')
            assert not os.path.isfile(
                'ensemble.log'), "ensemble.log file dumped"
            assert not os.path.isfile(
                'libE_stats.txt'), "libE_stats.txt file dumped"
Example #11
0
def test_manager_exception():
    """Checking dump of history and pickle file on abort"""
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    remove_file_if_exists(hfile_abort)
    remove_file_if_exists(pfile_abort)

    with mock.patch('libensemble.libE_manager.manager_main') as managerMock:
        managerMock.side_effect = Exception
        # Collision between libE.py and libE() (after mods to __init__.py) means
        #   libensemble.libE.comms_abort tries to refer to the function, not file
        with mock.patch('libensemble.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            # Need fake MPI to get past the Manager only check and dump history
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs={'comm': fake_mpi})
                pytest.fail('Expected exception')
            assert os.path.isfile(hfile_abort), "History file not dumped"
            assert os.path.isfile(pfile_abort), "Pickle file not dumped"
            os.remove(hfile_abort)
            os.remove(pfile_abort)

            # Test that History and Pickle files NOT created when disabled
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs={
                         'comm': fake_mpi,
                         'save_H_and_persis_on_abort': False
                     })
                pytest.fail('Expected exception')
            assert not os.path.isfile(hfile_abort), "History file dumped"
            assert not os.path.isfile(pfile_abort), "Pickle file dumped"