def test_run(auto_create_group, samples_per_group, num_cores,
             num_flows_per_core, error, error_code, record, reinject_packets):
    """Make sure that the run command carries out an experiment as would be
    expected."""
    system_info = SystemInfo(3, 1, {
        (x, y): ChipInfo(num_cores=18,
                         core_states=[AppState.run] + [AppState.idle] * 17,
                         working_links=set(Links),
                         largest_free_sdram_block=110*1024*1024,
                         largest_free_sram_block=1024*1024)
        for x in range(3)
        for y in range(1)
    })

    mock_mc = Mock()
    mock_mc.get_system_info.return_value = system_info

    mock_application_ctx = Mock()
    mock_application_ctx.__enter__ = Mock()
    mock_application_ctx.__exit__ = Mock()
    mock_mc.application.return_value = mock_application_ctx

    if reinject_packets:
        # If reinjecting, a core is added to every chip
        mock_mc.wait_for_cores_to_reach_state.return_value = len(system_info)
    else:
        # If not reinjecting, only the user-defined cores exist
        mock_mc.wait_for_cores_to_reach_state.return_value = num_cores

    def mock_sdram_file_read(size):
        return error_code + b"\0"*(size - 4)
    mock_sdram_file = Mock()
    mock_sdram_file.read.side_effect = mock_sdram_file_read

    mock_mc.sdram_alloc_as_filelike.return_value = mock_sdram_file

    e = Experiment(mock_mc)
    e.timestep = 1e-6
    e.warmup = 0.01
    e.duration = 0.01
    e.cooldown = 0.01
    e.flush_time = 0.01

    # Record the result of _construct_core_commands to allow checking of
    # memory allocation sizes
    construct_core_commands = e._construct_core_commands
    cores_commands = {}

    def wrapped_construct_core_commands(core, *args, **kwargs):
        commands = construct_core_commands(core=core, *args, **kwargs)
        cores_commands[core] = commands
        return commands
    e._construct_core_commands = Mock(
        side_effect=wrapped_construct_core_commands)

    if record:
        e.record_sent = True

    e.reinject_packets = reinject_packets

    # Create example cores. Cores are placed on sequential chips along the
    # x-axis.
    cores = [e.new_core(x, 0) for x in range(num_cores)]
    for c in cores:
        for _ in range(num_flows_per_core):
            e.new_flow(c, c)

    # Create example groups
    for num_samples in samples_per_group:
        with e.new_group():
            e.record_interval = e.duration / float(num_samples)

    # The run should fail with an exception when expected.
    if error and (num_cores > 0 or reinject_packets):
        with pytest.raises(NetworkTesterError) as exc_info:
            e.run(0x33, create_group_if_none_exist=auto_create_group)
        results = exc_info.value.results
    else:
        results = e.run(0x33, create_group_if_none_exist=auto_create_group)

    # The results should be of the correct type...
    assert isinstance(results, Results)

    # The results returned should be all zeros (since that is what was written
    # back)
    if record and num_cores > 0 and num_flows_per_core > 0:
        assert sum(results.totals()["sent"]) == 0

    # The supplied app ID should be used
    mock_mc.application.assert_called_once_with(0x33)

    # If reinjection is enabled, the binary should have been loaded
    print([call[1][0] for call in mock_mc.load_application.mock_calls])
    reinjector_loaded = any((len(call[1][0]) == 1 and
                             "reinjector.aplx" in list(call[1][0])[0])
                            for call in mock_mc.load_application.mock_calls)
    if reinject_packets:
        assert reinjector_loaded
    else:
        assert not reinjector_loaded

    # Each chip should have been issued with a suitable malloc for any cores
    # on it.
    for x, core in enumerate(cores):
        cmds_size = cores_commands[core].size
        if record:
            # The space required to record deadlines_missed and the sent
            # counters.
            result_size = (1 + ((1 + num_flows_per_core) *
                                sum(samples_per_group))) * 4
        else:
            # Just the status value and deadlines_missed
            result_size = (1 + sum(samples_per_group)) * 4
        size = max(cmds_size, result_size)
        core_num = e._allocations[core][Cores].start
        mock_mc.sdram_alloc_as_filelike.assert_any_call(size, x=x, y=0,
                                                        tag=core_num)

    # The correct number of barriers should have been reached
    num_groups = len(samples_per_group)
    if auto_create_group:
        num_groups = max(1, num_groups)
    assert len(mock_mc.send_signal.mock_calls) == num_groups
def test_run_callbacks():
    """Make sure that the run command's callbacks occur at the right time."""
    num_groups = 3

    system_info = SystemInfo(3, 1, {
        (x, y): ChipInfo(num_cores=18,
                         core_states=[AppState.run] + [AppState.idle] * 17,
                         working_links=set(Links),
                         largest_free_sdram_block=110*1024*1024,
                         largest_free_sram_block=1024*1024)
        for x in range(3)
        for y in range(1)
    })

    mock_mc = Mock()
    mock_mc.get_system_info.return_value = system_info

    mock_application_ctx = Mock()
    mock_application_ctx.__enter__ = Mock()
    mock_application_ctx.__exit__ = Mock()
    mock_mc.application.return_value = mock_application_ctx

    mock_mc.wait_for_cores_to_reach_state.return_value = len(system_info)

    def mock_sdram_file_read(size):
        return b"\0\0\0\0" + b"\0"*(size - 4)
    mock_sdram_file = Mock()
    mock_sdram_file.read.side_effect = mock_sdram_file_read

    mock_mc.sdram_alloc_as_filelike.return_value = mock_sdram_file

    e = Experiment(mock_mc)
    e.timestep = 1e-6
    e.warmup = 0.01
    e.duration = 0.01
    e.cooldown = 0.01
    e.flush_time = 0.01

    # Enough to cause a recording core to be added to every chip
    e.record_dropped_multicast = True

    # Create a number of groups
    groups = []
    for group_num in range(num_groups):
        groups.append(e.new_group())

    before_load_calls = []
    before_group_calls = []
    before_read_results_calls = []

    def before_load(experiment):
        before_load_calls.append(experiment)

        assert experiment is e

        # Loading should not have started
        assert not mock_mc.sdram_alloc_as_filelike.called

    def before_group(experiment, group):
        before_group_calls.append((experiment, group))

        assert experiment is e

        # Loading should have ocurred
        assert mock_mc.sdram_alloc_as_filelike.called

        # The group should appear in the correct sequence
        assert group is groups.pop(0)

        # The number of barriers reached should be progressing
        assert len(mock_mc.wait_for_cores_to_reach_state.mock_calls) == \
            (num_groups - len(groups))

    def before_read_results(experiment):
        before_read_results_calls.append(experiment)

        assert experiment is e

        # All groups should have been run
        assert len(groups) == 0

        # Reading should not have started
        assert not mock_sdram_file.read.called

    # The run should fail with an exception when expected.
    results = e.run(0x33,
                    before_load=before_load,
                    before_group=before_group,
                    before_read_results=before_read_results)

    # The results should come out as usual...
    assert isinstance(results, Results)

    # All callbacks should have been called
    assert len(before_load_calls) == 1
    assert len(before_group_calls) == num_groups
    assert len(before_read_results_calls) == 1
Exemple #3
0
def test_run(auto_create_group, samples_per_group, num_cores,
             num_flows_per_core, error, error_code, record, reinject_packets):
    """Make sure that the run command carries out an experiment as would be
    expected."""
    system_info = SystemInfo(
        3, 1,
        {(x, y): ChipInfo(num_cores=18,
                          core_states=[AppState.run] + [AppState.idle] * 17,
                          working_links=set(Links),
                          largest_free_sdram_block=110 * 1024 * 1024,
                          largest_free_sram_block=1024 * 1024)
         for x in range(3) for y in range(1)})

    mock_mc = Mock()
    mock_mc.get_system_info.return_value = system_info

    mock_application_ctx = Mock()
    mock_application_ctx.__enter__ = Mock()
    mock_application_ctx.__exit__ = Mock()
    mock_mc.application.return_value = mock_application_ctx

    if reinject_packets:
        # If reinjecting, a core is added to every chip
        mock_mc.wait_for_cores_to_reach_state.return_value = len(system_info)
    else:
        # If not reinjecting, only the user-defined cores exist
        mock_mc.wait_for_cores_to_reach_state.return_value = num_cores

    def mock_sdram_file_read(size):
        return error_code + b"\0" * (size - 4)

    mock_sdram_file = Mock()
    mock_sdram_file.read.side_effect = mock_sdram_file_read

    mock_mc.sdram_alloc_as_filelike.return_value = mock_sdram_file

    e = Experiment(mock_mc)
    e.timestep = 1e-6
    e.warmup = 0.01
    e.duration = 0.01
    e.cooldown = 0.01
    e.flush_time = 0.01

    # Record the result of _construct_core_commands to allow checking of
    # memory allocation sizes
    construct_core_commands = e._construct_core_commands
    cores_commands = {}

    def wrapped_construct_core_commands(core, *args, **kwargs):
        commands = construct_core_commands(core=core, *args, **kwargs)
        cores_commands[core] = commands
        return commands

    e._construct_core_commands = Mock(
        side_effect=wrapped_construct_core_commands)

    if record:
        e.record_sent = True

    e.reinject_packets = reinject_packets

    # Create example cores. Cores are placed on sequential chips along the
    # x-axis.
    cores = [e.new_core(x, 0) for x in range(num_cores)]
    for c in cores:
        for _ in range(num_flows_per_core):
            e.new_flow(c, c)

    # Create example groups
    for num_samples in samples_per_group:
        with e.new_group():
            e.record_interval = e.duration / float(num_samples)

    # The run should fail with an exception when expected.
    if error and (num_cores > 0 or reinject_packets):
        with pytest.raises(NetworkTesterError) as exc_info:
            e.run(0x33, create_group_if_none_exist=auto_create_group)
        results = exc_info.value.results
    else:
        results = e.run(0x33, create_group_if_none_exist=auto_create_group)

    # The results should be of the correct type...
    assert isinstance(results, Results)

    # The results returned should be all zeros (since that is what was written
    # back)
    if record and num_cores > 0 and num_flows_per_core > 0:
        assert sum(results.totals()["sent"]) == 0

    # The supplied app ID should be used
    mock_mc.application.assert_called_once_with(0x33)

    # If reinjection is enabled, the binary should have been loaded
    print([call[1][0] for call in mock_mc.load_application.mock_calls])
    reinjector_loaded = any(
        (len(call[1][0]) == 1 and "reinjector.aplx" in list(call[1][0])[0])
        for call in mock_mc.load_application.mock_calls)
    if reinject_packets:
        assert reinjector_loaded
    else:
        assert not reinjector_loaded

    # Each chip should have been issued with a suitable malloc for any cores
    # on it.
    for x, core in enumerate(cores):
        cmds_size = cores_commands[core].size
        if record:
            # The space required to record deadlines_missed and the sent
            # counters.
            result_size = (1 + (
                (1 + num_flows_per_core) * sum(samples_per_group))) * 4
        else:
            # Just the status value and deadlines_missed
            result_size = (1 + sum(samples_per_group)) * 4
        size = max(cmds_size, result_size)
        core_num = e._allocations[core][Cores].start
        mock_mc.sdram_alloc_as_filelike.assert_any_call(size,
                                                        x=x,
                                                        y=0,
                                                        tag=core_num)

    # The correct number of barriers should have been reached
    num_groups = len(samples_per_group)
    if auto_create_group:
        num_groups = max(1, num_groups)
    assert len(mock_mc.send_signal.mock_calls) == num_groups
Exemple #4
0
def test_run_callbacks():
    """Make sure that the run command's callbacks occur at the right time."""
    num_groups = 3

    system_info = SystemInfo(
        3, 1,
        {(x, y): ChipInfo(num_cores=18,
                          core_states=[AppState.run] + [AppState.idle] * 17,
                          working_links=set(Links),
                          largest_free_sdram_block=110 * 1024 * 1024,
                          largest_free_sram_block=1024 * 1024)
         for x in range(3) for y in range(1)})

    mock_mc = Mock()
    mock_mc.get_system_info.return_value = system_info

    mock_application_ctx = Mock()
    mock_application_ctx.__enter__ = Mock()
    mock_application_ctx.__exit__ = Mock()
    mock_mc.application.return_value = mock_application_ctx

    mock_mc.wait_for_cores_to_reach_state.return_value = len(system_info)

    def mock_sdram_file_read(size):
        return b"\0\0\0\0" + b"\0" * (size - 4)

    mock_sdram_file = Mock()
    mock_sdram_file.read.side_effect = mock_sdram_file_read

    mock_mc.sdram_alloc_as_filelike.return_value = mock_sdram_file

    e = Experiment(mock_mc)
    e.timestep = 1e-6
    e.warmup = 0.01
    e.duration = 0.01
    e.cooldown = 0.01
    e.flush_time = 0.01

    # Enough to cause a recording core to be added to every chip
    e.record_dropped_multicast = True

    # Create a number of groups
    groups = []
    for group_num in range(num_groups):
        groups.append(e.new_group())

    before_load_calls = []
    before_group_calls = []
    before_read_results_calls = []

    def before_load(experiment):
        before_load_calls.append(experiment)

        assert experiment is e

        # Loading should not have started
        assert not mock_mc.sdram_alloc_as_filelike.called

    def before_group(experiment, group):
        before_group_calls.append((experiment, group))

        assert experiment is e

        # Loading should have ocurred
        assert mock_mc.sdram_alloc_as_filelike.called

        # The group should appear in the correct sequence
        assert group is groups.pop(0)

        # The number of barriers reached should be progressing
        assert len(mock_mc.wait_for_cores_to_reach_state.mock_calls) == \
            (num_groups - len(groups))

    def before_read_results(experiment):
        before_read_results_calls.append(experiment)

        assert experiment is e

        # All groups should have been run
        assert len(groups) == 0

        # Reading should not have started
        assert not mock_sdram_file.read.called

    # The run should fail with an exception when expected.
    results = e.run(0x33,
                    before_load=before_load,
                    before_group=before_group,
                    before_read_results=before_read_results)

    # The results should come out as usual...
    assert isinstance(results, Results)

    # All callbacks should have been called
    assert len(before_load_calls) == 1
    assert len(before_group_calls) == num_groups
    assert len(before_read_results_calls) == 1
def test_run(auto_create_group, samples_per_group, num_vertices,
             num_nets_per_vertex, error, error_code, record, reinject_packets):
    """Make sure that the run command carries out an experiment as would be
    expected."""
    machine = Machine(3, 1)

    mock_mc = Mock()
    mock_mc.get_machine.return_value = machine

    mock_application_ctx = Mock()
    mock_application_ctx.__enter__ = Mock()
    mock_application_ctx.__exit__ = Mock()
    mock_mc.application.return_value = mock_application_ctx

    mock_mc.get_machine.return_value = machine

    if reinject_packets:
        # If reinjecting, a core is added to every chip
        mock_mc.wait_for_cores_to_reach_state.return_value = len(list(machine))
    else:
        # If not reinjecting, only the vertices are given cores
        mock_mc.wait_for_cores_to_reach_state.return_value = num_vertices

    def mock_sdram_file_read(size):
        return error_code + b"\0"*(size - 4)
    mock_sdram_file = Mock()
    mock_sdram_file.read.side_effect = mock_sdram_file_read

    mock_mc.sdram_alloc_as_filelike.return_value = mock_sdram_file

    e = Experiment(mock_mc)
    e.timestep = 1e-6
    e.warmup = 0.01
    e.duration = 0.01
    e.cooldown = 0.01
    e.flush_time = 0.01

    # Record the result of _construct_vertex_commands to allow checking of
    # memory allocation sizes
    construct_vertex_commands = e._construct_vertex_commands
    vertices_commands = {}

    def wrapped_construct_vertex_commands(vertex, *args, **kwargs):
        commands = construct_vertex_commands(vertex=vertex, *args, **kwargs)
        vertices_commands[vertex] = commands
        return commands
    e._construct_vertex_commands = Mock(
        side_effect=wrapped_construct_vertex_commands)

    if record:
        e.record_sent = True

    e.reinject_packets = reinject_packets

    # Create example vertices
    vertices = [e.new_vertex() for _ in range(num_vertices)]
    for v in vertices:
        for _ in range(num_nets_per_vertex):
            e.new_net(v, v)

    # Vertices are placed on sequential chips along the x-axis
    e.placements = {v: (x, 0) for x, v in enumerate(vertices)}

    # Create example groups
    for num_samples in samples_per_group:
        with e.new_group():
            e.record_interval = e.duration / float(num_samples)

    # The run should fail with an exception when expected.
    if error and (num_vertices > 0 or reinject_packets):
        with pytest.raises(NetworkTesterError) as exc_info:
            e.run(0x33, create_group_if_none_exist=auto_create_group)
        results = exc_info.value.results
    else:
        results = e.run(0x33, create_group_if_none_exist=auto_create_group)

    # The results should be of the correct type...
    assert isinstance(results, Results)

    # The results returned should be all zeros (since that is what was written
    # back)
    if record and num_vertices > 0 and num_nets_per_vertex > 0:
        assert sum(results.totals()["sent"]) == 0

    # The supplied app ID should be used
    mock_mc.application.assert_called_once_with(0x33)

    # If reinjection is enabled, the binary should have been loaded
    reinjector_loaded = any("reinjector.aplx" in call[1][0]
                            for call in mock_mc.load_application.mock_calls)
    if reinject_packets:
        assert reinjector_loaded
    else:
        assert not reinjector_loaded

    # Each chip should have been issued with a suitable malloc for any vertices
    # on it.
    for x, vertex in enumerate(vertices):
        cmds_size = vertices_commands[vertex].size
        if record:
            # The space required to record the sent counters.
            result_size = (1 + (num_nets_per_vertex *
                                sum(samples_per_group))) * 4
        else:
            result_size = 4  # Just the status value
        size = max(cmds_size, result_size)
        if reinject_packets:
            # During packet reinjection, core 1 is used.
            core_num = 2
        else:
            core_num = 1
        mock_mc.sdram_alloc_as_filelike.assert_any_call(size, x=x, y=0,
                                                        tag=core_num)

    # The correct number of barriers should have been reached
    num_groups = len(samples_per_group)
    if auto_create_group:
        num_groups = max(1, num_groups)
    assert len(mock_mc.send_signal.mock_calls) == num_groups