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 _check_full_value(expected): pull_state(key, value_len) actual = get_state(key, value_len) if actual != expected: msg = "Mismatch: actual {}, expected {})".format(actual, expected) print(msg) exit(1)
def read_input_submatrix(conf, key_prefix, row_idx, col_idx): sm_bytes = conf.get_bytes_per_submatrix(conf.n_splits) sm_size = conf.get_submatrix_size(conf.n_splits) full_key = conf.get_submatrix_key(key_prefix, conf.n_splits, row_idx, col_idx) sub_mat_data = get_state(full_key, sm_bytes) return np.frombuffer(sub_mat_data, dtype=np.float32).reshape(sm_size, sm_size)
def get_addition_result(conf, split_level, addition_def): sm_size = conf.get_submatrix_size(split_level) sm_byte_size = conf.get_bytes_per_submatrix(split_level) key_a = conf.get_intermediate_result_key(split_level, addition_def[0][0][0], addition_def[0][0][1], addition_def[0][1][0], addition_def[0][1][1], ) key_b = conf.get_intermediate_result_key(split_level, addition_def[1][0][0], addition_def[1][0][1], addition_def[1][1][0], addition_def[1][1][1], ) bytes_a = get_state(key_a, sm_byte_size) mat_a = np.frombuffer(bytes_a, dtype=np.float32).reshape(sm_size, sm_size) bytes_b = get_state(key_b, sm_byte_size) mat_b = np.frombuffer(bytes_b, dtype=np.float32).reshape(sm_size, sm_size) return mat_a + mat_b
def load_matrix_conf_from_state(): # Params are ints so need to work out what size they are dummy = np.array((1, 2), dtype=int32) param_len = len(dummy.tobytes()) param_bytes = get_state(MATRIX_CONF_STATE_KEY, param_len) params = np.frombuffer(param_bytes, dtype=int32) matrix_size = params[0] n_splits = params[1] conf = MatrixConf(matrix_size, n_splits) return conf
def faasm_main(): key = "pyStateTest" value_len = 10 expected = b"0199956789" # Check the full value actual = get_state(key, value_len) if actual != expected: msg = "Mismatch: actual {}, expected {}".format(actual, expected) print(msg) exit(1) # Check a couple of segments _check_segment(key, 0, 3, b"019") _check_segment(key, 4, 4, b"9567") print("Successful Python state reading check")
def test_distributed_multiplication(self, split_level): self.set_up_conf(split_level) # Set up the problem mat_a = random_matrix(self.conf.matrix_size) mat_b = random_matrix(self.conf.matrix_size) subdivide_matrix_into_state(self.conf, mat_a, SUBMATRICES_KEY_A) subdivide_matrix_into_state(self.conf, mat_b, SUBMATRICES_KEY_B) expected = np.dot(mat_a, mat_b) # Invoke the divide and conquer divide_and_conquer() # Load the result actual_bytes = get_state(RESULT_MATRIX_KEY, self.conf.bytes_per_matrix) actual = np.frombuffer(actual_bytes, dtype=np.float32).reshape( self.conf.matrix_size, self.conf.matrix_size) # Note that the floating point errors can creep up so we have a relatively high tolerance here np.testing.assert_array_almost_equal_nulp(actual, expected, nulp=20)
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 _read_submatrix_from_state(row_idx, col_idx): full_key = conf.get_submatrix_key(key_prefix, conf.n_splits, row_idx, col_idx) sm_bytes = conf.get_bytes_per_submatrix(conf.n_splits) return get_state(full_key, sm_bytes)
def get_dict_from_state(key): dict_size = get_state_size(key) pickled_dict = get_state(key, dict_size) return pickle.loads(pickled_dict)