-
Notifications
You must be signed in to change notification settings - Fork 727
FPGA: Improve the fpga_compile
tutorial
#1347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cad9d7f
restore from empty_project
KevinUTAT 54f8c6f
remove old files
KevinUTAT 032db08
change link style to inline
KevinUTAT 3335e4e
remove part5
KevinUTAT b6cea2f
Update DirectProgramming/C++SYCL_FPGA/Tutorials/GettingStarted/fpga_c…
KevinUTAT 1836ae7
Update DirectProgramming/C++SYCL_FPGA/Tutorials/GettingStarted/fpga_c…
KevinUTAT 3b35393
Merge branch 'development' into fpga_compile_update
KevinUTAT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
332 changes: 184 additions & 148 deletions
332
DirectProgramming/C++SYCL_FPGA/Tutorials/GettingStarted/fpga_compile/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ogramming/C++SYCL_FPGA/Tutorials/GettingStarted/fpga_compile/part1-C++/src/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
set(SOURCE_FILE vector_add.cpp) | ||
set(TARGET_NAME vector_add) | ||
|
||
# FPGA device selection | ||
if(DEFINED FPGA_DEVICE) | ||
message(STATUS "Ignoring FPGA_DEVICE: ${FPGA_DEVICE}, not applicable") | ||
endif() | ||
|
||
set(EMULATOR_TARGET ${TARGET_NAME}.fpga_emu) | ||
|
||
add_executable(${EMULATOR_TARGET} ${SOURCE_FILE}) | ||
add_custom_target(fpga_emu DEPENDS ${EMULATOR_TARGET}) | ||
|
||
# This code sample do not support simulator and fpga. | ||
# following targets are added to be compatiable with reg-tests | ||
set(SIMULATOR_TARGET ${TARGET_NAME}.fpga_sim) | ||
|
||
add_executable(${SIMULATOR_TARGET} ${SOURCE_FILE}) | ||
add_custom_target(fpga_sim DEPENDS ${SIMULATOR_TARGET}) | ||
|
||
set(FPGA_TARGET ${TARGET_NAME}.fpga) | ||
|
||
add_executable(${FPGA_TARGET} ${SOURCE_FILE}) | ||
add_custom_target(fpga DEPENDS ${FPGA_TARGET}) |
46 changes: 46 additions & 0 deletions
46
...ogramming/C++SYCL_FPGA/Tutorials/GettingStarted/fpga_compile/part1-C++/src/vector_add.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <iostream> | ||
|
||
void VectorAdd(const int *a_in, const int *b_in, int *c_out, int len) { | ||
for (int idx = 0; idx < len; idx++) { | ||
int a_val = a_in[idx]; | ||
int b_val = b_in[idx]; | ||
int sum = a_val + b_val; | ||
c_out[idx] = sum; | ||
} | ||
} | ||
|
||
constexpr int kVectSize = 256; | ||
|
||
int main() { | ||
|
||
// declare arrays and fill them | ||
int *vec_a = new int[kVectSize]; | ||
int *vec_b = new int[kVectSize]; | ||
int *vec_c = new int[kVectSize]; | ||
for (int i = 0; i < kVectSize; i++) { | ||
vec_a[i] = i; | ||
vec_b[i] = (kVectSize - i); | ||
} | ||
|
||
std::cout << "add two vectors of size " << kVectSize << std::endl; | ||
|
||
VectorAdd(vec_a, vec_b, vec_c, kVectSize); | ||
|
||
// verify that vector C is correct | ||
bool passed = true; | ||
for (int i = 0; i < kVectSize; i++) { | ||
int expected = vec_a[i] + vec_b[i]; | ||
if (vec_c[i] != expected) { | ||
std::cout << "idx=" << i << ": result " << vec_c[i] << ", expected (" << expected << ") A=" << vec_a[i] << " + B=" << vec_b[i] << std::endl; | ||
passed = false; | ||
} | ||
} | ||
|
||
std::cout << (passed ? "PASSED" : "FAILED") << std::endl; | ||
|
||
delete[] vec_a; | ||
delete[] vec_b; | ||
delete[] vec_c; | ||
|
||
return passed ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |
20 changes: 20 additions & 0 deletions
20
...C++SYCL_FPGA/Tutorials/GettingStarted/fpga_compile/part2-dpcpp_functor_usm/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
if(UNIX) | ||
# Direct CMake to use dpcpp rather than the default C++ compiler/linker | ||
set(CMAKE_CXX_COMPILER icpx) | ||
else() # Windows | ||
# Force CMake to use dpcpp rather than the default C++ compiler/linker | ||
# (needed on Windows only) | ||
include (CMakeForceCompiler) | ||
CMAKE_FORCE_CXX_COMPILER (icx-cl IntelDPCPP) | ||
include (Platform/Windows-Clang) | ||
endif() | ||
|
||
cmake_minimum_required (VERSION 3.7.2) | ||
|
||
project(FPGACompile CXX) | ||
|
||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
|
||
add_subdirectory (src) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...YCL_FPGA/Tutorials/GettingStarted/fpga_compile/part2-dpcpp_functor_usm/src/vector_add.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <iostream> | ||
|
||
// oneAPI headers | ||
#include <sycl/sycl.hpp> | ||
#include <sycl/ext/intel/fpga_extensions.hpp> | ||
|
||
// Forward declare the kernel name in the global scope. This is an FPGA best | ||
// practice that reduces name mangling in the optimization reports. | ||
class VectorAddID; | ||
|
||
struct VectorAdd { | ||
public: | ||
int *const vec_a_in; | ||
int *const vec_b_in; | ||
int *const vec_c_out; | ||
int len; | ||
|
||
void operator()() const { | ||
for (int idx = 0; idx < len; idx++) { | ||
int a_val = vec_a_in[idx]; | ||
int b_val = vec_b_in[idx]; | ||
int sum = a_val + b_val; | ||
vec_c_out[idx] = sum; | ||
} | ||
} | ||
}; | ||
|
||
constexpr int kVectSize = 256; | ||
|
||
int main() { | ||
bool passed = true; | ||
try { | ||
// Use compile-time macros to select either: | ||
// - the FPGA emulator device (CPU emulation of the FPGA) | ||
// - the FPGA device (a real FPGA) | ||
// - the simulator device | ||
#if FPGA_SIMULATOR | ||
auto selector = sycl::ext::intel::fpga_simulator_selector_v; | ||
#elif FPGA_HARDWARE | ||
auto selector = sycl::ext::intel::fpga_selector_v; | ||
#else // #if FPGA_EMULATOR | ||
auto selector = sycl::ext::intel::fpga_emulator_selector_v; | ||
#endif | ||
|
||
// create the device queue | ||
sycl::queue q(selector); | ||
|
||
auto device = q.get_device(); | ||
|
||
std::cout << "Running on device: " | ||
<< device.get_info<sycl::info::device::name>().c_str() | ||
<< std::endl; | ||
|
||
KevinUTAT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// declare arrays and fill them | ||
// allocate in shared memory so the kernel can see them | ||
int *vec_a = sycl::malloc_shared<int>(kVectSize, q); | ||
int *vec_b = sycl::malloc_shared<int>(kVectSize, q); | ||
int *vec_c = sycl::malloc_shared<int>(kVectSize, q); | ||
for (int i = 0; i < kVectSize; i++) { | ||
vec_a[i] = i; | ||
vec_b[i] = (kVectSize - i); | ||
} | ||
|
||
std::cout << "add two vectors of size " << kVectSize << std::endl; | ||
|
||
q.single_task<VectorAddID>(VectorAdd{vec_a, vec_b, vec_c, kVectSize}).wait(); | ||
|
||
// verify that vec_c is correct | ||
for (int i = 0; i < kVectSize; i++) { | ||
int expected = vec_a[i] + vec_b[i]; | ||
if (vec_c[i] != expected) { | ||
std::cout << "idx=" << i << ": result " << vec_c[i] << ", expected (" << expected << ") A=" << vec_a[i] << " + B=" << vec_b[i] << std::endl; | ||
passed = false; | ||
} | ||
} | ||
|
||
std::cout << (passed ? "PASSED" : "FAILED") << std::endl; | ||
|
||
sycl::free(vec_a, q); | ||
sycl::free(vec_b, q); | ||
sycl::free(vec_c, q); | ||
} catch (sycl::exception const &e) { | ||
// Catches exceptions in the host code. | ||
std::cerr << "Caught a SYCL host exception:\n" << e.what() << "\n"; | ||
|
||
// Most likely the runtime couldn't find FPGA hardware! | ||
if (e.code().value() == CL_DEVICE_NOT_FOUND) { | ||
std::cerr << "If you are targeting an FPGA, please ensure that your " | ||
"system has a correctly configured FPGA board.\n"; | ||
std::cerr << "Run sys_check in the oneAPI root directory to verify.\n"; | ||
std::cerr << "If you are targeting the FPGA emulator, compile with " | ||
"-DFPGA_EMULATOR.\n"; | ||
} | ||
std::terminate(); | ||
} | ||
return passed ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |
20 changes: 20 additions & 0 deletions
20
.../C++SYCL_FPGA/Tutorials/GettingStarted/fpga_compile/part3-dpcpp_lambda_usm/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
if(UNIX) | ||
# Direct CMake to use dpcpp rather than the default C++ compiler/linker | ||
set(CMAKE_CXX_COMPILER icpx) | ||
else() # Windows | ||
# Force CMake to use dpcpp rather than the default C++ compiler/linker | ||
# (needed on Windows only) | ||
include (CMakeForceCompiler) | ||
CMAKE_FORCE_CXX_COMPILER (icx-cl IntelDPCPP) | ||
include (Platform/Windows-Clang) | ||
endif() | ||
|
||
cmake_minimum_required (VERSION 3.7.2) | ||
|
||
project(FPGACompile CXX) | ||
|
||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
|
||
add_subdirectory (src) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.