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 = read_state(key_a, sm_byte_size) mat_a = np.frombuffer(bytes_a, dtype=np.float32).reshape(sm_size, sm_size) bytes_b = read_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 _check_full_value(expected): pull_state(key, value_len) actual = read_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 = read_state(full_key, sm_bytes) return np.frombuffer(sub_mat_data, dtype=np.float32).reshape( sm_size, sm_size )
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 = read_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 = read_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") return 0
def get_dict_from_state(key): dict_size = read_state_size(key) pickled_dict = read_state(key, dict_size) return pickle.loads(pickled_dict)
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 read_state(full_key, sm_bytes)
msg = { "user": "******", "function": "echo", } set_emulator_message(json.dumps(msg)) # Write and push state key = "pyStateTest" valueLen = 10 fullValue = b"0123456789" write_state(key, fullValue) push_state(key) # Read state back in pull_state(key, valueLen) actual = read_state(key, valueLen) print("In = {} out = {}".format(fullValue, actual)) # Update a segment segment = b"999" offset = 2 segmentLen = 3 modifiedValue = b"0199956789" write_state_offset(key, valueLen, offset, segment) push_state(key) pull_state(key, valueLen) actual = read_state(key, valueLen) actualSegment = read_state_offset(key, valueLen, offset, segmentLen) print("Expected = {} actual = {}".format(modifiedValue, actual)) print("Expected = {} actual = {}".format(segment, actualSegment))