From 05ac362c6c4a5a20bb7078cf18eabce7b01ce502 Mon Sep 17 00:00:00 2001 From: Ed-5100 Date: Wed, 15 Jan 2025 19:00:54 -0500 Subject: [PATCH] test: added singleton test --- tests/functional/singleton/BUILD.bazel | 58 ++++++++++++++++++++ tests/functional/singleton/vadd-host.cpp | 70 ++++++++++++++++++++++++ tests/functional/singleton/vadd.cpp | 45 +++++++++++++++ tests/functional/singleton/vadd.h | 8 +++ 4 files changed, 181 insertions(+) create mode 100644 tests/functional/singleton/BUILD.bazel create mode 100644 tests/functional/singleton/vadd-host.cpp create mode 100644 tests/functional/singleton/vadd.cpp create mode 100644 tests/functional/singleton/vadd.h diff --git a/tests/functional/singleton/BUILD.bazel b/tests/functional/singleton/BUILD.bazel new file mode 100644 index 00000000..852b34cc --- /dev/null +++ b/tests/functional/singleton/BUILD.bazel @@ -0,0 +1,58 @@ +"""The singleton mmaps and streams test for TAPA.""" + +# Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors. +# All rights reserved. The contributor(s) of this file has/have agreed to the +# RapidStream Contributor License Agreement. + +load("//bazel:tapa_rules.bzl", "tapa_xo") + +sh_test( + name = "singleton", + size = "medium", + srcs = ["//bazel:v++_env.sh"], + args = ["$(location singleton-host)"], + data = [":singleton-host"], + env = {"TAPA_CONCURRENCY": "1"}, +) + +sh_test( + name = "singleton-xosim", + size = "enormous", + timeout = "moderate", + srcs = ["//bazel:v++_env.sh"], + args = [ + "$(location singleton-host)", + "--bitstream=$(location singleton-xo)", + "--xosim_executable=$(location //tapa/cosim:tapa-fast-cosim)", + "1000", + ], + data = [ + ":singleton-host", + ":singleton-xo", + "//tapa/cosim:tapa-fast-cosim", + ], + tags = [ + "cpu:2", + ], +) + +cc_binary( + name = "singleton-host", + srcs = glob([ + "*.cpp", + "*.h", + ]), + deps = [ + "//tapa-lib:tapa", + "@gflags", + "@vitis_hls//:include", + ], +) + +tapa_xo( + name = "singleton-xo", + src = "vadd.cpp", + hdrs = glob(["*.h"]), + platform_name = "xilinx_u250_gen3x16_xdma_4_1_202210_1", + top_name = "VecAdd", +) diff --git a/tests/functional/singleton/vadd-host.cpp b/tests/functional/singleton/vadd-host.cpp new file mode 100644 index 00000000..83d0dd52 --- /dev/null +++ b/tests/functional/singleton/vadd-host.cpp @@ -0,0 +1,70 @@ +// Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors. +// All rights reserved. The contributor(s) of this file has/have agreed to the +// RapidStream Contributor License Agreement. + +#include +#include + +#include +#include + +#include "vadd.h" + +using std::clog; +using std::endl; +using std::vector; + +void VecAdd(tapa::mmaps a, tapa::mmaps b, + tapa::mmaps c, uint64_t n); + +DEFINE_string(bitstream, "", "path to bitstream file, run csim if empty"); + +int main(int argc, char* argv[]) { + gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true); + + const uint64_t n = argc > 1 ? atoll(argv[1]) : 1024 * 1024; + vector a[M]; + vector b[M]; + vector c[M]; + for (uint16_t i = 0; i < M; ++i) { + a[i] = vector(n, 0); + b[i] = vector(n, 0); + c[i] = vector(n, 0); + for (uint64_t j = 0; j < n; ++j) { + a[i][j] = static_cast(i * j); + b[i][j] = static_cast(i * j) * 2; + c[i][j] = 0.f; + } + } + int64_t kernel_time_ns = + tapa::invoke(VecAdd, FLAGS_bitstream, tapa::read_only_mmaps(a), + tapa::read_only_mmaps(b), + tapa::write_only_mmaps(c), n); + clog << "kernel time: " << kernel_time_ns * 1e-9 << " s" << endl; + + uint64_t num_errors = 0; + const uint64_t threshold = 10; // only report up to these errors + for (uint16_t i = 0; i < M; ++i) { + for (uint64_t j = 0; j < n; ++j) { + auto expected = i * j * 3; + auto actual = static_cast(c[i][j]); + if (actual != expected) { + if (num_errors < threshold) { + clog << "expected: " << expected << ", actual: " << actual << endl; + } else if (num_errors == threshold) { + clog << "..."; + } + ++num_errors; + } + } + } + if (num_errors == 0) { + clog << "PASS!" << endl; + } else { + if (num_errors > threshold) { + clog << " (+" << (num_errors - threshold) << " more errors)" << endl; + } + clog << "FAIL!" << endl; + } + return num_errors > 0 ? 1 : 0; +} diff --git a/tests/functional/singleton/vadd.cpp b/tests/functional/singleton/vadd.cpp new file mode 100644 index 00000000..d43b0c69 --- /dev/null +++ b/tests/functional/singleton/vadd.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors. +// All rights reserved. The contributor(s) of this file has/have agreed to the +// RapidStream Contributor License Agreement. + +#include + +#include + +#include "vadd.h" + +void Add(tapa::istreams& a, tapa::istreams& b, + tapa::ostreams& c, uint64_t n) { + for (uint64_t i = 0; i < n; ++i) { + for (uint64_t j = 0; j < M; ++j) { + c[j] << (a[j].read() + b[j].read()); + } + } +} + +void Mmap2Stream(tapa::mmap mmap, uint64_t n, + tapa::ostream& stream) { + for (uint64_t i = 0; i < n; ++i) { + stream << mmap[i]; + } +} + +void Stream2Mmap(tapa::istream& stream, tapa::mmap mmap, + uint64_t n) { + for (uint64_t i = 0; i < n; ++i) { + stream >> mmap[i]; + } +} + +void VecAdd(tapa::mmaps a, tapa::mmaps b, + tapa::mmaps c, uint64_t n) { + tapa::streams a_q("a"); + tapa::streams b_q("b"); + tapa::streams c_q("c"); + + tapa::task() + .invoke(Mmap2Stream, a, n, a_q) + .invoke(Mmap2Stream, b, n, b_q) + .invoke(Add, a_q, b_q, c_q, n) + .invoke(Stream2Mmap, c_q, c, n); +} diff --git a/tests/functional/singleton/vadd.h b/tests/functional/singleton/vadd.h new file mode 100644 index 00000000..073e35a2 --- /dev/null +++ b/tests/functional/singleton/vadd.h @@ -0,0 +1,8 @@ +// Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors. +// All rights reserved. The contributor(s) of this file has/have agreed to the +// RapidStream Contributor License Agreement. + +#include + +// when array size is 1, it forms a singleton array with no number in naming +constexpr int M = 1;