From ad1df2bb98ef710c42ae9fb16473d97462d0914e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:12:45 +0800 Subject: [PATCH] optimize: project structure --- .vscode/c_cpp_properties.json | 4 ++-- CMakeLists.txt | 10 ++++++++++ include/errors.hpp | 16 ++++++++++++++++ libs/CMakeLists.txt | 17 +++++++++++++++++ libs/errors.cpp | 29 +++++++++++++++++++++++++++++ tests/CMakeLists.txt | 7 ++++++- tests/basic.cpp | 20 +++++++------------- 7 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 include/errors.hpp create mode 100644 libs/CMakeLists.txt create mode 100644 libs/errors.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 446c9d6..879e2b5 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -20,7 +20,7 @@ "-fsycl" ], "cStandard": "c17", - "cppStandard": "c++17", + "cppStandard": "c++20", "intelliSenseMode": "windows-clang-x64", "configurationProvider": "ms-vscode.cmake-tools" }, @@ -43,7 +43,7 @@ "-fsycl" ], "cStandard": "c17", - "cppStandard": "c++17", + "cppStandard": "c++20", "intelliSenseMode": "linux-gcc-x64", "configurationProvider": "ms-vscode.cmake-tools" } diff --git a/CMakeLists.txt b/CMakeLists.txt index 61919ba..8b6479a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,8 +43,18 @@ endif() find_package(IntelSYCL REQUIRED) +include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") + set(COMPILE_FLAGS "-fsycl -Wall ${WIN_FLAG}") set(LINK_FLAGS "-fsycl") +add_subdirectory(libs) +message(STATUS "Collected libs: ${B14LIBS}") + enable_testing() add_subdirectory(tests) +message(STATUS "Collected tests: ${B14TESTS}") + +foreach(TARGET_NAME ${B14TESTS}) + target_link_libraries(${TARGET_NAME} ${B14LIBS}) +endforeach() diff --git a/include/errors.hpp b/include/errors.hpp new file mode 100644 index 0000000..c8f2116 --- /dev/null +++ b/include/errors.hpp @@ -0,0 +1,16 @@ +#ifndef _ERRORS_HPP_ +#define _ERRORS_HPP_ + +#include + +typedef enum { + errors_code_ok, + errors_code_sync_sycl_exception, + errors_code_std_exception, + errors_code_unknown_exception, +} errors_code_enum_t; + +// failed try to exec fn, catch and print .what() when exception is thrown. +errors_code_enum_t failed(std::function fn); + +#endif diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt new file mode 100644 index 0000000..9cba6ed --- /dev/null +++ b/libs/CMakeLists.txt @@ -0,0 +1,17 @@ +file(GLOB CPP_FILES "*.cpp") + +set(LOCAL_B14LIBS "") + +foreach(CPP_FILE ${CPP_FILES}) + # name without .cpp + get_filename_component(TARGET_NAME ${CPP_FILE} NAME_WE) + message(STATUS "Add lib: ${TARGET_NAME}") + add_library(${TARGET_NAME} STATIC ${CPP_FILE}) + + set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS}") + set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "${LINK_FLAGS}") + + list(APPEND LOCAL_B14LIBS ${TARGET_NAME}) +endforeach() + +set(B14LIBS ${LOCAL_B14LIBS} PARENT_SCOPE) diff --git a/libs/errors.cpp b/libs/errors.cpp new file mode 100644 index 0000000..e918fbe --- /dev/null +++ b/libs/errors.cpp @@ -0,0 +1,29 @@ +#include "errors.hpp" + +#include +#include +#include + +template +concept has_what_concept_t = requires(T t) { t.what(); }; + +template +void print_what(T e, std::string msg) { + std::cerr << msg << e.what() << std::endl; +} + +errors_code_enum_t failed(std::function fn) { + try { + fn(); + } catch (sycl::exception &e) { + print_what(e, "Caught sync SYCL exception: "); + return errors_code_sync_sycl_exception; + } catch (std::exception &e) { + print_what(e, "Caught std exception: "); + return errors_code_std_exception; + } catch (...) { + std::cerr << "Caught unknown exception." << std::endl; + return errors_code_unknown_exception; + } + return errors_code_ok; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2d07c28..3ae5351 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,13 +1,18 @@ file(GLOB CPP_FILES "*.cpp") +set(LOCAL_B14TESTS "") + foreach(CPP_FILE ${CPP_FILES}) # name without .cpp get_filename_component(TARGET_NAME ${CPP_FILE} NAME_WE) - message(STATUS "Add test ${TARGET_NAME}") + message(STATUS "Add test: ${TARGET_NAME}") add_executable(${TARGET_NAME} ${CPP_FILE}) set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS}") set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "${LINK_FLAGS}") add_test(NAME test_${TARGET_NAME} COMMAND ${TARGET_NAME}) + list(APPEND LOCAL_B14TESTS ${TARGET_NAME}) endforeach() + +set(B14TESTS ${LOCAL_B14TESTS} PARENT_SCOPE) diff --git a/tests/basic.cpp b/tests/basic.cpp index ef2a62c..1523d7d 100644 --- a/tests/basic.cpp +++ b/tests/basic.cpp @@ -5,6 +5,8 @@ #include #endif +#include "errors.hpp" + static const int N = 4; int main() { @@ -29,23 +31,15 @@ int main() { int *data = sycl::malloc_shared(N, q); for (int i = 0; i < N; i++) data[i] = i; - try { - q.single_task([=]() { + auto errn = failed([&]() { + q.parallel_for(sycl::range<1>(1), [=](sycl::id<1>) { for (int i = 0; i < N; i++) { data[i] *= 2; } }).wait(); - } catch (sycl::exception &e) { - // Do something to output or handle the exception - std::cout << "Caught sync SYCL exception: " << e.what() << "\n"; - return 1; - } catch (std::exception &e) { - std::cout << "Caught std exception: " << e.what() << "\n"; - return 2; - } catch (...) { - std::cout << "Caught unknown exception\n"; - return 3; - } + }); + + if (errn) return errn; for (int i = 0; i < N; i++) std::cout << data[i] << std::endl;