Skip to content

Commit

Permalink
Merge pull request #66 from db-tu-dresden/mwe_rtl_fpga
Browse files Browse the repository at this point in the history
Improved validation and cmake target selection
  • Loading branch information
alexKrauseTUD authored Oct 6, 2023
2 parents 9a1ead5 + eac44e3 commit d864d70
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion examples/oneAPIfpga/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ function(create_fpga_target targetName mainFile)
target_link_options(${exec_target_name} PRIVATE -qactypes -fsycl -fintelfpga -Xshardware -Xsboard=${fpga_board} -reuse-exe=${CMAKE_CURRENT_BINARY_DIR}/${exec_target_name})
endfunction()

if(NOT DEFINED ${TARGET})
if(NOT DEFINED TARGET)
message(STATUS "No target specified. Assuming emulator")
set(TARGET EMULATOR)
endif()
message(STATUS "BUILDING for ${TARGET}")
if(${TARGET} STREQUAL "EMULATOR")
create_fpga_emulator_target(clz_rtl clz_rtl_example.cpp)
elseif(${TARGET} STREQUAL "FPGA_HARDWARE")
Expand Down
41 changes: 30 additions & 11 deletions examples/oneAPIfpga/clz_rtl_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct count_leading_zero_kernel {
int main(void) {
// so far, only 32-bit unsigned integers are supported as RTL code

bool all_ok = true;

using namespace tsl;
executor<runtime::cpu> cpu_executor;
using cpu_simd = simd<uint32_t, avx512>;
Expand All @@ -24,6 +26,7 @@ int main(void) {
};
using fpga_simd = simd<uint32_t, oneAPIfpgaRTL, 512>;

auto expected_result = cpu_executor.allocate<uint32_t>(128);
// allocate memory on host
auto host_mem_data = cpu_executor.allocate<uint32_t>(128);
auto host_mem_result = cpu_executor.allocate<uint32_t>(128);
Expand All @@ -36,9 +39,13 @@ int main(void) {
auto usm_dev_mem_result = fpga_executor.allocate<uint32_t>(128, oneAPI::MEMORY_ON_DEVICE{});

// initialize input data
for (size_t i = 0; i < 128; i++) {
host_mem_data[0] = 0;
usm_host_mem_data[0] = 0;
expected_result[0] = 32;
for (uint32_t i = 1; i < 128; i++) {
host_mem_data[i] = i;
usm_host_mem_data[i] = i;
expected_result[i] = __builtin_clz(i);
}
// copy input data to FPGA device
fpga_executor.copy(usm_dev_mem_data, usm_host_mem_data, 128);
Expand All @@ -54,15 +61,21 @@ int main(void) {

// run kernel on CPU using avx512
cpu_executor.submit<cpu_simd, count_leading_zero_kernel>(host_mem_result, host_mem_data, (size_t)128);
// check results
for (size_t i = 0; i < 128; i++) {
if (host_mem_result[i] != expected_result[i]) {
std::cerr << "ERROR: host_mem_result[" << i << "] = " << host_mem_result[i] << " != expected_result[" << i << "] = " << expected_result[i] << std::endl;
all_ok = false;
}
}

// run kernel on FPGA using oneAPIfpgaRTL (RTL code is built seperately). Use USM-Host memory
fpga_executor.submit<fpga_simd, count_leading_zero_kernel>(usm_host_mem_result, usm_host_mem_data, (size_t)128);

// check results
for (size_t i = 0; i < 128; i++) {
if (host_mem_result[i] != usm_host_mem_result[i]) {
std::cerr << "ERROR: host_mem_result[" << i << "] = " << host_mem_result[i] << " != usm_host_mem_result[" << i << "] = " << usm_host_mem_result[i] << std::endl;
std::terminate();
if (host_mem_result[i] != expected_result[i]) {
std::cerr << "ERROR: usm_host_mem_result[" << i << "] = " << usm_host_mem_result[i] << " != expected_result[" << i << "] = " << expected_result[i] << std::endl;
all_ok = false;
}
}

Expand All @@ -71,12 +84,11 @@ int main(void) {

// copy output to host
fpga_executor.copy(usm_host_mem_result, usm_dev_mem_result, 128);

// check results
for (size_t i = 0; i < 128; i++) {
if (host_mem_result[i] != usm_host_mem_result[i]) {
std::cerr << "ERROR: host_mem_result[" << i << "] = " << host_mem_result[i] << " != usm_host_mem_result[" << i << "] = " << usm_host_mem_result[i] << std::endl;
std::terminate();
if (host_mem_result[i] != expected_result[i]) {
std::cerr << "ERROR: usm_host_mem_result[" << i << "] = " << usm_host_mem_result[i] << " != expected_result[" << i << "] = " << expected_result[i] << std::endl;
all_ok = false;
}
}

Expand All @@ -87,8 +99,15 @@ int main(void) {
fpga_executor.deallocate(usm_host_mem_data);
cpu_executor.deallocate(host_mem_result);
cpu_executor.deallocate(host_mem_data);
cpu_executor.deallocate(expected_result);

// done
std::cout << "Everything worked fine!" << std::endl;
return 0;
if (all_ok) {
std::cout << "Everything worked fine!" << std::endl;
return 0;
} else {
std::cout << "There were errors!" << std::endl;
return 1;
}

}

0 comments on commit d864d70

Please sign in to comment.