
from pathlib import Path
import struct

from utils import measure, read_binary_file

@measure
def run_sequential(num_files):

    # 1. READ files with format [int]_input.bin
    data = read_data(num_files)

    # 2. COMPUTE
    a = data[::2]
    b = data[1::2]
    r = compute_saxpy(a, b)

    # 3. COMPUTE...
    ret = compute_result(r)
    print("RESULT:", ret)

    # 4. WRITE RESULTS:
    write_data(r)

@measure
def write_data(r):
    #waste_time_rand()
    with open("results.bin", 'wb') as f:
        for num in r:
            f.write(struct.pack('f', num))

@measure
def read_data(num_files):
    ret = []
    for i in range(num_files):
        aux = Path(f"{i}_input.bin")
        ret.extend(read_binary_file(aux))
    return ret

@measure
def compute_result(r):
    return sum(r)

@measure
def compute_saxpy(a, b):
    ret = []
    for i, j in zip(a, b):
        ret.append((33 * i) + j)
    return ret
