예제 #1
0
파일: chain.py 프로젝트: yifei-xue/faasm
def faasm_main():
    print("Main chaining entry point")
    call_a = chain_this_with_input(chain_one, b'1234')
    call_b = chain_this_with_input(chain_two, b'5678')

    print("Awaiting calls {} and {}".format(call_a, call_b))

    res_a = await_call(call_a)
    res_b = await_call(call_b)

    if res_a != 0 or res_b != 0:
        print("Chained functions failed: {} {}".format(res_a, res_b))
        exit(1)
예제 #2
0
def faasm_main():
    # Write initial dictionary to state
    dict_a = {"alpha": "beta", "gamma": "delta"}
    write_dict_to_state(KEY_A, dict_a)

    # Make the chained call
    call_id = chain_this_with_input(check_pickle, b'')
    await_call(call_id)

    # Load from state again
    dict_b = get_dict_from_state(KEY_B)

    # Check expectation
    expected = {"alpha": "beta", "gamma": "delta", "epsilon": "zeta"}
    if dict_b != expected:
        print("Expected {} but got {}".format(expected, dict_b))
        exit(1)

    print("Dictionary as expected: {}".format(dict_b))
예제 #3
0
def chain_multiplications(conf, split_level, row_a, col_a, row_b, col_b):
    """
    Spawns 8 workers to do the relevant multiplication in parallel.
    - split level is how many times we've split the original matrix
    - row_a, col_a is the chunk of matrix A
    - row_b, col_b is the chunk of matrix B

    The row/ col values will specify which chunk of the current split level, not
    actual indices in the final input matrices. Those must only be calculated
    when the final multiplication is done.
    """
    call_ids = []

    # Next split down we'll double the number of submatrices
    next_split_level = split_level + 1
    next_row_a = 2 * row_a
    next_row_b = 2 * row_b
    next_col_a = 2 * col_a
    next_col_b = 2 * col_b

    # Splitting submatrix A into four, A11, A12, A21, A22 and same with B
    a11 = [next_row_a, next_col_a]
    a12 = [next_row_a, next_col_a + 1]
    a21 = [next_row_a + 1, next_col_a]
    a22 = [next_row_a + 1, next_col_a + 1]

    b11 = [next_row_b, next_col_b]
    b12 = [next_row_b, next_col_b + 1]
    b21 = [next_row_b + 1, next_col_b]
    b22 = [next_row_b + 1, next_col_b + 1]

    # Define the relevant multiplications and additions for these submatrices
    additions = [
        [(a11, b11), (a12, b21)],
        [(a11, b12), (a12, b22)],
        [(a21, b11), (a22, b21)],
        [(a21, b12), (a22, b22)],
    ]

    # Build a list of all the required multiplications
    multiplications = list()
    for mult_one, mult_two in additions:
        multiplications.append(mult_one)
        multiplications.append(mult_two)

    # Kick off the multiplications in parallel
    for submatrix_a, submatrix_b in multiplications:
        inputs_a = np.array(
            [
                next_split_level,
                submatrix_a[0],
                submatrix_a[1],
                submatrix_b[0],
                submatrix_b[1],
            ],
            dtype=int32,
        )

        call_ids.append(
            chain(distributed_divide_and_conquer, inputs_a.tobytes())
        )

    # Await completion
    for call_id in call_ids:
        await_call(call_id)

    # Go through and get the results
    r_1 = get_addition_result(conf, next_split_level, additions[0])
    r_2 = get_addition_result(conf, next_split_level, additions[1])
    r_3 = get_addition_result(conf, next_split_level, additions[2])
    r_4 = get_addition_result(conf, next_split_level, additions[3])

    # Reconstitute the result
    result = np.concatenate(
        (
            np.concatenate((r_1, r_2), axis=1),
            np.concatenate((r_3, r_4), axis=1),
        ),
        axis=0,
    )

    return result