Skip to content

Commit

Permalink
test: added singleton test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed-5100 committed Jan 16, 2025
1 parent 0a9cb9a commit 4d8280a
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/functional/singleton/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
70 changes: 70 additions & 0 deletions tests/functional/singleton/vadd-host.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>

#include <gflags/gflags.h>
#include <tapa.h>

#include "vadd.h"

using std::clog;
using std::endl;
using std::vector;

void VecAdd(tapa::mmaps<float, M> a, tapa::mmaps<float, M> b,
tapa::mmaps<float, M> 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<float> a[M];
vector<float> b[M];
vector<float> c[M];
for (uint16_t i = 0; i < M; ++i) {
a[i] = vector<float>(n, 0);
b[i] = vector<float>(n, 0);
c[i] = vector<float>(n, 0);
for (uint64_t j = 0; j < n; ++j) {
a[i][j] = static_cast<float>(i * j);
b[i][j] = static_cast<float>(i * j) * 2;
c[i][j] = 0.f;
}
}
int64_t kernel_time_ns =
tapa::invoke(VecAdd, FLAGS_bitstream, tapa::read_only_mmaps<float, M>(a),
tapa::read_only_mmaps<float, M>(b),
tapa::write_only_mmaps<float, M>(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<uint64_t>(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;
}
45 changes: 45 additions & 0 deletions tests/functional/singleton/vadd.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

#include <tapa.h>

#include "vadd.h"

void Add(tapa::istreams<float, M>& a, tapa::istreams<float, M>& b,
tapa::ostreams<float, M>& 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<float> mmap, uint64_t n,
tapa::ostream<float>& stream) {
for (uint64_t i = 0; i < n; ++i) {
stream << mmap[i];
}
}

void Stream2Mmap(tapa::istream<float>& stream, tapa::mmap<float> mmap,
uint64_t n) {
for (uint64_t i = 0; i < n; ++i) {
stream >> mmap[i];
}
}

void VecAdd(tapa::mmaps<float, M> a, tapa::mmaps<float, M> b,
tapa::mmaps<float, M> c, uint64_t n) {
tapa::streams<float, M> a_q("a");
tapa::streams<float, M> b_q("b");
tapa::streams<float, M> c_q("c");

tapa::task()
.invoke<tapa::join, M>(Mmap2Stream, a, n, a_q)
.invoke<tapa::join, M>(Mmap2Stream, b, n, b_q)
.invoke(Add, a_q, b_q, c_q, n)
.invoke<tapa::join, M>(Stream2Mmap, c_q, c, n);
}
6 changes: 6 additions & 0 deletions tests/functional/singleton/vadd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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.

// when array size is 1, it forms a singleton array with no number in naming
inline constexpr int M = 1;

0 comments on commit 4d8280a

Please sign in to comment.