Skip to content
This repository was archived by the owner on Dec 25, 2023. It is now read-only.

Commit d4e9d5a

Browse files
authored
Merge branch 'ymeng/perf-cmp' into ymeng/perf-signed to sign the commits (#17)
Updated to use released testdata Cherry-picked "warm up the fpga kernels"
1 parent a7cec19 commit d4e9d5a

File tree

6 files changed

+207
-14
lines changed

6 files changed

+207
-14
lines changed

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,30 @@ message(STATUS "ENABLE_BENCHMARK: ${ENABLE_BENCHMARK}")
2727
option(ENABLE_DOCS "Enable Documentation" OFF)
2828
message(STATUS "ENABLE_DOCS: ${ENABLE_DOCS}")
2929

30+
option(FPGA_USE_INTEL_HEXL "Use Intel HEXL" OFF)
31+
message(STATUS "FPGA_USE_INTEL_HEXL: ${FPGA_USE_INTEL_HEXL}")
32+
option(FPGA_BUILD_INTEL_HEXL "Build INTEL HEXL" OFF)
33+
message(STATUS "FPGA_BUILD_INTEL_HEXL: ${FPGA_BUILD_INTEL_HEXL}")
34+
3035
if (LINUX)
3136
include(GNUInstallDirs)
3237
else()
3338
set(CMAKE_INSTALL_INCLUDEDIR "include")
3439
set(CMAKE_INSTALL_LIBDIR "lib")
3540
endif()
3641

42+
if(FPGA_USE_INTEL_HEXL)
43+
if(FPGA_BUILD_INTEL_HEXL)
44+
message(STATUS "Intel HEXL: download ...")
45+
include(cmake/intel-hexl/intel-hexl.cmake)
46+
else()
47+
find_package(HEXL 1.2.4)
48+
if (NOT TARGET HEXL::hexl)
49+
message(FATAL_ERROR "Intel HEXL: not found")
50+
endif()
51+
endif()
52+
endif()
53+
3754
add_subdirectory(host)
3855
add_subdirectory(device)
3956

benchmark/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ function(bench_function kernel)
2121
target_link_libraries(bench_${KERNEL} PRIVATE benchmark::benchmark)
2222
target_link_libraries(bench_${KERNEL} PRIVATE nlohmann_json::nlohmann_json)
2323

24+
if(FPGA_USE_INTEL_HEXL)
25+
target_compile_options(bench_${KERNEL} PRIVATE -DFPGA_USE_INTEL_HEXL)
26+
if(FPGA_BUILD_INTEL_HEXL)
27+
add_dependencies(bench_${KERNEL} ext_intel_hexl)
28+
target_include_directories(bench_${KERNEL} PRIVATE ${CMAKE_BINARY_DIR}/hexl-install/${CMAKE_INSTALL_INCLUDEDIR})
29+
target_link_directories(bench_${KERNEL} PRIVATE ${CMAKE_BINARY_DIR}/hexl-install/${CMAKE_INSTALL_LIBDIR})
30+
target_link_libraries(bench_${KERNEL} PRIVATE ${CMAKE_BINARY_DIR}/hexl-install/${CMAKE_INSTALL_LIBDIR}/libhexl.so)
31+
else()
32+
get_target_property(
33+
HEXL_INCLUDE_DIR
34+
HEXL::hexl
35+
INTERFACE_INCLUDE_DIRECTORIES)
36+
target_include_directories(bench_${KERNEL} PRIVATE ${HEXL_INCLUDE_DIR})
37+
target_link_libraries(bench_${KERNEL} PRIVATE HEXL::hexl)
38+
endif()
39+
endif(FPGA_USE_INTEL_HEXL)
40+
2441
add_custom_command(
2542
TARGET bench_${KERNEL} POST_BUILD
2643
COMMAND rm -f ${KERNEL}.aocx

benchmark/bench_keyswitch.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
#include <benchmark/benchmark.h>
1212

13+
#ifdef FPGA_USE_INTEL_HEXL
14+
#include "hexl/hexl.hpp"
15+
#else
1316
#include "hexl-fpga.h"
17+
#endif
1418

1519
static uint32_t get_iter() {
1620
char* env = getenv("ITER");
@@ -101,8 +105,6 @@ std::vector<std::string> keyswitch::glob(const char* pattern) {
101105

102106
void keyswitch::setup_keyswitch(const std::vector<std::string>& files) {
103107
for (size_t i = 0; i < files.size(); i++) {
104-
std::cout << "Constructing Test Vector " << i << " from File ... "
105-
<< files[i] << std::endl;
106108
test_vectors_.push_back(KeySwitchTestVector(files[i].c_str()));
107109
}
108110

@@ -111,22 +113,35 @@ void keyswitch::setup_keyswitch(const std::vector<std::string>& files) {
111113
}
112114

113115
void keyswitch::bench_keyswitch() {
116+
#ifndef FPGA_USE_INTEL_HEXL
114117
intel::hexl::set_worksize_KeySwitch(test_vector_size_ * n_iter);
118+
#endif
119+
120+
#ifndef FPGA_USE_INTEL_HEXL
121+
// HEXL-FPGA version of keyswitch
122+
namespace ns = intel::hexl;
123+
#else
124+
// HEXL CPU version of keyswitch
125+
namespace ns = intel::hexl::internal;
126+
#endif
127+
115128
for (size_t n = 0; n < n_iter; n++) {
116129
for (size_t i = 0; i < test_vector_size_; i++) {
117-
intel::hexl::KeySwitch(test_vectors_[i].input.data(),
118-
test_vectors_[i].t_target_iter_ptr.data(),
119-
test_vectors_[0].coeff_count,
120-
test_vectors_[0].decomp_modulus_size,
121-
test_vectors_[0].key_modulus_size,
122-
test_vectors_[0].rns_modulus_size,
123-
test_vectors_[0].key_component_count,
124-
test_vectors_[0].moduli.data(),
125-
test_vectors_[0].key_vectors.data(),
126-
test_vectors_[0].modswitch_factors.data());
130+
ns::KeySwitch(test_vectors_[i].input.data(),
131+
test_vectors_[i].t_target_iter_ptr.data(),
132+
test_vectors_[0].coeff_count,
133+
test_vectors_[0].decomp_modulus_size,
134+
test_vectors_[0].key_modulus_size,
135+
test_vectors_[0].rns_modulus_size,
136+
test_vectors_[0].key_component_count,
137+
test_vectors_[0].moduli.data(),
138+
test_vectors_[0].key_vectors.data(),
139+
test_vectors_[0].modswitch_factors.data());
127140
}
128141
}
142+
#ifndef FPGA_USE_INTEL_HEXL
129143
intel::hexl::KeySwitchCompleted();
144+
#endif
130145
}
131146

132147
BENCHMARK_F(keyswitch, 16384_6_7_7_2)
@@ -149,6 +164,9 @@ BENCHMARK_F(keyswitch, 16384_6_7_7_2)
149164

150165
setup_keyswitch(files);
151166

167+
// warm up the FPGA kernels specially the twiddle factor dispatching kernel
168+
bench_keyswitch();
169+
152170
for (auto st : state) {
153171
bench_keyswitch();
154172
}

benchmark/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33

44
#include <benchmark/benchmark.h>
55

6+
#ifndef FPGA_USE_INTEL_HEXL
67
#include "hexl-fpga.h"
8+
#endif
79

810
int main(int argc, char** argv) {
11+
#ifndef FPGA_USE_INTEL_HEXL
912
intel::hexl::acquire_FPGA_resources();
10-
13+
#endif
1114
benchmark::Initialize(&argc, argv);
1215
benchmark::RunSpecifiedBenchmarks();
1316

17+
#ifndef FPGA_USE_INTEL_HEXL
1418
intel::hexl::release_FPGA_resources();
15-
19+
#endif
1620
return 0;
1721
}

cmake/intel-hexl/intel-hexl.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (C) 2020-2021 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
include(ExternalProject)
5+
6+
set(INTEL_HEXL_GIT_REPO_URL https://github.com/intel/hexl.git)
7+
set(INTEL_HEXL_GIT_LABEL v1.2.4)
8+
9+
ExternalProject_Add(
10+
ext_intel_hexl
11+
PREFIX ext_intel_hexl
12+
GIT_REPOSITORY ${INTEL_HEXL_GIT_REPO_URL}
13+
GIT_TAG ${INTEL_HEXL_GIT_LABEL}
14+
CMAKE_ARGS
15+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
16+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
17+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
18+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
19+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
20+
-DHEXL_SHARED_LIB=ON
21+
-DHEXL_EXPERIMENTAL=ON
22+
-DHEXL_DEBUG=OFF
23+
-DHEXL_BENCHMARK=OFF
24+
-DHEXL_COVERAGE=OFF
25+
-DHEXL_TESTING=OFF
26+
-DCMAKE_INSTALL_INCLUDEDIR=include
27+
-DCMAKE_INSTALL_LIBDIR=lib
28+
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/hexl-install
29+
EXCLUDE_FROM_ALL TRUE
30+
# Skip updates
31+
UPDATE_COMMAND "")
32+
33+
ExternalProject_Get_Property(ext_intel_hexl SOURCE_DIR BINARY_DIR)
34+
35+
add_library(libhexl INTERFACE)
36+
add_dependencies(libhexl ext_intel_hexl)
37+
target_include_directories(libhexl INTERFACE ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
38+
target_link_libraries(libhexl INTERFACE ${CMAKE_INSTALL_PREFIX}/${CAMKE_INSTALL_LIBDIR}/libhexl-fpga.so)

utils/perf-cmp.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Copyright (C) 2020-2021 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/usr/bin/env bash
5+
6+
workdir=$PWD
7+
results=${workdir}/results
8+
mkdir -p ${results}
9+
mkdir -p testdata
10+
11+
setup_testdata() {
12+
pushd testdata
13+
14+
# downloading bitstream keyswitch.aocx
15+
wget https://github.com/intel/hexl-fpga/releases/download/v1.1/keyswitch.aocx
16+
aocx=${PWD}/keyswitch.aocx
17+
export FPGA_BITSTREAM=${aocx}
18+
export FPGA_KERNEL=KEYSWITCH
19+
20+
# downloading test vectors testdata.zip
21+
wget https://github.com/intel/hexl-fpga/releases/download/v1.1/testdata.zip
22+
unzip testdata.zip
23+
export KEYSWITCH_DATA_DIR=${PWD}/testdata
24+
25+
popd
26+
}
27+
28+
build_cpu() {
29+
rm -rf build
30+
mkdir build
31+
32+
cd build
33+
cmake .. \
34+
-DCMAKE_INSTALL_PREFIX=./hexl-fpga-install \
35+
-DCMAKE_BUILD_TYPE=Release \
36+
-DCMAKE_CXX_COMPILER=g++ \
37+
-DCMAKE_C_COMPILER=gcc \
38+
-DENABLE_TESTS=ON \
39+
-DENABLE_BENCHMARK=ON \
40+
-DFPGA_USE_INTEL_HEXL=ON \
41+
-DFPGA_BUILD_INTEL_HEXL=ON
42+
43+
make -j
44+
cd ..
45+
}
46+
47+
build_fpga() {
48+
rm -rf build-fpga
49+
mkdir build-fpga
50+
51+
cd build-fpga
52+
cmake .. \
53+
-DCMAKE_INSTALL_PREFIX=./hexl-fpga-install \
54+
-DCMAKE_BUILD_TYPE=Release \
55+
-DCMAKE_CXX_COMPILER=g++ \
56+
-DCMAKE_C_COMPILER=gcc \
57+
-DENABLE_TESTS=ON \
58+
-DENABLE_BENCHMARK=ON \
59+
-DFPGA_USE_INTEL_HEXL=OFF \
60+
-DFPGA_BUILD_INTEL_HEXL=OFF
61+
62+
make -j
63+
cd ..
64+
}
65+
66+
run_cpu() {
67+
iter=$1
68+
export ITER=${iter}
69+
./bench_keyswitch > ${results}/cpu.iter-${iter}.log
70+
}
71+
72+
run_fpga() {
73+
iter=$1
74+
batch=$2
75+
export ITER=${iter}
76+
export BATCH_SIZE_KEYSWITCH=${batch}
77+
./bench_keyswitch > ${results}/fpga.iter-${iter}.batch-${batch}.log
78+
}
79+
80+
build_cpu
81+
build_fpga
82+
83+
setup_testdata
84+
85+
pushd build/benchmark
86+
for i in 1 1024 4096 16384
87+
do
88+
run_cpu $i
89+
done
90+
popd
91+
92+
aocl program acl0 ${aocx}
93+
pushd build-fpga/benchmark
94+
batch=128
95+
for i in 1 1024 4096 16384
96+
do
97+
run_fpga $i ${batch}
98+
done
99+
popd

0 commit comments

Comments
 (0)