def distributed_divide_and_conquer(input_bytes): conf = load_matrix_conf_from_state() input_args = np.frombuffer(input_bytes, dtype=int32) split_level = input_args[0] row_a = input_args[1] col_a = input_args[2] row_b = input_args[3] col_b = input_args[4] # If we're at the target number of splits, do the work if split_level == conf.n_splits: # Read in the relevant submatrices of each input matrix mat_a = read_input_submatrix(conf, SUBMATRICES_KEY_A, row_a, col_a) mat_b = read_input_submatrix(conf, SUBMATRICES_KEY_B, row_b, col_b) # Do the multiplication in memory result = np.dot(mat_a, mat_b) else: # Recursively kick off more divide and conquer result = chain_multiplications(conf, split_level, row_a, col_a, row_b, col_b) # Write the result result_key = conf.get_intermediate_result_key(split_level, row_a, col_a, row_b, col_b) set_state(result_key, result.tobytes())
def test_state_read_write(self): key = "pyStateTest" full_value = b'0123456789' set_state(key, full_value) push_state(key) expected_value_len = 10 value_len = get_state_size(key) self.assertEqual(expected_value_len, value_len) # Read state back in pull_state(key, value_len) actual = get_state(key, value_len) # Check values as expected self.assertEqual(full_value, actual) # Update a segment segment = b'999' offset = 2 segment_len = 3 modified_value = b'0199956789' set_state_offset(key, value_len, offset, segment) # Push and pull push_state(key) pull_state(key, value_len) # Check full value as expected actual = get_state(key, value_len) self.assertEqual(modified_value, actual) # Check getting a segment actual_segment = get_state_offset(key, value_len, offset, segment_len) self.assertEqual(segment, actual_segment)
def faasm_main(): # Set the full value initially set_state(key, full_value) push_state(key) # Check the full value has been written _check_full_value(full_value) # Update a segment set_state_offset(key, value_len, offset, segment) push_state_partial(key) # Check the full value again _check_full_value(modified_value) # Check just the segment actual_segment = get_state_offset(key, value_len, offset, segment_len) if actual_segment != segment: msg = "Mismatched segment: actual {}, expected {}".format( actual_segment, segment) print(msg) exit(1) print("Successful Python state writing check") return 0
def divide_and_conquer(): conf = load_matrix_conf_from_state() print("Running divide and conquer for {}x{} matrix with {} splits".format( conf.matrix_size, conf.matrix_size, conf.n_splits )) # Short-cut for no splits if conf.n_splits == 0: # Read in the relevant submatrices of each input matrix mat_a = read_input_submatrix(conf, SUBMATRICES_KEY_A, 0, 0) mat_b = read_input_submatrix(conf, SUBMATRICES_KEY_B, 0, 0) # Do the multiplication in memory result = np.dot(mat_a, mat_b) else: # Kick off the basic multiplication jobs result = chain_multiplications(conf, 0, 0, 0, 0, 0) # Write final result set_state(RESULT_MATRIX_KEY, result.tobytes())
from pyfaasm.core import check_python_bindings from pyfaasm.core import get_state, get_state_offset, set_state, set_state_offset, push_state, pull_state # Initial check check_python_bindings() # Write and push state key = "pyStateTest" valueLen = 10 fullValue = b'0123456789' set_state(key, fullValue) push_state(key) # Read state back in pull_state(key, valueLen) actual = get_state(key, valueLen) print("In = {} out = {}".format(fullValue, actual)) # Update a segment segment = b'999' offset = 2 segmentLen = 3 modifiedValue = b'0199956789' set_state_offset(key, valueLen, offset, segment) push_state(key) pull_state(key, valueLen) actual = get_state(key, valueLen) actualSegment = get_state_offset(key, valueLen, offset, segmentLen) print("Expected = {} actual = {}".format(modifiedValue, actual)) print("Expected = {} actual = {}".format(segment, actualSegment))
def write_matrix_params_to_state(matrix_size, n_splits): params = np.array((matrix_size, n_splits), dtype=int32) set_state(MATRIX_CONF_STATE_KEY, params.tobytes())
def _write_submatrix_to_state(sm_bytes, row_idx, col_idx): full_key = conf.get_submatrix_key(key_prefix, conf.n_splits, row_idx, col_idx) set_state(full_key, sm_bytes)
def write_dict_to_state(key, dict_in): pickled_dict = pickle.dumps(dict_in) set_state(key, pickled_dict)