2025-11-03 21:49:31 +00:00
|
|
|
from copapy import NumLike
|
2025-10-30 09:55:23 +00:00
|
|
|
from copapy.backend import Write, compile_to_dag, add_read_command
|
|
|
|
|
import subprocess
|
|
|
|
|
from copapy import _binwrite
|
|
|
|
|
import copapy.backend as backend
|
2025-11-03 14:00:56 +00:00
|
|
|
import copapy as cp
|
2025-10-30 09:55:23 +00:00
|
|
|
import os
|
2025-11-03 14:00:56 +00:00
|
|
|
import warnings
|
2025-11-09 21:53:07 +00:00
|
|
|
import pytest
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
|
# On Windows wsl and qemu-user is required:
|
|
|
|
|
# sudo apt install qemu-user
|
2025-11-03 14:00:56 +00:00
|
|
|
qemu_command = ['wsl', 'qemu-aarch64']
|
2025-10-30 09:55:23 +00:00
|
|
|
else:
|
2025-11-03 14:00:56 +00:00
|
|
|
qemu_command = ['qemu-aarch64']
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(command: list[str]) -> str:
|
|
|
|
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False)
|
|
|
|
|
assert result.returncode != 11, f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
|
|
|
|
assert result.returncode == 0, f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
|
|
|
|
return result.stdout
|
|
|
|
|
|
|
|
|
|
|
2025-11-03 14:00:56 +00:00
|
|
|
def check_for_qemu() -> bool:
|
|
|
|
|
command = qemu_command + ['--version']
|
|
|
|
|
try:
|
2025-11-27 09:10:13 +00:00
|
|
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
|
2025-11-03 21:49:31 +00:00
|
|
|
except Exception:
|
2025-11-03 14:00:56 +00:00
|
|
|
return False
|
|
|
|
|
return result.returncode == 0
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def function(c1: NumLike, c2: NumLike) -> tuple[NumLike, ...]:
|
|
|
|
|
i1 = c1 // 3.3 + 5
|
|
|
|
|
i2 = c2 * 5 + c1
|
|
|
|
|
r1 = i1 + i2 * 55 / 4
|
|
|
|
|
r2 = 4 * i2 + 5
|
|
|
|
|
|
|
|
|
|
return i1, i2, r1, r2
|
|
|
|
|
|
|
|
|
|
|
2025-11-09 21:53:07 +00:00
|
|
|
@pytest.mark.runner
|
2025-10-30 09:55:23 +00:00
|
|
|
def test_compile():
|
2025-11-03 14:00:56 +00:00
|
|
|
t1 = cp.vector([10, 11, 12]) + cp.vector(cp.variable(v) for v in range(3))
|
|
|
|
|
t2 = t1.sum()
|
2025-10-30 09:55:23 +00:00
|
|
|
|
2025-11-03 14:00:56 +00:00
|
|
|
t3 = cp.vector(cp.variable(1 / (v + 1)) for v in range(3))
|
|
|
|
|
t4 = ((t3 * t1) * 2).sum()
|
|
|
|
|
t5 = ((t3 * t1) * 2).magnitude()
|
|
|
|
|
|
|
|
|
|
ret = (t2, t4, t5)
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
out = [Write(r) for r in ret]
|
|
|
|
|
|
2025-11-11 07:47:52 +00:00
|
|
|
sdb = backend.stencil_db_from_package('arm64')
|
2025-10-30 09:55:23 +00:00
|
|
|
il, variables = compile_to_dag(out, sdb)
|
|
|
|
|
|
|
|
|
|
# run program command
|
|
|
|
|
il.write_com(_binwrite.Command.RUN_PROG)
|
|
|
|
|
|
|
|
|
|
for net in ret:
|
|
|
|
|
assert isinstance(net, backend.Net)
|
|
|
|
|
add_read_command(il, variables, net)
|
|
|
|
|
|
|
|
|
|
il.write_com(_binwrite.Command.END_COM)
|
|
|
|
|
|
|
|
|
|
print('* Data to runner:')
|
|
|
|
|
il.print()
|
|
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
il.to_file('build/runner/test-arm64.copapy')
|
2025-10-30 09:55:23 +00:00
|
|
|
|
2025-11-03 14:00:56 +00:00
|
|
|
if not check_for_qemu():
|
|
|
|
|
warnings.warn("qemu-aarch64 not found, aarch64 test skipped!", UserWarning)
|
2025-11-12 23:29:48 +00:00
|
|
|
elif not os.path.isfile('build/runner/coparun-aarch64'):
|
|
|
|
|
warnings.warn("aarch64 runner not found, aarch64 test skipped!", UserWarning)
|
2025-11-03 14:00:56 +00:00
|
|
|
else:
|
2025-11-12 23:29:48 +00:00
|
|
|
command = ['build/runner/coparun-aarch64', 'build/runner/test-arm64.copapy']
|
2025-11-03 14:00:56 +00:00
|
|
|
result = run_command(qemu_command + command)
|
|
|
|
|
print('* Output from runner:\n--')
|
|
|
|
|
print(result)
|
|
|
|
|
print('--')
|
|
|
|
|
|
|
|
|
|
assert 'Return value: 1' in result
|
|
|
|
|
|
|
|
|
|
# Compare to x86_64 reference results
|
|
|
|
|
assert " size=4 data=24 00 00 00" in result
|
|
|
|
|
assert " size=4 data=56 55 25 42" in result
|
|
|
|
|
assert " size=4 data=B4 F9 C8 41" in result
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
#test_example()
|
|
|
|
|
test_compile()
|